@hoststack.dev/sdk 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/LICENSE +21 -0
- package/README.md +107 -0
- package/dist/index.cjs +482 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +488 -0
- package/dist/index.d.ts +488 -0
- package/dist/index.js +474 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HostStack Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# @hoststack.dev/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for [HostStack](https://hoststack.dev) — the European PaaS for deploying web services, databases, cron jobs, and domains on Hetzner infrastructure.
|
|
4
|
+
|
|
5
|
+
> Think Render.com, but hosted in Europe, built for developers who care about latency and data residency.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@hoststack.dev/sdk)
|
|
8
|
+
[](https://www.npmjs.com/package/@hoststack.dev/sdk)
|
|
9
|
+
[](./LICENSE)
|
|
10
|
+
|
|
11
|
+
- **Website:** [hoststack.dev](https://hoststack.dev)
|
|
12
|
+
- **Documentation:** [hoststack.dev/docs](https://hoststack.dev/docs)
|
|
13
|
+
- **SDK reference:** [hoststack.dev/docs/sdk](https://hoststack.dev/docs/sdk)
|
|
14
|
+
- **Source:** [github.com/gethoststack/sdk](https://github.com/gethoststack/sdk)
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @hoststack.dev/sdk
|
|
20
|
+
# or
|
|
21
|
+
bun add @hoststack.dev/sdk
|
|
22
|
+
# or
|
|
23
|
+
pnpm add @hoststack.dev/sdk
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Requires Node.js 18+ (uses native `fetch`). Works in Bun and Deno too.
|
|
27
|
+
|
|
28
|
+
## Quick start
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { HostStack } from '@hoststack.dev/sdk';
|
|
32
|
+
|
|
33
|
+
const client = new HostStack({
|
|
34
|
+
apiKey: 'hs_live_your_api_key',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const teamId = 1;
|
|
38
|
+
|
|
39
|
+
// List your services
|
|
40
|
+
const { services } = await client.services.list(teamId);
|
|
41
|
+
|
|
42
|
+
// Trigger a deploy
|
|
43
|
+
const { deploy } = await client.deploys.trigger(teamId, 'svc_abc123');
|
|
44
|
+
|
|
45
|
+
// Stream runtime logs
|
|
46
|
+
const ac = new AbortController();
|
|
47
|
+
for await (const entry of client.services.streamLogs(teamId, 'svc_abc123', { signal: ac.signal })) {
|
|
48
|
+
console.log(entry.timestamp, entry.message);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Generate an API key from your [HostStack dashboard → Settings → API Keys](https://hoststack.dev).
|
|
53
|
+
|
|
54
|
+
## Resources
|
|
55
|
+
|
|
56
|
+
| Resource | Methods |
|
|
57
|
+
| --- | --- |
|
|
58
|
+
| `client.projects` | `list`, `get`, `create`, `update`, `delete` |
|
|
59
|
+
| `client.services` | `list`, `get`, `create`, `update`, `delete`, `suspend`, `resume`, `getMetrics`, `getConfig`, `updateConfig`, `getRuntimeLogs`, `streamLogs` |
|
|
60
|
+
| `client.deploys` | `list`, `get`, `trigger`, `cancel`, `rollback`, `getLogs` |
|
|
61
|
+
| `client.databases` | `list`, `get`, `create`, `update`, `delete`, `suspend`, `resume`, `getCredentials`, `resetPassword` |
|
|
62
|
+
| `client.domains` | `list`, `add`, `update`, `remove`, `verify` |
|
|
63
|
+
| `client.envVars` | `list`, `create`, `update`, `delete`, `bulkSet` |
|
|
64
|
+
| `client.cron` | `list`, `get`, `trigger` |
|
|
65
|
+
|
|
66
|
+
Every method's first argument is `teamId: number`. Full API reference: **[hoststack.dev/docs/sdk](https://hoststack.dev/docs/sdk)**.
|
|
67
|
+
|
|
68
|
+
## Error handling
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import {
|
|
72
|
+
HostStack,
|
|
73
|
+
AuthenticationError,
|
|
74
|
+
NotFoundError,
|
|
75
|
+
RateLimitError,
|
|
76
|
+
HostStackError,
|
|
77
|
+
} from '@hoststack.dev/sdk';
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
await client.services.get(teamId, 'svc_missing');
|
|
81
|
+
} catch (err) {
|
|
82
|
+
if (err instanceof NotFoundError) {
|
|
83
|
+
// 404
|
|
84
|
+
} else if (err instanceof AuthenticationError) {
|
|
85
|
+
// 401/403
|
|
86
|
+
} else if (err instanceof RateLimitError) {
|
|
87
|
+
// 429 — err.retryAfter is seconds to wait
|
|
88
|
+
} else if (err instanceof HostStackError) {
|
|
89
|
+
// any other API error — err.status, err.body
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Related packages
|
|
95
|
+
|
|
96
|
+
- **[@hoststack.dev/cli](https://www.npmjs.com/package/@hoststack.dev/cli)** — command-line interface for HostStack
|
|
97
|
+
- **[Terraform provider](https://github.com/gethoststack/terraform-provider-hoststack)** — manage HostStack resources as IaC
|
|
98
|
+
|
|
99
|
+
## Support
|
|
100
|
+
|
|
101
|
+
- Issues: [github.com/gethoststack/sdk/issues](https://github.com/gethoststack/sdk/issues)
|
|
102
|
+
- Docs: [hoststack.dev/docs](https://hoststack.dev/docs)
|
|
103
|
+
- Homepage: [hoststack.dev](https://hoststack.dev)
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT © [HostStack Contributors](https://hoststack.dev)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
var HostStackError = class extends Error {
|
|
5
|
+
statusCode;
|
|
6
|
+
constructor(statusCode, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "HostStackError";
|
|
9
|
+
this.statusCode = statusCode;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var AuthenticationError = class extends HostStackError {
|
|
13
|
+
constructor(message = "Authentication failed. Check your API key.") {
|
|
14
|
+
super(401, message);
|
|
15
|
+
this.name = "AuthenticationError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var NotFoundError = class extends HostStackError {
|
|
19
|
+
constructor(message = "Resource not found.") {
|
|
20
|
+
super(404, message);
|
|
21
|
+
this.name = "NotFoundError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var RateLimitError = class extends HostStackError {
|
|
25
|
+
constructor(message = "Rate limit exceeded. Try again later.") {
|
|
26
|
+
super(429, message);
|
|
27
|
+
this.name = "RateLimitError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/resources/cron.ts
|
|
32
|
+
var CronResource = class {
|
|
33
|
+
constructor(client) {
|
|
34
|
+
this.client = client;
|
|
35
|
+
}
|
|
36
|
+
client;
|
|
37
|
+
/** List cron executions for a service. */
|
|
38
|
+
async list(teamId, serviceId, options) {
|
|
39
|
+
const params = new URLSearchParams();
|
|
40
|
+
if (options?.limit) params.set("limit", String(options.limit));
|
|
41
|
+
const qs = params.toString();
|
|
42
|
+
return this.client.request(
|
|
43
|
+
"GET",
|
|
44
|
+
`/api/services/${teamId}/${serviceId}/cron-executions${qs ? `?${qs}` : ""}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
/** Get a single cron execution by ID. */
|
|
48
|
+
async get(teamId, serviceId, executionId) {
|
|
49
|
+
return this.client.request(
|
|
50
|
+
"GET",
|
|
51
|
+
`/api/services/${teamId}/${serviceId}/cron-executions/${executionId}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
/** Trigger an immediate cron execution. */
|
|
55
|
+
async trigger(teamId, serviceId) {
|
|
56
|
+
return this.client.request(
|
|
57
|
+
"POST",
|
|
58
|
+
`/api/services/${teamId}/${serviceId}/cron-executions/trigger`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/resources/databases.ts
|
|
64
|
+
var DatabasesResource = class {
|
|
65
|
+
constructor(client) {
|
|
66
|
+
this.client = client;
|
|
67
|
+
}
|
|
68
|
+
client;
|
|
69
|
+
/** List all databases for a project. */
|
|
70
|
+
async list(teamId, projectId) {
|
|
71
|
+
return this.client.request("GET", `/api/databases/${teamId}?projectId=${projectId}`);
|
|
72
|
+
}
|
|
73
|
+
/** Get a single database by ID. */
|
|
74
|
+
async get(teamId, databaseId) {
|
|
75
|
+
return this.client.request("GET", `/api/databases/${teamId}/${databaseId}`);
|
|
76
|
+
}
|
|
77
|
+
/** Create a new database. */
|
|
78
|
+
async create(teamId, data) {
|
|
79
|
+
return this.client.request("POST", `/api/databases/${teamId}`, data);
|
|
80
|
+
}
|
|
81
|
+
/** Update a database. */
|
|
82
|
+
async update(teamId, databaseId, data) {
|
|
83
|
+
return this.client.request("PATCH", `/api/databases/${teamId}/${databaseId}`, data);
|
|
84
|
+
}
|
|
85
|
+
/** Delete a database. */
|
|
86
|
+
async delete(teamId, databaseId) {
|
|
87
|
+
return this.client.request("DELETE", `/api/databases/${teamId}/${databaseId}`);
|
|
88
|
+
}
|
|
89
|
+
/** Suspend a database. */
|
|
90
|
+
async suspend(teamId, databaseId) {
|
|
91
|
+
return this.client.request("POST", `/api/databases/${teamId}/${databaseId}/suspend`);
|
|
92
|
+
}
|
|
93
|
+
/** Resume a suspended database. */
|
|
94
|
+
async resume(teamId, databaseId) {
|
|
95
|
+
return this.client.request("POST", `/api/databases/${teamId}/${databaseId}/resume`);
|
|
96
|
+
}
|
|
97
|
+
/** Get connection credentials. */
|
|
98
|
+
async getCredentials(teamId, databaseId) {
|
|
99
|
+
return this.client.request("GET", `/api/databases/${teamId}/${databaseId}/credentials`);
|
|
100
|
+
}
|
|
101
|
+
/** Reset the database password. */
|
|
102
|
+
async resetPassword(teamId, databaseId) {
|
|
103
|
+
return this.client.request("POST", `/api/databases/${teamId}/${databaseId}/reset-password`);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/resources/deploys.ts
|
|
108
|
+
var DeploysResource = class {
|
|
109
|
+
constructor(client) {
|
|
110
|
+
this.client = client;
|
|
111
|
+
}
|
|
112
|
+
client;
|
|
113
|
+
/** List deploys for a service. */
|
|
114
|
+
async list(teamId, serviceId) {
|
|
115
|
+
return this.client.request("GET", `/api/services/${teamId}/${serviceId}/deploys`);
|
|
116
|
+
}
|
|
117
|
+
/** Get a single deploy by ID. */
|
|
118
|
+
async get(teamId, serviceId, deployId) {
|
|
119
|
+
return this.client.request(
|
|
120
|
+
"GET",
|
|
121
|
+
`/api/services/${teamId}/${serviceId}/deploys/${deployId}`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
/** Trigger a new deploy. */
|
|
125
|
+
async trigger(teamId, serviceId, data) {
|
|
126
|
+
return this.client.request(
|
|
127
|
+
"POST",
|
|
128
|
+
`/api/services/${teamId}/${serviceId}/deploys`,
|
|
129
|
+
data ?? {}
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
/** Cancel an in-progress deploy. */
|
|
133
|
+
async cancel(teamId, serviceId, deployId) {
|
|
134
|
+
return this.client.request(
|
|
135
|
+
"POST",
|
|
136
|
+
`/api/services/${teamId}/${serviceId}/deploys/${deployId}/cancel`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
/** Rollback to a previous deploy. */
|
|
140
|
+
async rollback(teamId, serviceId, deployId) {
|
|
141
|
+
return this.client.request(
|
|
142
|
+
"POST",
|
|
143
|
+
`/api/services/${teamId}/${serviceId}/deploys/${deployId}/rollback`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
/** Get build logs for a deploy. */
|
|
147
|
+
async getLogs(teamId, serviceId, deployId) {
|
|
148
|
+
return this.client.request(
|
|
149
|
+
"GET",
|
|
150
|
+
`/api/services/${teamId}/${serviceId}/deploys/${deployId}/logs`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// src/resources/domains.ts
|
|
156
|
+
var DomainsResource = class {
|
|
157
|
+
constructor(client) {
|
|
158
|
+
this.client = client;
|
|
159
|
+
}
|
|
160
|
+
client;
|
|
161
|
+
/** List all domains for the active team. */
|
|
162
|
+
async list(teamId) {
|
|
163
|
+
return this.client.request("GET", `/api/domains/${teamId}`);
|
|
164
|
+
}
|
|
165
|
+
/** Add a custom domain. */
|
|
166
|
+
async add(teamId, data) {
|
|
167
|
+
return this.client.request("POST", `/api/domains/${teamId}`, data);
|
|
168
|
+
}
|
|
169
|
+
/** Update a domain. */
|
|
170
|
+
async update(teamId, domainId, data) {
|
|
171
|
+
return this.client.request("PATCH", `/api/domains/${teamId}/${domainId}`, data);
|
|
172
|
+
}
|
|
173
|
+
/** Remove a domain. */
|
|
174
|
+
async remove(teamId, domainId) {
|
|
175
|
+
return this.client.request("DELETE", `/api/domains/${teamId}/${domainId}`);
|
|
176
|
+
}
|
|
177
|
+
/** Verify domain DNS configuration. */
|
|
178
|
+
async verify(teamId, domainId) {
|
|
179
|
+
return this.client.request("POST", `/api/domains/${teamId}/${domainId}/verify`);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// src/resources/env-vars.ts
|
|
184
|
+
var EnvVarsResource = class {
|
|
185
|
+
constructor(client) {
|
|
186
|
+
this.client = client;
|
|
187
|
+
}
|
|
188
|
+
client;
|
|
189
|
+
/** List all environment variables for a service. */
|
|
190
|
+
async list(teamId, serviceId) {
|
|
191
|
+
return this.client.request("GET", `/api/services/${teamId}/${serviceId}/env`);
|
|
192
|
+
}
|
|
193
|
+
/** Create a new environment variable. */
|
|
194
|
+
async create(teamId, serviceId, data) {
|
|
195
|
+
return this.client.request("POST", `/api/services/${teamId}/${serviceId}/env`, data);
|
|
196
|
+
}
|
|
197
|
+
/** Update an environment variable. */
|
|
198
|
+
async update(teamId, serviceId, envVarId, data) {
|
|
199
|
+
return this.client.request(
|
|
200
|
+
"PATCH",
|
|
201
|
+
`/api/services/${teamId}/${serviceId}/env/${envVarId}`,
|
|
202
|
+
data
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
/** Delete an environment variable. */
|
|
206
|
+
async delete(teamId, serviceId, envVarId) {
|
|
207
|
+
return this.client.request(
|
|
208
|
+
"DELETE",
|
|
209
|
+
`/api/services/${teamId}/${serviceId}/env/${envVarId}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
/** Bulk set environment variables (create or update). */
|
|
213
|
+
async bulkSet(teamId, serviceId, data) {
|
|
214
|
+
return this.client.request("PUT", `/api/services/${teamId}/${serviceId}/env/bulk`, data);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// src/resources/projects.ts
|
|
219
|
+
var ProjectsResource = class {
|
|
220
|
+
constructor(client) {
|
|
221
|
+
this.client = client;
|
|
222
|
+
}
|
|
223
|
+
client;
|
|
224
|
+
/** List all projects for the active team. */
|
|
225
|
+
async list(teamId) {
|
|
226
|
+
return this.client.request("GET", `/api/projects/${teamId}`);
|
|
227
|
+
}
|
|
228
|
+
/** Get a single project by ID. */
|
|
229
|
+
async get(teamId, projectId) {
|
|
230
|
+
return this.client.request("GET", `/api/projects/${teamId}/${projectId}`);
|
|
231
|
+
}
|
|
232
|
+
/** Create a new project. */
|
|
233
|
+
async create(teamId, data) {
|
|
234
|
+
return this.client.request("POST", `/api/projects/${teamId}`, data);
|
|
235
|
+
}
|
|
236
|
+
/** Update a project. */
|
|
237
|
+
async update(teamId, projectId, data) {
|
|
238
|
+
return this.client.request("PATCH", `/api/projects/${teamId}/${projectId}`, data);
|
|
239
|
+
}
|
|
240
|
+
/** Delete a project. */
|
|
241
|
+
async delete(teamId, projectId) {
|
|
242
|
+
return this.client.request("DELETE", `/api/projects/${teamId}/${projectId}`);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/streaming.ts
|
|
247
|
+
async function* streamLogsViaPolling(fetch2, basePath, options = {}) {
|
|
248
|
+
const pollInterval = options.pollInterval ?? 2e3;
|
|
249
|
+
const initialLines = options.initialLines ?? 100;
|
|
250
|
+
let lastTimestamp = null;
|
|
251
|
+
const buildPath = (lines, since) => {
|
|
252
|
+
const params = new URLSearchParams();
|
|
253
|
+
if (options.stream) params.set("stream", options.stream);
|
|
254
|
+
if (lines !== void 0) params.set("lines", String(lines));
|
|
255
|
+
if (since) params.set("since", since);
|
|
256
|
+
const qs = params.toString();
|
|
257
|
+
return `${basePath}${qs ? `?${qs}` : ""}`;
|
|
258
|
+
};
|
|
259
|
+
const initialData = await fetch2(buildPath(initialLines));
|
|
260
|
+
const initial = normalizeEntries(initialData.logs);
|
|
261
|
+
for (const entry of initial) {
|
|
262
|
+
yield entry;
|
|
263
|
+
if (entry.timestamp > (lastTimestamp ?? "")) {
|
|
264
|
+
lastTimestamp = entry.timestamp;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
while (!options.signal?.aborted) {
|
|
268
|
+
await sleep(pollInterval, options.signal);
|
|
269
|
+
if (options.signal?.aborted) break;
|
|
270
|
+
const sinceTs = lastTimestamp ? new Date(lastTimestamp).toISOString() : void 0;
|
|
271
|
+
const data = await fetch2(buildPath(void 0, sinceTs)).catch(() => ({ logs: [] }));
|
|
272
|
+
const entries = normalizeEntries(data.logs);
|
|
273
|
+
for (const entry of entries) {
|
|
274
|
+
if (!lastTimestamp || entry.timestamp > lastTimestamp) {
|
|
275
|
+
yield entry;
|
|
276
|
+
lastTimestamp = entry.timestamp;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function normalizeEntries(logs) {
|
|
282
|
+
if (typeof logs === "string") {
|
|
283
|
+
const lines = logs.split("\n").filter(Boolean);
|
|
284
|
+
return lines.map((line) => ({
|
|
285
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
286
|
+
message: line
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
return logs;
|
|
290
|
+
}
|
|
291
|
+
function sleep(ms, signal) {
|
|
292
|
+
return new Promise((resolve, reject) => {
|
|
293
|
+
const timer = setTimeout(resolve, ms);
|
|
294
|
+
if (signal) {
|
|
295
|
+
signal.addEventListener("abort", () => {
|
|
296
|
+
clearTimeout(timer);
|
|
297
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/resources/services.ts
|
|
304
|
+
var ServicesResource = class {
|
|
305
|
+
constructor(client) {
|
|
306
|
+
this.client = client;
|
|
307
|
+
}
|
|
308
|
+
client;
|
|
309
|
+
/** List all services for the active team. */
|
|
310
|
+
async list(teamId) {
|
|
311
|
+
return this.client.request("GET", `/api/services/${teamId}`);
|
|
312
|
+
}
|
|
313
|
+
/** Get a single service by ID. */
|
|
314
|
+
async get(teamId, serviceId) {
|
|
315
|
+
return this.client.request("GET", `/api/services/${teamId}/${serviceId}`);
|
|
316
|
+
}
|
|
317
|
+
/** Create a new service. */
|
|
318
|
+
async create(teamId, data) {
|
|
319
|
+
return this.client.request("POST", `/api/services/${teamId}`, data);
|
|
320
|
+
}
|
|
321
|
+
/** Update a service. */
|
|
322
|
+
async update(teamId, serviceId, data) {
|
|
323
|
+
return this.client.request("PATCH", `/api/services/${teamId}/${serviceId}`, data);
|
|
324
|
+
}
|
|
325
|
+
/** Delete a service. */
|
|
326
|
+
async delete(teamId, serviceId) {
|
|
327
|
+
return this.client.request("DELETE", `/api/services/${teamId}/${serviceId}`);
|
|
328
|
+
}
|
|
329
|
+
/** Suspend a service. */
|
|
330
|
+
async suspend(teamId, serviceId) {
|
|
331
|
+
return this.client.request("POST", `/api/services/${teamId}/${serviceId}/suspend`);
|
|
332
|
+
}
|
|
333
|
+
/** Resume a suspended service. */
|
|
334
|
+
async resume(teamId, serviceId) {
|
|
335
|
+
return this.client.request("POST", `/api/services/${teamId}/${serviceId}/resume`);
|
|
336
|
+
}
|
|
337
|
+
/** Get service metrics. */
|
|
338
|
+
async getMetrics(teamId, serviceId) {
|
|
339
|
+
return this.client.request("GET", `/api/services/${teamId}/${serviceId}/metrics`);
|
|
340
|
+
}
|
|
341
|
+
/** Get service configuration. */
|
|
342
|
+
async getConfig(teamId, serviceId) {
|
|
343
|
+
return this.client.request("GET", `/api/services/${teamId}/${serviceId}/config`);
|
|
344
|
+
}
|
|
345
|
+
/** Update service configuration. */
|
|
346
|
+
async updateConfig(teamId, serviceId, data) {
|
|
347
|
+
return this.client.request("PATCH", `/api/services/${teamId}/${serviceId}/config`, data);
|
|
348
|
+
}
|
|
349
|
+
/** Get runtime logs for a service. */
|
|
350
|
+
async getRuntimeLogs(teamId, serviceId, options) {
|
|
351
|
+
const params = new URLSearchParams();
|
|
352
|
+
if (options?.lines) params.set("lines", String(options.lines));
|
|
353
|
+
if (options?.since) params.set("since", options.since);
|
|
354
|
+
if (options?.stream) params.set("stream", options.stream);
|
|
355
|
+
const qs = params.toString();
|
|
356
|
+
return this.client.request(
|
|
357
|
+
"GET",
|
|
358
|
+
`/api/services/${teamId}/${serviceId}/runtime-logs${qs ? `?${qs}` : ""}`
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Stream runtime logs for a service by polling the logs endpoint.
|
|
363
|
+
* Yields log entries as they appear. Use an AbortController to stop.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* const ac = new AbortController();
|
|
368
|
+
* for await (const entry of client.services.streamLogs(teamId, serviceId, { signal: ac.signal })) {
|
|
369
|
+
* console.log(entry.message);
|
|
370
|
+
* }
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
streamLogs(teamId, serviceId, options) {
|
|
374
|
+
const basePath = `/api/services/${teamId}/${serviceId}/runtime-logs`;
|
|
375
|
+
return streamLogsViaPolling(
|
|
376
|
+
(path) => this.client.request("GET", path),
|
|
377
|
+
basePath,
|
|
378
|
+
options
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// src/client.ts
|
|
384
|
+
var HostStack = class {
|
|
385
|
+
apiKey;
|
|
386
|
+
baseUrl;
|
|
387
|
+
/** Manage projects. */
|
|
388
|
+
projects;
|
|
389
|
+
/** Manage services (web, worker, cron). */
|
|
390
|
+
services;
|
|
391
|
+
/** Trigger and manage deployments. */
|
|
392
|
+
deploys;
|
|
393
|
+
/** Manage databases (Postgres, Redis). */
|
|
394
|
+
databases;
|
|
395
|
+
/** Manage custom domains. */
|
|
396
|
+
domains;
|
|
397
|
+
/** Manage service environment variables. */
|
|
398
|
+
envVars;
|
|
399
|
+
/** Manage cron job executions. */
|
|
400
|
+
cron;
|
|
401
|
+
constructor(options) {
|
|
402
|
+
if (!options.apiKey) {
|
|
403
|
+
throw new Error("apiKey is required");
|
|
404
|
+
}
|
|
405
|
+
this.apiKey = options.apiKey;
|
|
406
|
+
this.baseUrl = (options.baseUrl ?? "https://api.hoststack.dev").replace(/\/$/, "");
|
|
407
|
+
this.projects = new ProjectsResource(this);
|
|
408
|
+
this.services = new ServicesResource(this);
|
|
409
|
+
this.deploys = new DeploysResource(this);
|
|
410
|
+
this.databases = new DatabasesResource(this);
|
|
411
|
+
this.domains = new DomainsResource(this);
|
|
412
|
+
this.envVars = new EnvVarsResource(this);
|
|
413
|
+
this.cron = new CronResource(this);
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Make an authenticated request to the HostStack API.
|
|
417
|
+
* Used internally by resource classes. Can also be used for custom API calls.
|
|
418
|
+
*/
|
|
419
|
+
async request(method, path, body) {
|
|
420
|
+
const url = `${this.baseUrl}${path}`;
|
|
421
|
+
const headers = {
|
|
422
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
423
|
+
};
|
|
424
|
+
if (body !== void 0) {
|
|
425
|
+
headers["Content-Type"] = "application/json";
|
|
426
|
+
}
|
|
427
|
+
const res = await fetch(url, {
|
|
428
|
+
method,
|
|
429
|
+
headers,
|
|
430
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
431
|
+
});
|
|
432
|
+
if (!res.ok) {
|
|
433
|
+
const data = await res.json().catch(() => ({ error: "Unknown error" }));
|
|
434
|
+
const message = data.error ?? `HTTP ${res.status}`;
|
|
435
|
+
switch (res.status) {
|
|
436
|
+
case 401:
|
|
437
|
+
throw new AuthenticationError(message);
|
|
438
|
+
case 404:
|
|
439
|
+
throw new NotFoundError(message);
|
|
440
|
+
case 429:
|
|
441
|
+
throw new RateLimitError(message);
|
|
442
|
+
default:
|
|
443
|
+
throw new HostStackError(res.status, message);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (res.status === 204) {
|
|
447
|
+
return void 0;
|
|
448
|
+
}
|
|
449
|
+
return res.json();
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
// src/pagination.ts
|
|
454
|
+
function buildPaginationQuery(params) {
|
|
455
|
+
if (!params) return "";
|
|
456
|
+
const qs = new URLSearchParams();
|
|
457
|
+
if (params.limit !== void 0) qs.set("limit", String(params.limit));
|
|
458
|
+
if (params.offset !== void 0) qs.set("offset", String(params.offset));
|
|
459
|
+
const str = qs.toString();
|
|
460
|
+
return str ? `?${str}` : "";
|
|
461
|
+
}
|
|
462
|
+
function wrapArray(items, params) {
|
|
463
|
+
const limit = params?.limit ?? items.length;
|
|
464
|
+
const offset = params?.offset ?? 0;
|
|
465
|
+
return {
|
|
466
|
+
items,
|
|
467
|
+
total: items.length,
|
|
468
|
+
limit,
|
|
469
|
+
offset,
|
|
470
|
+
hasMore: false
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
exports.AuthenticationError = AuthenticationError;
|
|
475
|
+
exports.HostStack = HostStack;
|
|
476
|
+
exports.HostStackError = HostStackError;
|
|
477
|
+
exports.NotFoundError = NotFoundError;
|
|
478
|
+
exports.RateLimitError = RateLimitError;
|
|
479
|
+
exports.buildPaginationQuery = buildPaginationQuery;
|
|
480
|
+
exports.wrapArray = wrapArray;
|
|
481
|
+
//# sourceMappingURL=index.cjs.map
|
|
482
|
+
//# sourceMappingURL=index.cjs.map
|