@miosa/sdk 0.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 +181 -0
- package/dist/index.d.ts +4689 -0
- package/dist/index.js +6045 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
- package/src/client.ts +249 -0
- package/src/errors.ts +136 -0
- package/src/http.test.ts +374 -0
- package/src/http.ts +390 -0
- package/src/index.ts +452 -0
- package/src/resources/admin.ts +348 -0
- package/src/resources/analytics.ts +60 -0
- package/src/resources/api-keys.ts +119 -0
- package/src/resources/audit-log.ts +64 -0
- package/src/resources/benchmarks.ts +104 -0
- package/src/resources/builder-sessions.ts +75 -0
- package/src/resources/channels.ts +143 -0
- package/src/resources/checkpoints.ts +225 -0
- package/src/resources/command-center.ts +73 -0
- package/src/resources/community.ts +103 -0
- package/src/resources/completions.ts +104 -0
- package/src/resources/computer-auto-stop.ts +43 -0
- package/src/resources/computer-env.ts +76 -0
- package/src/resources/computer-logs.ts +50 -0
- package/src/resources/computer-osa.ts +76 -0
- package/src/resources/computer-ports.ts +91 -0
- package/src/resources/computer-terminal.ts +61 -0
- package/src/resources/computer-volumes.ts +64 -0
- package/src/resources/computer.ts +530 -0
- package/src/resources/computers.ts +75 -0
- package/src/resources/credits.ts +43 -0
- package/src/resources/cron-jobs.ts +191 -0
- package/src/resources/custom_domains.ts +123 -0
- package/src/resources/dashboard.ts +49 -0
- package/src/resources/databases.ts +218 -0
- package/src/resources/deployments.ts +777 -0
- package/src/resources/desktop.ts +134 -0
- package/src/resources/email.ts +212 -0
- package/src/resources/embeddings.ts +36 -0
- package/src/resources/events.ts +296 -0
- package/src/resources/exec.ts +319 -0
- package/src/resources/external-keys.ts +79 -0
- package/src/resources/files.test.ts +339 -0
- package/src/resources/files.ts +220 -0
- package/src/resources/flat-custom-domains.ts +127 -0
- package/src/resources/functions.ts +178 -0
- package/src/resources/health-checks.ts +165 -0
- package/src/resources/integrations.ts +183 -0
- package/src/resources/mcp.ts +70 -0
- package/src/resources/models.ts +45 -0
- package/src/resources/network_policy.ts +73 -0
- package/src/resources/open-computers/agents.ts +91 -0
- package/src/resources/open-computers/apps.ts +102 -0
- package/src/resources/open-computers/clusters.ts +88 -0
- package/src/resources/open-computers/desktop.ts +34 -0
- package/src/resources/open-computers/files.ts +97 -0
- package/src/resources/open-computers/hosts.ts +85 -0
- package/src/resources/open-computers/index.ts +98 -0
- package/src/resources/open-computers/jobs.ts +75 -0
- package/src/resources/open-computers/open_computers.test.ts +288 -0
- package/src/resources/open-computers/secrets.ts +115 -0
- package/src/resources/open-computers/terminal.ts +33 -0
- package/src/resources/open-computers/tunnels.ts +87 -0
- package/src/resources/open-computers/types.ts +343 -0
- package/src/resources/open-computers/workspaces.ts +135 -0
- package/src/resources/project-auth.ts +142 -0
- package/src/resources/project-integrations.ts +133 -0
- package/src/resources/provider-defaults.ts +89 -0
- package/src/resources/regions.ts +94 -0
- package/src/resources/sandbox-templates.ts +195 -0
- package/src/resources/sandboxes.live.test.ts +92 -0
- package/src/resources/sandboxes.test.ts +624 -0
- package/src/resources/sandboxes.ts +1173 -0
- package/src/resources/settings.ts +143 -0
- package/src/resources/snapshots-standalone.ts +51 -0
- package/src/resources/storage.ts +221 -0
- package/src/resources/tenant.ts +39 -0
- package/src/resources/usage.ts +85 -0
- package/src/resources/volumes.ts +117 -0
- package/src/resources/webhooks.ts +171 -0
- package/src/types.ts +460 -0
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@miosa/sdk",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "TypeScript SDK for the MIOSA API — cloud VM desktop infrastructure for AI agents",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "MIOSA <hello@miosa.ai>",
|
|
7
|
+
"homepage": "https://miosa.ai",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/miosa-ai/sdk-typescript"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"miosa",
|
|
14
|
+
"ai-agents",
|
|
15
|
+
"computer-use",
|
|
16
|
+
"desktop-automation",
|
|
17
|
+
"cloud-vm",
|
|
18
|
+
"sdk"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"dev": "tsup --watch",
|
|
38
|
+
"typecheck": "tsc --noEmit",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:watch": "vitest",
|
|
41
|
+
"test:coverage": "vitest run --coverage",
|
|
42
|
+
"lint": "eslint src --ext .ts",
|
|
43
|
+
"clean": "rm -rf dist"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^20.0.0",
|
|
47
|
+
"@types/ws": "^8.18.1",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
49
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
50
|
+
"@vitest/coverage-v8": "^1.0.0",
|
|
51
|
+
"eslint": "^8.0.0",
|
|
52
|
+
"tsup": "^8.0.0",
|
|
53
|
+
"typescript": "^5.4.0",
|
|
54
|
+
"vitest": "^1.0.0"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=18.0.0"
|
|
58
|
+
},
|
|
59
|
+
"optionalDependencies": {
|
|
60
|
+
"ws": "^8.20.0",
|
|
61
|
+
"undici": "^6.0.0 || ^7.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { HttpClient } from "./http.js";
|
|
2
|
+
import { Admin } from "./resources/admin.js";
|
|
3
|
+
import { Analytics } from "./resources/analytics.js";
|
|
4
|
+
import { ApiKeys } from "./resources/api-keys.js";
|
|
5
|
+
import { AuditLog } from "./resources/audit-log.js";
|
|
6
|
+
import { Benchmarks } from "./resources/benchmarks.js";
|
|
7
|
+
import { BuilderSessions } from "./resources/builder-sessions.js";
|
|
8
|
+
import { Channels } from "./resources/channels.js";
|
|
9
|
+
import { CommandCenter } from "./resources/command-center.js";
|
|
10
|
+
import { Community } from "./resources/community.js";
|
|
11
|
+
import { Completions } from "./resources/completions.js";
|
|
12
|
+
import { Computers } from "./resources/computers.js";
|
|
13
|
+
import { Credits } from "./resources/credits.js";
|
|
14
|
+
import { CronJobs } from "./resources/cron-jobs.js";
|
|
15
|
+
import { Dashboard } from "./resources/dashboard.js";
|
|
16
|
+
import { Databases } from "./resources/databases.js";
|
|
17
|
+
import { Deployments } from "./resources/deployments.js";
|
|
18
|
+
import { Email } from "./resources/email.js";
|
|
19
|
+
import { Embeddings } from "./resources/embeddings.js";
|
|
20
|
+
import { ExternalKeys } from "./resources/external-keys.js";
|
|
21
|
+
import { FlatCustomDomains } from "./resources/flat-custom-domains.js";
|
|
22
|
+
import { Functions } from "./resources/functions.js";
|
|
23
|
+
import { HealthChecks } from "./resources/health-checks.js";
|
|
24
|
+
import { Integrations } from "./resources/integrations.js";
|
|
25
|
+
import { Mcp } from "./resources/mcp.js";
|
|
26
|
+
import { Models } from "./resources/models.js";
|
|
27
|
+
import { OpenComputers } from "./resources/open-computers/index.js";
|
|
28
|
+
import { ProjectAuth } from "./resources/project-auth.js";
|
|
29
|
+
import { ProjectIntegrations } from "./resources/project-integrations.js";
|
|
30
|
+
import { ProviderDefaults } from "./resources/provider-defaults.js";
|
|
31
|
+
import { Regions } from "./resources/regions.js";
|
|
32
|
+
import { Sandboxes } from "./resources/sandboxes.js";
|
|
33
|
+
import { SandboxTemplates } from "./resources/sandbox-templates.js";
|
|
34
|
+
import { Settings } from "./resources/settings.js";
|
|
35
|
+
import { SnapshotsStandalone } from "./resources/snapshots-standalone.js";
|
|
36
|
+
import { Storage } from "./resources/storage.js";
|
|
37
|
+
import { Tenant } from "./resources/tenant.js";
|
|
38
|
+
import { Usage } from "./resources/usage.js";
|
|
39
|
+
import { Volumes } from "./resources/volumes.js";
|
|
40
|
+
import { Webhooks } from "./resources/webhooks.js";
|
|
41
|
+
import type { MiosaClientConfig } from "./types.js";
|
|
42
|
+
|
|
43
|
+
const DEFAULT_BASE_URL = "https://api.miosa.ai/api/v1";
|
|
44
|
+
const DEFAULT_TIMEOUT = 30_000;
|
|
45
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The top-level MIOSA client.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { Miosa } from '@miosa/sdk';
|
|
53
|
+
*
|
|
54
|
+
* const miosa = new Miosa({ apiKey: 'msk_u_...' });
|
|
55
|
+
*
|
|
56
|
+
* const computer = await miosa.computers.create({ name: 'my-agent' });
|
|
57
|
+
* await computer.start();
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export class Miosa {
|
|
61
|
+
/** Current tenant plan, limits, and live usage counters. */
|
|
62
|
+
readonly tenant: Tenant;
|
|
63
|
+
|
|
64
|
+
/** Datacenter regions, compute sizes, pricing, community templates. */
|
|
65
|
+
readonly regions: Regions;
|
|
66
|
+
|
|
67
|
+
/** Tenant settings — workspace config, branding, BYOK provider keys. */
|
|
68
|
+
readonly settings: Settings;
|
|
69
|
+
|
|
70
|
+
/** Aggregated platform dashboard and health overview. */
|
|
71
|
+
readonly dashboard: Dashboard;
|
|
72
|
+
|
|
73
|
+
/** Admin-scoped analytics overview and timeseries metrics. */
|
|
74
|
+
readonly analytics: Analytics;
|
|
75
|
+
|
|
76
|
+
/** Admin-scoped audit log event stream. */
|
|
77
|
+
readonly auditLog: AuditLog;
|
|
78
|
+
|
|
79
|
+
/** Current-period usage summary, sessions, and report queries. */
|
|
80
|
+
readonly usage: Usage;
|
|
81
|
+
|
|
82
|
+
/** Notification channels — Slack, Discord, email, etc. */
|
|
83
|
+
readonly channels: Channels;
|
|
84
|
+
|
|
85
|
+
/** OAuth integrations — GitHub, Slack, Linear, Discord. */
|
|
86
|
+
readonly integrations: Integrations;
|
|
87
|
+
|
|
88
|
+
/** Per-project integrations (Stripe, Resend, Twilio, etc.). */
|
|
89
|
+
readonly projectIntegrations: ProjectIntegrations;
|
|
90
|
+
|
|
91
|
+
/** Built-in auth for generated apps inside sandboxes/deployments. */
|
|
92
|
+
readonly projectAuth: ProjectAuth;
|
|
93
|
+
|
|
94
|
+
/** BYOK encrypted per-user provider keys. */
|
|
95
|
+
readonly externalKeys: ExternalKeys;
|
|
96
|
+
|
|
97
|
+
/** Model Context Protocol — JSON-RPC dispatch + streaming channel. */
|
|
98
|
+
readonly mcp: Mcp;
|
|
99
|
+
|
|
100
|
+
/** Computer management — create, list, get, delete. */
|
|
101
|
+
readonly computers: Computers;
|
|
102
|
+
|
|
103
|
+
/** Sandboxes — native code-execution environments under `/sandboxes`. */
|
|
104
|
+
readonly sandboxes: Sandboxes;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Deployments — publish from a sandbox to a stable production URL.
|
|
108
|
+
* Versions, releases, rollback, custom domains.
|
|
109
|
+
*/
|
|
110
|
+
readonly deployments: Deployments;
|
|
111
|
+
|
|
112
|
+
/** Credit balance and usage. */
|
|
113
|
+
readonly credits: Credits;
|
|
114
|
+
|
|
115
|
+
/** Admin surface (`/api/v1/admin/*`) — requires an admin credential. */
|
|
116
|
+
readonly admin: Admin;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* OpenComputers — BYOC host management: register your own machines and use
|
|
120
|
+
* them like cloud computers (jobs, files, tunnels, AI agents, clusters).
|
|
121
|
+
*/
|
|
122
|
+
readonly openComputers: OpenComputers;
|
|
123
|
+
|
|
124
|
+
/** Managed Postgres databases — lifecycle, credentials, logs. */
|
|
125
|
+
readonly databases: Databases;
|
|
126
|
+
|
|
127
|
+
/** Managed object storage — buckets, objects, presigned URLs. */
|
|
128
|
+
readonly storage: Storage;
|
|
129
|
+
|
|
130
|
+
/** Persistent block storage volumes. */
|
|
131
|
+
readonly volumes: Volumes;
|
|
132
|
+
|
|
133
|
+
/** Tenant-scoped custom domains across all resources. */
|
|
134
|
+
readonly customDomains: FlatCustomDomains;
|
|
135
|
+
|
|
136
|
+
/** Serverless edge functions — CRUD and invoke. */
|
|
137
|
+
readonly functions: Functions;
|
|
138
|
+
|
|
139
|
+
/** Scheduled cron jobs — CRUD, pause/resume, execution history. */
|
|
140
|
+
readonly cronJobs: CronJobs;
|
|
141
|
+
|
|
142
|
+
/** Uptime health checks. */
|
|
143
|
+
readonly healthChecks: HealthChecks;
|
|
144
|
+
|
|
145
|
+
/** Outgoing tenant webhooks — CRUD, test, delivery history. */
|
|
146
|
+
readonly webhooks: Webhooks;
|
|
147
|
+
|
|
148
|
+
/** Sandbox templates — CRUD, build-spec schema, builds. */
|
|
149
|
+
readonly sandboxTemplates: SandboxTemplates;
|
|
150
|
+
|
|
151
|
+
/** API key management — list, create, delete. */
|
|
152
|
+
readonly apiKeys: ApiKeys;
|
|
153
|
+
|
|
154
|
+
// ── P3 / P4 resources ──────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
/** Available LLM models via the intelligence gateway. */
|
|
157
|
+
readonly models: Models;
|
|
158
|
+
|
|
159
|
+
/** OpenAI-compatible text + chat completions (supports SSE streaming). */
|
|
160
|
+
readonly completions: Completions;
|
|
161
|
+
|
|
162
|
+
/** OpenAI-compatible embedding vectors. */
|
|
163
|
+
readonly embeddings: Embeddings;
|
|
164
|
+
|
|
165
|
+
/** Admin: fleet-wide + per-tenant LLM provider routing defaults. */
|
|
166
|
+
readonly providerDefaults: ProviderDefaults;
|
|
167
|
+
|
|
168
|
+
/** Admin: trigger + inspect platform benchmark runs. */
|
|
169
|
+
readonly benchmarks: Benchmarks;
|
|
170
|
+
|
|
171
|
+
/** Read-only views of the Optimal AI agent fleet. */
|
|
172
|
+
readonly commandCenter: CommandCenter;
|
|
173
|
+
|
|
174
|
+
/** Community template + agent catalog. */
|
|
175
|
+
readonly community: Community;
|
|
176
|
+
|
|
177
|
+
/** Admin email campaigns, templates, and inbox. */
|
|
178
|
+
readonly email: Email;
|
|
179
|
+
|
|
180
|
+
/** Builder UI session metadata. */
|
|
181
|
+
readonly builderSessions: BuilderSessions;
|
|
182
|
+
|
|
183
|
+
/** Admin: fleet-wide snapshot index. */
|
|
184
|
+
readonly snapshotsStandalone: SnapshotsStandalone;
|
|
185
|
+
|
|
186
|
+
private readonly http: HttpClient;
|
|
187
|
+
|
|
188
|
+
constructor(config: MiosaClientConfig) {
|
|
189
|
+
if (!config.apiKey) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
'Miosa: apiKey is required. Pass { apiKey: "msk_u_..." } or set MIOSA_API_KEY.',
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (!config.apiKey.startsWith("msk_")) {
|
|
196
|
+
// Soft warning — allow non-standard keys in tests / self-hosted
|
|
197
|
+
console.warn(
|
|
198
|
+
'[miosa] Warning: API key does not start with "msk_". Double-check your key.',
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this.http = new HttpClient({
|
|
203
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
|
|
204
|
+
apiKey: config.apiKey,
|
|
205
|
+
timeout: config.timeout ?? DEFAULT_TIMEOUT,
|
|
206
|
+
maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
this.tenant = new Tenant(this.http);
|
|
210
|
+
this.regions = new Regions(this.http);
|
|
211
|
+
this.settings = new Settings(this.http);
|
|
212
|
+
this.dashboard = new Dashboard(this.http);
|
|
213
|
+
this.analytics = new Analytics(this.http);
|
|
214
|
+
this.auditLog = new AuditLog(this.http);
|
|
215
|
+
this.usage = new Usage(this.http);
|
|
216
|
+
this.channels = new Channels(this.http);
|
|
217
|
+
this.integrations = new Integrations(this.http);
|
|
218
|
+
this.projectIntegrations = new ProjectIntegrations(this.http);
|
|
219
|
+
this.projectAuth = new ProjectAuth(this.http);
|
|
220
|
+
this.externalKeys = new ExternalKeys(this.http);
|
|
221
|
+
this.mcp = new Mcp(this.http);
|
|
222
|
+
this.computers = new Computers(this.http);
|
|
223
|
+
this.sandboxes = new Sandboxes(this.http);
|
|
224
|
+
this.deployments = new Deployments(this.http);
|
|
225
|
+
this.credits = new Credits(this.http);
|
|
226
|
+
this.admin = new Admin(this.http);
|
|
227
|
+
this.openComputers = new OpenComputers(this.http);
|
|
228
|
+
this.databases = new Databases(this.http);
|
|
229
|
+
this.storage = new Storage(this.http);
|
|
230
|
+
this.volumes = new Volumes(this.http);
|
|
231
|
+
this.customDomains = new FlatCustomDomains(this.http);
|
|
232
|
+
this.functions = new Functions(this.http);
|
|
233
|
+
this.cronJobs = new CronJobs(this.http);
|
|
234
|
+
this.healthChecks = new HealthChecks(this.http);
|
|
235
|
+
this.webhooks = new Webhooks(this.http);
|
|
236
|
+
this.sandboxTemplates = new SandboxTemplates(this.http);
|
|
237
|
+
this.apiKeys = new ApiKeys(this.http);
|
|
238
|
+
this.models = new Models(this.http);
|
|
239
|
+
this.completions = new Completions(this.http);
|
|
240
|
+
this.embeddings = new Embeddings(this.http);
|
|
241
|
+
this.providerDefaults = new ProviderDefaults(this.http);
|
|
242
|
+
this.benchmarks = new Benchmarks(this.http);
|
|
243
|
+
this.commandCenter = new CommandCenter(this.http);
|
|
244
|
+
this.community = new Community(this.http);
|
|
245
|
+
this.email = new Email(this.http);
|
|
246
|
+
this.builderSessions = new BuilderSessions(this.http);
|
|
247
|
+
this.snapshotsStandalone = new SnapshotsStandalone(this.http);
|
|
248
|
+
}
|
|
249
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
export interface MiosaErrorBody {
|
|
2
|
+
error?: {
|
|
3
|
+
code?: string;
|
|
4
|
+
message?: string;
|
|
5
|
+
details?: unknown;
|
|
6
|
+
};
|
|
7
|
+
message?: string;
|
|
8
|
+
code?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class MiosaError extends Error {
|
|
12
|
+
readonly status: number;
|
|
13
|
+
readonly code: string;
|
|
14
|
+
readonly details: unknown;
|
|
15
|
+
readonly requestId: string | undefined;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
message: string,
|
|
19
|
+
status: number,
|
|
20
|
+
code: string,
|
|
21
|
+
details?: unknown,
|
|
22
|
+
requestId?: string,
|
|
23
|
+
) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = "MiosaError";
|
|
26
|
+
this.status = status;
|
|
27
|
+
this.code = code;
|
|
28
|
+
this.details = details;
|
|
29
|
+
this.requestId = requestId;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static fromResponse(
|
|
33
|
+
status: number,
|
|
34
|
+
body: MiosaErrorBody,
|
|
35
|
+
requestId?: string,
|
|
36
|
+
): MiosaError {
|
|
37
|
+
const message = body.error?.message ?? body.message ?? `HTTP ${status}`;
|
|
38
|
+
const code = body.error?.code ?? body.code ?? "UNKNOWN_ERROR";
|
|
39
|
+
const details = body.error?.details;
|
|
40
|
+
|
|
41
|
+
if (status === 401 || status === 403) {
|
|
42
|
+
return new AuthError(message, status, code, details, requestId);
|
|
43
|
+
}
|
|
44
|
+
if (status === 404) {
|
|
45
|
+
return new NotFoundError(message, code, details, requestId);
|
|
46
|
+
}
|
|
47
|
+
if (status === 429) {
|
|
48
|
+
return new RateLimitError(message, details, requestId);
|
|
49
|
+
}
|
|
50
|
+
if (status === 402) {
|
|
51
|
+
return new InsufficientCreditsError(message, details, requestId);
|
|
52
|
+
}
|
|
53
|
+
if (status >= 400 && status < 500) {
|
|
54
|
+
return new ValidationError(message, status, code, details, requestId);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return new MiosaError(message, status, code, details, requestId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class AuthError extends MiosaError {
|
|
62
|
+
constructor(
|
|
63
|
+
message: string,
|
|
64
|
+
status: number = 401,
|
|
65
|
+
code: string = "UNAUTHORIZED",
|
|
66
|
+
details?: unknown,
|
|
67
|
+
requestId?: string,
|
|
68
|
+
) {
|
|
69
|
+
super(message, status, code, details, requestId);
|
|
70
|
+
this.name = "AuthError";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class NotFoundError extends MiosaError {
|
|
75
|
+
constructor(
|
|
76
|
+
message: string,
|
|
77
|
+
code: string = "NOT_FOUND",
|
|
78
|
+
details?: unknown,
|
|
79
|
+
requestId?: string,
|
|
80
|
+
) {
|
|
81
|
+
super(message, 404, code, details, requestId);
|
|
82
|
+
this.name = "NotFoundError";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class RateLimitError extends MiosaError {
|
|
87
|
+
readonly retryAfter: number | undefined;
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
message: string,
|
|
91
|
+
details?: unknown,
|
|
92
|
+
requestId?: string,
|
|
93
|
+
retryAfter?: number,
|
|
94
|
+
) {
|
|
95
|
+
super(message, 429, "RATE_LIMITED", details, requestId);
|
|
96
|
+
this.name = "RateLimitError";
|
|
97
|
+
this.retryAfter = retryAfter;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class InsufficientCreditsError extends MiosaError {
|
|
102
|
+
constructor(message: string, details?: unknown, requestId?: string) {
|
|
103
|
+
super(message, 402, "INSUFFICIENT_CREDITS", details, requestId);
|
|
104
|
+
this.name = "InsufficientCreditsError";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export class ValidationError extends MiosaError {
|
|
109
|
+
constructor(
|
|
110
|
+
message: string,
|
|
111
|
+
status: number,
|
|
112
|
+
code: string = "VALIDATION_ERROR",
|
|
113
|
+
details?: unknown,
|
|
114
|
+
requestId?: string,
|
|
115
|
+
) {
|
|
116
|
+
super(message, status, code, details, requestId);
|
|
117
|
+
this.name = "ValidationError";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class TimeoutError extends MiosaError {
|
|
122
|
+
constructor(message: string = "Request timed out") {
|
|
123
|
+
super(message, 408, "TIMEOUT");
|
|
124
|
+
this.name = "TimeoutError";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export class NetworkError extends MiosaError {
|
|
129
|
+
readonly cause: Error;
|
|
130
|
+
|
|
131
|
+
constructor(message: string, cause: Error) {
|
|
132
|
+
super(message, 0, "NETWORK_ERROR");
|
|
133
|
+
this.name = "NetworkError";
|
|
134
|
+
this.cause = cause;
|
|
135
|
+
}
|
|
136
|
+
}
|