Added isTest to the Trigger.run context param to detect when a trigger runs in from the test page

This commit is contained in:
Eric Allam
2023-01-26 09:32:51 +00:00
parent 75d65511b3
commit e63d354144
10 changed files with 24 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Added isTest to TriggerContext
+4
View File
@@ -87,6 +87,10 @@ const trigger = new Trigger({
for more info.
</ParamField>
<ParamField path="isTest" type="boolean">
Whether or not this trigger is being run as a test.
</ParamField>
<ParamField path="sendEvent" type="function">
A function that can be used to send an event to trigger.dev. See the [Sending
Events](/functions/send-event) page for more info.
@@ -664,6 +664,7 @@ function createTaskQueue() {
"x-env": run.environment.slug,
"x-workflow-run-id": run.id,
"x-ttl": run.workflow.triggerTtlInSeconds,
"x-is-test": run.isTest ? "true" : "false",
},
{
eventTimestamp: run.event.timestamp.getTime(),
+2
View File
@@ -24,6 +24,7 @@ export type WorkflowRunControllerOptions = {
environment: string;
apiKey: string;
organizationId: string;
isTest: boolean;
};
};
@@ -37,6 +38,7 @@ export class WorkflowRunController {
environment: string;
apiKey: string;
organizationId: string;
isTest: boolean;
};
#logger: Logger;
+4
View File
@@ -420,6 +420,10 @@ export class TriggerServer {
organizationId: properties["x-org-id"],
environment: properties["x-env"],
apiKey: properties["x-api-key"],
isTest:
typeof properties["x-is-test"] === "string"
? properties["x-is-test"] === "true"
: false,
},
});
+4
View File
@@ -39,6 +39,10 @@ new Trigger({
wallTime: new Date(),
});
if (ctx.isTest) {
await ctx.logger.warn("This is only a test");
}
const response = await ctx.fetch("do-fetch", `${event.url}${event.path}`, {
method: event.method,
responseSchema: z.any(),
@@ -14,6 +14,7 @@ export const HostRPCSchema = {
workflowId: z.string(),
organizationId: z.string(),
apiKey: z.string(),
isTest: z.boolean().default(false),
}),
}),
response: z.boolean(),
@@ -7,6 +7,7 @@ const Catalog = {
data: TriggerWorkflowMessageSchema,
properties: WorkflowRunEventPropertiesSchema.extend({
"x-ttl": z.coerce.number().optional(),
"x-is-test": z.string().default("false"),
}),
},
};
+1
View File
@@ -357,6 +357,7 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
environment: data.meta.environment,
apiKey: data.meta.apiKey,
organizationId: data.meta.organizationId,
isTest: data.meta.isTest,
logger: new ContextLogger(async (level, message, properties) => {
await serverRPC.send("SEND_LOG", {
runId: data.id,
+1
View File
@@ -62,6 +62,7 @@ export interface TriggerContext {
apiKey: string;
organizationId: string;
logger: TriggerLogger;
isTest: boolean;
sendEvent(key: string, event: TriggerCustomEvent): Promise<void>;
waitFor(key: string, options: WaitForOptions): Promise<void>;
waitUntil(key: string, date: Date): Promise<void>;