@cosmoner/sdk 1.2.0 → 1.3.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/README.md CHANGED
@@ -86,6 +86,44 @@ Set `maxRetries: 0` to disable retries entirely.
86
86
 
87
87
  \* At least one of `html` or `text` must be provided.
88
88
 
89
+ ## Apps
90
+
91
+ ```ts
92
+ const { data: apps } = await client.apps.list();
93
+ const web = apps.find((app) => app.name === "web")!;
94
+
95
+ const { data: started } = await client.apps.deploy(web.id, { tag: "v2" });
96
+ const finished = await client.apps.waitForDeployment(web.id, started.id);
97
+
98
+ if (finished.phase !== "ACTIVE") throw new Error(finished.error ?? finished.phase);
99
+ ```
100
+
101
+ ### `client.apps.deploy(appId, params?)`
102
+
103
+ Starts deploying an image app — one that runs an image from a Cosmoner registry
104
+ — and returns the deployment without waiting. Apps built from a repository are
105
+ refused. Needs an API key with `apps:write`.
106
+
107
+ | Parameter | Type | Required | Description |
108
+ | ----------- | -------- | -------- | ----------------------------------------------------- |
109
+ | `tag` | `string` | No | Tag to deploy from the app's repository |
110
+ | `digest` | `string` | No | Exact image, `sha256:<64 hex characters>` |
111
+ | `projectId` | `string` | No | Overrides the client-level project |
112
+
113
+ Pass `tag` or `digest`, not both. With neither, the image the app already names
114
+ is pulled again.
115
+
116
+ ### `client.apps.waitForDeployment(appId, deploymentId, params?)`
117
+
118
+ Polls until the deployment finishes and resolves with it in its final phase —
119
+ `ACTIVE`, `ERROR`, `CANCELED` or `SUPERSEDED`. It rejects only when a request
120
+ fails or `timeout` (default 10 minutes) passes; the deployment carries on either
121
+ way. `interval` defaults to 3 seconds, and `onPoll` is called with every result.
122
+ Needs `apps:read`.
123
+
124
+ `client.apps.list()` and `client.apps.getDeployment(appId, deploymentId)` are
125
+ the single calls underneath.
126
+
89
127
  ## Deployment files
90
128
 
91
129
  Validates a `.cosmoner/deployment.yaml` — the file you commit to describe how a
package/dist/client.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /** Entry-point client exposing the API's service namespaces. */
2
2
  import { type CosmonerConfig } from "./config";
3
+ import { AppsService } from "./services/apps";
3
4
  import { EmailService } from "./services/email";
4
5
  import { WebhooksService } from "./services/webhooks";
5
6
  /**
@@ -22,6 +23,7 @@ export declare class Cosmoner {
22
23
  readonly maxRetries: number;
23
24
  private readonly config;
24
25
  private readonly transport;
26
+ readonly apps: AppsService;
25
27
  readonly email: EmailService;
26
28
  readonly webhooks: WebhooksService;
27
29
  constructor(config: CosmonerConfig);
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { AuthenticationError, ConflictError, CosmonerConnectionError, CosmonerEr
5
5
  export { APP_SCHEMA_URL, DEPLOYMENT_FILE_PATHS, DEPLOYMENT_KEY_ORDER, DEPLOYMENT_VERSION, MAX_DEPLOYMENT_BYTES, } from "./deployment/spec";
6
6
  export { validateDeployment, validateDeploymentDocument, } from "./deployment/validate";
7
7
  export type { DeploymentBuild, DeploymentEnvVar, DeploymentIssue, DeploymentIssueSeverity, DeploymentService, DeploymentTemplate, DeploymentValidationResult, ValidateDeploymentOptions, } from "./deployment/types";
8
+ export { AppsService, FINISHED_DEPLOYMENT_PHASES, type App, type AppDeployment, type AppStatus, type DeployAppParams, type DeployAppResponse, type DeploymentPhase, type GetDeploymentResponse, type ImageDeployPolicy, type ListAppsResponse, type WaitForDeploymentParams, } from "./services/apps";
8
9
  export { EmailService, type SendEmailParams, type SendEmailResponse, } from "./services/email";
9
10
  export { WEBHOOK_EVENT_TYPES, WebhooksService, type CreateWebhookEndpointParams, type CreateWebhookEndpointResponse, type DeleteWebhookEndpointResponse, type GetWebhookEndpointResponse, type ListDeliveriesParams, type ListWebhookDeliveriesResponse, type ListWebhookEndpointsResponse, type ReplayWebhookDeliveryResponse, type ResumeWebhookEndpointResponse, type RotateWebhookSecretResponse, type TestWebhookEndpointResponse, type UpdateWebhookEndpointParams, type UpdateWebhookEndpointResponse, type WebhookDelivery, type WebhookDeliveryStatus, type WebhookDisabledReason, type WebhookEndpoint, type WebhookEndpointStats, type WebhookEndpointWithSecret, type WebhookEventType, } from "./services/webhooks";
10
11
  export { constructEvent, DEFAULT_TOLERANCE_SECONDS, DELIVERY_ID_HEADER, EVENT_TYPE_HEADER, SIGNATURE_HEADER, verifyWebhookSignature, type VerifyWebhookParams, type WebhookEvent, } from "./webhooks/signature";
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  APP_SCHEMA_URL: () => APP_SCHEMA_URL,
24
+ AppsService: () => AppsService,
24
25
  AuthenticationError: () => AuthenticationError,
25
26
  ConflictError: () => ConflictError,
26
27
  Cosmoner: () => Cosmoner,
@@ -37,6 +38,7 @@ __export(index_exports, {
37
38
  DEPLOYMENT_VERSION: () => DEPLOYMENT_VERSION,
38
39
  EVENT_TYPE_HEADER: () => EVENT_TYPE_HEADER,
39
40
  EmailService: () => EmailService,
41
+ FINISHED_DEPLOYMENT_PHASES: () => FINISHED_DEPLOYMENT_PHASES,
40
42
  InsufficientScopeError: () => InsufficientScopeError,
41
43
  MAX_DEPLOYMENT_BYTES: () => MAX_DEPLOYMENT_BYTES,
42
44
  NotFoundError: () => NotFoundError,
@@ -87,6 +89,89 @@ function resolveProjectId(config, override) {
87
89
  return projectId;
88
90
  }
89
91
 
92
+ // src/services/apps.ts
93
+ var FINISHED_DEPLOYMENT_PHASES = [
94
+ "ACTIVE",
95
+ "ERROR",
96
+ "CANCELED",
97
+ "SUPERSEDED"
98
+ ];
99
+ var DEFAULT_POLL_INTERVAL_MS = 3e3;
100
+ var DEFAULT_WAIT_TIMEOUT_MS = 10 * 6e4;
101
+ var TAG_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$/;
102
+ var DIGEST_PATTERN = /^sha256:[a-f0-9]{64}$/;
103
+ var AppsService = class {
104
+ constructor(transport, config) {
105
+ this.transport = transport;
106
+ this.config = config;
107
+ }
108
+ /** Builds the collection route for the resolved project. */
109
+ basePath(projectId) {
110
+ return `/v1/projects/${resolveProjectId(this.config, projectId)}/apps`;
111
+ }
112
+ /** Lists every app in the project, newest first. */
113
+ async list(params = {}) {
114
+ return this.transport.request("GET", this.basePath(params.projectId));
115
+ }
116
+ /**
117
+ * Starts deploying an image app and returns the deployment without waiting.
118
+ *
119
+ * Only image apps pulling from a Cosmoner registry can be deployed this way;
120
+ * the API rejects apps built from a repository. Pair with
121
+ * `waitForDeployment()` to learn whether the rollout succeeded.
122
+ */
123
+ async deploy(appId, params = {}) {
124
+ if (!appId) throw new Error("appId is required");
125
+ if (params.tag && params.digest) throw new Error("Pass either tag or digest, not both");
126
+ if (params.tag !== void 0 && !TAG_PATTERN.test(params.tag)) {
127
+ throw new Error(`Invalid image tag "${params.tag}"`);
128
+ }
129
+ if (params.digest !== void 0 && !DIGEST_PATTERN.test(params.digest)) {
130
+ throw new Error("digest must be sha256:<64 hex characters>");
131
+ }
132
+ return this.transport.request(
133
+ "POST",
134
+ `${this.basePath(params.projectId)}/${appId}/deployments`,
135
+ { body: { tag: params.tag, digest: params.digest } }
136
+ );
137
+ }
138
+ /** Fetches one deployment's current phase. */
139
+ async getDeployment(appId, deploymentId, params = {}) {
140
+ if (!appId) throw new Error("appId is required");
141
+ if (!deploymentId) throw new Error("deploymentId is required");
142
+ return this.transport.request(
143
+ "GET",
144
+ `${this.basePath(params.projectId)}/${appId}/deployments/${deploymentId}`
145
+ );
146
+ }
147
+ /**
148
+ * Polls a deployment until it finishes, and returns it in its final phase.
149
+ *
150
+ * Resolves for every finished phase, failures included — check `phase` for
151
+ * `ACTIVE`. Rejects only when the request fails or `timeout` passes first,
152
+ * in which case the deployment keeps going server-side.
153
+ */
154
+ async waitForDeployment(appId, deploymentId, params = {}) {
155
+ const interval = params.interval ?? DEFAULT_POLL_INTERVAL_MS;
156
+ const timeout = params.timeout ?? DEFAULT_WAIT_TIMEOUT_MS;
157
+ if (interval <= 0) throw new Error("interval must be greater than 0");
158
+ if (timeout <= 0) throw new Error("timeout must be greater than 0");
159
+ const deadline = Date.now() + timeout;
160
+ for (; ; ) {
161
+ const { data } = await this.getDeployment(appId, deploymentId, params);
162
+ params.onPoll?.(data);
163
+ if (FINISHED_DEPLOYMENT_PHASES.includes(data.phase)) return data;
164
+ const remaining = deadline - Date.now();
165
+ if (remaining <= 0) {
166
+ throw new Error(
167
+ `Deployment ${deploymentId} was still ${data.phase} after ${Math.round(timeout / 1e3)}s`
168
+ );
169
+ }
170
+ await new Promise((resolve) => setTimeout(resolve, Math.min(interval, remaining)));
171
+ }
172
+ }
173
+ };
174
+
90
175
  // src/services/email.ts
91
176
  var EmailService = class {
92
177
  constructor(transport, config) {
@@ -507,7 +592,7 @@ function sleep(ms) {
507
592
  }
508
593
 
509
594
  // src/version.ts
510
- var VERSION = "1.2.0";
595
+ var VERSION = "1.3.0";
511
596
  var USER_AGENT = `cosmoner-node/${VERSION}`;
512
597
 
513
598
  // src/transport.ts
@@ -629,6 +714,7 @@ var Cosmoner = class {
629
714
  this.timeout = this.config.timeout;
630
715
  this.maxRetries = this.config.maxRetries;
631
716
  this.transport = new Transport(this.config);
717
+ this.apps = new AppsService(this.transport, this.config);
632
718
  this.email = new EmailService(this.transport, this.config);
633
719
  this.webhooks = new WebhooksService(this.transport, this.config);
634
720
  }
@@ -970,6 +1056,7 @@ var index_default = Cosmoner;
970
1056
  // Annotate the CommonJS export names for ESM import in node:
971
1057
  0 && (module.exports = {
972
1058
  APP_SCHEMA_URL,
1059
+ AppsService,
973
1060
  AuthenticationError,
974
1061
  ConflictError,
975
1062
  Cosmoner,
@@ -986,6 +1073,7 @@ var index_default = Cosmoner;
986
1073
  DEPLOYMENT_VERSION,
987
1074
  EVENT_TYPE_HEADER,
988
1075
  EmailService,
1076
+ FINISHED_DEPLOYMENT_PHASES,
989
1077
  InsufficientScopeError,
990
1078
  MAX_DEPLOYMENT_BYTES,
991
1079
  NotFoundError,
package/dist/index.mjs CHANGED
@@ -29,6 +29,89 @@ function resolveProjectId(config, override) {
29
29
  return projectId;
30
30
  }
31
31
 
32
+ // src/services/apps.ts
33
+ var FINISHED_DEPLOYMENT_PHASES = [
34
+ "ACTIVE",
35
+ "ERROR",
36
+ "CANCELED",
37
+ "SUPERSEDED"
38
+ ];
39
+ var DEFAULT_POLL_INTERVAL_MS = 3e3;
40
+ var DEFAULT_WAIT_TIMEOUT_MS = 10 * 6e4;
41
+ var TAG_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$/;
42
+ var DIGEST_PATTERN = /^sha256:[a-f0-9]{64}$/;
43
+ var AppsService = class {
44
+ constructor(transport, config) {
45
+ this.transport = transport;
46
+ this.config = config;
47
+ }
48
+ /** Builds the collection route for the resolved project. */
49
+ basePath(projectId) {
50
+ return `/v1/projects/${resolveProjectId(this.config, projectId)}/apps`;
51
+ }
52
+ /** Lists every app in the project, newest first. */
53
+ async list(params = {}) {
54
+ return this.transport.request("GET", this.basePath(params.projectId));
55
+ }
56
+ /**
57
+ * Starts deploying an image app and returns the deployment without waiting.
58
+ *
59
+ * Only image apps pulling from a Cosmoner registry can be deployed this way;
60
+ * the API rejects apps built from a repository. Pair with
61
+ * `waitForDeployment()` to learn whether the rollout succeeded.
62
+ */
63
+ async deploy(appId, params = {}) {
64
+ if (!appId) throw new Error("appId is required");
65
+ if (params.tag && params.digest) throw new Error("Pass either tag or digest, not both");
66
+ if (params.tag !== void 0 && !TAG_PATTERN.test(params.tag)) {
67
+ throw new Error(`Invalid image tag "${params.tag}"`);
68
+ }
69
+ if (params.digest !== void 0 && !DIGEST_PATTERN.test(params.digest)) {
70
+ throw new Error("digest must be sha256:<64 hex characters>");
71
+ }
72
+ return this.transport.request(
73
+ "POST",
74
+ `${this.basePath(params.projectId)}/${appId}/deployments`,
75
+ { body: { tag: params.tag, digest: params.digest } }
76
+ );
77
+ }
78
+ /** Fetches one deployment's current phase. */
79
+ async getDeployment(appId, deploymentId, params = {}) {
80
+ if (!appId) throw new Error("appId is required");
81
+ if (!deploymentId) throw new Error("deploymentId is required");
82
+ return this.transport.request(
83
+ "GET",
84
+ `${this.basePath(params.projectId)}/${appId}/deployments/${deploymentId}`
85
+ );
86
+ }
87
+ /**
88
+ * Polls a deployment until it finishes, and returns it in its final phase.
89
+ *
90
+ * Resolves for every finished phase, failures included — check `phase` for
91
+ * `ACTIVE`. Rejects only when the request fails or `timeout` passes first,
92
+ * in which case the deployment keeps going server-side.
93
+ */
94
+ async waitForDeployment(appId, deploymentId, params = {}) {
95
+ const interval = params.interval ?? DEFAULT_POLL_INTERVAL_MS;
96
+ const timeout = params.timeout ?? DEFAULT_WAIT_TIMEOUT_MS;
97
+ if (interval <= 0) throw new Error("interval must be greater than 0");
98
+ if (timeout <= 0) throw new Error("timeout must be greater than 0");
99
+ const deadline = Date.now() + timeout;
100
+ for (; ; ) {
101
+ const { data } = await this.getDeployment(appId, deploymentId, params);
102
+ params.onPoll?.(data);
103
+ if (FINISHED_DEPLOYMENT_PHASES.includes(data.phase)) return data;
104
+ const remaining = deadline - Date.now();
105
+ if (remaining <= 0) {
106
+ throw new Error(
107
+ `Deployment ${deploymentId} was still ${data.phase} after ${Math.round(timeout / 1e3)}s`
108
+ );
109
+ }
110
+ await new Promise((resolve) => setTimeout(resolve, Math.min(interval, remaining)));
111
+ }
112
+ }
113
+ };
114
+
32
115
  // src/services/email.ts
33
116
  var EmailService = class {
34
117
  constructor(transport, config) {
@@ -449,7 +532,7 @@ function sleep(ms) {
449
532
  }
450
533
 
451
534
  // src/version.ts
452
- var VERSION = "1.2.0";
535
+ var VERSION = "1.3.0";
453
536
  var USER_AGENT = `cosmoner-node/${VERSION}`;
454
537
 
455
538
  // src/transport.ts
@@ -571,6 +654,7 @@ var Cosmoner = class {
571
654
  this.timeout = this.config.timeout;
572
655
  this.maxRetries = this.config.maxRetries;
573
656
  this.transport = new Transport(this.config);
657
+ this.apps = new AppsService(this.transport, this.config);
574
658
  this.email = new EmailService(this.transport, this.config);
575
659
  this.webhooks = new WebhooksService(this.transport, this.config);
576
660
  }
@@ -911,6 +995,7 @@ function result(issues, template, options) {
911
995
  var index_default = Cosmoner;
912
996
  export {
913
997
  APP_SCHEMA_URL,
998
+ AppsService,
914
999
  AuthenticationError,
915
1000
  ConflictError,
916
1001
  Cosmoner,
@@ -927,6 +1012,7 @@ export {
927
1012
  DEPLOYMENT_VERSION,
928
1013
  EVENT_TYPE_HEADER,
929
1014
  EmailService,
1015
+ FINISHED_DEPLOYMENT_PHASES,
930
1016
  InsufficientScopeError,
931
1017
  MAX_DEPLOYMENT_BYTES,
932
1018
  NotFoundError,
@@ -0,0 +1,105 @@
1
+ /** Apps service namespace — finding apps and rolling image apps onto a new image. */
2
+ import { type ResolvedConfig } from "../config";
3
+ import type { Transport } from "../transport";
4
+ /** Lifecycle state of an app as a whole. */
5
+ export type AppStatus = "DEPLOYING" | "RUNNING" | "STOPPED" | "ERROR" | "TERMINATED";
6
+ /** What a registry push does to an image app. */
7
+ export type ImageDeployPolicy = "TAG" | "NEWEST" | "MANUAL";
8
+ /**
9
+ * An app as the API lists it.
10
+ *
11
+ * Only the fields a deploy script is likely to read are typed; the API returns
12
+ * more, and they are present on the object at runtime.
13
+ */
14
+ export interface App {
15
+ id: string;
16
+ name: string;
17
+ subdomain: string;
18
+ status: AppStatus;
19
+ url: string | null;
20
+ /** Set for apps built from a repository, null for image apps. */
21
+ gitRepo: string | null;
22
+ /** The image an image app runs, null for apps built from a repository. */
23
+ containerImage: string | null;
24
+ imageDeployPolicy: ImageDeployPolicy;
25
+ createdAt: string;
26
+ updatedAt: string;
27
+ }
28
+ /** Phase of one deployment. */
29
+ export type DeploymentPhase = "PENDING" | "BUILDING" | "DEPLOYING" | "ACTIVE" | "ERROR" | "CANCELED" | "SUPERSEDED";
30
+ /** Phases after which a deployment will not change again. */
31
+ export declare const FINISHED_DEPLOYMENT_PHASES: readonly DeploymentPhase[];
32
+ /** One deployment of an app. */
33
+ export interface AppDeployment {
34
+ id: string;
35
+ phase: DeploymentPhase;
36
+ /** Why it happened, e.g. "registry push" or "api deploy". */
37
+ cause: string | null;
38
+ /** The image reference that was deployed. */
39
+ imageRef: string | null;
40
+ imageDigest: string | null;
41
+ error: string | null;
42
+ startedAt: string;
43
+ finishedAt: string | null;
44
+ }
45
+ /** Options accepted by every method, for working across projects. */
46
+ export interface ProjectScopedParams {
47
+ /** Overrides the client-level default project for this call. */
48
+ projectId?: string;
49
+ }
50
+ /**
51
+ * Arguments accepted by `client.apps.deploy()`.
52
+ *
53
+ * Pass `tag` or `digest` to roll onto that image from the repository the app
54
+ * already pulls from, or neither to re-resolve the image the app names now.
55
+ */
56
+ export interface DeployAppParams extends ProjectScopedParams {
57
+ tag?: string;
58
+ /** `sha256:` followed by 64 hex characters. */
59
+ digest?: string;
60
+ }
61
+ /** Options accepted by `client.apps.waitForDeployment()`. */
62
+ export interface WaitForDeploymentParams extends ProjectScopedParams {
63
+ /** Milliseconds between polls. Defaults to 3s. */
64
+ interval?: number;
65
+ /** Milliseconds to wait in total before giving up. Defaults to 10 minutes. */
66
+ timeout?: number;
67
+ /** Called with every poll result, including the last. */
68
+ onPoll?: (deployment: AppDeployment) => void;
69
+ }
70
+ interface Envelope<T> {
71
+ success: true;
72
+ data: T;
73
+ }
74
+ export type ListAppsResponse = Envelope<App[]>;
75
+ export type DeployAppResponse = Envelope<AppDeployment>;
76
+ export type GetDeploymentResponse = Envelope<AppDeployment>;
77
+ /** App lookup and image deploy operations for a project. */
78
+ export declare class AppsService {
79
+ private readonly transport;
80
+ private readonly config;
81
+ constructor(transport: Transport, config: ResolvedConfig);
82
+ /** Builds the collection route for the resolved project. */
83
+ private basePath;
84
+ /** Lists every app in the project, newest first. */
85
+ list(params?: ProjectScopedParams): Promise<ListAppsResponse>;
86
+ /**
87
+ * Starts deploying an image app and returns the deployment without waiting.
88
+ *
89
+ * Only image apps pulling from a Cosmoner registry can be deployed this way;
90
+ * the API rejects apps built from a repository. Pair with
91
+ * `waitForDeployment()` to learn whether the rollout succeeded.
92
+ */
93
+ deploy(appId: string, params?: DeployAppParams): Promise<DeployAppResponse>;
94
+ /** Fetches one deployment's current phase. */
95
+ getDeployment(appId: string, deploymentId: string, params?: ProjectScopedParams): Promise<GetDeploymentResponse>;
96
+ /**
97
+ * Polls a deployment until it finishes, and returns it in its final phase.
98
+ *
99
+ * Resolves for every finished phase, failures included — check `phase` for
100
+ * `ACTIVE`. Rejects only when the request fails or `timeout` passes first,
101
+ * in which case the deployment keeps going server-side.
102
+ */
103
+ waitForDeployment(appId: string, deploymentId: string, params?: WaitForDeploymentParams): Promise<AppDeployment>;
104
+ }
105
+ export {};
package/dist/version.d.ts CHANGED
@@ -8,5 +8,5 @@
8
8
  * the release workflow runs that straight after `npm version`, and
9
9
  * `version.test.ts` fails the build if the two ever disagree.
10
10
  */
11
- export declare const VERSION = "1.2.0";
12
- export declare const USER_AGENT = "cosmoner-node/1.2.0";
11
+ export declare const VERSION = "1.3.0";
12
+ export declare const USER_AGENT = "cosmoner-node/1.3.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmoner/sdk",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Cosmoner SDK for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -30,6 +30,11 @@
30
30
  "cosmoner",
31
31
  "sdk"
32
32
  ],
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/datablock-dev/cosmoner-sdk.git",
36
+ "directory": "javascript"
37
+ },
33
38
  "license": "MIT",
34
39
  "dependencies": {
35
40
  "yaml": "^2.6.0"