@nucarium/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/LICENSE-APACHE +176 -0
- package/LICENSE-MIT +21 -0
- package/README.md +76 -2
- package/dist/generated/client.d.ts +185 -0
- package/dist/generated/client.js +162 -0
- package/dist/generated/schema.d.ts +1788 -0
- package/dist/generated/schema.js +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/runtime.d.ts +42 -0
- package/dist/runtime.js +104 -0
- package/package.json +54 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Nucarium Watch read API, for Node, Deno, Bun and Workers.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { Watch } from '@nucarium/sdk';
|
|
6
|
+
*
|
|
7
|
+
* const watch = new Watch({ apiKey: process.env.NUCARIUM_API_KEY! });
|
|
8
|
+
* const inbox = await watch.getInbox({ attention: ['needs_input'] });
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export * from './generated/client.js';
|
|
12
|
+
export type { components, paths } from './generated/schema.js';
|
|
13
|
+
export { DEFAULT_BASE_URL, type Problem, queryString, WatchError, type WatchOptions, } from './runtime.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Nucarium Watch read API, for Node, Deno, Bun and Workers.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { Watch } from '@nucarium/sdk';
|
|
6
|
+
*
|
|
7
|
+
* const watch = new Watch({ apiKey: process.env.NUCARIUM_API_KEY! });
|
|
8
|
+
* const inbox = await watch.getInbox({ attention: ['needs_input'] });
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export * from './generated/client.js';
|
|
12
|
+
export { DEFAULT_BASE_URL, queryString, WatchError, } from './runtime.js';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every generated method stands on: one `fetch`, the key, and the
|
|
3
|
+
* API's problem documents turned into `WatchError`. The methods themselves
|
|
4
|
+
* are in src/generated/client.ts.
|
|
5
|
+
*/
|
|
6
|
+
export interface WatchOptions {
|
|
7
|
+
/** An API key, `nrk_…`. Made by an owner in the console, on Orchard. */
|
|
8
|
+
apiKey: string;
|
|
9
|
+
/** Where Watch is. Hosted Watch unless said otherwise. */
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
/** The `fetch` to send with. The platform's own unless said otherwise. */
|
|
12
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
13
|
+
}
|
|
14
|
+
export declare const DEFAULT_BASE_URL = "https://api.nucarium.com";
|
|
15
|
+
/** The RFC 7807 problem document every error from the API is. */
|
|
16
|
+
export interface Problem {
|
|
17
|
+
type: string;
|
|
18
|
+
title: string;
|
|
19
|
+
status: number;
|
|
20
|
+
code: string;
|
|
21
|
+
detail?: string;
|
|
22
|
+
[member: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* An answer outside 2xx. `code` is the API's stable `ProblemCode`, or
|
|
26
|
+
* `unreachable` when no answer came at all.
|
|
27
|
+
*/
|
|
28
|
+
export declare class WatchError extends Error {
|
|
29
|
+
readonly status: number;
|
|
30
|
+
readonly code: string;
|
|
31
|
+
readonly problem: Problem | null;
|
|
32
|
+
/** Seconds to wait, from `Retry-After`, on `rate_limited`. */
|
|
33
|
+
readonly retryAfter: number | null;
|
|
34
|
+
constructor(status: number, code: string, message: string, problem?: Problem | null, retryAfter?: number | null, options?: ErrorOptions);
|
|
35
|
+
}
|
|
36
|
+
/** Lists go as `a,b,c`, the way the API takes them. */
|
|
37
|
+
export declare function queryString(query: object): string;
|
|
38
|
+
export declare class BaseClient {
|
|
39
|
+
#private;
|
|
40
|
+
constructor(options: WatchOptions);
|
|
41
|
+
protected request<T>(method: string, template: string, pathParams: Record<string, string>, query: object): Promise<T>;
|
|
42
|
+
}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every generated method stands on: one `fetch`, the key, and the
|
|
3
|
+
* API's problem documents turned into `WatchError`. The methods themselves
|
|
4
|
+
* are in src/generated/client.ts.
|
|
5
|
+
*/
|
|
6
|
+
export const DEFAULT_BASE_URL = 'https://api.nucarium.com';
|
|
7
|
+
/**
|
|
8
|
+
* An answer outside 2xx. `code` is the API's stable `ProblemCode`, or
|
|
9
|
+
* `unreachable` when no answer came at all.
|
|
10
|
+
*/
|
|
11
|
+
export class WatchError extends Error {
|
|
12
|
+
status;
|
|
13
|
+
code;
|
|
14
|
+
problem;
|
|
15
|
+
/** Seconds to wait, from `Retry-After`, on `rate_limited`. */
|
|
16
|
+
retryAfter;
|
|
17
|
+
constructor(status, code, message, problem = null, retryAfter = null, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.name = 'WatchError';
|
|
20
|
+
this.status = status;
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.problem = problem;
|
|
23
|
+
this.retryAfter = retryAfter;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Lists go as `a,b,c`, the way the API takes them. */
|
|
27
|
+
export function queryString(query) {
|
|
28
|
+
const params = new URLSearchParams();
|
|
29
|
+
for (const [name, value] of Object.entries(query)) {
|
|
30
|
+
if (value === undefined || value === null)
|
|
31
|
+
continue;
|
|
32
|
+
// An empty list filters nothing: it is left out, as if it were not given.
|
|
33
|
+
if (Array.isArray(value) && value.length === 0)
|
|
34
|
+
continue;
|
|
35
|
+
params.set(name, Array.isArray(value) ? value.join(',') : String(value));
|
|
36
|
+
}
|
|
37
|
+
const text = params.toString();
|
|
38
|
+
return text ? `?${text}` : '';
|
|
39
|
+
}
|
|
40
|
+
function isProblem(body) {
|
|
41
|
+
return (typeof body === 'object' &&
|
|
42
|
+
body !== null &&
|
|
43
|
+
typeof body.code === 'string' &&
|
|
44
|
+
typeof body.title === 'string');
|
|
45
|
+
}
|
|
46
|
+
export class BaseClient {
|
|
47
|
+
#apiKey;
|
|
48
|
+
#baseUrl;
|
|
49
|
+
#fetch;
|
|
50
|
+
constructor(options) {
|
|
51
|
+
if (!options.apiKey?.startsWith('nrk_')) {
|
|
52
|
+
throw new TypeError('apiKey must be a Watch API key: nrk_…');
|
|
53
|
+
}
|
|
54
|
+
const baseUrl = new URL(options.baseUrl ?? DEFAULT_BASE_URL);
|
|
55
|
+
const local = ['localhost', '127.0.0.1', '[::1]'].includes(baseUrl.hostname);
|
|
56
|
+
if (baseUrl.protocol !== 'https:' && !(baseUrl.protocol === 'http:' && local)) {
|
|
57
|
+
throw new TypeError('baseUrl must be https, so the key is never sent in the clear.');
|
|
58
|
+
}
|
|
59
|
+
this.#apiKey = options.apiKey;
|
|
60
|
+
this.#baseUrl = baseUrl.toString().replace(/\/+$/, '');
|
|
61
|
+
this.#fetch = options.fetch ?? ((request) => globalThis.fetch(request));
|
|
62
|
+
}
|
|
63
|
+
async request(method, template, pathParams, query) {
|
|
64
|
+
const path = template.replace(/\{([a-z_]+)\}/g, (_, name) => {
|
|
65
|
+
const value = pathParams[name];
|
|
66
|
+
if (typeof value !== 'string' || value === '' || value === '.' || value === '..') {
|
|
67
|
+
throw new TypeError(`${name} must be an id.`);
|
|
68
|
+
}
|
|
69
|
+
return encodeURIComponent(value);
|
|
70
|
+
});
|
|
71
|
+
const request = new Request(`${this.#baseUrl}${path}${queryString(query)}`, {
|
|
72
|
+
method,
|
|
73
|
+
headers: { accept: 'application/json', authorization: `Bearer ${this.#apiKey}` },
|
|
74
|
+
// Watch never redirects. A redirect is answered as itself, never
|
|
75
|
+
// followed, so the key is never carried to another address.
|
|
76
|
+
redirect: 'manual',
|
|
77
|
+
});
|
|
78
|
+
let response;
|
|
79
|
+
try {
|
|
80
|
+
response = await this.#fetch(request);
|
|
81
|
+
}
|
|
82
|
+
catch (cause) {
|
|
83
|
+
throw new WatchError(0, 'unreachable', 'Watch did not answer.', null, null, { cause });
|
|
84
|
+
}
|
|
85
|
+
const text = await response.text();
|
|
86
|
+
let body = null;
|
|
87
|
+
if (text !== '') {
|
|
88
|
+
try {
|
|
89
|
+
body = JSON.parse(text);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
body = null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (response.ok)
|
|
96
|
+
return body;
|
|
97
|
+
const retryAfter = Number(response.headers.get('retry-after'));
|
|
98
|
+
const wait = Number.isFinite(retryAfter) && retryAfter > 0 ? retryAfter : null;
|
|
99
|
+
if (isProblem(body)) {
|
|
100
|
+
throw new WatchError(response.status, body.code, body.detail ?? body.title, body, wait);
|
|
101
|
+
}
|
|
102
|
+
throw new WatchError(response.status, 'internal_error', `Watch answered ${response.status}.`, null, wait);
|
|
103
|
+
}
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nucarium/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Nucarium Watch read API for TypeScript and JavaScript: the inbox, sessions, machines, environments and alerts, with an API key.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT OR Apache-2.0",
|
|
7
|
+
"homepage": "https://nucarium.dev/api/sdks",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/nucarium/nucarium.git",
|
|
11
|
+
"directory": "packages/sdk"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"nucarium",
|
|
15
|
+
"watch",
|
|
16
|
+
"coding agents",
|
|
17
|
+
"api",
|
|
18
|
+
"sdk"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
},
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE-APACHE",
|
|
36
|
+
"LICENSE-MIT"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"generate": "node scripts/generate.mjs",
|
|
43
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
44
|
+
"check": "node scripts/generate.mjs --check && tsc --noEmit -p tsconfig.json && biome check .",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"prepack": "npm run check && npm test && npm run build"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@biomejs/biome": "^2.6.8",
|
|
50
|
+
"@nucarium/contract": "*",
|
|
51
|
+
"openapi-typescript": "^7.13.0",
|
|
52
|
+
"typescript": "^6.0.3",
|
|
53
|
+
"vitest": "^5.1.1",
|
|
54
|
+
"yaml": "^2.9.1"
|
|
55
|
+
}
|
|
56
|
+
}
|