@project-ajax/sdk 0.0.28 → 0.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,24 +10,42 @@ export declare function parseEnvironment(name: string): Environment;
10
10
  export declare class TokenNotSetError extends Error {
11
11
  constructor(message?: string);
12
12
  }
13
- export declare function parseEnvironmentName(name: string): Environment;
13
+ /**
14
+ * Manages configuration for the CLI.
15
+ *
16
+ * Environment variables take precedence over the config file.
17
+ *
18
+ * | Environment Variable | Config File | Description |
19
+ * |----------------------|-------------|-------------|
20
+ * | WORKERS_TOKEN | token | The token to use for authentication |
21
+ * | WORKERS_ENVIRONMENT | environment | The environment to use |
22
+ * | WORKERS_WORKER_ID | workerId | The worker ID to use |
23
+ * | WORKERS_BASE_URL | baseUrl | The base URL to use |
24
+ */
14
25
  export declare class Config {
15
26
  #private;
16
- constructor(configFilePath: string, configFile: ConfigFile);
17
- setEnvironment(environment: Environment): Promise<void>;
18
- setBaseUrl(baseUrl: string): Promise<void>;
27
+ constructor(opts: {
28
+ configFilePath: string;
29
+ configFile: ConfigFile;
30
+ processEnv: NodeJS.ProcessEnv;
31
+ });
19
32
  get baseUrl(): string | undefined;
20
33
  get token(): string | null;
34
+ get environment(): "local" | "staging" | "dev" | "prod" | undefined;
35
+ get workerId(): string | null;
36
+ setEnvironment(environment: Environment): Promise<void>;
37
+ setBaseUrl(baseUrl: string): Promise<void>;
38
+ setToken(token: string | null): Promise<void>;
39
+ setWorkerId(workerId: string | null): Promise<void>;
21
40
  get tokenInfo(): {
22
41
  token: string;
23
42
  spaceId: string;
24
43
  cellId: string;
25
44
  };
26
- get environment(): "local" | "staging" | "dev" | "prod" | undefined;
27
- get workerId(): string | null;
28
- setToken(token: string | null): Promise<void>;
29
- setWorkerId(workerId: string | null): Promise<void>;
30
- static load(configFilePath: string): Promise<Config>;
45
+ static load(opts: {
46
+ configFilePath: string;
47
+ processEnv: NodeJS.ProcessEnv;
48
+ }): Promise<Config>;
31
49
  }
32
50
  export declare function extractPayloadFromToken(token: string): {
33
51
  spaceId: string;
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/cli/config.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,8CAA+C,CAAC;AACzE,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAExD,UAAU,UAAU;IACnB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAM1D;AAED,qBAAa,gBAAiB,SAAQ,KAAK;gBAEzC,OAAO,GAAE,MAA6D;CAKvE;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAM9D;AAED,qBAAa,MAAM;;gBAIN,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU;IAKpD,cAAc,CAAC,WAAW,EAAE,WAAW;IAKvC,UAAU,CAAC,OAAO,EAAE,MAAM;IAKhC,IAAI,OAAO,uBAEV;IAED,IAAI,KAAK,kBAER;IAED,IAAI,SAAS,IAAI;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAQlE;IAED,IAAI,WAAW,qDAEd;IAED,IAAI,QAAQ,kBAEX;IAEK,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAK7B,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;WAa5B,IAAI,CAAC,cAAc,EAAE,MAAM;CAyBxC;AAQD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG;IACvD,OAAO,EAAE,MAA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MAAM,CAAC,UAAU,CAAC;KAC9B;CA6BD;AAQD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG;IACvD,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CACf,CAoCA"}
@@ -13,46 +13,36 @@ class TokenNotSetError extends Error {
13
13
  this.name = "TokenNotSetError";
14
14
  }
15
15
  }
16
- function parseEnvironmentName(name) {
17
- if (["local", "staging", "dev", "prod"].includes(name)) {
18
- return name;
19
- }
20
- throw new Error(`Invalid environment name: ${name}`);
21
- }
22
16
  class Config {
23
17
  #configFile;
24
18
  #configFilePath;
25
- constructor(configFilePath, configFile) {
26
- this.#configFilePath = configFilePath;
27
- this.#configFile = configFile;
28
- }
29
- async setEnvironment(environment) {
30
- this.#configFile.environment = environment;
31
- await this.#writeConfigFile();
32
- }
33
- async setBaseUrl(baseUrl) {
34
- this.#configFile.baseUrl = baseUrl;
35
- await this.#writeConfigFile();
19
+ #env;
20
+ constructor(opts) {
21
+ this.#configFilePath = opts.configFilePath;
22
+ this.#configFile = opts.configFile;
23
+ this.#env = opts.processEnv;
36
24
  }
25
+ // Getters read from the environment variables, and then the config file.
37
26
  get baseUrl() {
38
- return this.#configFile.baseUrl;
27
+ return this.#env.WORKERS_BASE_URL ?? this.#configFile.baseUrl;
39
28
  }
40
29
  get token() {
41
- return this.#configFile.token;
42
- }
43
- get tokenInfo() {
44
- const token = this.#configFile.token;
45
- if (!token) {
46
- throw new TokenNotSetError();
47
- }
48
- const { spaceId, cellId } = extractPayloadFromToken(token);
49
- return { token, spaceId, cellId };
30
+ return this.#env.WORKERS_TOKEN ?? this.#configFile.token;
50
31
  }
51
32
  get environment() {
52
- return this.#configFile.environment;
33
+ return this.#env.WORKERS_ENVIRONMENT ? parseEnvironment(this.#env.WORKERS_ENVIRONMENT) : this.#configFile.environment;
53
34
  }
54
35
  get workerId() {
55
- return this.#configFile.workerId;
36
+ return this.#env.WORKERS_WORKER_ID ?? this.#configFile.workerId;
37
+ }
38
+ // Setters update the config file, and then write it to disk.
39
+ async setEnvironment(environment) {
40
+ this.#configFile.environment = environment;
41
+ await this.#writeConfigFile();
42
+ }
43
+ async setBaseUrl(baseUrl) {
44
+ this.#configFile.baseUrl = baseUrl;
45
+ await this.#writeConfigFile();
56
46
  }
57
47
  async setToken(token) {
58
48
  this.#configFile.token = token;
@@ -69,10 +59,22 @@ class Config {
69
59
  "utf-8"
70
60
  );
71
61
  }
72
- static async load(configFilePath) {
73
- const absConfigFilePath = path.resolve(process.cwd(), configFilePath);
62
+ get tokenInfo() {
63
+ const token = this.token;
64
+ if (!token) {
65
+ throw new TokenNotSetError();
66
+ }
67
+ const { spaceId, cellId } = extractPayloadFromToken(token);
68
+ return { token, spaceId, cellId };
69
+ }
70
+ static async load(opts) {
71
+ const absConfigFilePath = path.resolve(process.cwd(), opts.configFilePath);
74
72
  const configFile = await Config.#readConfigFile(absConfigFilePath);
75
- return new Config(absConfigFilePath, configFile);
73
+ return new Config({
74
+ configFilePath: absConfigFilePath,
75
+ configFile,
76
+ processEnv: opts.processEnv
77
+ });
76
78
  }
77
79
  static async #readConfigFile(configFilePath) {
78
80
  let configFile;
@@ -128,6 +130,5 @@ export {
128
130
  Environments,
129
131
  TokenNotSetError,
130
132
  extractPayloadFromToken,
131
- parseEnvironment,
132
- parseEnvironmentName
133
+ parseEnvironment
133
134
  };
@@ -1 +1 @@
1
- {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/cli/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CA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AC,KAAK,EAAE,KAAK,CAAC,IAAI,SAAS,QAAQ,GAAG,EAAE,EACzE,OAAO,EAAE,CACR,IAAI,EAAE,cAAc,EACpB,KAAK,EAAE,KAAK,EACZ,GAAG,IAAI,EAAE,IAAI,KACT,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,IAGxB,MAAM,YAAY,EAClB,OAAO,WAAW,GAAG,KAAK,EAC1B,GAAG,MAAM,IAAI,mBAWd;AAED,wBAAgB,kBAAkB,CACjC,KAAK,CAAC,KAAK,EACX,KAAK,CAAC,IAAI,SAAS,QAAQ,GAAG,EAAE,EAEhC,OAAO,EAAE,CACR,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,KAAK,EACZ,GAAG,IAAI,EAAE,IAAI,KACT,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,UAvBlB,YAAY,8DA2CnB"}
@@ -2,18 +2,21 @@ import { ApiClient } from "./api/client.js";
2
2
  import { Config } from "./config.js";
3
3
  function buildHandler(handler) {
4
4
  return async function(flags, ...args) {
5
- const config = await Config.load(flags.config ?? "./workers.json");
5
+ const config = await Config.load({
6
+ configFilePath: flags.config ?? "./workers.json",
7
+ processEnv: process.env
8
+ });
6
9
  this.writer.debugEnabled = flags.debug;
7
10
  await handler.call({ ...this, config }, flags, ...args);
8
11
  };
9
12
  }
10
13
  function buildAuthedHandler(handler) {
11
14
  return buildHandler(function(flags, ...args) {
15
+ const { token, cellId } = this.config.tokenInfo;
12
16
  const environment = this.config.environment;
13
17
  if (!environment) {
14
18
  throw new Error("Unexpected error: Environment not set");
15
19
  }
16
- const { token, cellId } = this.config.tokenInfo;
17
20
  const client = new ApiClient({
18
21
  token,
19
22
  environment,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@project-ajax/sdk",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "description": "An SDK for building workers for the Project Ajax platform",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
package/src/cli/config.ts CHANGED
@@ -28,57 +28,63 @@ export class TokenNotSetError extends Error {
28
28
  }
29
29
  }
30
30
 
31
- export function parseEnvironmentName(name: string): Environment {
32
- if (["local", "staging", "dev", "prod"].includes(name)) {
33
- return name as Environment;
34
- }
35
-
36
- throw new Error(`Invalid environment name: ${name}`);
37
- }
38
-
31
+ /**
32
+ * Manages configuration for the CLI.
33
+ *
34
+ * Environment variables take precedence over the config file.
35
+ *
36
+ * | Environment Variable | Config File | Description |
37
+ * |----------------------|-------------|-------------|
38
+ * | WORKERS_TOKEN | token | The token to use for authentication |
39
+ * | WORKERS_ENVIRONMENT | environment | The environment to use |
40
+ * | WORKERS_WORKER_ID | workerId | The worker ID to use |
41
+ * | WORKERS_BASE_URL | baseUrl | The base URL to use |
42
+ */
39
43
  export class Config {
40
44
  readonly #configFile: ConfigFile;
41
45
  readonly #configFilePath: string;
46
+ readonly #env: NodeJS.ProcessEnv;
42
47
 
43
- constructor(configFilePath: string, configFile: ConfigFile) {
44
- this.#configFilePath = configFilePath;
45
- this.#configFile = configFile;
46
- }
47
-
48
- async setEnvironment(environment: Environment) {
49
- this.#configFile.environment = environment;
50
- await this.#writeConfigFile();
48
+ constructor(opts: {
49
+ configFilePath: string;
50
+ configFile: ConfigFile;
51
+ processEnv: NodeJS.ProcessEnv;
52
+ }) {
53
+ this.#configFilePath = opts.configFilePath;
54
+ this.#configFile = opts.configFile;
55
+ this.#env = opts.processEnv;
51
56
  }
52
57
 
53
- async setBaseUrl(baseUrl: string) {
54
- this.#configFile.baseUrl = baseUrl;
55
- await this.#writeConfigFile();
56
- }
58
+ // Getters read from the environment variables, and then the config file.
57
59
 
58
60
  get baseUrl() {
59
- return this.#configFile.baseUrl;
61
+ return this.#env.WORKERS_BASE_URL ?? this.#configFile.baseUrl;
60
62
  }
61
63
 
62
64
  get token() {
63
- return this.#configFile.token;
65
+ return this.#env.WORKERS_TOKEN ?? this.#configFile.token;
64
66
  }
65
67
 
66
- get tokenInfo(): { token: string; spaceId: string; cellId: string } {
67
- const token = this.#configFile.token;
68
- if (!token) {
69
- throw new TokenNotSetError();
70
- }
68
+ get environment() {
69
+ return this.#env.WORKERS_ENVIRONMENT
70
+ ? parseEnvironment(this.#env.WORKERS_ENVIRONMENT)
71
+ : this.#configFile.environment;
72
+ }
71
73
 
72
- const { spaceId, cellId } = extractPayloadFromToken(token);
73
- return { token, spaceId, cellId };
74
+ get workerId() {
75
+ return this.#env.WORKERS_WORKER_ID ?? this.#configFile.workerId;
74
76
  }
75
77
 
76
- get environment() {
77
- return this.#configFile.environment;
78
+ // Setters update the config file, and then write it to disk.
79
+
80
+ async setEnvironment(environment: Environment) {
81
+ this.#configFile.environment = environment;
82
+ await this.#writeConfigFile();
78
83
  }
79
84
 
80
- get workerId() {
81
- return this.#configFile.workerId;
85
+ async setBaseUrl(baseUrl: string) {
86
+ this.#configFile.baseUrl = baseUrl;
87
+ await this.#writeConfigFile();
82
88
  }
83
89
 
84
90
  async setToken(token: string | null) {
@@ -99,10 +105,27 @@ export class Config {
99
105
  );
100
106
  }
101
107
 
102
- static async load(configFilePath: string) {
103
- const absConfigFilePath = path.resolve(process.cwd(), configFilePath);
108
+ get tokenInfo(): { token: string; spaceId: string; cellId: string } {
109
+ const token = this.token;
110
+ if (!token) {
111
+ throw new TokenNotSetError();
112
+ }
113
+
114
+ const { spaceId, cellId } = extractPayloadFromToken(token);
115
+ return { token, spaceId, cellId };
116
+ }
117
+
118
+ static async load(opts: {
119
+ configFilePath: string;
120
+ processEnv: NodeJS.ProcessEnv;
121
+ }) {
122
+ const absConfigFilePath = path.resolve(process.cwd(), opts.configFilePath);
104
123
  const configFile = await Config.#readConfigFile(absConfigFilePath);
105
- return new Config(absConfigFilePath, configFile);
124
+ return new Config({
125
+ configFilePath: absConfigFilePath,
126
+ configFile,
127
+ processEnv: opts.processEnv,
128
+ });
106
129
  }
107
130
 
108
131
  static async #readConfigFile(configFilePath: string) {
@@ -24,7 +24,10 @@ export function buildHandler<const FLAGS, const ARGS extends BaseArgs = []>(
24
24
  flags: GlobalFlags & FLAGS,
25
25
  ...args: ARGS
26
26
  ) {
27
- const config = await Config.load(flags.config ?? "./workers.json");
27
+ const config = await Config.load({
28
+ configFilePath: flags.config ?? "./workers.json",
29
+ processEnv: process.env,
30
+ });
28
31
 
29
32
  this.writer.debugEnabled = flags.debug;
30
33
 
@@ -43,13 +46,14 @@ export function buildAuthedHandler<
43
46
  ) => Promise<void> | void,
44
47
  ) {
45
48
  return buildHandler(function (flags: FLAGS, ...args: ARGS) {
49
+ // Check token first - this will throw TokenNotSetError with helpful message if not authenticated
50
+ const { token, cellId } = this.config.tokenInfo;
51
+
46
52
  const environment = this.config.environment;
47
53
  if (!environment) {
48
- // Unexpected to get here when token is set
49
54
  throw new Error("Unexpected error: Environment not set");
50
55
  }
51
56
 
52
- const { token, cellId } = this.config.tokenInfo;
53
57
  const client = new ApiClient({
54
58
  token,
55
59
  environment,