@superartifacts/sdk 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # @superartifacts/sdk
2
2
 
3
3
  The TypeScript SDK for the [Super Artifacts](https://superartifacts.app) REST API. No runtime
4
- dependencies. Node 20.3+, Bun, Deno and Cloudflare Workers.
4
+ dependencies.
5
+
6
+ Node 20.3+. It uses only the standard `fetch`, so other runtimes that provide it may work; only
7
+ Node is tested.
5
8
 
6
9
  ```bash
7
10
  npm install @superartifacts/sdk
@@ -9,6 +12,8 @@ npm install @superartifacts/sdk
9
12
 
10
13
  **Server-side only.** The API refuses any request carrying an `Origin` header, which means every
11
14
  browser, and a key on a page is a leaked key. The client throws if you construct it in a browser.
15
+ `dangerouslyAllowBrowser: true` turns that check off for use behind your own proxy, and ships the
16
+ key to every visitor.
12
17
 
13
18
  ## Quickstart
14
19
 
@@ -43,9 +48,12 @@ Every method takes one object holding the operation's path, query and body field
43
48
  optional second argument for this request alone:
44
49
 
45
50
  ```ts
46
- await sa.artifacts.update({ artifact: 'hello', visibility: 'private' }, { timeout: 10_000 })
51
+ await sa.artifacts.update({ artifact: 'hello', visibility: 'private' }, { timeout: 30_000 })
47
52
  ```
48
53
 
54
+ Responses are untyped JSON until the API describes them: every result is an open object, so
55
+ destructure and index it without a cast.
56
+
49
57
  Resources: `account`, `artifacts`, `live`, `uploads`, `data`, `schema`, `entries`, `secrets`,
50
58
  `templates`, `projects`, `shares`, `comments`, `mentions`, `signatures`.
51
59
 
@@ -66,12 +74,13 @@ Per request: `{ idempotencyKey, timeout, signal, headers }`.
66
74
  ## Errors
67
75
 
68
76
  Every failure is a `SuperArtifactsError` with `status`, `code`, `message` and, when the API asked
69
- you to wait, `retryAfter` in seconds. Anything else the API said is in `details`.
77
+ you to wait, `retryAfter` in seconds. Anything else the API said is in `details`. A missing key
78
+ throws before any request, so that `AuthenticationError` has no `status` or `code`.
70
79
 
71
80
  | Class | When |
72
81
  | --- | --- |
73
82
  | `InvalidArgumentsError` | 400 |
74
- | `AuthenticationError` | 401 |
83
+ | `AuthenticationError` | 401, or no key to send |
75
84
  | `PlanLimitError` | 402 |
76
85
  | `PermissionError` | 403 `forbidden` |
77
86
  | `ScopeRequiredError` | 403 `scope_required`: a read key called a write |
package/dist/index.cjs CHANGED
@@ -53,8 +53,8 @@ var SuperArtifactsError = class extends Error {
53
53
  constructor(status, code, message, retryAfter, details = {}, options) {
54
54
  super(message, options);
55
55
  this.name = new.target.name;
56
- this.status = status;
57
- this.code = code;
56
+ if (status !== void 0) this.status = status;
57
+ if (code !== void 0) this.code = code;
58
58
  if (retryAfter !== void 0) this.retryAfter = retryAfter;
59
59
  this.details = details;
60
60
  }
@@ -121,7 +121,7 @@ function errorFor(status, text, retryAfterHeader) {
121
121
  }
122
122
 
123
123
  // src/core.ts
124
- var VERSION = "0.1.0";
124
+ var VERSION = "0.1.1";
125
125
  var DEFAULT_BASE_URL = "https://superartifacts.app";
126
126
  var API_KEY_ENV = "SUPERARTIFACTS_API_KEY";
127
127
  var MAX_RETRY_AFTER_SECONDS = 60;
@@ -160,8 +160,10 @@ var Core = class {
160
160
  }
161
161
  const apiKey = options.apiKey ?? globalThis.process?.env?.[API_KEY_ENV];
162
162
  if (!apiKey) {
163
- throw new Error(
164
- `No API key. Set ${API_KEY_ENV} or pass { apiKey }. Create one at https://superartifacts.app/settings/api-keys`
163
+ throw new AuthenticationError(
164
+ void 0,
165
+ void 0,
166
+ `No API key. Pass { apiKey } or set ${API_KEY_ENV}. Create one at https://superartifacts.app/settings/api-keys.`
165
167
  );
166
168
  }
167
169
  this.#apiKey = apiKey;
@@ -235,7 +237,8 @@ var Core = class {
235
237
  const text = await response.text();
236
238
  if (response.ok) return parseAnswer(text, response.status);
237
239
  failure = errorFor(response.status, text, response.headers.get("retry-after"));
238
- const { status, retryAfter } = failure;
240
+ const { status } = response;
241
+ const { retryAfter } = failure;
239
242
  if (status === 429 || retry && [502, 503, 504].includes(status)) {
240
243
  if (retryAfter !== void 0) delay = retryAfter * 1e3;
241
244
  retry = left && (retryAfter ?? 0) <= MAX_RETRY_AFTER_SECONDS;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- declare const VERSION = "0.1.0";
1
+ declare const VERSION = "0.1.1";
2
2
  interface ClientOptions {
3
3
  /** Defaults to the SUPERARTIFACTS_API_KEY environment variable. */
4
4
  apiKey?: string;
@@ -45,6 +45,13 @@ type Query<Op> = Op extends {
45
45
  } ? [NonNullable<Q>] extends [never] ? object : NonNullable<Q> : object;
46
46
  /** Path, query and body fields of an operation, in one object. */
47
47
  type Params<Op> = Path<Op> & Query<Op> & Body<Op>;
48
+ type JsonObject = {
49
+ [key: string]: any;
50
+ };
51
+ /** R's named properties, without the index signature an open object carries. */
52
+ type Declared<R> = keyof {
53
+ [K in keyof R as string extends K ? never : K]: unknown;
54
+ };
48
55
  type Result<Op> = Op extends {
49
56
  responses: {
50
57
  200: {
@@ -53,7 +60,7 @@ type Result<Op> = Op extends {
53
60
  };
54
61
  };
55
62
  };
56
- } ? R : unknown;
63
+ } ? [Declared<R>] extends [never] ? JsonObject : R : unknown;
57
64
  declare class Core {
58
65
  #private;
59
66
  constructor(options?: ClientOptions);
@@ -70,16 +77,19 @@ declare class Core {
70
77
  protected send(url: URL | string, init: RequestInit, repeatable: boolean, options?: RequestOptions): Promise<Record<string, unknown>>;
71
78
  }
72
79
 
73
- /** Anything this SDK throws for a failed call. `status` is 0 when no answer arrived. */
80
+ /**
81
+ * Anything this SDK throws for a failed call. `status` is 0 when no answer arrived, and it
82
+ * and `code` are absent when no request was made: an `AuthenticationError` for a missing key.
83
+ */
74
84
  declare class SuperArtifactsError extends Error {
75
- readonly status: number;
85
+ readonly status?: number;
76
86
  /** Stable and machine-readable; `message` is for a person. */
77
- readonly code: string;
87
+ readonly code?: string;
78
88
  /** Seconds the API asked to wait. */
79
89
  readonly retryAfter?: number;
80
90
  /** Everything else the API said, e.g. `fix` or `approvalUrl`. */
81
91
  readonly details: Record<string, unknown>;
82
- constructor(status: number, code: string, message: string, retryAfter?: number, details?: Record<string, unknown>, options?: ErrorOptions);
92
+ constructor(status: number | undefined, code: string | undefined, message: string, retryAfter?: number, details?: Record<string, unknown>, options?: ErrorOptions);
83
93
  }
84
94
  declare class InvalidArgumentsError extends SuperArtifactsError {
85
95
  }
@@ -2349,7 +2359,7 @@ declare class Resources extends Core {
2349
2359
  readonly signatures: SignaturesResource;
2350
2360
  }
2351
2361
 
2352
- type Row = Record<string, unknown>;
2362
+ type Row = JsonObject;
2353
2363
  declare class Data extends DataResource {
2354
2364
  /** Every row `listArtifactRows` matches, page by page. */
2355
2365
  iterArtifactRows(params: Params<operations['listArtifactRows']>, options?: RequestOptions): AsyncGenerator<Row>;
@@ -2386,4 +2396,4 @@ declare class SuperArtifacts extends Resources {
2386
2396
  }
2387
2397
 
2388
2398
  export { AuthenticationError, ConflictError, ConnectionError, InternalError, InvalidArgumentsError, NotFoundError, OPERATIONS, PayloadTooLargeError, PermissionError, PlanLimitError, RateLimitError, ScopeRequiredError, SuperArtifacts, SuperArtifactsError, TimeoutError, VERSION, SuperArtifacts as default, errorFor };
2389
- export type { ClientOptions, FileToUpload, Operation, Params, RequestOptions, Result, components, operations };
2399
+ export type { ClientOptions, FileToUpload, JsonObject, Operation, Params, RequestOptions, Result, components, operations };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare const VERSION = "0.1.0";
1
+ declare const VERSION = "0.1.1";
2
2
  interface ClientOptions {
3
3
  /** Defaults to the SUPERARTIFACTS_API_KEY environment variable. */
4
4
  apiKey?: string;
@@ -45,6 +45,13 @@ type Query<Op> = Op extends {
45
45
  } ? [NonNullable<Q>] extends [never] ? object : NonNullable<Q> : object;
46
46
  /** Path, query and body fields of an operation, in one object. */
47
47
  type Params<Op> = Path<Op> & Query<Op> & Body<Op>;
48
+ type JsonObject = {
49
+ [key: string]: any;
50
+ };
51
+ /** R's named properties, without the index signature an open object carries. */
52
+ type Declared<R> = keyof {
53
+ [K in keyof R as string extends K ? never : K]: unknown;
54
+ };
48
55
  type Result<Op> = Op extends {
49
56
  responses: {
50
57
  200: {
@@ -53,7 +60,7 @@ type Result<Op> = Op extends {
53
60
  };
54
61
  };
55
62
  };
56
- } ? R : unknown;
63
+ } ? [Declared<R>] extends [never] ? JsonObject : R : unknown;
57
64
  declare class Core {
58
65
  #private;
59
66
  constructor(options?: ClientOptions);
@@ -70,16 +77,19 @@ declare class Core {
70
77
  protected send(url: URL | string, init: RequestInit, repeatable: boolean, options?: RequestOptions): Promise<Record<string, unknown>>;
71
78
  }
72
79
 
73
- /** Anything this SDK throws for a failed call. `status` is 0 when no answer arrived. */
80
+ /**
81
+ * Anything this SDK throws for a failed call. `status` is 0 when no answer arrived, and it
82
+ * and `code` are absent when no request was made: an `AuthenticationError` for a missing key.
83
+ */
74
84
  declare class SuperArtifactsError extends Error {
75
- readonly status: number;
85
+ readonly status?: number;
76
86
  /** Stable and machine-readable; `message` is for a person. */
77
- readonly code: string;
87
+ readonly code?: string;
78
88
  /** Seconds the API asked to wait. */
79
89
  readonly retryAfter?: number;
80
90
  /** Everything else the API said, e.g. `fix` or `approvalUrl`. */
81
91
  readonly details: Record<string, unknown>;
82
- constructor(status: number, code: string, message: string, retryAfter?: number, details?: Record<string, unknown>, options?: ErrorOptions);
92
+ constructor(status: number | undefined, code: string | undefined, message: string, retryAfter?: number, details?: Record<string, unknown>, options?: ErrorOptions);
83
93
  }
84
94
  declare class InvalidArgumentsError extends SuperArtifactsError {
85
95
  }
@@ -2349,7 +2359,7 @@ declare class Resources extends Core {
2349
2359
  readonly signatures: SignaturesResource;
2350
2360
  }
2351
2361
 
2352
- type Row = Record<string, unknown>;
2362
+ type Row = JsonObject;
2353
2363
  declare class Data extends DataResource {
2354
2364
  /** Every row `listArtifactRows` matches, page by page. */
2355
2365
  iterArtifactRows(params: Params<operations['listArtifactRows']>, options?: RequestOptions): AsyncGenerator<Row>;
@@ -2386,4 +2396,4 @@ declare class SuperArtifacts extends Resources {
2386
2396
  }
2387
2397
 
2388
2398
  export { AuthenticationError, ConflictError, ConnectionError, InternalError, InvalidArgumentsError, NotFoundError, OPERATIONS, PayloadTooLargeError, PermissionError, PlanLimitError, RateLimitError, ScopeRequiredError, SuperArtifacts, SuperArtifactsError, TimeoutError, VERSION, SuperArtifacts as default, errorFor };
2389
- export type { ClientOptions, FileToUpload, Operation, Params, RequestOptions, Result, components, operations };
2399
+ export type { ClientOptions, FileToUpload, JsonObject, Operation, Params, RequestOptions, Result, components, operations };
package/dist/index.js CHANGED
@@ -10,8 +10,8 @@ var SuperArtifactsError = class extends Error {
10
10
  constructor(status, code, message, retryAfter, details = {}, options) {
11
11
  super(message, options);
12
12
  this.name = new.target.name;
13
- this.status = status;
14
- this.code = code;
13
+ if (status !== void 0) this.status = status;
14
+ if (code !== void 0) this.code = code;
15
15
  if (retryAfter !== void 0) this.retryAfter = retryAfter;
16
16
  this.details = details;
17
17
  }
@@ -78,7 +78,7 @@ function errorFor(status, text, retryAfterHeader) {
78
78
  }
79
79
 
80
80
  // src/core.ts
81
- var VERSION = "0.1.0";
81
+ var VERSION = "0.1.1";
82
82
  var DEFAULT_BASE_URL = "https://superartifacts.app";
83
83
  var API_KEY_ENV = "SUPERARTIFACTS_API_KEY";
84
84
  var MAX_RETRY_AFTER_SECONDS = 60;
@@ -117,8 +117,10 @@ var Core = class {
117
117
  }
118
118
  const apiKey = options.apiKey ?? globalThis.process?.env?.[API_KEY_ENV];
119
119
  if (!apiKey) {
120
- throw new Error(
121
- `No API key. Set ${API_KEY_ENV} or pass { apiKey }. Create one at https://superartifacts.app/settings/api-keys`
120
+ throw new AuthenticationError(
121
+ void 0,
122
+ void 0,
123
+ `No API key. Pass { apiKey } or set ${API_KEY_ENV}. Create one at https://superartifacts.app/settings/api-keys.`
122
124
  );
123
125
  }
124
126
  this.#apiKey = apiKey;
@@ -192,7 +194,8 @@ var Core = class {
192
194
  const text = await response.text();
193
195
  if (response.ok) return parseAnswer(text, response.status);
194
196
  failure = errorFor(response.status, text, response.headers.get("retry-after"));
195
- const { status, retryAfter } = failure;
197
+ const { status } = response;
198
+ const { retryAfter } = failure;
196
199
  if (status === 429 || retry && [502, 503, 504].includes(status)) {
197
200
  if (retryAfter !== void 0) delay = retryAfter * 1e3;
198
201
  retry = left && (retryAfter ?? 0) <= MAX_RETRY_AFTER_SECONDS;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superartifacts/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "The TypeScript SDK for the Super Artifacts REST API: publish artifacts, read their data, and manage projects, shares and templates from a server.",
5
5
  "license": "MIT",
6
6
  "type": "module",