(path: string, anyOptions: any)
| 16 | export type SendEventCommandOptions = z.infer<typeof SendEventCommandOptionsSchema>; |
| 17 | |
| 18 | export async function sendEventCommand(path: string, anyOptions: any) { |
| 19 | const result = SendEventCommandOptionsSchema.safeParse(anyOptions); |
| 20 | |
| 21 | if (!result.success) { |
| 22 | logger.error(result.error.message); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | console.log("Sending event", { options: result.data }); |
| 27 | |
| 28 | const options = result.data; |
| 29 | |
| 30 | const resolvedPath = resolvePath(path); |
| 31 | |
| 32 | // Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL |
| 33 | const apiDetails = await getTriggerApiDetails(resolvedPath, options.envFile); |
| 34 | |
| 35 | if (!apiDetails) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | const { apiUrl, apiKey } = apiDetails; |
| 40 | |
| 41 | const parsedPayload = safeJSONParse(options.payload); |
| 42 | |
| 43 | if (typeof parsedPayload !== "object") { |
| 44 | logger.error( |
| 45 | `The payload must be a valid JSON object. You can also pipe the payload in via stdin.` |
| 46 | ); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | const id = options.id ?? randomUUID(); |
| 51 | const name = options.name; |
| 52 | |
| 53 | const spinner = ora(`[trigger.dev] Sending event ${name} with id ${id}`).start(); |
| 54 | |
| 55 | const triggerApi = new TriggerApi(apiKey, apiUrl); |
| 56 | |
| 57 | const ok = await triggerApi.sendEvent(id, name, parsedPayload); |
| 58 | |
| 59 | if (ok) { |
| 60 | spinner.succeed(`[trigger.dev] Event ${name} with id ${id} sent`); |
| 61 | } else { |
| 62 | spinner.fail(`[trigger.dev] Event ${name} with id ${id} failed to send`); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | function safeJSONParse(payload: string): any { |
| 67 | try { |
no test coverage detected
searching dependent graphs…