react-shopwave-connect 0.3.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 +71 -3
- package/dist/core/index.cjs +511 -0
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +391 -1
- package/dist/core/index.d.ts +391 -1
- package/dist/core/index.js +481 -1
- package/dist/core/index.js.map +1 -1
- package/dist/hooks/index.cjs +456 -0
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.cts +229 -1
- package/dist/hooks/index.d.ts +229 -1
- package/dist/hooks/index.js +452 -1
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +595 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +444 -1
- package/dist/index.d.ts +444 -1
- package/dist/index.js +560 -1
- package/dist/index.js.map +1 -1
- package/dist/next/index.cjs +126 -2
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.d.cts +82 -2
- package/dist/next/index.d.ts +82 -2
- package/dist/next/index.js +125 -3
- package/dist/next/index.js.map +1 -1
- package/dist/server/index.cjs +122 -2
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +90 -2
- package/dist/server/index.d.ts +90 -2
- package/dist/server/index.js +120 -3
- package/dist/server/index.js.map +1 -1
- package/package.json +1 -1
package/dist/hooks/index.js
CHANGED
|
@@ -113,6 +113,20 @@ async function readBody(response) {
|
|
|
113
113
|
return { raw: text };
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Low-level request: resolves `{ status, body }` for 2xx responses and throws
|
|
118
|
+
* {@link ShopwaveApiError} otherwise. `init.headers` are merged over the
|
|
119
|
+
* standard ones (Accept + Authorization).
|
|
120
|
+
*/
|
|
121
|
+
async function apiRequest(path, init, options = {}) {
|
|
122
|
+
return send(path, {
|
|
123
|
+
...init,
|
|
124
|
+
headers: {
|
|
125
|
+
...baseHeaders(options),
|
|
126
|
+
...init.headers
|
|
127
|
+
}
|
|
128
|
+
}, options);
|
|
129
|
+
}
|
|
116
130
|
async function send(path, init, options) {
|
|
117
131
|
const doFetch = resolveFetch(options);
|
|
118
132
|
const url = (options.baseUrl ?? "") + path;
|
|
@@ -498,6 +512,364 @@ function buildConsumerMap(consumers) {
|
|
|
498
512
|
return map;
|
|
499
513
|
}
|
|
500
514
|
|
|
515
|
+
//#endregion
|
|
516
|
+
//#region src/core/resources.ts
|
|
517
|
+
/**
|
|
518
|
+
* - merchant: `GET /merchant`; `POST /merchant` with `{ merchant: { id, … } }`
|
|
519
|
+
* updates it (without an `id` the API would create a second merchant, so the
|
|
520
|
+
* route requires one).
|
|
521
|
+
* - user: `GET /user` (read-only here; the API's `POST /user` invites users and
|
|
522
|
+
* needs the admin scope).
|
|
523
|
+
*/
|
|
524
|
+
const SHOPWAVE_RESOURCES = Object.freeze({
|
|
525
|
+
merchant: {
|
|
526
|
+
route: "merchant",
|
|
527
|
+
key: "merchant",
|
|
528
|
+
upstream: "merchant",
|
|
529
|
+
writable: true
|
|
530
|
+
},
|
|
531
|
+
user: {
|
|
532
|
+
route: "user",
|
|
533
|
+
key: "user",
|
|
534
|
+
upstream: "user",
|
|
535
|
+
writable: false
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
/** `UploadKind` → the API's `contentType` value. */
|
|
539
|
+
const UPLOAD_KINDS = Object.freeze({
|
|
540
|
+
merchant: "merchant",
|
|
541
|
+
merchantFeature: "merchantFeature",
|
|
542
|
+
product: "product",
|
|
543
|
+
user: "user",
|
|
544
|
+
applicationLogo: "applicaionLogo",
|
|
545
|
+
applicationImages: "applicaionImages"
|
|
546
|
+
});
|
|
547
|
+
/** How uploads are addressed (app route and Shopwave uploader). */
|
|
548
|
+
const SHOPWAVE_UPLOAD = Object.freeze({
|
|
549
|
+
route: "upload",
|
|
550
|
+
upstream: "uploader",
|
|
551
|
+
kindHeader: "contentType",
|
|
552
|
+
fileField: "file",
|
|
553
|
+
kindField: "kind"
|
|
554
|
+
});
|
|
555
|
+
function isUploadKind(value) {
|
|
556
|
+
return typeof value === "string" && Object.prototype.hasOwnProperty.call(UPLOAD_KINDS, value);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
//#endregion
|
|
560
|
+
//#region src/core/imageFit.ts
|
|
561
|
+
/**
|
|
562
|
+
* The centred part of a `srcWidth × srcHeight` image with the aspect ratio of
|
|
563
|
+
* `dstWidth × dstHeight` (like CSS `object-fit: cover`).
|
|
564
|
+
*/
|
|
565
|
+
function coverCrop(srcWidth, srcHeight, dstWidth, dstHeight) {
|
|
566
|
+
if (![
|
|
567
|
+
srcWidth,
|
|
568
|
+
srcHeight,
|
|
569
|
+
dstWidth,
|
|
570
|
+
dstHeight
|
|
571
|
+
].every((n) => Number.isFinite(n) && n > 0)) throw new Error("coverCrop: sizes must be positive numbers");
|
|
572
|
+
const srcRatio = srcWidth / srcHeight;
|
|
573
|
+
const dstRatio = dstWidth / dstHeight;
|
|
574
|
+
if (srcRatio > dstRatio) {
|
|
575
|
+
const sw = Math.round(srcHeight * dstRatio);
|
|
576
|
+
return {
|
|
577
|
+
sx: Math.round((srcWidth - sw) / 2),
|
|
578
|
+
sy: 0,
|
|
579
|
+
sw,
|
|
580
|
+
sh: srcHeight
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
const sh = Math.round(srcWidth / dstRatio);
|
|
584
|
+
return {
|
|
585
|
+
sx: 0,
|
|
586
|
+
sy: Math.round((srcHeight - sh) / 2),
|
|
587
|
+
sw: srcWidth,
|
|
588
|
+
sh
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
function requireBrowserImages(fn) {
|
|
592
|
+
if (typeof createImageBitmap !== "function") throw new Error(`${fn} needs a browser (createImageBitmap is not available)`);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Returns the image at exactly `width × height`: scaled and centre-cropped
|
|
596
|
+
* (never stretched). Browser only. The result is a `File` named like the
|
|
597
|
+
* input, with the output type's extension.
|
|
598
|
+
*/
|
|
599
|
+
async function fitImageToSize(file, width, height, options = {}) {
|
|
600
|
+
requireBrowserImages("fitImageToSize");
|
|
601
|
+
const { type = "image/png", quality, keepIfExact = true } = options;
|
|
602
|
+
const name = file.name || "image";
|
|
603
|
+
const ext = type === "image/jpeg" ? "jpg" : type.split("/")[1];
|
|
604
|
+
const outName = `${name.replace(/\.[^.]+$/, "")}.${ext}`;
|
|
605
|
+
const bitmap = await createImageBitmap(file);
|
|
606
|
+
try {
|
|
607
|
+
if (keepIfExact && bitmap.width === width && bitmap.height === height && file.type === type) return file instanceof File ? file : new File([file], outName, { type });
|
|
608
|
+
const { sx, sy, sw, sh } = coverCrop(bitmap.width, bitmap.height, width, height);
|
|
609
|
+
let blob;
|
|
610
|
+
if (typeof OffscreenCanvas === "function") {
|
|
611
|
+
const canvas = new OffscreenCanvas(width, height);
|
|
612
|
+
const ctx = canvas.getContext("2d");
|
|
613
|
+
if (!ctx) throw new Error("fitImageToSize: no 2D canvas context");
|
|
614
|
+
ctx.imageSmoothingQuality = "high";
|
|
615
|
+
ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
|
|
616
|
+
blob = await canvas.convertToBlob({
|
|
617
|
+
type,
|
|
618
|
+
quality
|
|
619
|
+
});
|
|
620
|
+
} else {
|
|
621
|
+
const canvas = document.createElement("canvas");
|
|
622
|
+
canvas.width = width;
|
|
623
|
+
canvas.height = height;
|
|
624
|
+
const ctx = canvas.getContext("2d");
|
|
625
|
+
if (!ctx) throw new Error("fitImageToSize: no 2D canvas context");
|
|
626
|
+
ctx.imageSmoothingQuality = "high";
|
|
627
|
+
ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
|
|
628
|
+
blob = await new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(/* @__PURE__ */ new Error("fitImageToSize: encoding failed")), type, quality));
|
|
629
|
+
}
|
|
630
|
+
return new File([blob], outName, { type });
|
|
631
|
+
} finally {
|
|
632
|
+
bitmap.close?.();
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
//#endregion
|
|
637
|
+
//#region src/core/upload.ts
|
|
638
|
+
/**
|
|
639
|
+
* HTTPS host that serves the Shopwave CDN's images. The API returns
|
|
640
|
+
* `http://static.merchantstack.com/images/…`, which has no HTTPS endpoint
|
|
641
|
+
* (checked Sep 2026), so pages served over HTTPS can't show it.
|
|
642
|
+
*/
|
|
643
|
+
const SHOPWAVE_IMAGE_BASE_URL = "https://s3-eu-west-1.amazonaws.com/static.merchantstack.com/images";
|
|
644
|
+
const STATIC_HOST = /^https?:\/\/static\.merchantstack\.com\/images\//i;
|
|
645
|
+
/**
|
|
646
|
+
* Turns an image path from the API into a URL a page can load over HTTPS:
|
|
647
|
+
* `http://static.merchantstack.com/images/x.png` → `<base>/x.png`, and a
|
|
648
|
+
* relative path (`merchant/5644/logo/x.png`) → `<base>/merchant/5644/logo/x.png`.
|
|
649
|
+
* Other absolute URLs are returned unchanged; empty input gives `""`.
|
|
650
|
+
*/
|
|
651
|
+
function getImageUrl(path, base = SHOPWAVE_IMAGE_BASE_URL) {
|
|
652
|
+
if (!path) return "";
|
|
653
|
+
const root = base.replace(/\/+$/, "");
|
|
654
|
+
if (STATIC_HOST.test(path)) return `${root}/${path.replace(STATIC_HOST, "")}`;
|
|
655
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path) || path.startsWith("//")) return path;
|
|
656
|
+
return `${root}/${path.replace(/^\/+/, "")}`;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Uploads one image to the Shopwave CDN through the app's `/api/upload` route
|
|
660
|
+
* (`createShopwaveApi(...).upload()`), and returns its id and URL.
|
|
661
|
+
*
|
|
662
|
+
* The file only lands on the CDN (under `…/images/<kind>/<merchantId>/…`); a
|
|
663
|
+
* record has to point at it to use it. Live API (Sep 2026): the upload answers
|
|
664
|
+
* 201 `{ fileName, path }`.
|
|
665
|
+
*/
|
|
666
|
+
async function uploadImage(file, params, options = {}) {
|
|
667
|
+
if (!file || typeof file.size !== "number") throw new Error("uploadImage: file must be a File or Blob");
|
|
668
|
+
if (!isUploadKind(params?.kind)) throw new Error(`uploadImage: unknown kind "${params?.kind}"`);
|
|
669
|
+
const name = params.fileName ?? file.name ?? "upload";
|
|
670
|
+
const form = new FormData();
|
|
671
|
+
form.append(SHOPWAVE_UPLOAD.fileField, file, name);
|
|
672
|
+
form.append(SHOPWAVE_UPLOAD.kindField, params.kind);
|
|
673
|
+
const { status, body: raw } = await apiRequest(`/api/${SHOPWAVE_UPLOAD.route}`, {
|
|
674
|
+
method: "POST",
|
|
675
|
+
body: form
|
|
676
|
+
}, options);
|
|
677
|
+
const body = raw;
|
|
678
|
+
assertNoApiErrors(body, status);
|
|
679
|
+
const id = typeof body?.fileName === "string" ? body.fileName : "";
|
|
680
|
+
const path = typeof body?.path === "string" ? body.path : "";
|
|
681
|
+
if (!id) throw new ShopwaveApiError(status, `Shopwave API error (${status}): the upload response has no fileName`, { body });
|
|
682
|
+
return {
|
|
683
|
+
id,
|
|
684
|
+
url: getImageUrl(path),
|
|
685
|
+
path
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
//#endregion
|
|
690
|
+
//#region src/core/merchantImages.ts
|
|
691
|
+
/**
|
|
692
|
+
* Where each slot lives, its upload kind and its exact size (from adminV1's
|
|
693
|
+
* merchant settings page).
|
|
694
|
+
*/
|
|
695
|
+
const MERCHANT_IMAGE_SLOTS = Object.freeze({
|
|
696
|
+
receipt: {
|
|
697
|
+
field: "logo.receipt",
|
|
698
|
+
uploadKind: "merchant",
|
|
699
|
+
width: 576,
|
|
700
|
+
height: 325
|
|
701
|
+
},
|
|
702
|
+
square: {
|
|
703
|
+
field: "logo.square",
|
|
704
|
+
uploadKind: "merchant",
|
|
705
|
+
width: 1e3,
|
|
706
|
+
height: 1e3
|
|
707
|
+
},
|
|
708
|
+
featured: {
|
|
709
|
+
field: "feature[0]",
|
|
710
|
+
uploadKind: "merchantFeature",
|
|
711
|
+
width: 1200,
|
|
712
|
+
height: 600
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
/**
|
|
716
|
+
* The file name in an image value: `"http://…/logo/abc.png"` → `"abc.png"`,
|
|
717
|
+
* `"abc.png"` → `"abc.png"`. Query strings and fragments are ignored.
|
|
718
|
+
* Returns `null` for empty or non-string values.
|
|
719
|
+
*/
|
|
720
|
+
function imageFileName(value) {
|
|
721
|
+
if (typeof value !== "string") return null;
|
|
722
|
+
const last = value.trim().replace(/[?#].*$/, "").split("/").pop() ?? "";
|
|
723
|
+
return last ? decodeURIComponent(last) : null;
|
|
724
|
+
}
|
|
725
|
+
function toStored(value) {
|
|
726
|
+
if (typeof value === "string") return imageFileName(value) ?? void 0;
|
|
727
|
+
if (Array.isArray(value)) {
|
|
728
|
+
const items = value.map(toStored).filter((v) => v !== void 0 && v !== null && v !== "");
|
|
729
|
+
return items.length > 0 ? items : void 0;
|
|
730
|
+
}
|
|
731
|
+
if (value && typeof value === "object") {
|
|
732
|
+
const out = {};
|
|
733
|
+
for (const [k, v] of Object.entries(value)) {
|
|
734
|
+
const stored = toStored(v);
|
|
735
|
+
if (stored !== void 0 && stored !== null && stored !== "") out[k] = stored;
|
|
736
|
+
}
|
|
737
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* The `imageIds` to send on a save: every URL is turned back into its file
|
|
742
|
+
* name (what adminV1 does with images you didn't change) and empty values
|
|
743
|
+
* are dropped. No images gives `{}`.
|
|
744
|
+
*/
|
|
745
|
+
function toStoredImageIds(imageIds) {
|
|
746
|
+
return toStored(imageIds) ?? {};
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Uploads an image for a merchant slot: fits it to the slot's size, uploads
|
|
750
|
+
* it with the slot's kind, and returns `{ id, url, path }`. Put `id` into
|
|
751
|
+
* `imageIds` with {@link setMerchantImage} and save with `updateMerchant`.
|
|
752
|
+
*/
|
|
753
|
+
async function uploadMerchantImage(file, slot, options = {}) {
|
|
754
|
+
const def = MERCHANT_IMAGE_SLOTS[slot];
|
|
755
|
+
if (!def) throw new Error(`uploadMerchantImage: unknown slot "${slot}"`);
|
|
756
|
+
const { fit = true, fitOptions,...requestOptions } = options;
|
|
757
|
+
return uploadImage(fit ? await fitImageToSize(file, def.width, def.height, fitOptions) : file, { kind: def.uploadKind }, requestOptions);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
//#endregion
|
|
761
|
+
//#region src/core/merchant.ts
|
|
762
|
+
const MERCHANT_PATH = `/api/${SHOPWAVE_RESOURCES.merchant.route}`;
|
|
763
|
+
/** Where each flat link field lives in `merchant.links`. */
|
|
764
|
+
const MERCHANT_LINK_PATHS = Object.freeze({
|
|
765
|
+
website: ["website", "home"],
|
|
766
|
+
twitter: ["social", "twitter"],
|
|
767
|
+
facebook: ["social", "facebook"],
|
|
768
|
+
instagram: ["social", "instagram"]
|
|
769
|
+
});
|
|
770
|
+
/**
|
|
771
|
+
* Object fields of the merchant. The API keeps one you leave out, and
|
|
772
|
+
* replaces it as a whole when you send it.
|
|
773
|
+
*/
|
|
774
|
+
const MERCHANT_OBJECT_FIELDS = [
|
|
775
|
+
"colours",
|
|
776
|
+
"links",
|
|
777
|
+
"imageIds"
|
|
778
|
+
];
|
|
779
|
+
/** Fields the API sets itself; `updateMerchant` never sends them back. */
|
|
780
|
+
const MERCHANT_READ_ONLY_FIELDS = ["createdDate", "modifiedDate"];
|
|
781
|
+
/**
|
|
782
|
+
* Longest values the API stores. Longer values are cut off silently
|
|
783
|
+
* (live API, Sep 2026), so `updateMerchant` rejects them instead.
|
|
784
|
+
* `vatNumber` fits a UK VAT number without the `GB` prefix.
|
|
785
|
+
*/
|
|
786
|
+
const MERCHANT_FIELD_MAX_LENGTH = Object.freeze({
|
|
787
|
+
companyNumber: 11,
|
|
788
|
+
vatNumber: 9
|
|
789
|
+
});
|
|
790
|
+
/** Returns `"<field> is longer than <n> characters"` messages for over-long fields. */
|
|
791
|
+
function validateMerchant(merchant) {
|
|
792
|
+
const problems = [];
|
|
793
|
+
for (const [field, max] of Object.entries(MERCHANT_FIELD_MAX_LENGTH)) {
|
|
794
|
+
const value = merchant[field];
|
|
795
|
+
if (typeof value === "string" && value.length > max) problems.push(`${field} is longer than ${max} characters`);
|
|
796
|
+
}
|
|
797
|
+
return problems;
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Reads the logged-in user's merchant (a user has exactly one), or `null`
|
|
801
|
+
* when the API returns none.
|
|
802
|
+
*/
|
|
803
|
+
async function fetchMerchant(options = {}) {
|
|
804
|
+
const json = await apiGet(MERCHANT_PATH, {}, options);
|
|
805
|
+
assertNoApiErrors(json, 200);
|
|
806
|
+
return json?.merchant ?? null;
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Updates the merchant and returns it as stored.
|
|
810
|
+
*
|
|
811
|
+
* Shopwave's `POST /merchant` replaces the whole record: a scalar field you
|
|
812
|
+
* leave out (`companyNumber`, `vatNumber`, `note`, …) is set to null, while
|
|
813
|
+
* an object field you leave out (`colours`, `links`, `imageIds`) is kept and
|
|
814
|
+
* one you send replaces the stored one; sending `{}` clears it (live API,
|
|
815
|
+
* Sep 2026). So by default the
|
|
816
|
+
* current merchant is read first and the patch is merged over it.
|
|
817
|
+
*
|
|
818
|
+
* `imageIds` is always sent as file names ({@link toStoredImageIds}), so you
|
|
819
|
+
* can pass back what you read. See `MerchantImageIds` for the shape.
|
|
820
|
+
*
|
|
821
|
+
* The API answers 205 with an empty body, so the merchant is read again after
|
|
822
|
+
* the save and that is what's returned (it shows exactly what was kept).
|
|
823
|
+
* Throws before sending when a value is longer than the API stores
|
|
824
|
+
* ({@link MERCHANT_FIELD_MAX_LENGTH}).
|
|
825
|
+
*/
|
|
826
|
+
async function updateMerchant(patch, options = {}) {
|
|
827
|
+
const { merge = true,...requestOptions } = options;
|
|
828
|
+
let merchant = { ...patch };
|
|
829
|
+
if (merge) {
|
|
830
|
+
const current = await fetchMerchant(requestOptions);
|
|
831
|
+
if (!current) throw new ShopwaveApiError(404, "Shopwave API error (404): no merchant to update");
|
|
832
|
+
if (patch.id != null && String(patch.id) !== String(current.id)) throw new Error(`Merchant id ${patch.id} is not the logged-in merchant (${current.id})`);
|
|
833
|
+
merchant = {
|
|
834
|
+
...current,
|
|
835
|
+
...patch,
|
|
836
|
+
id: current.id
|
|
837
|
+
};
|
|
838
|
+
for (const field of MERCHANT_OBJECT_FIELDS) if (!(field in patch)) delete merchant[field];
|
|
839
|
+
}
|
|
840
|
+
if (merchant.imageIds && typeof merchant.imageIds === "object") merchant.imageIds = toStoredImageIds(merchant.imageIds);
|
|
841
|
+
if (merchant.id == null || merchant.id === "") throw new Error("merchant id is missing");
|
|
842
|
+
for (const field of MERCHANT_READ_ONLY_FIELDS) delete merchant[field];
|
|
843
|
+
const problems = validateMerchant(merchant);
|
|
844
|
+
if (problems.length > 0) throw new Error(`Invalid merchant: ${problems.join("; ")}`);
|
|
845
|
+
const { status, body: raw } = await apiRequest(MERCHANT_PATH, {
|
|
846
|
+
method: "PUT",
|
|
847
|
+
headers: { "Content-Type": "application/json" },
|
|
848
|
+
body: JSON.stringify({ merchant })
|
|
849
|
+
}, requestOptions);
|
|
850
|
+
const body = raw;
|
|
851
|
+
assertNoApiErrors(body, status);
|
|
852
|
+
const echoed = body?.merchant && typeof body.merchant === "object" ? body.merchant : null;
|
|
853
|
+
if (echoed) return {
|
|
854
|
+
...merchant,
|
|
855
|
+
...echoed,
|
|
856
|
+
id: Number(echoed.id ?? merchant.id)
|
|
857
|
+
};
|
|
858
|
+
return await fetchMerchant(requestOptions) ?? {
|
|
859
|
+
...merchant,
|
|
860
|
+
id: Number(merchant.id)
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
//#endregion
|
|
865
|
+
//#region src/core/user.ts
|
|
866
|
+
/** Reads the logged-in user, or `null` when the API returns none. */
|
|
867
|
+
async function fetchUser(options = {}) {
|
|
868
|
+
const json = await apiGet(`/api/${SHOPWAVE_RESOURCES.user.route}`, {}, options);
|
|
869
|
+
assertNoApiErrors(json, 200);
|
|
870
|
+
return json?.user ?? null;
|
|
871
|
+
}
|
|
872
|
+
|
|
501
873
|
//#endregion
|
|
502
874
|
//#region src/hooks/shared.ts
|
|
503
875
|
function toMessage(e) {
|
|
@@ -855,6 +1227,85 @@ function useSession(options) {
|
|
|
855
1227
|
}), [options?.baseUrl]);
|
|
856
1228
|
}
|
|
857
1229
|
|
|
1230
|
+
//#endregion
|
|
1231
|
+
//#region src/hooks/useMerchant.ts
|
|
1232
|
+
/**
|
|
1233
|
+
* The logged-in user's merchant, fetched on mount. `data` is `null` while
|
|
1234
|
+
* loading or when the API has no merchant.
|
|
1235
|
+
*
|
|
1236
|
+
* @example
|
|
1237
|
+
* const { data: merchant, loading, refetch } = useMerchant();
|
|
1238
|
+
*/
|
|
1239
|
+
function useMerchant(options) {
|
|
1240
|
+
return useQuery((signal) => fetchMerchant({
|
|
1241
|
+
...options,
|
|
1242
|
+
signal
|
|
1243
|
+
}), [options?.baseUrl, options?.token]);
|
|
1244
|
+
}
|
|
1245
|
+
/**
|
|
1246
|
+
* Manually-triggered merchant update (see `updateMerchant`: the patch is merged
|
|
1247
|
+
* over the current merchant by default). Resolves to the saved merchant, or
|
|
1248
|
+
* `null` on error (then `error` / `errorStatus` are set).
|
|
1249
|
+
*
|
|
1250
|
+
* @example
|
|
1251
|
+
* const { mutate: saveMerchant, loading: saving } = useUpdateMerchant();
|
|
1252
|
+
* const saved = await saveMerchant({ vatNumber: "GB123" });
|
|
1253
|
+
*/
|
|
1254
|
+
function useUpdateMerchant(options) {
|
|
1255
|
+
return useMutation((patch) => updateMerchant(patch, options), [
|
|
1256
|
+
options?.baseUrl,
|
|
1257
|
+
options?.token,
|
|
1258
|
+
options?.merge
|
|
1259
|
+
]);
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
//#endregion
|
|
1263
|
+
//#region src/hooks/useUser.ts
|
|
1264
|
+
/**
|
|
1265
|
+
* The logged-in user, fetched on mount. `errorStatus` is 401 when logged out.
|
|
1266
|
+
*
|
|
1267
|
+
* @example
|
|
1268
|
+
* const { data: user } = useUser();
|
|
1269
|
+
*/
|
|
1270
|
+
function useUser(options) {
|
|
1271
|
+
const enabled = options?.enabled ?? true;
|
|
1272
|
+
return useQuery((signal) => fetchUser({
|
|
1273
|
+
...options,
|
|
1274
|
+
signal
|
|
1275
|
+
}), [options?.baseUrl, options?.token], enabled);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
//#endregion
|
|
1279
|
+
//#region src/hooks/useUploadImage.ts
|
|
1280
|
+
/**
|
|
1281
|
+
* Manually-triggered image upload. Resolves to `{ id, url }`, or `null` on
|
|
1282
|
+
* error (then `error` / `errorStatus` are set).
|
|
1283
|
+
*
|
|
1284
|
+
* @example
|
|
1285
|
+
* const { mutate: upload, loading: uploading } = useUploadImage();
|
|
1286
|
+
* const image = await upload(file, { kind: "merchant" });
|
|
1287
|
+
*/
|
|
1288
|
+
function useUploadImage(options) {
|
|
1289
|
+
return useMutation((file, params) => uploadImage(file, params, options), [options?.baseUrl, options?.token]);
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* Manually-triggered upload for a merchant image slot (`receipt`, `square`,
|
|
1293
|
+
* `featured`): fits the image to the slot's exact size, uploads it with the
|
|
1294
|
+
* slot's kind, and resolves to `{ id, url }`. Put `id` into `imageIds` with
|
|
1295
|
+
* `setMerchantImage`.
|
|
1296
|
+
*
|
|
1297
|
+
* @example
|
|
1298
|
+
* const { mutate: upload } = useUploadMerchantImage();
|
|
1299
|
+
* const image = await upload(file, "square");
|
|
1300
|
+
*/
|
|
1301
|
+
function useUploadMerchantImage(options) {
|
|
1302
|
+
return useMutation((file, slot) => uploadMerchantImage(file, slot, options), [
|
|
1303
|
+
options?.baseUrl,
|
|
1304
|
+
options?.token,
|
|
1305
|
+
options?.fit
|
|
1306
|
+
]);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
858
1309
|
//#endregion
|
|
859
1310
|
//#region src/hooks/useBasketReport.ts
|
|
860
1311
|
/**
|
|
@@ -959,5 +1410,5 @@ function useBasketReport(options) {
|
|
|
959
1410
|
}
|
|
960
1411
|
|
|
961
1412
|
//#endregion
|
|
962
|
-
export { useBasketReport, useCategory, useConsumer, useDelete, useEmployee, useLogout, useProduct, usePromotion, useReport, useSession, useStore, useSubmit };
|
|
1413
|
+
export { useBasketReport, useCategory, useConsumer, useDelete, useEmployee, useLogout, useMerchant, useProduct, usePromotion, useReport, useSession, useStore, useSubmit, useUpdateMerchant, useUploadImage, useUploadMerchantImage, useUser };
|
|
963
1414
|
//# sourceMappingURL=index.js.map
|