react-shopwave-connect 0.2.0 → 0.4.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 +215 -24
- package/dist/core/index.cjs +903 -88
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +604 -23
- package/dist/core/index.d.ts +604 -23
- package/dist/core/index.js +845 -89
- package/dist/core/index.js.map +1 -1
- package/dist/hooks/index.cjs +735 -109
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.cts +263 -12
- package/dist/hooks/index.d.ts +263 -12
- package/dist/hooks/index.js +732 -111
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +1087 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +670 -24
- package/dist/index.d.ts +670 -24
- package/dist/index.js +1025 -109
- package/dist/index.js.map +1 -1
- package/dist/next/index.cjs +560 -2
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.d.cts +249 -1
- package/dist/next/index.d.ts +249 -1
- package/dist/next/index.js +556 -3
- package/dist/next/index.js.map +1 -1
- package/dist/server/index.cjs +531 -1
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +258 -1
- package/dist/server/index.d.ts +258 -1
- package/dist/server/index.js +521 -2
- package/dist/server/index.js.map +1 -1
- package/package.json +1 -1
package/dist/server/index.d.ts
CHANGED
|
@@ -173,5 +173,262 @@ declare class ShopwaveAuthError extends Error {
|
|
|
173
173
|
get isInvalidGrant(): boolean;
|
|
174
174
|
}
|
|
175
175
|
//#endregion
|
|
176
|
-
|
|
176
|
+
//#region src/core/entities.d.ts
|
|
177
|
+
/**
|
|
178
|
+
* How each Shopwave entity is addressed, shared by the client functions below
|
|
179
|
+
* and the server route handlers in `react-shopwave-connect/next`, so the two
|
|
180
|
+
* halves of the contract can't drift apart.
|
|
181
|
+
*/
|
|
182
|
+
interface EntityDefinition {
|
|
183
|
+
/** App route segment: `/api/<route>` and `/api/<route>/<id>`. */
|
|
184
|
+
route: string;
|
|
185
|
+
/** Key of the entity map in requests and responses (`{ products: { … } }`). */
|
|
186
|
+
collection: string;
|
|
187
|
+
/** Shopwave API path (`GET/POST/DELETE {apiUrl}/<upstream>`). */
|
|
188
|
+
upstream: string;
|
|
189
|
+
/** Header that filters reads by id (`productIds: 1,2`). */
|
|
190
|
+
idsHeader: string;
|
|
191
|
+
/** Header that names the record to delete (`productId: 1`). */
|
|
192
|
+
idHeader: string;
|
|
193
|
+
/** False for read-only entities (the API has no POST for them). */
|
|
194
|
+
writable: boolean;
|
|
195
|
+
/**
|
|
196
|
+
* How a delete is done upstream:
|
|
197
|
+
* - `"delete"`: `DELETE {apiUrl}/<upstream>` with the `idHeader` (205, empty body)
|
|
198
|
+
* - `"retire"`: the API has no DELETE; the record is retired by POSTing
|
|
199
|
+
* `{ id, [retireField]: <now> }` (employees: `exitDate`, promotions: `endDate`)
|
|
200
|
+
* - `"none"`: can't be deleted
|
|
201
|
+
*/
|
|
202
|
+
deleteMode: "delete" | "retire" | "none";
|
|
203
|
+
/** Date field set to "now" when `deleteMode` is `"retire"`. */
|
|
204
|
+
retireField?: string;
|
|
205
|
+
}
|
|
206
|
+
type EntityKind = "product" | "category" | "store" | "promotion" | "employee" | "consumer";
|
|
207
|
+
/** Entities that can be created/updated through the API. */
|
|
208
|
+
type WritableEntityKind = Exclude<EntityKind, "consumer">;
|
|
209
|
+
/** Entities that can be deleted (or retired) through the API. */
|
|
210
|
+
type DeletableEntityKind = Exclude<EntityKind, "consumer">;
|
|
211
|
+
/**
|
|
212
|
+
* Addressing per entity, following the Shopwave API reference
|
|
213
|
+
* (https://developer.merchantstack.com/api-reference.html) and checked against
|
|
214
|
+
* the live API where noted:
|
|
215
|
+
* - category/product/store: GET filtered by `<x>Ids`, POST upsert, DELETE with `<x>Id` → 205.
|
|
216
|
+
* - promotion: no DELETE; "deleting" a promotion ends it by setting `endDate` to now.
|
|
217
|
+
* - employee: POST updates `roleId`, `joinedDate`, `exitDate` (names can't be
|
|
218
|
+
* changed); no DELETE (live API: "Cannot DELETE /employee") → retired via `exitDate`.
|
|
219
|
+
* - consumer: read-only (`GET` with `ids`, comma-separated); live POST → 404.
|
|
220
|
+
*/
|
|
221
|
+
declare const SHOPWAVE_ENTITIES: Readonly<Record<EntityKind, EntityDefinition>>;
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/core/resources.d.ts
|
|
224
|
+
/**
|
|
225
|
+
* Singleton resources (one per logged-in user) and the uploader.
|
|
226
|
+
*
|
|
227
|
+
* Like `SHOPWAVE_ENTITIES`, this table is shared by the client functions
|
|
228
|
+
* (`fetchMerchant`, `fetchUser`, `uploadImage`, …) and the server route
|
|
229
|
+
* handlers in `react-shopwave-connect/next`, so the two halves of the contract
|
|
230
|
+
* can't drift apart. These aren't in `SHOPWAVE_ENTITIES` because they aren't
|
|
231
|
+
* keyed collections: `GET /merchant` answers `{ merchant: {…} }`, not a map by id.
|
|
232
|
+
*/
|
|
233
|
+
interface ResourceDefinition {
|
|
234
|
+
/** App route: `/api/<route>`. */
|
|
235
|
+
route: string;
|
|
236
|
+
/** Key of the object in requests and responses (`{ merchant: {…} }`). */
|
|
237
|
+
key: string;
|
|
238
|
+
/** Shopwave API path. */
|
|
239
|
+
upstream: string;
|
|
240
|
+
/** False for read-only resources. */
|
|
241
|
+
writable: boolean;
|
|
242
|
+
}
|
|
243
|
+
type ResourceKind = "merchant" | "user";
|
|
244
|
+
/**
|
|
245
|
+
* - merchant: `GET /merchant`; `POST /merchant` with `{ merchant: { id, … } }`
|
|
246
|
+
* updates it (without an `id` the API would create a second merchant, so the
|
|
247
|
+
* route requires one).
|
|
248
|
+
* - user: `GET /user` (read-only here; the API's `POST /user` invites users and
|
|
249
|
+
* needs the admin scope).
|
|
250
|
+
*/
|
|
251
|
+
declare const SHOPWAVE_RESOURCES: Readonly<Record<ResourceKind, ResourceDefinition>>;
|
|
252
|
+
/**
|
|
253
|
+
* What an upload is for. Sent to Shopwave as the `contentType` header of
|
|
254
|
+
* `PUT /uploader`. `merchant` is for the receipt and square logos (stored
|
|
255
|
+
* under `…/merchant/<id>/logo/`), `merchantFeature` for the featured image.
|
|
256
|
+
* The API spells two of its values `applicaionLogo` / `applicaionImages`; use
|
|
257
|
+
* the correctly spelt names here and the server maps them.
|
|
258
|
+
*/
|
|
259
|
+
type UploadKind = "merchant" | "merchantFeature" | "product" | "user" | "applicationLogo" | "applicationImages";
|
|
260
|
+
/** `UploadKind` → the API's `contentType` value. */
|
|
261
|
+
declare const UPLOAD_KINDS: Readonly<Record<UploadKind, string>>;
|
|
262
|
+
/** How uploads are addressed (app route and Shopwave uploader). */
|
|
263
|
+
declare const SHOPWAVE_UPLOAD: Readonly<{
|
|
264
|
+
/** App route: `POST /api/<route>` (multipart `file` + `kind`). */
|
|
265
|
+
route: "upload";
|
|
266
|
+
/** Shopwave path: `PUT {apiUrl}/uploader` (multipart `file`). */
|
|
267
|
+
upstream: "uploader";
|
|
268
|
+
/** Header that carries the upload kind upstream. */
|
|
269
|
+
kindHeader: "contentType";
|
|
270
|
+
/** Multipart field name for the file, on both sides. */
|
|
271
|
+
fileField: "file";
|
|
272
|
+
/** Multipart field name for the kind in the app request. */
|
|
273
|
+
kindField: "kind";
|
|
274
|
+
}>;
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/server/api.d.ts
|
|
277
|
+
/**
|
|
278
|
+
* The OAuth token a caller sent with the request, without its scheme, or `null`.
|
|
279
|
+
*
|
|
280
|
+
* Reads, in order: `Authorization: OAuth|Bearer <token>` (what the SDK sends
|
|
281
|
+
* from 0.3), the `token` header (SDK ≤ 0.2 writes), and `extras.token`
|
|
282
|
+
* (SDK ≤ 0.2 reads).
|
|
283
|
+
*/
|
|
284
|
+
declare function readRequestToken(request: Request): string | null;
|
|
285
|
+
/**
|
|
286
|
+
* `extras` keys that are never forwarded upstream (case-insensitive).
|
|
287
|
+
* `Content-Type` is allowed (SDK clients have always sent `application/json`
|
|
288
|
+
* on reads) but is replaced by the form content type on writes.
|
|
289
|
+
*/
|
|
290
|
+
declare const BLOCKED_EXTRAS: ReadonlySet<string>;
|
|
291
|
+
/**
|
|
292
|
+
* Parses the `extras` request header into upstream headers.
|
|
293
|
+
* Returns `null` when the header isn't a JSON object.
|
|
294
|
+
*/
|
|
295
|
+
declare function parseExtras(raw: string | null, blocked?: ReadonlySet<string>): Record<string, string> | null;
|
|
296
|
+
declare class RequestBodyError extends Error {}
|
|
297
|
+
/**
|
|
298
|
+
* Normalises a write request body into the Shopwave envelope
|
|
299
|
+
* `{ <collection>: { <ref>: entity, … } }`.
|
|
300
|
+
*
|
|
301
|
+
* Accepts:
|
|
302
|
+
* - the SDK ≥ 0.3 shape `{ <collection>: { "0": {...}, "1": {...} } }` (refs kept),
|
|
303
|
+
* - the older AdminUI shapes `{ <collection>: { new: {...} } }` / `{ updated: {...} }`,
|
|
304
|
+
* - a bare entity object `{ title: … }`.
|
|
305
|
+
*
|
|
306
|
+
* With `forcedId` (item routes) exactly one entity is allowed and its `id` is
|
|
307
|
+
* set from the URL.
|
|
308
|
+
*/
|
|
309
|
+
declare function normalizeWriteBody(def: Pick<EntityDefinition, "collection">, body: unknown, forcedId?: string): Record<string, Record<string, Record<string, unknown>>>;
|
|
310
|
+
interface ShopwaveApiConfig {
|
|
311
|
+
/** Shopwave API base URL, e.g. `process.env.SHOPWAVE_API_SERVER_URL`. */
|
|
312
|
+
apiUrl: string;
|
|
313
|
+
/**
|
|
314
|
+
* `Authorization` header value for the logged-in user (`"OAuth <token>"`),
|
|
315
|
+
* or `null` when logged out. Called again with `forceRefresh: true` when the
|
|
316
|
+
* API reports the token expired.
|
|
317
|
+
*/
|
|
318
|
+
getAuthorization?: (options?: {
|
|
319
|
+
forceRefresh?: boolean;
|
|
320
|
+
}) => Promise<string | null>;
|
|
321
|
+
/**
|
|
322
|
+
* Accept a token sent by the caller (`Authorization: OAuth <token>`, or the
|
|
323
|
+
* legacy `token` header / `extras.token`) instead of the session. The token
|
|
324
|
+
* is only forwarded to the Shopwave API, which validates it. Defaults to `true`
|
|
325
|
+
* (SDK integration tests, scripts and server-to-server calls rely on it).
|
|
326
|
+
*/
|
|
327
|
+
allowRequestToken?: boolean;
|
|
328
|
+
/** `x-accept-version` sent upstream. Defaults to `"2.0"`. */
|
|
329
|
+
apiVersion?: string;
|
|
330
|
+
/** Custom fetch for the upstream call. */
|
|
331
|
+
fetch?: typeof fetch;
|
|
332
|
+
/** Override how an entity is addressed (e.g. a different id header). */
|
|
333
|
+
entities?: Partial<Record<EntityKind, Partial<EntityDefinition>>>;
|
|
334
|
+
/** Extra `extras` keys to drop, on top of {@link BLOCKED_EXTRAS}. */
|
|
335
|
+
blockedExtras?: string[];
|
|
336
|
+
/**
|
|
337
|
+
* Called when the upstream call throws (network error). Defaults to a
|
|
338
|
+
* `console.error` with the method and path only — never headers or tokens.
|
|
339
|
+
*/
|
|
340
|
+
onError?: (error: unknown, context: {
|
|
341
|
+
method: string;
|
|
342
|
+
path: string;
|
|
343
|
+
}) => void;
|
|
344
|
+
}
|
|
345
|
+
interface ForwardInit {
|
|
346
|
+
/** HTTP method sent upstream. Defaults to `GET`. */
|
|
347
|
+
method?: string;
|
|
348
|
+
/** Shopwave API path, e.g. `"product"`. */
|
|
349
|
+
path: string;
|
|
350
|
+
/** Extra upstream headers; they win over `extras`. */
|
|
351
|
+
headers?: Record<string, string>;
|
|
352
|
+
/** Sent as the form field `postBody=<JSON>`. */
|
|
353
|
+
postBody?: unknown;
|
|
354
|
+
/** Sent as a multipart body (e.g. uploads). Ignored when `postBody` is set. */
|
|
355
|
+
formData?: FormData;
|
|
356
|
+
/** Forward the caller's `extras` header as upstream headers. Defaults to `true`. */
|
|
357
|
+
forwardExtras?: boolean;
|
|
358
|
+
}
|
|
359
|
+
/** Second argument Next.js (and similar) pass to dynamic route handlers. */
|
|
360
|
+
interface RouteContext {
|
|
361
|
+
params?: Promise<Record<string, string | string[] | undefined>> | Record<string, string | string[] | undefined>;
|
|
362
|
+
}
|
|
363
|
+
type RouteHandler = (request: Request, context?: RouteContext) => Promise<Response>;
|
|
364
|
+
interface CollectionHandlers {
|
|
365
|
+
/** List/filter via `extras` (e.g. `productIds`, `deleted`). */
|
|
366
|
+
GET: RouteHandler;
|
|
367
|
+
/** Create or update (Shopwave upserts: an `id` means update). */
|
|
368
|
+
POST: RouteHandler;
|
|
369
|
+
/** Same as POST (kept for older clients). */
|
|
370
|
+
PUT: RouteHandler;
|
|
371
|
+
}
|
|
372
|
+
interface ItemHandlers {
|
|
373
|
+
/** Read one record by the `[id]` route param. */
|
|
374
|
+
GET: RouteHandler;
|
|
375
|
+
/** Update the record at `[id]` (the id in the URL wins). */
|
|
376
|
+
PUT: RouteHandler;
|
|
377
|
+
/** Delete the record at `[id]`. Shopwave answers 205 with an empty body. */
|
|
378
|
+
DELETE: RouteHandler;
|
|
379
|
+
}
|
|
380
|
+
interface ResourceHandlers {
|
|
381
|
+
/** Read the resource (`GET {apiUrl}/<upstream>`). */
|
|
382
|
+
GET: RouteHandler;
|
|
383
|
+
/**
|
|
384
|
+
* Update it: body `{ <key>: { id, … } }` or the bare object. The `id` is
|
|
385
|
+
* required (without one Shopwave would create a new record). 405 for
|
|
386
|
+
* read-only resources.
|
|
387
|
+
*/
|
|
388
|
+
PUT: RouteHandler;
|
|
389
|
+
/** Same as PUT. */
|
|
390
|
+
POST: RouteHandler;
|
|
391
|
+
}
|
|
392
|
+
interface UploadOptions {
|
|
393
|
+
/** Largest accepted file, in bytes. Defaults to 10 MB. */
|
|
394
|
+
maxBytes?: number;
|
|
395
|
+
/**
|
|
396
|
+
* Accepted MIME types. An entry ending in `/` is a prefix (`"image/"`).
|
|
397
|
+
* Defaults to `["image/"]`.
|
|
398
|
+
*/
|
|
399
|
+
accept?: string[];
|
|
400
|
+
}
|
|
401
|
+
interface UploadHandlers {
|
|
402
|
+
/**
|
|
403
|
+
* Multipart `file` + `kind` (see `UploadKind`) → `PUT {apiUrl}/uploader`.
|
|
404
|
+
* Answers with Shopwave's reply (201 `{ fileName, path, api }`); 400 for a
|
|
405
|
+
* missing file or unknown kind, 413 when too large, 415 for other types.
|
|
406
|
+
*/
|
|
407
|
+
POST: RouteHandler;
|
|
408
|
+
}
|
|
409
|
+
interface ShopwaveApiHandlers {
|
|
410
|
+
/** Calls the Shopwave API for this request (token, extras, refresh-retry) and returns its answer. */
|
|
411
|
+
forward(request: Request, init: ForwardInit): Promise<Response>;
|
|
412
|
+
/** Handlers for `app/api/<route>/route.ts`. */
|
|
413
|
+
collection(kind: EntityKind): CollectionHandlers;
|
|
414
|
+
/** Handlers for `app/api/<route>/[id]/route.ts`. */
|
|
415
|
+
item(kind: EntityKind, options?: {
|
|
416
|
+
param?: string;
|
|
417
|
+
}): ItemHandlers;
|
|
418
|
+
/** GET-only proxy for a Shopwave path (e.g. `"report"`). */
|
|
419
|
+
passthrough(path: string): {
|
|
420
|
+
GET: RouteHandler;
|
|
421
|
+
};
|
|
422
|
+
/** Handlers for a singleton resource: `app/api/merchant/route.ts`, `app/api/user/route.ts`. */
|
|
423
|
+
resource(kind: ResourceKind): ResourceHandlers;
|
|
424
|
+
/** Handler for `app/api/upload/route.ts`. */
|
|
425
|
+
upload(options?: UploadOptions): UploadHandlers;
|
|
426
|
+
/** The entity table in use (defaults merged with `config.entities`). */
|
|
427
|
+
entity(kind: EntityKind): EntityDefinition;
|
|
428
|
+
}
|
|
429
|
+
/** `YYYY-MM-DD HH:mm:ss` in UTC, the datetime format Shopwave writes use (e.g. promotions). */
|
|
430
|
+
declare function shopwaveDateTime(date?: Date): string;
|
|
431
|
+
declare function createShopwaveApiHandlers(config: ShopwaveApiConfig): ShopwaveApiHandlers;
|
|
432
|
+
//#endregion
|
|
433
|
+
export { BLOCKED_EXTRAS, type CollectionHandlers, type DeletableEntityKind, type EntityDefinition, type EntityKind, type ForwardInit, type ItemHandlers, RequestBodyError, type ResourceDefinition, type ResourceHandlers, type ResourceKind, type RouteContext, type RouteHandler, SHOPWAVE_ENTITIES, SHOPWAVE_RESOURCES, SHOPWAVE_TOKEN_EXPIRED_ERROR_ID, SHOPWAVE_UPLOAD, type ShopwaveApiConfig, type ShopwaveApiHandlers, ShopwaveAuthError, type ShopwaveAuthErrorCode, type ShopwaveOAuthClient, type ShopwaveOAuthConfig, type ShopwaveToken, type ShopwaveTokenResponse, UPLOAD_KINDS, type UploadHandlers, type UploadKind, type UploadOptions, type WritableEntityKind, assertOAuthConfig, authorizationHeader, createShopwaveApiHandlers, createShopwaveOAuth, createState, isExpiredTokenResponse, isTokenExpired, normalizeStoredToken, normalizeWriteBody, parseExtras, readRequestToken, safeEqual, sanitizeReturnTo, shopwaveDateTime, tokenFromResponse };
|
|
177
434
|
//# sourceMappingURL=index.d.ts.map
|