fix: append nanoid for dynamic trigger reg event (#628)

* fix: append nanoid for dynamic trigger reg event

* use idempotency-key as event-id

* add changeset
This commit is contained in:
Hemachandar
2023-10-30 21:54:03 +05:30
committed by GitHub
parent 2635f4bd10
commit 9c4be40adc
5 changed files with 39 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
use idempotency-key as event-id for dynamic-trigger registrations
@@ -10,6 +10,7 @@ import { authenticateApiRequest } from "~/services/apiAuth.server";
import { IngestSendEvent } from "~/services/events/ingestSendEvent.server";
import { logger } from "~/services/logger.server";
import { RegisterTriggerSourceServiceV2 } from "~/services/triggers/registerTriggerSourceV2.server";
import { nanoid } from "nanoid";
const ParamsSchema = z.object({
endpointSlug: z.string(),
@@ -17,6 +18,10 @@ const ParamsSchema = z.object({
key: z.string(),
});
const HeadersSchema = z.object({
"idempotency-key": z.string().optional(),
});
export async function action({ request, params }: ActionFunctionArgs) {
logger.info("Registering trigger", { url: request.url });
@@ -77,11 +82,18 @@ export async function action({ request, params }: ActionFunctionArgs) {
dynamicTriggerId: parsedParams.data.id,
};
const headers = HeadersSchema.safeParse(Object.fromEntries(request.headers));
const eventId =
headers.success && headers.data["idempotency-key"]
? headers.data["idempotency-key"]
: `${registration.id}:${nanoid()}`;
const ingestEventService = new IngestSendEvent();
await ingestEventService.call(
authenticatedEnv,
{
id: registration.id,
id: eventId,
name: REGISTER_SOURCE_EVENT_V2,
source: "trigger.dev",
payload,
+12 -5
View File
@@ -289,7 +289,8 @@ export class ApiClient {
client: string,
id: string,
key: string,
payload: RegisterTriggerBodyV2
payload: RegisterTriggerBodyV2,
idempotencyKey?: string
): Promise<RegisterSourceEventV2> {
const apiKey = await this.#apiKey();
@@ -298,15 +299,21 @@ export class ApiClient {
payload,
});
const headers: HeadersInit = {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
};
if (idempotencyKey) {
headers["Idempotency-Key"] = idempotencyKey;
}
const response = await zodfetch(
RegisterSourceEventSchemaV2,
`${this.#apiUrl}/api/v2/${client}/triggers/${id}/registrations/${key}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
headers: headers,
body: JSON.stringify(payload),
}
);
+7 -2
View File
@@ -623,8 +623,13 @@ export class TriggerClient {
this.#registeredSchedules[key] = jobs;
}
async registerTrigger(id: string, key: string, options: RegisterTriggerBodyV2) {
return this.#client.registerTrigger(this.id, id, key, options);
async registerTrigger(
id: string,
key: string,
options: RegisterTriggerBodyV2,
idempotencyKey?: string
) {
return this.#client.registerTrigger(this.id, id, key, options, idempotencyKey);
}
async getAuth(id: string) {
+2 -1
View File
@@ -140,7 +140,8 @@ export class DynamicTrigger<
return this.#client.registerTrigger(
this.id,
key,
this.registeredTriggerForParams(params, options)
this.registeredTriggerForParams(params, options),
task.idempotencyKey
);
},
{