Initial httpTrigger types working
This commit is contained in:
@@ -7,8 +7,21 @@ const StringMatchSchema = z.union([
|
||||
z.array(z.union(stringPatternMatchers)),
|
||||
]);
|
||||
|
||||
export const HTTPMethodUnionSchema = z.union([
|
||||
z.literal("GET"),
|
||||
z.literal("POST"),
|
||||
z.literal("PUT"),
|
||||
z.literal("PATCH"),
|
||||
z.literal("DELETE"),
|
||||
z.literal("HEAD"),
|
||||
z.literal("OPTIONS"),
|
||||
]);
|
||||
|
||||
export const RequestFilterSchema = z.object({
|
||||
headers: z.record(StringMatchSchema),
|
||||
query: z.record(StringMatchSchema),
|
||||
method: z.array(HTTPMethodUnionSchema).optional(),
|
||||
headers: z.record(StringMatchSchema).optional(),
|
||||
query: z.record(StringMatchSchema).optional(),
|
||||
body: EventFilterSchema.optional(),
|
||||
});
|
||||
|
||||
export type RequestFilter = z.infer<typeof RequestFilterSchema>;
|
||||
|
||||
@@ -47,7 +47,7 @@ import { runLocalStorage } from "./runLocalStorage";
|
||||
import { DynamicTrigger, DynamicTriggerOptions } from "./triggers/dynamic";
|
||||
import { EventTrigger } from "./triggers/eventTrigger";
|
||||
import { ExternalSource } from "./triggers/externalSource";
|
||||
import { HttpTriggerOptions, httpTrigger } from "./triggers/httpTrigger";
|
||||
import { HttpTriggerOptions, RequestData, httpTrigger } from "./triggers/httpTrigger";
|
||||
import { DynamicIntervalOptions, DynamicSchedule } from "./triggers/scheduled";
|
||||
import type {
|
||||
EventSpecification,
|
||||
@@ -472,7 +472,7 @@ export class TriggerClient {
|
||||
return new DynamicTrigger(this, options);
|
||||
}
|
||||
|
||||
defineHttpTrigger<TEvent extends any = any>(options: HttpTriggerOptions<TEvent>) {
|
||||
defineHttpTrigger<TEvent extends RequestData = any>(options: HttpTriggerOptions<TEvent>) {
|
||||
return httpTrigger<TEvent>(options);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
import { EventFilter, TriggerMetadata, deepMergeFilters } from "@trigger.dev/core";
|
||||
import {
|
||||
DisplayProperty,
|
||||
EventFilter,
|
||||
RequestFilter,
|
||||
TriggerMetadata,
|
||||
deepMergeFilters,
|
||||
} from "@trigger.dev/core";
|
||||
import { Job } from "../job";
|
||||
import { TriggerClient } from "../triggerClient";
|
||||
import { EventSpecification, EventSpecificationExample, SchemaParser, Trigger } from "../types";
|
||||
import { formatSchemaErrors } from "../utils/formatSchemaErrors";
|
||||
import { ParsedPayloadSchemaError } from "../errors";
|
||||
import { z } from "zod";
|
||||
|
||||
type Options<TEventSpecification extends EventSpecification<any>> = {
|
||||
id: string;
|
||||
@@ -37,13 +44,26 @@ export class HttpTrigger<TEventSpecification extends EventSpecification<any>>
|
||||
}
|
||||
}
|
||||
|
||||
type RequestContext = {
|
||||
secret: string | undefined;
|
||||
};
|
||||
|
||||
/** Configuration options for an EventTrigger */
|
||||
export type HttpTriggerOptions<TEvent> = {
|
||||
id: string;
|
||||
schema?: SchemaParser<TEvent>;
|
||||
source?: string;
|
||||
/** The hostname of the webhook, e.g. whatsapp.com */
|
||||
hostname: string;
|
||||
title?: string;
|
||||
icon?: string;
|
||||
bodySchema?: SchemaParser<TEvent>;
|
||||
filter?: EventFilter;
|
||||
examples?: EventSpecificationExample[];
|
||||
properties?: DisplayProperty[];
|
||||
verify?: {
|
||||
requestFilter: RequestFilter;
|
||||
onRequest: (request: Request, context: RequestContext) => Promise<Response>;
|
||||
};
|
||||
transform?: (request: Request) => Promise<TEvent>;
|
||||
};
|
||||
|
||||
/** `eventTrigger()` is set as a [Job's trigger](https://trigger.dev/docs/sdk/job) to subscribe to an event a Job from [a sent event](https://trigger.dev/docs/sdk/triggerclient/instancemethods/sendevent)
|
||||
@@ -56,13 +76,14 @@ export function httpTrigger<TEvent extends any = any>(
|
||||
id: options.id,
|
||||
event: {
|
||||
name: options.id,
|
||||
title: "Event",
|
||||
source: options.source ?? "trigger.dev",
|
||||
icon: "custom-event",
|
||||
title: options.title ?? "HTTP Trigger",
|
||||
source: options.hostname,
|
||||
icon: options.icon ?? "world-www",
|
||||
properties: options.properties,
|
||||
examples: options.examples,
|
||||
parsePayload: (rawPayload: any) => {
|
||||
if (options.schema) {
|
||||
const results = options.schema.safeParse(rawPayload);
|
||||
if (options.bodySchema) {
|
||||
const results = options.bodySchema.safeParse(rawPayload.body);
|
||||
|
||||
if (!results.success) {
|
||||
throw new ParsedPayloadSchemaError(formatSchemaErrors(results.error.issues));
|
||||
|
||||
@@ -12,9 +12,23 @@ export const client = new TriggerClient({
|
||||
|
||||
const whatsApp = client.defineHttpTrigger({
|
||||
id: "whatsapp",
|
||||
schema: z.object({
|
||||
message: z.string(),
|
||||
}),
|
||||
hostname: "whatsapp.com",
|
||||
// bodySchema: z.object({
|
||||
// mesaaaasage: z.string(),
|
||||
// }),
|
||||
verify: {
|
||||
requestFilter: {
|
||||
method: ["GET"],
|
||||
},
|
||||
onRequest: async (request, context) => {
|
||||
const searchParams = new URL(request.url).searchParams;
|
||||
if (searchParams.get("verify_token") !== context.secret) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
return new Response(searchParams.get("challenge") ?? "OK", { status: 200 });
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
client.defineJob({
|
||||
@@ -24,7 +38,7 @@ client.defineJob({
|
||||
enabled: true,
|
||||
trigger: whatsApp,
|
||||
run: async (payload, io, ctx) => {
|
||||
// ^?
|
||||
// ^?
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user