@vecsnap/sdk 0.1.0 → 0.2.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 +9 -0
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +1 -1
- package/dist/generated/sdk.gen.d.ts +67 -13
- package/dist/generated/sdk.gen.js +106 -12
- package/dist/generated/types.gen.d.ts +2710 -814
- package/dist/index.d.ts +21 -0
- package/dist/index.js +62 -11
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
|
@@ -5,9 +5,21 @@ export { signWebhook, verifyWebhook, WebhookVerificationError, WEBHOOK_TOLERANCE
|
|
|
5
5
|
export type * from "./generated/types.gen.ts";
|
|
6
6
|
/** Where the API answers. */
|
|
7
7
|
export declare const DEFAULT_BASE_URL = "https://api.vecsnap.com/v1";
|
|
8
|
+
/** The statuses a conversion ends in: it changes no more. */
|
|
9
|
+
export declare const ENDED: ReadonlySet<Conversion["status"]>;
|
|
8
10
|
export interface VecSnapOptions {
|
|
9
11
|
/** `vs_test_…` (free: a sample result) or `vs_live_…`. Default: the `VECSNAP_API_KEY` environment variable. */
|
|
10
12
|
apiKey?: string;
|
|
13
|
+
/** A connected app's pass (`vsp_…`), in place of a key: only VecSnap's own apps hold one, and it reaches what the app made. */
|
|
14
|
+
pass?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Asked when the API refuses the pass (401 `invalid_pass`: past its hour, or ended): a fresh pass, or null when the
|
|
17
|
+
* connection is over (its refresh pass refused), and the call answers that refusal. A REJECTION is the refresh's own
|
|
18
|
+
* failure (offline, a server's bad moment): the call fails with it and the pass is kept, so a dropped request never reads
|
|
19
|
+
* as a connection ended. Asked once for any number of calls refused together; the refused call is then made again with the
|
|
20
|
+
* fresh pass, once.
|
|
21
|
+
*/
|
|
22
|
+
refreshPass?: () => Promise<string | null>;
|
|
11
23
|
/** Default: the `VECSNAP_BASE_URL` environment variable, else `https://api.vecsnap.com/v1` (a proxy, a test server). */
|
|
12
24
|
baseUrl?: string;
|
|
13
25
|
/** Your own `fetch` (a test's, a proxy's). Default: the platform's. */
|
|
@@ -59,3 +71,12 @@ type Operations = typeof operations;
|
|
|
59
71
|
export type BoundOperations = {
|
|
60
72
|
[K in keyof Operations as Operations[K] extends (...args: never[]) => unknown ? K : never]: Operations[K] extends (options: infer O) => infer R ? (options?: Omit<NonNullable<O>, "client">) => R : never;
|
|
61
73
|
};
|
|
74
|
+
/**
|
|
75
|
+
* A generated call's answer, or its refusal thrown as a `VecSnapError` (the one reading of an error, `VecSnapError.fromBody`):
|
|
76
|
+
* `const credits = await unwrap(vecsnap.api.getCredits())`.
|
|
77
|
+
*/
|
|
78
|
+
export declare function unwrap<T>(result: {
|
|
79
|
+
data?: T;
|
|
80
|
+
error?: unknown;
|
|
81
|
+
response?: Response;
|
|
82
|
+
}): Promise<T>;
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
* const result = await vecsnap.vectorize({ file: await readFile("logo.png"), filename: "logo.png" });
|
|
8
8
|
* await writeFile("logo.svg", await vecsnap.download(result));
|
|
9
9
|
*
|
|
10
|
+
* A connected app of VecSnap's own (the Figma plugin: `auth.md` § 8b) signs in as a person and holds a PASS, not a key:
|
|
11
|
+
* `new VecSnap({ pass, refreshPass })`. A pass lasts an hour; when the API refuses it (401 `invalid_pass`), `refreshPass`
|
|
12
|
+
* is asked once for a fresh one (the app's own refresh at `/oauth/token`) and the call is made again with it.
|
|
13
|
+
*
|
|
10
14
|
* Every operation a key may call is GENERATED from the API's own document (`./generated/`, never edited by hand —
|
|
11
15
|
* `npm --prefix web run gen:clients`) and reachable as `vecsnap.api.<operationId>`. This file adds what generation cannot:
|
|
12
16
|
* `vectorize` sends a file as a form and WAITS through a 202 (a conversion that outlasts the call), every refusal is a
|
|
@@ -21,25 +25,69 @@ export { signWebhook, verifyWebhook, WebhookVerificationError, WEBHOOK_TOLERANCE
|
|
|
21
25
|
/** Where the API answers. */
|
|
22
26
|
export const DEFAULT_BASE_URL = "https://api.vecsnap.com/v1";
|
|
23
27
|
/** The statuses a conversion ends in: it changes no more. */
|
|
24
|
-
const ENDED = new Set(["completed", "failed", "expired", "canceled"]);
|
|
28
|
+
export const ENDED = new Set(["completed", "failed", "expired", "canceled"]);
|
|
25
29
|
export class VecSnap {
|
|
26
30
|
/** Every operation a key may call, generated from the API's document, bound to this client: `vecsnap.api.getCredits()`. */
|
|
27
31
|
api;
|
|
28
32
|
#client;
|
|
29
33
|
#fetch;
|
|
30
34
|
#baseUrl;
|
|
31
|
-
|
|
35
|
+
/** The key, or the pass as last refreshed: every call reads it when it is made. */
|
|
36
|
+
#token;
|
|
37
|
+
#refreshPass;
|
|
38
|
+
/** The one refresh in flight: calls refused together share it. */
|
|
39
|
+
#refreshing = null;
|
|
32
40
|
constructor(options = {}) {
|
|
33
|
-
const apiKey = options.apiKey ?? globalThis.process?.env?.VECSNAP_API_KEY;
|
|
34
|
-
if (!apiKey)
|
|
35
|
-
throw new VecSnapError("missing_api_key", "no API key: pass { apiKey } or set VECSNAP_API_KEY (vecsnap.com/app/developers)", 0);
|
|
36
|
-
this.#apiKey = apiKey;
|
|
37
41
|
const env = globalThis.process?.env;
|
|
42
|
+
const token = options.pass ?? options.apiKey ?? env?.VECSNAP_API_KEY;
|
|
43
|
+
if (!token)
|
|
44
|
+
throw new VecSnapError("missing_api_key", "no API key: pass { apiKey } or set VECSNAP_API_KEY (vecsnap.com/app/developers)", 0);
|
|
45
|
+
this.#token = token;
|
|
46
|
+
this.#refreshPass = options.pass !== undefined ? (options.refreshPass ?? null) : null;
|
|
38
47
|
this.#baseUrl = (options.baseUrl ?? env?.VECSNAP_BASE_URL ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
39
48
|
this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
40
|
-
this.#client = createClient({ baseUrl: this.#baseUrl, fetch: this.#
|
|
49
|
+
this.#client = createClient({ baseUrl: this.#baseUrl, fetch: (input, init) => this.#send(input, init), auth: () => this.#token });
|
|
41
50
|
this.api = bind(this.#client);
|
|
42
51
|
}
|
|
52
|
+
/** A call to the API: a pass the API refuses is refreshed once, and the call made again with the fresh one. */
|
|
53
|
+
async #send(input, init) {
|
|
54
|
+
// A request's body is read once: a copy is kept for the second try, made before the first (the bodies sent here are small JSON;
|
|
55
|
+
// a file goes as a form, `init.body`, which is sent again as it is).
|
|
56
|
+
const again = this.#refreshPass && input instanceof Request ? input.clone() : null;
|
|
57
|
+
const sentWith = input instanceof Request ? input.headers.get("authorization") : new Headers(init?.headers).get("authorization");
|
|
58
|
+
const response = await this.#fetch(input, init);
|
|
59
|
+
if (!this.#refreshPass || response.status !== 401)
|
|
60
|
+
return response;
|
|
61
|
+
const code = await response
|
|
62
|
+
.clone()
|
|
63
|
+
.json()
|
|
64
|
+
.then((body) => body?.error?.code, () => null);
|
|
65
|
+
if (code !== "invalid_pass")
|
|
66
|
+
return response;
|
|
67
|
+
// Refused with a pass another call has already replaced: sent again with the new one, never a second refresh (a refresh token
|
|
68
|
+
// may be good for one use).
|
|
69
|
+
const fresh = sentWith !== `Bearer ${this.#token}` ? this.#token : await this.#refreshed();
|
|
70
|
+
if (fresh === null)
|
|
71
|
+
return response;
|
|
72
|
+
const authorization = `Bearer ${fresh}`;
|
|
73
|
+
if (again) {
|
|
74
|
+
again.headers.set("authorization", authorization);
|
|
75
|
+
return this.#fetch(again);
|
|
76
|
+
}
|
|
77
|
+
return this.#fetch(input, { ...init, headers: { ...Object.fromEntries(new Headers(init?.headers).entries()), authorization } });
|
|
78
|
+
}
|
|
79
|
+
#refreshed() {
|
|
80
|
+
this.#refreshing ??= this.#refreshPass()
|
|
81
|
+
.then((fresh) => {
|
|
82
|
+
if (fresh)
|
|
83
|
+
this.#token = fresh;
|
|
84
|
+
return fresh || null;
|
|
85
|
+
})
|
|
86
|
+
.finally(() => {
|
|
87
|
+
this.#refreshing = null;
|
|
88
|
+
});
|
|
89
|
+
return this.#refreshing;
|
|
90
|
+
}
|
|
43
91
|
/**
|
|
44
92
|
* Convert one image and wait for its SVG. The answer is the conversion and a link to each SVG file (`files`), valid
|
|
45
93
|
* until `filesExpireAt`. A conversion that outlasts one call (the API answers 202) is waited for here — read again with
|
|
@@ -119,7 +167,7 @@ export class VecSnap {
|
|
|
119
167
|
return new Uint8Array(await response.arrayBuffer());
|
|
120
168
|
}
|
|
121
169
|
async #vectorizeOnce(input, idempotencyKey, wait, signal) {
|
|
122
|
-
const headers = { authorization: `Bearer ${this.#
|
|
170
|
+
const headers = { authorization: `Bearer ${this.#token}`, "idempotency-key": idempotencyKey, accept: "application/json" };
|
|
123
171
|
let body;
|
|
124
172
|
if (input.file !== undefined) {
|
|
125
173
|
const form = new FormData();
|
|
@@ -138,7 +186,7 @@ export class VecSnap {
|
|
|
138
186
|
headers["content-type"] = "application/json";
|
|
139
187
|
body = JSON.stringify({ url: input.url, inputKey: input.inputKey, wait, preset: input.preset, options: input.options, filename: input.filename });
|
|
140
188
|
}
|
|
141
|
-
const response = await this.#
|
|
189
|
+
const response = await this.#send(`${this.#baseUrl}/vectorize`, { method: "POST", headers, body, signal });
|
|
142
190
|
if (response.status === 200)
|
|
143
191
|
return { done: true, body: (await response.json()) };
|
|
144
192
|
if (response.status === 202)
|
|
@@ -154,8 +202,11 @@ function bind(client) {
|
|
|
154
202
|
}
|
|
155
203
|
return bound;
|
|
156
204
|
}
|
|
157
|
-
/**
|
|
158
|
-
|
|
205
|
+
/**
|
|
206
|
+
* A generated call's answer, or its refusal thrown as a `VecSnapError` (the one reading of an error, `VecSnapError.fromBody`):
|
|
207
|
+
* `const credits = await unwrap(vecsnap.api.getCredits())`.
|
|
208
|
+
*/
|
|
209
|
+
export async function unwrap(result) {
|
|
159
210
|
if (result.data !== undefined && result.response?.ok)
|
|
160
211
|
return result.data;
|
|
161
212
|
return Promise.reject(VecSnapError.fromBody(result.response?.status ?? 0, result.error ?? null, result.response?.headers ?? null));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vecsnap/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The VecSnap API from TypeScript and JavaScript: turn an image into a clean SVG in one call.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,5 +35,8 @@
|
|
|
35
35
|
"vector",
|
|
36
36
|
"image",
|
|
37
37
|
"sdk"
|
|
38
|
-
]
|
|
38
|
+
],
|
|
39
|
+
"vecsnap": {
|
|
40
|
+
"contract": "c0d9c79e388ab026"
|
|
41
|
+
}
|
|
39
42
|
}
|