@antelopejs/cloud-sdk 0.0.0-stage → 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/README.md +63 -2
- package/dist/api-error.d.ts +21 -0
- package/dist/api-error.js +27 -0
- package/dist/api.d.ts +193 -0
- package/dist/api.js +224 -0
- package/dist/environments.d.ts +48 -0
- package/dist/environments.js +81 -0
- package/dist/generated/cloud-api.d.ts +1612 -0
- package/dist/generated/cloud-api.js +6 -0
- package/dist/guards.d.ts +9 -0
- package/dist/guards.js +16 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/resources.d.ts +43 -0
- package/dist/resources.js +2 -0
- package/dist/result.d.ts +25 -0
- package/dist/result.js +36 -0
- package/dist/sse.d.ts +6 -0
- package/dist/sse.js +45 -0
- package/openapi/cloud.openapi.yaml +556 -0
- package/openapi/environments.yaml +341 -0
- package/openapi/resources.openapi.yaml +274 -0
- package/openapi/variables.parts.yaml +127 -0
- package/package.json +68 -4
package/README.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@antelopejs/cloud-sdk`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A typed client for the AntelopeJS Cloud API: the client the `ajs-cloud` CLI
|
|
4
|
+
uses, without the CLI. Its only runtime dependency is `openapi-fetch`.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @antelopejs/cloud-sdk
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Node.js 20.19 or later is required.
|
|
11
|
+
|
|
12
|
+
## Client
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { CloudApi, CloudApiError } from "@antelopejs/cloud-sdk";
|
|
16
|
+
|
|
17
|
+
const api = new CloudApi({ token: process.env.ANTELOPE_CLOUD_TOKEN! });
|
|
18
|
+
try {
|
|
19
|
+
const projects = await api.listProjects();
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (error instanceof CloudApiError) console.error(error.status, error.code);
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The client talks to `https://api.antelopejs.cloud` (`DEFAULT_API_URL`). Pass
|
|
26
|
+
`baseUrl` to reach another deployment, and `fetch` to send the requests
|
|
27
|
+
yourself, such as through a proxy or a test double.
|
|
28
|
+
|
|
29
|
+
Every refusal is a `CloudApiError` with the HTTP `status`, the API's `code`,
|
|
30
|
+
the `params` its message interpolates, and `retryAfterS` when the API asked to
|
|
31
|
+
wait. An upload the API accepts with an answer that is not a deployment receipt
|
|
32
|
+
is an `InvalidApiResponseError`. `@antelopejs/cloud-sdk/errors` exports both
|
|
33
|
+
classes without loading the client.
|
|
34
|
+
|
|
35
|
+
## Device login
|
|
36
|
+
|
|
37
|
+
A device login (RFC 8628) gets a token without handling a password:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { CloudApiError, createDevice, exchangeDevice } from "@antelopejs/cloud-sdk";
|
|
41
|
+
|
|
42
|
+
const PENDING = new Set(["authorization_pending", "slow_down"]);
|
|
43
|
+
const device = await createDevice("My tool");
|
|
44
|
+
console.log(`Approve ${device.user_code} at ${device.verification_uri_complete}`);
|
|
45
|
+
let token;
|
|
46
|
+
while (!token) {
|
|
47
|
+
await new Promise((done) => setTimeout(done, device.interval * 1000));
|
|
48
|
+
token = await exchangeDevice(device.device_code).catch((error) => {
|
|
49
|
+
if (error instanceof CloudApiError && PENDING.has(error.code)) return undefined;
|
|
50
|
+
throw error;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Until a person approves the code, `exchangeDevice` refuses with
|
|
56
|
+
`authorization_pending`, or `slow_down` when polled too fast: RFC 8628 then
|
|
57
|
+
asks for five more seconds between polls. Both functions take the same
|
|
58
|
+
`baseUrl` and `fetch` options as the client.
|
|
59
|
+
|
|
60
|
+
## Types
|
|
61
|
+
|
|
62
|
+
`paths`, `components` and `operations` are the types generated from
|
|
63
|
+
`openapi/cloud.openapi.yaml`, which the package also ships with the other
|
|
64
|
+
files of `openapi/` that it references.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Values an API error message interpolates, by name. */
|
|
2
|
+
export type ApiErrorParams = Readonly<Record<string, string>>;
|
|
3
|
+
/** A request the Cloud API answered with an error status. */
|
|
4
|
+
export declare class CloudApiError extends Error {
|
|
5
|
+
readonly status: number;
|
|
6
|
+
readonly code: string;
|
|
7
|
+
/** Values the refusal names, such as the Node.js versions it supports. */
|
|
8
|
+
readonly params: ApiErrorParams;
|
|
9
|
+
/** Seconds the API asked to wait (`Retry-After`), when it sent them. */
|
|
10
|
+
readonly retryAfterS?: number | undefined;
|
|
11
|
+
constructor(status: number, code: string, body?: string,
|
|
12
|
+
/** Values the refusal names, such as the Node.js versions it supports. */
|
|
13
|
+
params?: ApiErrorParams,
|
|
14
|
+
/** Seconds the API asked to wait (`Retry-After`), when it sent them. */
|
|
15
|
+
retryAfterS?: number | undefined);
|
|
16
|
+
}
|
|
17
|
+
/** An answer the API accepted a request with, in a shape this client cannot read. */
|
|
18
|
+
export declare class InvalidApiResponseError extends Error {
|
|
19
|
+
readonly code = "invalid_api_response";
|
|
20
|
+
constructor();
|
|
21
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** A request the Cloud API answered with an error status. */
|
|
2
|
+
export class CloudApiError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
params;
|
|
6
|
+
retryAfterS;
|
|
7
|
+
constructor(status, code, body,
|
|
8
|
+
/** Values the refusal names, such as the Node.js versions it supports. */
|
|
9
|
+
params = {},
|
|
10
|
+
/** Seconds the API asked to wait (`Retry-After`), when it sent them. */
|
|
11
|
+
retryAfterS) {
|
|
12
|
+
super(body || code);
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.params = params;
|
|
16
|
+
this.retryAfterS = retryAfterS;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** An answer the API accepted a request with, in a shape this client cannot read. */
|
|
20
|
+
export class InvalidApiResponseError extends Error {
|
|
21
|
+
code = "invalid_api_response";
|
|
22
|
+
constructor() {
|
|
23
|
+
super("invalid_api_response");
|
|
24
|
+
this.name = "InvalidApiResponseError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=api-error.js.map
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { EnvironmentsApi, ProjectRepositoryApi } from "./environments.js";
|
|
2
|
+
import type { components } from "./generated/cloud-api.js";
|
|
3
|
+
import { type SseEvent } from "./sse.js";
|
|
4
|
+
/** What the API answers to an accepted source upload. */
|
|
5
|
+
export interface DeploymentReceipt {
|
|
6
|
+
deploymentId: string;
|
|
7
|
+
buildId: string;
|
|
8
|
+
status: "queued";
|
|
9
|
+
}
|
|
10
|
+
export type DeviceAuthorization = components["schemas"]["DeviceAuthorization"];
|
|
11
|
+
export type DeviceToken = components["schemas"]["DeviceToken"];
|
|
12
|
+
export type EnvironmentVariable = components["schemas"]["EnvironmentVariable"];
|
|
13
|
+
export type EnvironmentVariableList = components["schemas"]["EnvironmentVariableList"];
|
|
14
|
+
export type NewProject = components["schemas"]["NewProject"];
|
|
15
|
+
export type NewUploadService = components["schemas"]["NewUploadService"];
|
|
16
|
+
export type VariableOrigin = components["schemas"]["VariableOrigin"];
|
|
17
|
+
export type VariableWrite = components["schemas"]["VariableWrite"];
|
|
18
|
+
export type WorkspaceProvisioning = components["schemas"]["WorkspaceProvisioning"];
|
|
19
|
+
/** The production Cloud API, used when no `baseUrl` is given. */
|
|
20
|
+
export declare const DEFAULT_API_URL = "https://api.antelopejs.cloud";
|
|
21
|
+
/** Where requests go and how they are sent, without a session. */
|
|
22
|
+
export interface EndpointOptions {
|
|
23
|
+
/** The API origin, such as a test deployment. Defaults to {@link DEFAULT_API_URL}. */
|
|
24
|
+
baseUrl?: string;
|
|
25
|
+
/** Sends every request, in place of the global `fetch`. */
|
|
26
|
+
fetch?: typeof fetch;
|
|
27
|
+
}
|
|
28
|
+
/** A client signed in with a token. */
|
|
29
|
+
export interface CloudApiOptions extends EndpointOptions {
|
|
30
|
+
/** The bearer token a device login or the console issued. */
|
|
31
|
+
token: string;
|
|
32
|
+
}
|
|
33
|
+
/** How a log read is shaped: one bounded page, or a tail from a cursor. */
|
|
34
|
+
export interface LogRequest {
|
|
35
|
+
follow: boolean;
|
|
36
|
+
lastEventId?: string;
|
|
37
|
+
/** Called once the API accepted the read, before its first frame. */
|
|
38
|
+
onOpen?: () => void;
|
|
39
|
+
}
|
|
40
|
+
/** The Cloud API, signed in with one token. Every refusal is a {@link CloudApiError}. */
|
|
41
|
+
export declare class CloudApi {
|
|
42
|
+
private readonly options;
|
|
43
|
+
private readonly client;
|
|
44
|
+
private readonly baseUrl;
|
|
45
|
+
private readonly send;
|
|
46
|
+
/** A project's environments: the branches Cloud builds and serves, their promotion and health. */
|
|
47
|
+
readonly environments: EnvironmentsApi;
|
|
48
|
+
/** The GitHub repository a project builds from. */
|
|
49
|
+
readonly projectRepository: ProjectRepositoryApi;
|
|
50
|
+
constructor(options: CloudApiOptions);
|
|
51
|
+
listProjects(): Promise<{
|
|
52
|
+
projects: {
|
|
53
|
+
id: string;
|
|
54
|
+
slug: string;
|
|
55
|
+
status: string;
|
|
56
|
+
}[];
|
|
57
|
+
}>;
|
|
58
|
+
readProject(projectId: string): Promise<{
|
|
59
|
+
id: string;
|
|
60
|
+
slug: string;
|
|
61
|
+
region: string;
|
|
62
|
+
environments: components["schemas"]["Environment"][];
|
|
63
|
+
}>;
|
|
64
|
+
workspaceProvisioning(): Promise<{
|
|
65
|
+
phase: "pending" | "reconciling" | "ready" | "blocked" | "revoking" | "revoked";
|
|
66
|
+
owner: string | null;
|
|
67
|
+
reasonCode: string | null;
|
|
68
|
+
retryAt: string | null;
|
|
69
|
+
canRetry: boolean;
|
|
70
|
+
}>;
|
|
71
|
+
readDeployment(deploymentId: string): Promise<{
|
|
72
|
+
id: string;
|
|
73
|
+
status: string;
|
|
74
|
+
failure: {
|
|
75
|
+
key: string;
|
|
76
|
+
params?: {
|
|
77
|
+
[key: string]: unknown;
|
|
78
|
+
};
|
|
79
|
+
} | null;
|
|
80
|
+
builds?: components["schemas"]["DeploymentBuild"][];
|
|
81
|
+
}>;
|
|
82
|
+
onboardingState(): Promise<{
|
|
83
|
+
canCreateProjects: boolean;
|
|
84
|
+
regions: string[];
|
|
85
|
+
defaultRegion: string;
|
|
86
|
+
}>;
|
|
87
|
+
createProject(body: NewProject): Promise<{
|
|
88
|
+
projectId: string;
|
|
89
|
+
slug: string;
|
|
90
|
+
region: string;
|
|
91
|
+
environmentId: string;
|
|
92
|
+
serviceId?: string | null;
|
|
93
|
+
}>;
|
|
94
|
+
createService(projectId: string, environmentId: string, body: NewUploadService): Promise<{
|
|
95
|
+
serviceId: string;
|
|
96
|
+
name: string;
|
|
97
|
+
appName: string;
|
|
98
|
+
}>;
|
|
99
|
+
listVariables(projectId: string, environmentId: string): Promise<{
|
|
100
|
+
variables: components["schemas"]["EnvironmentVariable"][];
|
|
101
|
+
}>;
|
|
102
|
+
/** Secret values are write-only: the answer never carries one. */
|
|
103
|
+
setVariable(projectId: string, environmentId: string, serviceId: string, key: string, body: VariableWrite): Promise<{
|
|
104
|
+
serviceId: string;
|
|
105
|
+
key: string;
|
|
106
|
+
secret: boolean;
|
|
107
|
+
value: string | null;
|
|
108
|
+
origin: components["schemas"]["VariableOrigin"];
|
|
109
|
+
}>;
|
|
110
|
+
deleteVariable(projectId: string, environmentId: string, serviceId: string, key: string): Promise<void>;
|
|
111
|
+
/** The databases an environment runs, with their size, keep and cost. */
|
|
112
|
+
environmentResources(projectId: string, environmentId: string): Promise<{
|
|
113
|
+
environmentId: string;
|
|
114
|
+
resources: components["schemas"]["EnvironmentResource"][];
|
|
115
|
+
}>;
|
|
116
|
+
/**
|
|
117
|
+
* What applying the project's modules would do to an environment: the
|
|
118
|
+
* databases to create, those that already serve a module, the needs Cloud
|
|
119
|
+
* cannot provide, the monthly cost and the service limit.
|
|
120
|
+
*
|
|
121
|
+
* @param modules The npm package names of the modules in `antelope.config.ts`
|
|
122
|
+
*/
|
|
123
|
+
planResources(projectId: string, environmentId: string, modules: readonly string[]): Promise<{
|
|
124
|
+
create: components["schemas"]["PlannedResource"][];
|
|
125
|
+
existing: components["schemas"]["ExistingResource"][];
|
|
126
|
+
unprovided: components["schemas"]["UnprovidedResource"][];
|
|
127
|
+
cost: components["schemas"]["MonthlyCost"] | null;
|
|
128
|
+
limits: components["schemas"]["ResourcePlanLimits"];
|
|
129
|
+
refusals: components["schemas"]["ResourcePlanRefusal"][];
|
|
130
|
+
canApply: boolean;
|
|
131
|
+
}>;
|
|
132
|
+
/**
|
|
133
|
+
* Creates the databases the modules need. The server computes the plan
|
|
134
|
+
* again from the environment's state; it refuses with 409 before any write
|
|
135
|
+
* when the plan would pass the environment's service limit.
|
|
136
|
+
*
|
|
137
|
+
* @param modules The npm package names of the modules in `antelope.config.ts`
|
|
138
|
+
*/
|
|
139
|
+
applyResources(projectId: string, environmentId: string, modules: readonly string[]): Promise<{
|
|
140
|
+
created: components["schemas"]["EnvironmentResource"][];
|
|
141
|
+
plan: components["schemas"]["ResourcePlan"];
|
|
142
|
+
}>;
|
|
143
|
+
serviceSettings(serviceId: string): Promise<{
|
|
144
|
+
serviceId: string;
|
|
145
|
+
scaling: {
|
|
146
|
+
bounds: components["schemas"]["ServiceBounds"];
|
|
147
|
+
replicaLimit: number;
|
|
148
|
+
pricing: components["schemas"]["ServicePricing"] | null;
|
|
149
|
+
};
|
|
150
|
+
volumeLimits: {
|
|
151
|
+
minGb: number;
|
|
152
|
+
maxGb: number;
|
|
153
|
+
removedRetentionDays: number;
|
|
154
|
+
};
|
|
155
|
+
}>;
|
|
156
|
+
deploymentHistory(serviceId: string): Promise<{
|
|
157
|
+
activeBuilds?: components["schemas"]["ActiveBuild"][];
|
|
158
|
+
desiredRevisionId: string | null;
|
|
159
|
+
revisions: {
|
|
160
|
+
id: string;
|
|
161
|
+
number: number;
|
|
162
|
+
isDesired: boolean;
|
|
163
|
+
canRollback: boolean;
|
|
164
|
+
state?: "preparing" | "pending" | "succeeded" | "failed";
|
|
165
|
+
deploymentId?: string | null;
|
|
166
|
+
}[];
|
|
167
|
+
}>;
|
|
168
|
+
rollback(serviceId: string, revisionId: string): Promise<{
|
|
169
|
+
[key: string]: unknown;
|
|
170
|
+
}>;
|
|
171
|
+
/**
|
|
172
|
+
* Uploads a service's source as one tar archive and queues its build.
|
|
173
|
+
*
|
|
174
|
+
* @throws {InvalidApiResponseError} when the API accepts it with another answer than a receipt
|
|
175
|
+
*/
|
|
176
|
+
deploy(serviceId: string, archive: Uint8Array): Promise<DeploymentReceipt>;
|
|
177
|
+
progress(deploymentId: string, signal?: AbortSignal): AsyncGenerator<SseEvent>;
|
|
178
|
+
logs(serviceId: string, request: LogRequest): AsyncGenerator<SseEvent>;
|
|
179
|
+
private raw;
|
|
180
|
+
private stream;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Starts a device login (RFC 8628): the codes a person approves in the console.
|
|
184
|
+
*
|
|
185
|
+
* @param deviceName How the console names the device asking for a token
|
|
186
|
+
* @throws {CloudApiError} carrying `retryAfterS` when the API rate-limits logins
|
|
187
|
+
*/
|
|
188
|
+
export declare function createDevice(deviceName: string, options?: EndpointOptions): Promise<DeviceAuthorization>;
|
|
189
|
+
/**
|
|
190
|
+
* Trades an approved device code for a token. Until a person approves it,
|
|
191
|
+
* the API refuses with `authorization_pending` or `slow_down`.
|
|
192
|
+
*/
|
|
193
|
+
export declare function exchangeDevice(deviceCode: string, options?: EndpointOptions): Promise<DeviceToken>;
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import createClient from "openapi-fetch";
|
|
2
|
+
import { CloudApiError, InvalidApiResponseError } from "./api-error.js";
|
|
3
|
+
import { EnvironmentsApi, ProjectRepositoryApi } from "./environments.js";
|
|
4
|
+
import { isRecord, isString } from "./guards.js";
|
|
5
|
+
import { acceptedOf, dataOf, errorCode, errorParams, isApiErrorBody, refusal, } from "./result.js";
|
|
6
|
+
import { parseSse } from "./sse.js";
|
|
7
|
+
/** The production Cloud API, used when no `baseUrl` is given. */
|
|
8
|
+
export const DEFAULT_API_URL = "https://api.antelopejs.cloud";
|
|
9
|
+
const SOURCE_MIME_TYPE = "application/x-tar";
|
|
10
|
+
const SOURCE_FILENAME = "source.tar";
|
|
11
|
+
const HTTP_TOO_MANY_REQUESTS = 429;
|
|
12
|
+
function isDeploymentReceipt(value) {
|
|
13
|
+
return (isRecord(value) &&
|
|
14
|
+
isString(value.deploymentId) &&
|
|
15
|
+
isString(value.buildId) &&
|
|
16
|
+
value.status === "queued");
|
|
17
|
+
}
|
|
18
|
+
/** `Retry-After` as delta-seconds; the HTTP-date form, or nothing usable, is no hint. */
|
|
19
|
+
function retryAfterSeconds(header) {
|
|
20
|
+
if (!header || !/^\d+$/.test(header.trim()))
|
|
21
|
+
return undefined;
|
|
22
|
+
return Number(header.trim());
|
|
23
|
+
}
|
|
24
|
+
function endpointClient(options = {}) {
|
|
25
|
+
return createClient({
|
|
26
|
+
baseUrl: options.baseUrl ?? DEFAULT_API_URL,
|
|
27
|
+
fetch: options.fetch,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** The Cloud API, signed in with one token. Every refusal is a {@link CloudApiError}. */
|
|
31
|
+
export class CloudApi {
|
|
32
|
+
options;
|
|
33
|
+
client;
|
|
34
|
+
baseUrl;
|
|
35
|
+
send;
|
|
36
|
+
/** A project's environments: the branches Cloud builds and serves, their promotion and health. */
|
|
37
|
+
environments;
|
|
38
|
+
/** The GitHub repository a project builds from. */
|
|
39
|
+
projectRepository;
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.baseUrl = (options.baseUrl ?? DEFAULT_API_URL).replace(/\/+$/, "");
|
|
43
|
+
this.send = options.fetch ?? fetch;
|
|
44
|
+
this.client = createClient({
|
|
45
|
+
baseUrl: this.baseUrl,
|
|
46
|
+
headers: { Authorization: `Bearer ${options.token}` },
|
|
47
|
+
fetch: options.fetch,
|
|
48
|
+
});
|
|
49
|
+
this.environments = new EnvironmentsApi(this.client);
|
|
50
|
+
this.projectRepository = new ProjectRepositoryApi(this.client);
|
|
51
|
+
}
|
|
52
|
+
async listProjects() {
|
|
53
|
+
const result = await this.client.GET("/api/cloud/projects/");
|
|
54
|
+
return dataOf(result);
|
|
55
|
+
}
|
|
56
|
+
async readProject(projectId) {
|
|
57
|
+
const result = await this.client.GET("/api/cloud/projects/{projectId}", {
|
|
58
|
+
params: { path: { projectId } },
|
|
59
|
+
});
|
|
60
|
+
return dataOf(result);
|
|
61
|
+
}
|
|
62
|
+
async workspaceProvisioning() {
|
|
63
|
+
const result = await this.client.GET("/api/cloud/workspace-provisioning");
|
|
64
|
+
return dataOf(result);
|
|
65
|
+
}
|
|
66
|
+
async readDeployment(deploymentId) {
|
|
67
|
+
const result = await this.client.GET("/api/cloud/deployments/{deploymentId}", { params: { path: { deploymentId } } });
|
|
68
|
+
return dataOf(result);
|
|
69
|
+
}
|
|
70
|
+
async onboardingState() {
|
|
71
|
+
const result = await this.client.GET("/api/cloud/onboarding/");
|
|
72
|
+
return dataOf(result);
|
|
73
|
+
}
|
|
74
|
+
async createProject(body) {
|
|
75
|
+
const result = await this.client.POST("/api/cloud/projects/", { body });
|
|
76
|
+
return dataOf(result);
|
|
77
|
+
}
|
|
78
|
+
async createService(projectId, environmentId, body) {
|
|
79
|
+
const result = await this.client.POST("/api/cloud/projects/{projectId}/environments/{environmentId}/services", { params: { path: { projectId, environmentId } }, body });
|
|
80
|
+
return dataOf(result);
|
|
81
|
+
}
|
|
82
|
+
async listVariables(projectId, environmentId) {
|
|
83
|
+
const result = await this.client.GET("/api/cloud/projects/{projectId}/environments/{environmentId}/variables", { params: { path: { projectId, environmentId } } });
|
|
84
|
+
return dataOf(result);
|
|
85
|
+
}
|
|
86
|
+
/** Secret values are write-only: the answer never carries one. */
|
|
87
|
+
async setVariable(projectId, environmentId, serviceId, key, body) {
|
|
88
|
+
const result = await this.client.PUT("/api/cloud/projects/{projectId}/environments/{environmentId}/services/{serviceId}/variables/{key}", {
|
|
89
|
+
params: { path: { projectId, environmentId, serviceId, key } },
|
|
90
|
+
body,
|
|
91
|
+
});
|
|
92
|
+
return dataOf(result);
|
|
93
|
+
}
|
|
94
|
+
async deleteVariable(projectId, environmentId, serviceId, key) {
|
|
95
|
+
const result = await this.client.DELETE("/api/cloud/projects/{projectId}/environments/{environmentId}/services/{serviceId}/variables/{key}", {
|
|
96
|
+
params: { path: { projectId, environmentId, serviceId, key } },
|
|
97
|
+
parseAs: "text",
|
|
98
|
+
});
|
|
99
|
+
acceptedOf(result);
|
|
100
|
+
}
|
|
101
|
+
/** The databases an environment runs, with their size, keep and cost. */
|
|
102
|
+
async environmentResources(projectId, environmentId) {
|
|
103
|
+
const result = await this.client.GET("/api/cloud/projects/{projectId}/environments/{environmentId}/resources", { params: { path: { projectId, environmentId } } });
|
|
104
|
+
return dataOf(result);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* What applying the project's modules would do to an environment: the
|
|
108
|
+
* databases to create, those that already serve a module, the needs Cloud
|
|
109
|
+
* cannot provide, the monthly cost and the service limit.
|
|
110
|
+
*
|
|
111
|
+
* @param modules The npm package names of the modules in `antelope.config.ts`
|
|
112
|
+
*/
|
|
113
|
+
async planResources(projectId, environmentId, modules) {
|
|
114
|
+
const result = await this.client.POST("/api/cloud/projects/{projectId}/environments/{environmentId}/resource-plan", {
|
|
115
|
+
params: { path: { projectId, environmentId } },
|
|
116
|
+
body: { modules: [...modules] },
|
|
117
|
+
});
|
|
118
|
+
return dataOf(result);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Creates the databases the modules need. The server computes the plan
|
|
122
|
+
* again from the environment's state; it refuses with 409 before any write
|
|
123
|
+
* when the plan would pass the environment's service limit.
|
|
124
|
+
*
|
|
125
|
+
* @param modules The npm package names of the modules in `antelope.config.ts`
|
|
126
|
+
*/
|
|
127
|
+
async applyResources(projectId, environmentId, modules) {
|
|
128
|
+
const result = await this.client.POST("/api/cloud/projects/{projectId}/environments/{environmentId}/resource-plan/apply", {
|
|
129
|
+
params: { path: { projectId, environmentId } },
|
|
130
|
+
body: { modules: [...modules] },
|
|
131
|
+
});
|
|
132
|
+
return dataOf(result);
|
|
133
|
+
}
|
|
134
|
+
async serviceSettings(serviceId) {
|
|
135
|
+
const result = await this.client.GET("/api/cloud/services/{serviceId}/settings", { params: { path: { serviceId } } });
|
|
136
|
+
return dataOf(result);
|
|
137
|
+
}
|
|
138
|
+
async deploymentHistory(serviceId) {
|
|
139
|
+
const result = await this.client.GET("/api/cloud/services/{serviceId}/deployments", {
|
|
140
|
+
params: { path: { serviceId } },
|
|
141
|
+
});
|
|
142
|
+
return dataOf(result);
|
|
143
|
+
}
|
|
144
|
+
async rollback(serviceId, revisionId) {
|
|
145
|
+
const result = await this.client.POST("/api/cloud/services/{serviceId}/deployments/{revisionId}/rollback", { params: { path: { serviceId, revisionId } } });
|
|
146
|
+
return dataOf(result);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Uploads a service's source as one tar archive and queues its build.
|
|
150
|
+
*
|
|
151
|
+
* @throws {InvalidApiResponseError} when the API accepts it with another answer than a receipt
|
|
152
|
+
*/
|
|
153
|
+
async deploy(serviceId, archive) {
|
|
154
|
+
const body = new FormData();
|
|
155
|
+
body.append("source", new Blob([new Uint8Array(archive)], { type: SOURCE_MIME_TYPE }), SOURCE_FILENAME);
|
|
156
|
+
const response = await this.raw(`/api/cloud/services/${encodeURIComponent(serviceId)}/source-deployments`, { method: "POST", body });
|
|
157
|
+
const receipt = await response.json();
|
|
158
|
+
if (!isDeploymentReceipt(receipt))
|
|
159
|
+
throw new InvalidApiResponseError();
|
|
160
|
+
return receipt;
|
|
161
|
+
}
|
|
162
|
+
progress(deploymentId, signal) {
|
|
163
|
+
return this.stream(`/api/cloud/deployments/${encodeURIComponent(deploymentId)}/progress`, { Accept: "text/event-stream" }, signal);
|
|
164
|
+
}
|
|
165
|
+
logs(serviceId, request) {
|
|
166
|
+
const accept = { Accept: "text/event-stream" };
|
|
167
|
+
const headers = request.lastEventId
|
|
168
|
+
? { ...accept, "Last-Event-ID": request.lastEventId }
|
|
169
|
+
: accept;
|
|
170
|
+
const query = request.follow ? "?follow=true" : "";
|
|
171
|
+
return this.stream(`/api/cloud/services/${encodeURIComponent(serviceId)}/logs${query}`, headers, undefined, request.onOpen);
|
|
172
|
+
}
|
|
173
|
+
async raw(path, init) {
|
|
174
|
+
const headers = new Headers(init?.headers);
|
|
175
|
+
headers.set("Authorization", `Bearer ${this.options.token}`);
|
|
176
|
+
const response = await this.send(`${this.baseUrl}${path}`, {
|
|
177
|
+
...init,
|
|
178
|
+
headers,
|
|
179
|
+
});
|
|
180
|
+
if (response.ok)
|
|
181
|
+
return response;
|
|
182
|
+
const text = await response.text();
|
|
183
|
+
let body = text;
|
|
184
|
+
try {
|
|
185
|
+
body = JSON.parse(text);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
/** The response is already useful as plain text. */
|
|
189
|
+
}
|
|
190
|
+
throw refusal(response.status, isApiErrorBody(body) ? body : undefined, text);
|
|
191
|
+
}
|
|
192
|
+
async *stream(path, headers, signal, onOpen) {
|
|
193
|
+
const response = await this.raw(path, { headers, signal });
|
|
194
|
+
onOpen?.();
|
|
195
|
+
yield* parseSse(response);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Starts a device login (RFC 8628): the codes a person approves in the console.
|
|
200
|
+
*
|
|
201
|
+
* @param deviceName How the console names the device asking for a token
|
|
202
|
+
* @throws {CloudApiError} carrying `retryAfterS` when the API rate-limits logins
|
|
203
|
+
*/
|
|
204
|
+
export async function createDevice(deviceName, options) {
|
|
205
|
+
const result = await endpointClient(options).POST("/api/cloud/cli/device", {
|
|
206
|
+
body: { device_name: deviceName },
|
|
207
|
+
});
|
|
208
|
+
if (result.response.status === HTTP_TOO_MANY_REQUESTS) {
|
|
209
|
+
const body = isApiErrorBody(result.error) ? result.error : undefined;
|
|
210
|
+
throw new CloudApiError(result.response.status, errorCode(body), undefined, errorParams(body), retryAfterSeconds(result.response.headers.get("retry-after")));
|
|
211
|
+
}
|
|
212
|
+
return dataOf(result);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Trades an approved device code for a token. Until a person approves it,
|
|
216
|
+
* the API refuses with `authorization_pending` or `slow_down`.
|
|
217
|
+
*/
|
|
218
|
+
export async function exchangeDevice(deviceCode, options) {
|
|
219
|
+
const result = await endpointClient(options).POST("/api/cloud/cli/token", {
|
|
220
|
+
body: { device_code: deviceCode },
|
|
221
|
+
});
|
|
222
|
+
return dataOf(result);
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Client } from "openapi-fetch";
|
|
2
|
+
import type { components, paths } from "./generated/cloud-api.js";
|
|
3
|
+
export type EnvironmentDetail = components["schemas"]["EnvironmentDetail"];
|
|
4
|
+
export type EnvironmentDeployment = components["schemas"]["EnvironmentDeployment"];
|
|
5
|
+
export type EnvironmentHealth = components["schemas"]["EnvironmentHealth"];
|
|
6
|
+
export type NewEnvironment = components["schemas"]["NewEnvironment"];
|
|
7
|
+
export type Promotion = components["schemas"]["Promotion"];
|
|
8
|
+
export type PromotionReceipt = components["schemas"]["PromotionReceipt"];
|
|
9
|
+
export type ServiceHealth = components["schemas"]["ServiceHealth"];
|
|
10
|
+
type CloudClient = Client<paths>;
|
|
11
|
+
/**
|
|
12
|
+
* A project's environments. Each one is a git branch Cloud builds and serves,
|
|
13
|
+
* with its own services, database and secrets.
|
|
14
|
+
*/
|
|
15
|
+
export declare class EnvironmentsApi {
|
|
16
|
+
private readonly client;
|
|
17
|
+
constructor(client: CloudClient);
|
|
18
|
+
/** Every environment of the project, durable ones first, previews after. */
|
|
19
|
+
list(projectId: string): Promise<EnvironmentDetail[]>;
|
|
20
|
+
read(projectId: string, environmentId: string): Promise<EnvironmentDetail>;
|
|
21
|
+
create(projectId: string, body: NewEnvironment): Promise<{
|
|
22
|
+
environmentId: string;
|
|
23
|
+
name: string;
|
|
24
|
+
}>;
|
|
25
|
+
/** Deletes the environment, its services and its previews. A protected one refuses. */
|
|
26
|
+
delete(projectId: string, environmentId: string): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Builds and deploys a commit of the project's repository, or a branch tip,
|
|
29
|
+
* to the environment. The serving version keeps the traffic until the new
|
|
30
|
+
* one passes its health check.
|
|
31
|
+
*/
|
|
32
|
+
promote(projectId: string, environmentId: string, promotion: Promotion): Promise<PromotionReceipt>;
|
|
33
|
+
/** The health check settings of each service and the latest results. */
|
|
34
|
+
health(projectId: string, environmentId: string): Promise<EnvironmentHealth>;
|
|
35
|
+
}
|
|
36
|
+
/** The GitHub repository a project builds from, as `owner/name`. */
|
|
37
|
+
export declare class ProjectRepositoryApi {
|
|
38
|
+
private readonly client;
|
|
39
|
+
constructor(client: CloudClient);
|
|
40
|
+
read(projectId: string): Promise<string | null>;
|
|
41
|
+
/**
|
|
42
|
+
* Links the project to a repository a GitHub installation of the workspace
|
|
43
|
+
* grants; null unlinks it, and the answer falls back to the repository most
|
|
44
|
+
* of its services build from.
|
|
45
|
+
*/
|
|
46
|
+
link(projectId: string, repository: string | null): Promise<string | null>;
|
|
47
|
+
}
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { acceptedOf, dataOf } from "./result.js";
|
|
2
|
+
const ENVIRONMENTS = "/api/cloud/projects/{projectId}/environments";
|
|
3
|
+
const ENVIRONMENT = "/api/cloud/projects/{projectId}/environments/{environmentId}";
|
|
4
|
+
/**
|
|
5
|
+
* A project's environments. Each one is a git branch Cloud builds and serves,
|
|
6
|
+
* with its own services, database and secrets.
|
|
7
|
+
*/
|
|
8
|
+
export class EnvironmentsApi {
|
|
9
|
+
client;
|
|
10
|
+
constructor(client) {
|
|
11
|
+
this.client = client;
|
|
12
|
+
}
|
|
13
|
+
/** Every environment of the project, durable ones first, previews after. */
|
|
14
|
+
async list(projectId) {
|
|
15
|
+
const result = await this.client.GET(ENVIRONMENTS, {
|
|
16
|
+
params: { path: { projectId } },
|
|
17
|
+
});
|
|
18
|
+
return dataOf(result).environments;
|
|
19
|
+
}
|
|
20
|
+
async read(projectId, environmentId) {
|
|
21
|
+
const result = await this.client.GET(ENVIRONMENT, {
|
|
22
|
+
params: { path: { projectId, environmentId } },
|
|
23
|
+
});
|
|
24
|
+
return dataOf(result);
|
|
25
|
+
}
|
|
26
|
+
async create(projectId, body) {
|
|
27
|
+
const result = await this.client.POST(ENVIRONMENTS, {
|
|
28
|
+
params: { path: { projectId } },
|
|
29
|
+
body,
|
|
30
|
+
});
|
|
31
|
+
return dataOf(result);
|
|
32
|
+
}
|
|
33
|
+
/** Deletes the environment, its services and its previews. A protected one refuses. */
|
|
34
|
+
async delete(projectId, environmentId) {
|
|
35
|
+
const result = await this.client.POST(`${ENVIRONMENT}/deletion`, {
|
|
36
|
+
params: { path: { projectId, environmentId } },
|
|
37
|
+
parseAs: "text",
|
|
38
|
+
});
|
|
39
|
+
acceptedOf(result);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Builds and deploys a commit of the project's repository, or a branch tip,
|
|
43
|
+
* to the environment. The serving version keeps the traffic until the new
|
|
44
|
+
* one passes its health check.
|
|
45
|
+
*/
|
|
46
|
+
async promote(projectId, environmentId, promotion) {
|
|
47
|
+
const result = await this.client.POST(`${ENVIRONMENT}/promotion`, {
|
|
48
|
+
params: { path: { projectId, environmentId } },
|
|
49
|
+
body: promotion,
|
|
50
|
+
});
|
|
51
|
+
return dataOf(result);
|
|
52
|
+
}
|
|
53
|
+
/** The health check settings of each service and the latest results. */
|
|
54
|
+
async health(projectId, environmentId) {
|
|
55
|
+
const result = await this.client.GET(`${ENVIRONMENT}/health`, {
|
|
56
|
+
params: { path: { projectId, environmentId } },
|
|
57
|
+
});
|
|
58
|
+
return dataOf(result);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** The GitHub repository a project builds from, as `owner/name`. */
|
|
62
|
+
export class ProjectRepositoryApi {
|
|
63
|
+
client;
|
|
64
|
+
constructor(client) {
|
|
65
|
+
this.client = client;
|
|
66
|
+
}
|
|
67
|
+
async read(projectId) {
|
|
68
|
+
const result = await this.client.GET("/api/cloud/projects/{projectId}/repository", { params: { path: { projectId } } });
|
|
69
|
+
return dataOf(result).repository;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Links the project to a repository a GitHub installation of the workspace
|
|
73
|
+
* grants; null unlinks it, and the answer falls back to the repository most
|
|
74
|
+
* of its services build from.
|
|
75
|
+
*/
|
|
76
|
+
async link(projectId, repository) {
|
|
77
|
+
const result = await this.client.PUT("/api/cloud/projects/{projectId}/repository", { params: { path: { projectId } }, body: { repository } });
|
|
78
|
+
return dataOf(result).repository;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=environments.js.map
|