union of two types
how do i typeguard in convex?
in typescript i usually do type guards to handle this situtation, how to do that in convex?
function isText(invoice: args ): invoice is object1 {
return invoice.type === "text";
}
in typescript i usually do type guards to handle this situtation, how to do that in convex?
function isText(invoice: args ): invoice is object1 {
return invoice.type === "text";
}
export const webhooks = internalAction({
args: {
object: v.string(),
entry: v.array(
v.object({
id: v.string(),
changes: v.array(
v.object({
field: v.string(),
value: v.object({
messaging_product: v.string(),
metadata: v.object({
phone_number_id: v.string(),
display_phone_number: v.string(),
}),
contacts: v.array(
v.object({
profile: v.object({
name: v.string(),
}),
wa_id: v.string(),
})
),
messages: v.array(
v.union(
v.object({
from: v.string(),
id: v.string(),
timestamp: v.string(),
type: v.literal("text"),
text: v.object({
body: v.string(),
preview_url: v.optional(v.boolean()),
}),
}),
v.object({
from: v.string(),
id: v.string(),
timestamp: v.string(),
type: v.literal("image"),
image: v.object({
mime_type: v.string(),
sha256: v.string(),
id: v.string(),
}),
})
..
},
handler: async (ctx, args) => {
..
const type = value.messages[0].type;
if (type === "text") {
await ctx.runAction(internal.datatypes.text.run, args);
} ...
await ctx.runMutation(internal.data.insert, args);
},
});export const webhooks = internalAction({
args: {
object: v.string(),
entry: v.array(
v.object({
id: v.string(),
changes: v.array(
v.object({
field: v.string(),
value: v.object({
messaging_product: v.string(),
metadata: v.object({
phone_number_id: v.string(),
display_phone_number: v.string(),
}),
contacts: v.array(
v.object({
profile: v.object({
name: v.string(),
}),
wa_id: v.string(),
})
),
messages: v.array(
v.union(
v.object({
from: v.string(),
id: v.string(),
timestamp: v.string(),
type: v.literal("text"),
text: v.object({
body: v.string(),
preview_url: v.optional(v.boolean()),
}),
}),
v.object({
from: v.string(),
id: v.string(),
timestamp: v.string(),
type: v.literal("image"),
image: v.object({
mime_type: v.string(),
sha256: v.string(),
id: v.string(),
}),
})
..
},
handler: async (ctx, args) => {
..
const type = value.messages[0].type;
if (type === "text") {
await ctx.runAction(internal.datatypes.text.run, args);
} ...
await ctx.runMutation(internal.data.insert, args);
},
});