@genvoris/node 1.0.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 +132 -0
- package/dist/index.d.mts +328 -0
- package/dist/index.d.ts +328 -0
- package/dist/index.js +384 -0
- package/dist/index.mjs +353 -0
- package/package.json +52 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var GenvorisAPIError = class extends Error {
|
|
3
|
+
constructor(args) {
|
|
4
|
+
super(args.message ?? args.code);
|
|
5
|
+
this.name = "GenvorisAPIError";
|
|
6
|
+
this.status = args.status;
|
|
7
|
+
this.code = args.code;
|
|
8
|
+
this.requestId = args.requestId;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var GenvorisAuthError = class extends GenvorisAPIError {
|
|
12
|
+
constructor(args) {
|
|
13
|
+
super(args);
|
|
14
|
+
this.name = "GenvorisAuthError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var GenvorisRateLimitError = class extends GenvorisAPIError {
|
|
18
|
+
constructor(args) {
|
|
19
|
+
super(args);
|
|
20
|
+
this.name = "GenvorisRateLimitError";
|
|
21
|
+
this.retryAfterSeconds = args.retryAfterSeconds ?? 60;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var GenvorisValidationError = class extends GenvorisAPIError {
|
|
25
|
+
constructor(args) {
|
|
26
|
+
super(args);
|
|
27
|
+
this.name = "GenvorisValidationError";
|
|
28
|
+
this.fieldErrors = args.fieldErrors ?? {};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/http.ts
|
|
33
|
+
var RETRY_STATUSES = /* @__PURE__ */ new Set([429, 502, 503, 504]);
|
|
34
|
+
var MAX_DELAY_MS = 8e3;
|
|
35
|
+
var DEFAULT_BASE_URL = "https://genvoris.org/api/v1";
|
|
36
|
+
var SDK_VERSION = "1.0.0";
|
|
37
|
+
function sleep(ms) {
|
|
38
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
39
|
+
}
|
|
40
|
+
async function request(config, path, opts = {}, attempt = 0) {
|
|
41
|
+
const { method = "GET", body, query } = opts;
|
|
42
|
+
const fetchFn = config.fetch ?? globalThis.fetch;
|
|
43
|
+
const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
44
|
+
let url = `${baseUrl}${path}`;
|
|
45
|
+
if (query) {
|
|
46
|
+
const params = new URLSearchParams();
|
|
47
|
+
for (const [k, v] of Object.entries(query)) {
|
|
48
|
+
if (v !== void 0 && v !== null) params.set(k, String(v));
|
|
49
|
+
}
|
|
50
|
+
const qs = params.toString();
|
|
51
|
+
if (qs) url += `?${qs}`;
|
|
52
|
+
}
|
|
53
|
+
const controller = new AbortController();
|
|
54
|
+
const timerId = setTimeout(
|
|
55
|
+
() => controller.abort(),
|
|
56
|
+
config.timeoutMs ?? 3e4
|
|
57
|
+
);
|
|
58
|
+
let res;
|
|
59
|
+
try {
|
|
60
|
+
res = await fetchFn(url, {
|
|
61
|
+
method,
|
|
62
|
+
headers: {
|
|
63
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"User-Agent": `genvoris-node/${SDK_VERSION}`,
|
|
66
|
+
...config.defaultHeaders
|
|
67
|
+
},
|
|
68
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0,
|
|
69
|
+
signal: controller.signal
|
|
70
|
+
});
|
|
71
|
+
} finally {
|
|
72
|
+
clearTimeout(timerId);
|
|
73
|
+
}
|
|
74
|
+
if (res.ok) {
|
|
75
|
+
if (res.status === 204) return void 0;
|
|
76
|
+
return res.json();
|
|
77
|
+
}
|
|
78
|
+
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
79
|
+
let errBody = {};
|
|
80
|
+
try {
|
|
81
|
+
errBody = await res.json();
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
const code = errBody.error ?? "unknown_error";
|
|
85
|
+
const message = errBody.message ?? code;
|
|
86
|
+
if (RETRY_STATUSES.has(res.status)) {
|
|
87
|
+
const maxRetries = config.maxRetries ?? 3;
|
|
88
|
+
if (attempt < maxRetries) {
|
|
89
|
+
const base = Math.min(Math.pow(2, attempt) * 250, MAX_DELAY_MS);
|
|
90
|
+
const delay = Math.random() * base;
|
|
91
|
+
await sleep(delay);
|
|
92
|
+
return request(config, path, opts, attempt + 1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const errorBase = { status: res.status, code, message, requestId };
|
|
96
|
+
if (res.status === 401 || res.status === 403) {
|
|
97
|
+
throw new GenvorisAuthError(errorBase);
|
|
98
|
+
}
|
|
99
|
+
if (res.status === 429) {
|
|
100
|
+
const retryAfter = res.headers.get("retry-after");
|
|
101
|
+
throw new GenvorisRateLimitError({
|
|
102
|
+
...errorBase,
|
|
103
|
+
retryAfterSeconds: retryAfter ? Number(retryAfter) : 60
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (res.status === 400 || res.status === 422) {
|
|
107
|
+
throw new GenvorisValidationError({
|
|
108
|
+
...errorBase,
|
|
109
|
+
fieldErrors: errBody.fieldErrors ?? {}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
throw new GenvorisAPIError(errorBase);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/resources/customers.ts
|
|
116
|
+
var CustomersResource = class {
|
|
117
|
+
constructor(config) {
|
|
118
|
+
this.config = config;
|
|
119
|
+
}
|
|
120
|
+
/** Create or upsert an end-customer. */
|
|
121
|
+
create(params) {
|
|
122
|
+
return request(this.config, "/customers", {
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: params
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/** Retrieve a single customer by their Genvoris `id`. */
|
|
128
|
+
retrieve(id) {
|
|
129
|
+
return request(
|
|
130
|
+
this.config,
|
|
131
|
+
`/customers/${encodeURIComponent(id)}`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
/** Update a customer's email, plan, status, or metadata. */
|
|
135
|
+
update(id, params) {
|
|
136
|
+
return request(
|
|
137
|
+
this.config,
|
|
138
|
+
`/customers/${encodeURIComponent(id)}`,
|
|
139
|
+
{ method: "PATCH", body: params }
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
/** List customers with optional status filter and cursor pagination. */
|
|
143
|
+
list(params = {}) {
|
|
144
|
+
return request(this.config, "/customers", {
|
|
145
|
+
query: params
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** Soft-cancel a customer. Future try-ons return 402 `cancelled`. */
|
|
149
|
+
cancel(id) {
|
|
150
|
+
return request(
|
|
151
|
+
this.config,
|
|
152
|
+
`/customers/${encodeURIComponent(id)}`,
|
|
153
|
+
{ method: "DELETE" }
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
/** Return the customer's current-period quota state and usage history. */
|
|
157
|
+
usage(id) {
|
|
158
|
+
return request(
|
|
159
|
+
this.config,
|
|
160
|
+
`/customers/${encodeURIComponent(id)}/usage`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
/** List active widget sessions for this customer. */
|
|
164
|
+
sessions(id) {
|
|
165
|
+
return request(
|
|
166
|
+
this.config,
|
|
167
|
+
`/customers/${encodeURIComponent(id)}/sessions`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// src/resources/plans.ts
|
|
173
|
+
var PlansResource = class {
|
|
174
|
+
constructor(config) {
|
|
175
|
+
this.config = config;
|
|
176
|
+
}
|
|
177
|
+
/** List all plans. Pass `{ include_inactive: true }` to include disabled ones. */
|
|
178
|
+
list(params = {}) {
|
|
179
|
+
return request(this.config, "/plans", {
|
|
180
|
+
query: params
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/** Create a new plan. Returns `201 Created`. */
|
|
184
|
+
create(params) {
|
|
185
|
+
return request(this.config, "/plans", {
|
|
186
|
+
method: "POST",
|
|
187
|
+
body: params
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/** Retrieve a single plan. */
|
|
191
|
+
retrieve(id) {
|
|
192
|
+
return request(this.config, `/plans/${encodeURIComponent(id)}`);
|
|
193
|
+
}
|
|
194
|
+
/** Update any plan fields. */
|
|
195
|
+
update(id, params) {
|
|
196
|
+
return request(this.config, `/plans/${encodeURIComponent(id)}`, {
|
|
197
|
+
method: "PATCH",
|
|
198
|
+
body: params
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Soft-disable a plan. Existing customers keep quota until cancelled or
|
|
203
|
+
* reassigned. The plan stops appearing in `list()` unless `include_inactive`
|
|
204
|
+
* is `true`.
|
|
205
|
+
*/
|
|
206
|
+
archive(id) {
|
|
207
|
+
return request(this.config, `/plans/${encodeURIComponent(id)}`, {
|
|
208
|
+
method: "DELETE"
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/resources/sessions.ts
|
|
214
|
+
var SessionsResource = class {
|
|
215
|
+
constructor(config) {
|
|
216
|
+
this.config = config;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Mint a short-lived widget session token for a customer.
|
|
220
|
+
*
|
|
221
|
+
* The token should be sent to your frontend and passed to the
|
|
222
|
+
* Genvoris widget — never exposed in client-side code directly.
|
|
223
|
+
*/
|
|
224
|
+
mint({ customerId, ttlSeconds }) {
|
|
225
|
+
return request(
|
|
226
|
+
this.config,
|
|
227
|
+
`/customers/${encodeURIComponent(customerId)}/sessions`,
|
|
228
|
+
{
|
|
229
|
+
method: "POST",
|
|
230
|
+
body: ttlSeconds !== void 0 ? { expires_in: ttlSeconds } : void 0
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// src/resources/webhooks.ts
|
|
237
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
238
|
+
function hexToBytes(hex) {
|
|
239
|
+
if (hex.length % 2 !== 0) throw new Error("genvoris: invalid hex string");
|
|
240
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
241
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
242
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
243
|
+
}
|
|
244
|
+
return bytes;
|
|
245
|
+
}
|
|
246
|
+
var WebhooksResource = class {
|
|
247
|
+
constructor(config) {
|
|
248
|
+
this.config = config;
|
|
249
|
+
}
|
|
250
|
+
/** List all webhook endpoints for your store. */
|
|
251
|
+
list() {
|
|
252
|
+
return request(this.config, "/webhooks");
|
|
253
|
+
}
|
|
254
|
+
/** Register a new webhook endpoint. */
|
|
255
|
+
create(params) {
|
|
256
|
+
return request(this.config, "/webhooks", {
|
|
257
|
+
method: "POST",
|
|
258
|
+
body: params
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
/** Send a synthetic `webhook.test` ping to the endpoint. */
|
|
262
|
+
test(id) {
|
|
263
|
+
return request(
|
|
264
|
+
this.config,
|
|
265
|
+
`/webhooks/${encodeURIComponent(id)}/test`,
|
|
266
|
+
{ method: "POST" }
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
/** Delete a webhook endpoint. */
|
|
270
|
+
delete(id) {
|
|
271
|
+
return request(
|
|
272
|
+
this.config,
|
|
273
|
+
`/webhooks/${encodeURIComponent(id)}`,
|
|
274
|
+
{ method: "DELETE" }
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
// -------------------------------------------------------------------------
|
|
278
|
+
// Static helpers
|
|
279
|
+
// -------------------------------------------------------------------------
|
|
280
|
+
/**
|
|
281
|
+
* Verify the `X-Genvoris-Signature` header and return the parsed event.
|
|
282
|
+
*
|
|
283
|
+
* Throws if the signature is invalid, the timestamp is too old, or the
|
|
284
|
+
* header is malformed. Uses `crypto.timingSafeEqual` to prevent
|
|
285
|
+
* timing attacks.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* import { WebhooksResource } from '@genvoris/node';
|
|
290
|
+
*
|
|
291
|
+
* const event = WebhooksResource.verify({
|
|
292
|
+
* payload: req.body, // raw Buffer — do NOT parse JSON first
|
|
293
|
+
* header: req.header('x-genvoris-signature') ?? '',
|
|
294
|
+
* secret: process.env.GENVORIS_WEBHOOK_SECRET!,
|
|
295
|
+
* });
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
static verify({
|
|
299
|
+
payload,
|
|
300
|
+
header,
|
|
301
|
+
secret,
|
|
302
|
+
toleranceSeconds = 300
|
|
303
|
+
}) {
|
|
304
|
+
const raw = typeof payload === "string" ? payload : new TextDecoder().decode(payload);
|
|
305
|
+
const parts = {};
|
|
306
|
+
for (const part of header.split(",")) {
|
|
307
|
+
const eq = part.indexOf("=");
|
|
308
|
+
if (eq !== -1) parts[part.slice(0, eq).trim()] = part.slice(eq + 1).trim();
|
|
309
|
+
}
|
|
310
|
+
const { t, v1 } = parts;
|
|
311
|
+
if (!t || !v1) {
|
|
312
|
+
throw new Error("genvoris: invalid signature header \u2014 expected t=...,v1=...");
|
|
313
|
+
}
|
|
314
|
+
const ts = parseInt(t, 10);
|
|
315
|
+
if (!Number.isFinite(ts)) {
|
|
316
|
+
throw new Error("genvoris: invalid signature timestamp");
|
|
317
|
+
}
|
|
318
|
+
const age = Math.abs(Date.now() / 1e3 - ts);
|
|
319
|
+
if (age > toleranceSeconds) {
|
|
320
|
+
throw new Error(
|
|
321
|
+
`genvoris: signature timestamp too old (${Math.round(age)}s > ${toleranceSeconds}s tolerance)`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
const expectedHex = createHmac("sha256", secret).update(`${ts}.${raw}`).digest("hex");
|
|
325
|
+
const a = hexToBytes(expectedHex);
|
|
326
|
+
const b = hexToBytes(v1);
|
|
327
|
+
if (a.length !== b.length || !timingSafeEqual(a, b)) {
|
|
328
|
+
throw new Error("genvoris: signature mismatch");
|
|
329
|
+
}
|
|
330
|
+
return JSON.parse(raw);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// src/index.ts
|
|
335
|
+
var Genvoris = class {
|
|
336
|
+
constructor(config) {
|
|
337
|
+
if (!config?.apiKey) {
|
|
338
|
+
throw new Error("Genvoris: apiKey is required");
|
|
339
|
+
}
|
|
340
|
+
this.customers = new CustomersResource(config);
|
|
341
|
+
this.plans = new PlansResource(config);
|
|
342
|
+
this.sessions = new SessionsResource(config);
|
|
343
|
+
this.webhooks = new WebhooksResource(config);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
export {
|
|
347
|
+
GenvorisAPIError,
|
|
348
|
+
GenvorisAuthError,
|
|
349
|
+
GenvorisRateLimitError,
|
|
350
|
+
GenvorisValidationError,
|
|
351
|
+
WebhooksResource,
|
|
352
|
+
Genvoris as default
|
|
353
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@genvoris/node",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official Node.js SDK for the Genvoris Virtual Try-On API",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"genvoris",
|
|
33
|
+
"virtual-try-on",
|
|
34
|
+
"vton",
|
|
35
|
+
"ecommerce",
|
|
36
|
+
"fashion",
|
|
37
|
+
"ai"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/DevSajjadAli/genvoris-node"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://docs.genvoris.org",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/DevSajjadAli/genvoris-node/issues",
|
|
46
|
+
"email": "support@genvoris.org"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"typescript": "^5.4.5",
|
|
50
|
+
"tsup": "^8.0.2"
|
|
51
|
+
}
|
|
52
|
+
}
|