Added a more helpful message when adding an API key

This commit is contained in:
Eric Allam
2023-02-27 11:09:38 +00:00
parent a5ff50f737
commit 87a3bbee8e
4 changed files with 34 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Added a more helpful error message when missing an API key
+2
View File
@@ -37,8 +37,10 @@
"build:tsup": "tsup --dts-resolve"
},
"dependencies": {
"chalk": "^5.2.0",
"debug": "^4.3.4",
"evt": "^2.4.13",
"get-caller-file": "^2.0.5",
"node-fetch": "2.6.x",
"slug": "^6.0.0",
"terminal-link": "^3.0.0",
+23
View File
@@ -1,9 +1,11 @@
import { TriggerClient } from "../client";
import { LogLevel } from "internal-bridge";
import { TriggerEvent } from "../events";
import chalk from "chalk";
import type { TriggerContext } from "../types";
import { z } from "zod";
import terminalLink from "terminal-link";
export type TriggerOptions<TSchema extends z.ZodTypeAny> = {
id: string;
@@ -31,6 +33,23 @@ export class Trigger<TSchema extends z.ZodTypeAny> {
}
async listen() {
if (this.#isMissingApiKey) {
console.log(
`${chalk.red("Trigger.dev error")}: ${chalk.bold(
this.id
)} is missing an API key, please set the TRIGGER_API_KEY environment variable or pass the apiKey option to the Trigger constructor. ${terminalLink(
"Get your API key here",
"https://app.trigger.dev",
{
fallback(text, url) {
return `${text} 👉 ${url}`;
},
}
)}`
);
return;
}
if (!this.#client) {
this.#client = new TriggerClient(this, this.options);
}
@@ -53,4 +72,8 @@ export class Trigger<TSchema extends z.ZodTypeAny> {
get on() {
return this.options.on;
}
get #isMissingApiKey() {
return !this.options.apiKey && !process.env.TRIGGER_API_KEY;
}
}
+4
View File
@@ -1187,8 +1187,10 @@ importers:
'@types/slug': ^5.0.3
'@types/uuid': ^9.0.0
'@types/ws': ^8.5.3
chalk: ^5.2.0
debug: ^4.3.4
evt: ^2.4.13
get-caller-file: ^2.0.5
internal-bridge: workspace:*
node-fetch: 2.6.x
rimraf: ^3.0.2
@@ -1203,8 +1205,10 @@ importers:
zod-error: ^1.1.0
zod-to-json-schema: ^3.20.2
dependencies:
chalk: 5.2.0
debug: 4.3.4
evt: 2.4.13
get-caller-file: 2.0.5
node-fetch: 2.6.7
slug: 6.1.0
terminal-link: 3.0.0