@cdwr/fly-node 0.1.0

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.
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@cdwr/fly-node",
3
+ "version": "0.1.0",
4
+ "description": "The flyctl node wrapper for programmatic deployments to fly.io.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/codeware-sthlm/codeware.git",
8
+ "directory": "packages/fly-node"
9
+ },
10
+ "author": {
11
+ "name": "Codeware Sthlm",
12
+ "email": "hello@codeware.se",
13
+ "url": "https://codeware.se"
14
+ },
15
+ "license": "MIT",
16
+ "keywords": [
17
+ "Fly",
18
+ "Flyctl",
19
+ "Fly.io",
20
+ "Node",
21
+ "Zod"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "dependencies": {
28
+ "zod": "^3.23.8"
29
+ },
30
+ "exports": {
31
+ ".": {
32
+ "types": "./src/index.d.ts",
33
+ "import": "./index.js",
34
+ "require": "./index.cjs"
35
+ }
36
+ },
37
+ "types": "./src/index.d.ts",
38
+ "main": "./index.js",
39
+ "module": "./index.js"
40
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/fly.class';
2
+ export type { Config, DeployAppOptions, DeployResponse } from './lib/types';
@@ -0,0 +1,358 @@
1
+ import type { AppOrConfig, Config, CreateAppOptions, DeployAppOptions, DeployResponse, DestroyAppOptions, ListAppResponse, ListCertForAllResponse, ListCertForAppResponse, ListSecretForAllResponse, ListSecretForAppResponse, SetAppSecretsOptions, ShowConfigOptions, ShowConfigResponse, StatusExtendedResponse, StatusResponse, UnsetAppSecretsOptions } from './types';
2
+ /**
3
+ * Manages deployments to Fly.io using the `flyctl` CLI tool.
4
+ */
5
+ export declare class Fly {
6
+ /** Use underscore to avoid conflict with public `config` property */
7
+ private _config;
8
+ /**
9
+ * Whether a user is authenticated via local login.
10
+ * In this case a token is not needed, though it can still have been provided.
11
+ */
12
+ private authByLogin;
13
+ private initialized;
14
+ private logger;
15
+ /**
16
+ * Get the app name from the instance config
17
+ */
18
+ private get instanceApp();
19
+ /**
20
+ * Get the config file path from the instance config
21
+ */
22
+ private get instanceConfig();
23
+ constructor(config?: Config);
24
+ /**
25
+ * Verify that the Fly client is ready to use.
26
+ *
27
+ * This means that the Fly CLI is installed and authenticated to Fly.io.
28
+ *
29
+ * Use as ad-hoc check when needed instead of waiting for the first Fly command to run.
30
+ *
31
+ * @param mode - Whether to use assertion mode instead of returning `false`
32
+ * @returns `true` if the Fly client is ready, `false` otherwise
33
+ * @throws An error in assertion mode if the Fly client is not ready
34
+ */
35
+ isReady(mode?: 'assert'): Promise<boolean>;
36
+ /**
37
+ * Manage the Fly CLI tool
38
+ */
39
+ cli: {
40
+ /**
41
+ * Check if the Fly CLI tool is installed
42
+ *
43
+ * @returns `true` if the Fly CLI tool is installed, `false` otherwise
44
+ */
45
+ isInstalled: () => Promise<boolean>;
46
+ };
47
+ /**
48
+ * Manage apps
49
+ */
50
+ apps: {
51
+ /**
52
+ * Create a new application with a generated name when not provided.
53
+ *
54
+ * @param options - Options for creating an application
55
+ * @returns The name of the created application
56
+ * @throws An error if the application cannot be created
57
+ */
58
+ create: (options?: CreateAppOptions) => Promise<string>;
59
+ /**
60
+ * Destroy an application and make sure it gets detached from any Postgres clusters
61
+ *
62
+ * @param app - The name of the app to destroy
63
+ * @param options - Options for destroying an application
64
+ * @throws An error if the app cannot be destroyed
65
+ */
66
+ destroy: (app: string, options?: DestroyAppOptions) => Promise<void>;
67
+ /**
68
+ * List all applications
69
+ *
70
+ * @returns A list of applications
71
+ * @throws An error if listing applications fails
72
+ */
73
+ list: () => Promise<Array<ListAppResponse>>;
74
+ };
75
+ /**
76
+ * Manage certificates
77
+ */
78
+ certs: {
79
+ /**
80
+ * Add a certificate for a domain to an application
81
+ *
82
+ * @param hostname - The hostname to add a certificate for
83
+ * @param options - Options for adding a certificate
84
+ * @throws An error if the certificate cannot be added
85
+ */
86
+ add: (hostname: string, options?: AppOrConfig) => Promise<void>;
87
+ /**
88
+ * List certificates for an application
89
+ *
90
+ * @param options - List certificates for an application or all certificates
91
+ * @returns A list of certificates
92
+ * @throws An error if listing certificates fails
93
+ */
94
+ list: <T extends AppOrConfig | "all">(options?: T) => Promise<T extends "all" ? Array<ListCertForAllResponse> : Array<ListCertForAppResponse>>;
95
+ /**
96
+ * Remove a certificate for a domain from an application
97
+ *
98
+ * @param hostname - The hostname to remove a certificate for
99
+ * @param options - Options for removing a certificate
100
+ * @throws An error if the certificate cannot be removed
101
+ */
102
+ remove: (hostname: string, options?: AppOrConfig) => Promise<void>;
103
+ };
104
+ /**
105
+ * Manage an app's configuration
106
+ */
107
+ config: {
108
+ /**
109
+ * Show an application's configuration.
110
+ *
111
+ * @param options - Options for showing an application's configuration
112
+ * @returns The application's JSON configuration
113
+ * @throws An error if the configuration cannot be found or parsed
114
+ */
115
+ show: (options?: ShowConfigOptions) => Promise<ShowConfigResponse>;
116
+ };
117
+ /**
118
+ * Deploy an application.
119
+ *
120
+ * A valid config file is required but the app doesn't have to exist,
121
+ * since it gets created if needed.
122
+ *
123
+ * The app name is selected from:
124
+ * 1. Provided app name
125
+ * 2. Instance app name
126
+ * 3. App name from the provided config file
127
+ *
128
+ * Existing secrets are preserved.
129
+ *
130
+ * @param options - Options for deploying an application
131
+ * @throws An error if the deployment fails
132
+ */
133
+ deploy: (options?: DeployAppOptions) => Promise<DeployResponse>;
134
+ /**
135
+ * Manage application secrets
136
+ */
137
+ secrets: {
138
+ /**
139
+ * Set secrets for an application
140
+ *
141
+ * @param secrets - The secrets to set
142
+ * @param options - Options for setting secrets
143
+ * @throws An error if the secrets cannot be set
144
+ */
145
+ set: (secrets: Record<string, string>, options?: SetAppSecretsOptions) => Promise<void>;
146
+ /**
147
+ * List secrets for an application
148
+ *
149
+ * @param options - List secrets for an application or all secrets
150
+ * @returns A list of secrets
151
+ * @throws An error if listing secrets fails
152
+ */
153
+ list: <T extends AppOrConfig | "all">(options?: T) => Promise<T extends "all" ? Array<ListSecretForAllResponse> : Array<ListSecretForAppResponse>>;
154
+ /**
155
+ * Unset secrets for an application
156
+ *
157
+ * @param keys - The keys to unset
158
+ * @param options - Options for unsetting secrets
159
+ * @throws An error if the secrets cannot be unset
160
+ */
161
+ unset: (keys: string | Array<string>, options?: UnsetAppSecretsOptions) => Promise<void>;
162
+ };
163
+ /**
164
+ * Get the status details of an application
165
+ *
166
+ * @param options - Options for getting app status
167
+ * @returns The app status, or `null` if an app could not be found
168
+ */
169
+ status: (options?: AppOrConfig) => Promise<StatusResponse | null>;
170
+ /**
171
+ * Get the extended details of an application
172
+ *
173
+ * @param options - Options for getting app details
174
+ * @returns The app details, or `null` if an app could not be found
175
+ */
176
+ statusExtended: (options?: AppOrConfig) => Promise<StatusExtendedResponse | null>;
177
+ /**
178
+ * @private
179
+ * A name will be generated when not provided.
180
+ * @returns The name of the created application
181
+ * @throws An error if the application cannot be created
182
+ */
183
+ private createApp;
184
+ /**
185
+ * @private
186
+ * @returns The name of the deployed application
187
+ * @throws An error if the deployment fails
188
+ */
189
+ private deployApp;
190
+ /**
191
+ * @private
192
+ * @throws An error if the Postgres database cannot be attached
193
+ */
194
+ private attachPostgres;
195
+ /**
196
+ * @private
197
+ * @throws An error if the app cannot be destroyed
198
+ */
199
+ private destroyApp;
200
+ /**
201
+ * @private
202
+ * @throws An error if the Postgres database cannot be detached from an app
203
+ */
204
+ private detachPostgres;
205
+ /**
206
+ * @private
207
+ * @returns The name of the application
208
+ * @throws An error if the secrets cannot be set
209
+ */
210
+ private setAppSecrets;
211
+ /**
212
+ * @private
213
+ * @returns The name of the application
214
+ * @throws An error if the secrets cannot be unset
215
+ */
216
+ private unsetAppSecrets;
217
+ /**
218
+ * @private
219
+ * @returns The name of the application
220
+ * @throws An error if the domain cannot be added
221
+ */
222
+ private addAppCertificate;
223
+ /**
224
+ * @private
225
+ * @returns The name of the application
226
+ * @throws An error if the domain cannot be removed
227
+ */
228
+ private removeAppCertificate;
229
+ /**
230
+ * @private
231
+ * @returns A list of all applications or `null` if listing applications fails and `nullOnError` is used
232
+ * @throws An error if listing applications fails and `throwOnError` is used
233
+ */
234
+ private fetchAllApps;
235
+ /**
236
+ * @private
237
+ * @returns All certificates for all apps or `null` if the certificates cannot be retrieved and `nullOnError` is used
238
+ * @throws An error if the certificates cannot be retrieved
239
+ */
240
+ private fetchAllCerts;
241
+ /**
242
+ * @private
243
+ * @returns All postgres clusters or `null` if the clusters cannot be retrieved and `nullOnError` is used
244
+ * @throws An error if the clusters cannot be retrieved
245
+ */
246
+ private fetchAllPostgres;
247
+ /**
248
+ * @private
249
+ * @returns All secrets for all apps or `null` if the secrets cannot be retrieved and `nullOnError` is used
250
+ * @throws An error if the secrets cannot be retrieved
251
+ */
252
+ private fetchAllSecrets;
253
+ /**
254
+ * @private
255
+ * @returns The app status or `null` if the app cannot be found and `nullOnError` is used
256
+ * @throws An error if the app cannot be found and `throwOnError` is used
257
+ */
258
+ private fetchAppStatus;
259
+ /**
260
+ * @private
261
+ * @returns The certificates for an app or `null` if the certificates cannot be retrieved and `nullOnError` is used
262
+ * @throws An error if the certificates cannot be retrieved and `throwOnError` is used
263
+ */
264
+ private fetchAppCerts;
265
+ /**
266
+ * @private
267
+ * @returns The secrets for an app or `null` if the secrets cannot be retrieved and `nullOnError` is used
268
+ * @throws An error if the secrets cannot be retrieved and `throwOnError` is used
269
+ */
270
+ private fetchAppSecrets;
271
+ /**
272
+ * **Important!** User/app names containing dashes are returned as underscores.
273
+ *
274
+ * @private
275
+ * @returns The users for a postgres database app or `null` if the users cannot be retrieved and `nullOnError` is used
276
+ * @throws An error if the users cannot be retrieved and `throwOnError` is used
277
+ */
278
+ private fetchPostgresUsers;
279
+ /**
280
+ * @private
281
+ * @returns The config or `null` if the config cannot be retrieved and `nullOnError` is used
282
+ * @throws An error if the config cannot be retrieved and `throwOnError` is used
283
+ */
284
+ private showConfig;
285
+ /**
286
+ * @private
287
+ * Ensure Fly CLI is installed and authenticated
288
+ *
289
+ * @throws An error if Fly CLI is not installed or authentication fails
290
+ */
291
+ private ensureInitialized;
292
+ /**
293
+ * @private
294
+ * Execute a fly command and optionally return the output as JSON
295
+ *
296
+ * @param command - The fly command to execute (empty array items are ignored)
297
+ * @returns The JSON output of the command, or the raw output if not JSON
298
+ * @throws An error if the command fails
299
+ */
300
+ private execFly;
301
+ /**
302
+ * @private
303
+ * Get the arguments for `AppOrConfig` options
304
+ * where either the application or configuration can be provided
305
+ * but not both.
306
+ *
307
+ * First check `options` and then the class configuration.
308
+ *
309
+ * Should be used in Fly commands with optional flags
310
+ * - `--app`
311
+ * - `--config`
312
+ *
313
+ * @param options - Options to get the arguments for
314
+ * @returns The arguments to use in a fly command
315
+ */
316
+ private getAppOrConfigArgs;
317
+ /**
318
+ * @private
319
+ * Get the Postgres clusters attached to an app
320
+ *
321
+ * @param app - The app to get the attached Postgres clusters for
322
+ * @returns The attached Postgres clusters
323
+ * @throws An error if the Postgres clusters cannot be retrieved
324
+ */
325
+ private getAttachedPostgresClusters;
326
+ /**
327
+ * @private
328
+ * Check if the Fly CLI is installed
329
+ *
330
+ * @returns `true` if the Fly CLI is installed, `false` otherwise
331
+ */
332
+ private isInstalled;
333
+ /**
334
+ * @private
335
+ * Normalize the options matching `AppOrConfig` type.
336
+ *
337
+ * A missing value gets an empty string.
338
+ *
339
+ * @param options - The options to normalize
340
+ * @returns The normalized options
341
+ */
342
+ private normalizeOptions;
343
+ /**
344
+ * Escape an argument for Fly cli
345
+ *
346
+ * @param arg - The argument to escape
347
+ * @returns The escaped argument
348
+ */
349
+ private safeArg;
350
+ /**
351
+ * @private
352
+ * Trace the CLI calls, mocks and results
353
+ *
354
+ * @param type - The type of trace
355
+ * @param msg - The message to trace
356
+ */
357
+ private traceCLI;
358
+ }
@@ -0,0 +1,93 @@
1
+ import type { CreateAppResponse, ListAppResponse, ListCertForAllResponse, ListCertForAppResponse, ListPostgresResponse, ListPostgresUsersResponse, ListSecretForAllResponse, ListSecretForAppResponse, StatusResponse } from './types';
2
+ /**
3
+ * Mock definitions for the tests which are used in the mock rules and data.
4
+ */
5
+ export declare const mockDefs: {
6
+ /** Test api token */
7
+ token: string;
8
+ /** Test organization used for all apps and configs */
9
+ org: string;
10
+ /** Test app is deployed and attached to postgres */
11
+ testApp: string;
12
+ /** Test config is available and the app is deployed with it */
13
+ testConfig: string;
14
+ /** New app does not exist to begin with but will be created and deployed */
15
+ newApp: string;
16
+ newConfig: string;
17
+ /** New app with a generated name */
18
+ generatedAppName: string;
19
+ /** Postgres cluster attached to test app */
20
+ postgresAttached: string;
21
+ /** Postgres cluster never attached to any app */
22
+ postgresNotAttached: string;
23
+ /** Unknown app that does not exist and will never be found */
24
+ unknownApp: string;
25
+ /** Unknown config that does not exist */
26
+ unknownConfig: string;
27
+ /** Test secret that exists in the app */
28
+ testSecret: string;
29
+ };
30
+ /**
31
+ * Mock `fly apps list` response for testing.
32
+ */
33
+ export declare const mockAppsListResponse: Array<ListAppResponse>;
34
+ /**
35
+ * Mock `fly apps create` response for testing.
36
+ *
37
+ * @param app - The app name to use in the response (defaults to `mockDefs.testApp`)
38
+ */
39
+ export declare const mockCreateAppResponse: (app?: string) => CreateAppResponse;
40
+ /**
41
+ * Mock `fly certs list` response for testing.
42
+ */
43
+ export declare const mockListCertForAppResponse: Array<ListCertForAppResponse>;
44
+ /**
45
+ * Mock `fly certs list` response for all apps for testing.
46
+ *
47
+ * All apps from `mockAppsListResponse` gets the same certificates from `mockListCertForAppResponse`
48
+ */
49
+ export declare const mockListCertForAllResponse: Array<ListCertForAllResponse>;
50
+ /**
51
+ * Mock `fly postgres list` response for testing.
52
+ */
53
+ export declare const mockListPostgresResponse: Array<ListPostgresResponse>;
54
+ /**
55
+ * Mock `fly postgres users list` response for testing.
56
+ */
57
+ export declare const mockListPostgresUsersResponse: Array<ListPostgresUsersResponse>;
58
+ /**
59
+ * Mock `fly secrets list` response for testing.
60
+ */
61
+ export declare const mockListSecretForAppResponse: Array<ListSecretForAppResponse>;
62
+ /**
63
+ * Mock `fly secrets list` response for all apps for testing.
64
+ *
65
+ * All apps from `mockAppsListResponse` gets the same secrets from `mockListSecretForAppResponse`
66
+ */
67
+ export declare const mockListSecretForAllResponse: Array<ListSecretForAllResponse>;
68
+ /**
69
+ * Mock `fly config show` response for testing.
70
+ *
71
+ * @param app - The app name to use in the response (defaults to `mockDefs.testApp`)
72
+ */
73
+ export declare const mockShowConfigResponse: (app?: string) => {
74
+ app: string;
75
+ primaryRegion: string;
76
+ build: {
77
+ dockerfile: string;
78
+ };
79
+ httpService: {
80
+ internalPort: number;
81
+ forceHttps: boolean;
82
+ autoStopMachines: boolean;
83
+ autoStartMachines: boolean;
84
+ minMachinesRunning: number;
85
+ processes: string[];
86
+ };
87
+ };
88
+ /**
89
+ * Mock `fly status` response for testing.
90
+ *
91
+ * @param app - The app name to use in the response (defaults to `mockDefs.testApp`)
92
+ */
93
+ export declare const mockStatusResponse: (app?: string) => StatusResponse;
@@ -0,0 +1,43 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Transformed apps create response schema
4
+ *
5
+ * ```ts
6
+ * fly apps create --app [name] --json
7
+ * ```
8
+ */
9
+ export declare const AppsCreateTransformedResponseSchema: z.ZodEffects<z.ZodObject<{
10
+ id: z.ZodString;
11
+ name: z.ZodString;
12
+ organization: z.ZodObject<{
13
+ id: z.ZodString;
14
+ name: z.ZodString;
15
+ }, "strip", z.ZodTypeAny, {
16
+ name: string;
17
+ id: string;
18
+ }, {
19
+ name: string;
20
+ id: string;
21
+ }>;
22
+ }, "strip", z.ZodTypeAny, {
23
+ name: string;
24
+ id: string;
25
+ organization: {
26
+ name: string;
27
+ id: string;
28
+ };
29
+ }, {
30
+ name: string;
31
+ id: string;
32
+ organization: {
33
+ name: string;
34
+ id: string;
35
+ };
36
+ }>, {
37
+ name: string;
38
+ id: string;
39
+ organization: {
40
+ name: string;
41
+ id: string;
42
+ };
43
+ }, unknown>;
@@ -0,0 +1,77 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Transformed apps list response schema
4
+ *
5
+ * ```ts
6
+ * fly apps list --json
7
+ * ```
8
+ */
9
+ export declare const AppsListTransformedResponseSchema: z.ZodArray<z.ZodEffects<z.ZodObject<{
10
+ id: z.ZodString;
11
+ name: z.ZodString;
12
+ status: z.ZodString;
13
+ deployed: z.ZodBoolean;
14
+ hostname: z.ZodString;
15
+ organization: z.ZodObject<{
16
+ name: z.ZodString;
17
+ slug: z.ZodString;
18
+ }, "strip", z.ZodTypeAny, {
19
+ name: string;
20
+ slug: string;
21
+ }, {
22
+ name: string;
23
+ slug: string;
24
+ }>;
25
+ currentRelease: z.ZodNullable<z.ZodObject<{
26
+ status: z.ZodString;
27
+ createdAt: z.ZodString;
28
+ }, "strip", z.ZodTypeAny, {
29
+ status: string;
30
+ createdAt: string;
31
+ }, {
32
+ status: string;
33
+ createdAt: string;
34
+ }>>;
35
+ }, "strip", z.ZodTypeAny, {
36
+ name: string;
37
+ status: string;
38
+ id: string;
39
+ organization: {
40
+ name: string;
41
+ slug: string;
42
+ };
43
+ deployed: boolean;
44
+ hostname: string;
45
+ currentRelease: {
46
+ status: string;
47
+ createdAt: string;
48
+ } | null;
49
+ }, {
50
+ name: string;
51
+ status: string;
52
+ id: string;
53
+ organization: {
54
+ name: string;
55
+ slug: string;
56
+ };
57
+ deployed: boolean;
58
+ hostname: string;
59
+ currentRelease: {
60
+ status: string;
61
+ createdAt: string;
62
+ } | null;
63
+ }>, {
64
+ name: string;
65
+ status: string;
66
+ id: string;
67
+ organization: {
68
+ name: string;
69
+ slug: string;
70
+ };
71
+ deployed: boolean;
72
+ hostname: string;
73
+ currentRelease: {
74
+ status: string;
75
+ createdAt: string;
76
+ } | null;
77
+ }, unknown>, "many">;
@@ -0,0 +1,26 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * certs list response extended with application name
4
+ */
5
+ export declare const CertsListWithAppTransformedResponseSchema: z.ZodArray<z.ZodEffects<z.ZodObject<z.objectUtil.extendShape<{
6
+ createdAt: z.ZodString;
7
+ hostname: z.ZodString;
8
+ clientStatus: z.ZodString;
9
+ }, {
10
+ app: z.ZodString;
11
+ }>, "strip", z.ZodTypeAny, {
12
+ hostname: string;
13
+ createdAt: string;
14
+ clientStatus: string;
15
+ app: string;
16
+ }, {
17
+ hostname: string;
18
+ createdAt: string;
19
+ clientStatus: string;
20
+ app: string;
21
+ }>, {
22
+ hostname: string;
23
+ createdAt: string;
24
+ clientStatus: string;
25
+ app: string;
26
+ }, unknown>, "many">;
@@ -0,0 +1,45 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Fly certs list response element schema
4
+ *
5
+ * ```ts
6
+ * fly certs list --app [name] --json
7
+ * ```
8
+ */
9
+ export declare const CertsListFlyResponseElementSchema: z.ZodObject<{
10
+ createdAt: z.ZodString;
11
+ hostname: z.ZodString;
12
+ clientStatus: z.ZodString;
13
+ }, "strip", z.ZodTypeAny, {
14
+ hostname: string;
15
+ createdAt: string;
16
+ clientStatus: string;
17
+ }, {
18
+ hostname: string;
19
+ createdAt: string;
20
+ clientStatus: string;
21
+ }>;
22
+ /**
23
+ * Transformed certs list response schema
24
+ *
25
+ * ```ts
26
+ * fly certs list --app [name] --json
27
+ * ```
28
+ */
29
+ export declare const CertsListTransformedResponseSchema: z.ZodArray<z.ZodEffects<z.ZodObject<{
30
+ createdAt: z.ZodString;
31
+ hostname: z.ZodString;
32
+ clientStatus: z.ZodString;
33
+ }, "strip", z.ZodTypeAny, {
34
+ hostname: string;
35
+ createdAt: string;
36
+ clientStatus: string;
37
+ }, {
38
+ hostname: string;
39
+ createdAt: string;
40
+ clientStatus: string;
41
+ }>, {
42
+ hostname: string;
43
+ createdAt: string;
44
+ clientStatus: string;
45
+ }, unknown>, "many">;