@zappie/sdk 0.9.0 → 0.10.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/CHANGELOG.md +24 -0
- package/README.md +2 -0
- package/dist/branches/index.d.ts +76 -0
- package/dist/branches/index.js +44 -0
- package/dist/branches/index.js.map +6 -0
- package/dist/cart/validate.js +9 -3
- package/dist/cart/validate.js.map +1 -1
- package/dist/checkout/embed.js +12 -14
- package/dist/checkout/embed.js.map +1 -1
- package/dist/checkout/react.d.ts +5 -0
- package/dist/checkout/react.js +26 -19
- package/dist/checkout/react.js.map +1 -1
- package/dist/customer/client.js +43 -50
- package/dist/customer/client.js.map +1 -1
- package/dist/customer/index.d.ts +4 -0
- package/dist/customer/navigation.js +20 -20
- package/dist/customer/navigation.js.map +1 -1
- package/dist/customer/react.d.ts +3 -1
- package/dist/customer/react.js +2 -0
- package/dist/customer/react.js.map +1 -1
- package/dist/customer/request.js +19 -18
- package/dist/customer/request.js.map +1 -1
- package/dist/debug.js +1 -1
- package/dist/debug.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/customer-http.js +17 -19
- package/dist/internal/customer-http.js.map +1 -1
- package/dist/navigation/index.d.ts +55 -0
- package/dist/navigation/index.js +56 -0
- package/dist/navigation/index.js.map +6 -0
- package/dist/navigation/react.d.ts +20 -0
- package/dist/navigation/react.js +18 -0
- package/dist/navigation/react.js.map +6 -0
- package/dist/options/index.d.ts +75 -0
- package/dist/options/index.js +104 -0
- package/dist/options/index.js.map +6 -0
- package/dist/server/chosen-branch.js +28 -0
- package/dist/server/chosen-branch.js.map +6 -0
- package/dist/server/index.d.ts +37 -3
- package/dist/server/index.js +4 -0
- package/dist/server/index.js.map +1 -1
- package/dist/transport/mappers.js +1 -1
- package/dist/transport/mappers.js.map +1 -1
- package/package.json +18 -2
package/dist/customer/index.d.ts
CHANGED
|
@@ -140,11 +140,15 @@ type CustomerListener = (state: AuthState, customer: SessionCustomer | null) =>
|
|
|
140
140
|
declare function createCustomer(options?: CustomerClientOptions): {
|
|
141
141
|
readonly state: AuthState;
|
|
142
142
|
readonly customer: SessionCustomer | null;
|
|
143
|
+
/** Whether a sign-in or sign-out is leaving the page. It ends when the back-forward cache shows the page again. */
|
|
144
|
+
readonly pending: boolean;
|
|
143
145
|
session: () => Promise<SessionCustomer | null>;
|
|
144
146
|
/** Stops the visibility and back/forward-cache re-checks and drops listeners. */
|
|
145
147
|
dispose(): void;
|
|
146
148
|
onChange(listener: CustomerListener): () => void;
|
|
149
|
+
/** Go to sign-in and come back to `returnTo`, this page by default. A call while pending does nothing. */
|
|
147
150
|
signIn(returnTo?: string): void;
|
|
151
|
+
/** Sign out through Zappie hosting. A call while pending does nothing. */
|
|
148
152
|
signOut(): void;
|
|
149
153
|
profile: () => Promise<CustomerProfile>;
|
|
150
154
|
addresses(): Promise<SavedAddress[]>;
|
|
@@ -9,32 +9,32 @@ function currentPath() {
|
|
|
9
9
|
}
|
|
10
10
|
return location.pathname + location.search;
|
|
11
11
|
}
|
|
12
|
-
function
|
|
12
|
+
function leaveTo(path, post) {
|
|
13
13
|
if (typeof document === "undefined") {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
const element = document.createElement(
|
|
17
|
+
post ? "form" : "a"
|
|
18
|
+
);
|
|
19
|
+
element.target = "_top";
|
|
20
|
+
if (post) {
|
|
21
|
+
element.method = "POST";
|
|
22
|
+
element.action = path;
|
|
23
|
+
element.hidden = true;
|
|
24
|
+
} else {
|
|
25
|
+
element.href = path;
|
|
26
|
+
}
|
|
27
|
+
document.body.appendChild(element);
|
|
28
|
+
if (post) {
|
|
29
|
+
element.submit();
|
|
30
|
+
} else {
|
|
31
|
+
element.click();
|
|
24
32
|
}
|
|
25
|
-
|
|
26
|
-
form.method = "POST";
|
|
27
|
-
form.action = path;
|
|
28
|
-
form.target = "_top";
|
|
29
|
-
form.hidden = true;
|
|
30
|
-
document.body.appendChild(form);
|
|
31
|
-
form.submit();
|
|
32
|
-
form.remove();
|
|
33
|
+
element.remove();
|
|
33
34
|
}
|
|
34
35
|
export {
|
|
35
36
|
currentPath,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
signOut
|
|
37
|
+
leaveTo,
|
|
38
|
+
safeReturnTo
|
|
39
39
|
};
|
|
40
40
|
//# sourceMappingURL=navigation.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/customer/navigation.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,sBAAsB;AAExB,SAAS,aAAa,OAAmC;AAC9D,SAAO,eAAe,KAAK,KAAK;AAClC;AAEO,SAAS,cAAsB;AACpC,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO;AAAA,EACT;AACA,SAAO,SAAS,WAAW,SAAS;AACtC;
|
|
4
|
+
"mappings": ";AAAA,SAAS,sBAAsB;AAExB,SAAS,aAAa,OAAmC;AAC9D,SAAO,eAAe,KAAK,KAAK;AAClC;AAEO,SAAS,cAAsB;AACpC,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO;AAAA,EACT;AACA,SAAO,SAAS,WAAW,SAAS;AACtC;AAGO,SAAS,QAAQ,MAAc,MAAsB;AAC1D,MAAI,OAAO,aAAa,aAAa;AACnC;AAAA,EACF;AACA,QAAM,UAAU,SAAS;AAAA,IACvB,OAAO,SAAS;AAAA,EAClB;AACA,UAAQ,SAAS;AACjB,MAAI,MAAM;AACR,YAAQ,SAAS;AACjB,YAAQ,SAAS;AACjB,YAAQ,SAAS;AAAA,EACnB,OAAO;AACL,YAAQ,OAAO;AAAA,EACjB;AACA,WAAS,KAAK,YAAY,OAAO;AACjC,MAAI,MAAM;AACR,YAAQ,OAAO;AAAA,EACjB,OAAO;AACL,YAAQ,MAAM;AAAA,EAChB;AACA,UAAQ,OAAO;AACjB;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/customer/react.d.ts
CHANGED
|
@@ -13,11 +13,13 @@ declare function CustomerProvider({ client, children, }: {
|
|
|
13
13
|
}): react.JSX.Element;
|
|
14
14
|
/**
|
|
15
15
|
* Subscribe to the nearest CustomerProvider and return auth state, customer and session actions.
|
|
16
|
-
*
|
|
16
|
+
* `pending` is true from a `signIn` or `signOut` until the page unloads, so the control that started it can show it
|
|
17
|
+
* is busy. Throws if you have not mounted a CustomerProvider.
|
|
17
18
|
*/
|
|
18
19
|
declare function useAuth(): {
|
|
19
20
|
state: _zappie_sdk_customer.AuthState;
|
|
20
21
|
customer: SessionCustomer | null;
|
|
22
|
+
pending: boolean;
|
|
21
23
|
signIn: (returnTo?: string) => void;
|
|
22
24
|
signOut: () => void;
|
|
23
25
|
refresh: () => Promise<SessionCustomer | null>;
|
package/dist/customer/react.js
CHANGED
|
@@ -33,6 +33,7 @@ function key(client) {
|
|
|
33
33
|
const customer = client.customer;
|
|
34
34
|
return [
|
|
35
35
|
client.state,
|
|
36
|
+
client.pending,
|
|
36
37
|
customer?.id,
|
|
37
38
|
customer?.given_name,
|
|
38
39
|
customer?.phone_masked
|
|
@@ -50,6 +51,7 @@ function useAuth() {
|
|
|
50
51
|
() => ({
|
|
51
52
|
state: client.state,
|
|
52
53
|
customer: client.customer,
|
|
54
|
+
pending: client.pending,
|
|
53
55
|
signIn: client.signIn,
|
|
54
56
|
signOut: client.signOut,
|
|
55
57
|
refresh: client.session
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/customer/react.tsx"],
|
|
4
|
-
"mappings": ";;;AAGA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAqBH;AAnBJ,IAAM,kBAAkB,cAAqC,IAAI;AAM1D,SA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UAAU;AAAA,IACV,UAAU;AAAA,EACZ,EAAE,KAAK,GAAG;AACZ;AAOO,SAAS,UAAU;AACxB,QAAM,SAAS,UAAU;AACzB,QAAM,YAAY;AAAA,IAChB,CAAC,WAAuB,OAAO,SAAS,MAAM,OAAO,CAAC;AAAA,IACtD,CAAC,MAAM;AAAA,EACT;AACA,QAAM,WAAW,YAAY,MAAM,IAAI,MAAM,GAAG,CAAC,MAAM,CAAC;AAExD,QAAM,QAAQ,qBAAqB,WAAW,UAAU,QAAQ;AAChE,SAAO;AAAA,IACL,OAAO;AAAA,MACL,OAAO,OAAO;AAAA,MACd,UAAU,OAAO;AAAA,MACjB,SAAS,OAAO;AAAA,MAChB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO;AAAA,MAChB,SAAS,OAAO;AAAA,IAClB;AAAA,IACA,CAAC,QAAQ,KAAK;AAAA,EAChB;AACF;AAMO,SAAS,cAAsC;AACpD,SAAO,QAAQ,EAAE;AACnB;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/customer/request.js
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
// src/customer/request.ts
|
|
2
2
|
import { apiError, requestWithRetry } from "../internal/customer-http.js";
|
|
3
3
|
import { CustomerError } from "./error.js";
|
|
4
|
+
async function readCustomerResponse(response, service) {
|
|
5
|
+
if (!response.ok) {
|
|
6
|
+
const value = await apiError(
|
|
7
|
+
response,
|
|
8
|
+
`The ${service} service answered ${response.status}`
|
|
9
|
+
);
|
|
10
|
+
throw new CustomerError(
|
|
11
|
+
response.status,
|
|
12
|
+
value.apiCode,
|
|
13
|
+
value.message,
|
|
14
|
+
value.param,
|
|
15
|
+
value.issues
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
return response.status === 204 ? void 0 : await response.json();
|
|
19
|
+
}
|
|
4
20
|
function createCustomerRequest(base, fetcher, unauthorized) {
|
|
5
21
|
return async (path, init = {}) => {
|
|
6
22
|
const response = await requestWithRetry(
|
|
@@ -12,26 +28,11 @@ function createCustomerRequest(base, fetcher, unauthorized) {
|
|
|
12
28
|
if (response.status === 401) {
|
|
13
29
|
unauthorized();
|
|
14
30
|
}
|
|
15
|
-
|
|
16
|
-
const value = await apiError(
|
|
17
|
-
response,
|
|
18
|
-
`The customer service answered ${response.status}`
|
|
19
|
-
);
|
|
20
|
-
throw new CustomerError(
|
|
21
|
-
response.status,
|
|
22
|
-
value.apiCode,
|
|
23
|
-
value.message,
|
|
24
|
-
value.param,
|
|
25
|
-
value.issues
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
if (response.status === 204) {
|
|
29
|
-
return void 0;
|
|
30
|
-
}
|
|
31
|
-
return await response.json();
|
|
31
|
+
return readCustomerResponse(response, "customer");
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
34
|
export {
|
|
35
|
-
createCustomerRequest
|
|
35
|
+
createCustomerRequest,
|
|
36
|
+
readCustomerResponse
|
|
36
37
|
};
|
|
37
38
|
//# sourceMappingURL=request.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/customer/request.ts"],
|
|
4
|
-
"mappings": ";AAAA,SAAS,UAAU,wBAAwB;AAC3C,SAAS,qBAAqB;
|
|
4
|
+
"mappings": ";AAAA,SAAS,UAAU,wBAAwB;AAC3C,SAAS,qBAAqB;AAQ9B,eAAsB,qBACpB,UACA,SACY;AACZ,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,QAAQ,MAAM;AAAA,MAClB;AAAA,MACA,OAAO,OAAO,qBAAqB,SAAS,MAAM;AAAA,IACpD;AACA,UAAM,IAAI;AAAA,MACR,SAAS;AAAA,MACT,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,EACF;AACA,SAAQ,SAAS,WAAW,MAAM,SAAY,MAAM,SAAS,KAAK;AACpE;AAEO,SAAS,sBACd,MACA,SACA,cACiB;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AW,KAAK,eAAe,MAAM,CAAC;AAChE,SAAO,KAAK,UAAU,MAAM;AAC1B,UAAM,QAAQ,OAAO,KAAK;AAC1B,UAAM,OAAO,WAAW,QAAQ,MAAM,QAAQ;AAC9C,QAAI,QAAQ,SAAS,OAAO;AAC1B,cAAQ;AACR,WAAK,SAAS,IAAI;AAAA,IACpB;AAAA,EACF,CAAC;AACH;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { ZappieError, isZappieError } from "./errors.js";
|
|
|
10
10
|
export * from "./models.js";
|
|
11
11
|
import { format, money } from "./money.js";
|
|
12
12
|
import { RestTransport } from "./transport/rest.js";
|
|
13
|
-
var VERSION = true ? "0.
|
|
13
|
+
var VERSION = true ? "0.10.1" : "0.0.0-development";
|
|
14
14
|
export {
|
|
15
15
|
API_VERSION,
|
|
16
16
|
RestTransport,
|
package/dist/index.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"mappings": ";AAUA,SAAS,uBAAuB;AAChC,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAClC,SAAS,yBAAyB;AAElC,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAU7B,SAAS,mBAAmB;AAM5B,SAAS,aAAa,qBAAqB;AAE3C,cAAc;AACd,SAAS,QAAQ,aAAa;AAE9B,SAAS,qBAAqB;AA/BvB,IAAM,UACX,OACI,
|
|
4
|
+
"mappings": ";AAUA,SAAS,uBAAuB;AAChC,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAClC,SAAS,yBAAyB;AAElC,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAU7B,SAAS,mBAAmB;AAM5B,SAAS,aAAa,qBAAqB;AAE3C,cAAc;AACd,SAAS,QAAQ,aAAa;AAE9B,SAAS,qBAAqB;AA/BvB,IAAM,UACX,OACI,WACA;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -45,28 +45,26 @@ function wait(ms, signal) {
|
|
|
45
45
|
signal?.addEventListener("abort", aborted, { once: true });
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
async function requestWithRetry(fetcher, input, init, message) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
async function requestWithRetry(fetcher, input, init, message, retry = true) {
|
|
49
|
+
try {
|
|
50
|
+
return await fetcher(input, {
|
|
51
|
+
...init,
|
|
52
|
+
credentials: "same-origin",
|
|
53
|
+
headers: { Accept: "application/json", ...init.headers }
|
|
54
|
+
});
|
|
55
|
+
} catch (cause) {
|
|
56
|
+
if (init.signal?.aborted) {
|
|
57
|
+
throw init.signal.reason ?? cause;
|
|
58
|
+
}
|
|
59
|
+
if (!retry) {
|
|
60
|
+
throw new ZappieError("network_error", message, {
|
|
61
|
+
cause,
|
|
62
|
+
retryable: true
|
|
55
63
|
});
|
|
56
|
-
} catch (cause) {
|
|
57
|
-
if (init.signal?.aborted) {
|
|
58
|
-
throw init.signal.reason ?? cause;
|
|
59
|
-
}
|
|
60
|
-
if (attempt === 1) {
|
|
61
|
-
throw new ZappieError("network_error", message, {
|
|
62
|
-
cause,
|
|
63
|
-
retryable: true
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
await wait(400, init.signal ?? void 0);
|
|
67
64
|
}
|
|
65
|
+
await wait(400, init.signal ?? void 0);
|
|
66
|
+
return requestWithRetry(fetcher, input, init, message, false);
|
|
68
67
|
}
|
|
69
|
-
throw new ZappieError("network_error", message, { retryable: true });
|
|
70
68
|
}
|
|
71
69
|
export {
|
|
72
70
|
ZappieError,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/internal/customer-http.ts"],
|
|
4
|
-
"mappings": ";AAEA,SAAS,mBAA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H;AAGA,eAAsB,iBACpB,SACA,OACA,MACA,SACA,QAAQ,MACW;AACnB,MAAI;AACF,WAAO,MAAM,QAAQ,OAAO;AAAA,MAC1B,GAAG;AAAA,MACH,aAAa;AAAA,MACb,SAAS,EAAE,QAAQ,oBAAoB,GAAG,KAAK,QAAQ;AAAA,IACzD,CAAC;AAAA,EACH,SAAS,OAAO;AACd,QAAI,KAAK,QAAQ,SAAS;AACxB,YAAM,KAAK,OAAO,UAAU;AAAA,IAC9B;AACA,QAAI,CAAC,OAAO;AACV,YAAM,IAAI,YAAY,iBAAiB,SAAS;AAAA,QAC9C;AAAA,QACA,WAAW;AAAA,MACb,CAAC;AAAA,IACH;AACA,UAAM,KAAK,KAAK,KAAK,UAAU,MAAS;AACxC,WAAO,iBAAiB,SAAS,OAAO,MAAM,SAAS,KAAK;AAAA,EAC9D;AACF;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** The parts of a link's click or a form's submit event that `follow` reads. */
|
|
2
|
+
type FollowEvent = {
|
|
3
|
+
defaultPrevented: boolean;
|
|
4
|
+
preventDefault(): void;
|
|
5
|
+
currentTarget: EventTarget | null;
|
|
6
|
+
button?: number;
|
|
7
|
+
metaKey?: boolean;
|
|
8
|
+
ctrlKey?: boolean;
|
|
9
|
+
shiftKey?: boolean;
|
|
10
|
+
altKey?: boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The pending state of a trigger that leaves the page, such as Pay, sign-in or sign-out, from the
|
|
14
|
+
* click until the page unloads.
|
|
15
|
+
*/
|
|
16
|
+
type PendingNavigation = {
|
|
17
|
+
/** Whether the page is leaving. */
|
|
18
|
+
readonly pending: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Run `leave`, which sends the browser away: a request and then `location.assign`, a sign-in, or
|
|
21
|
+
* nothing for a plain link the browser follows. The navigation stays pending after `leave`
|
|
22
|
+
* settles, until the page unloads. It ends when `leave` throws or rejects, when you call `end`,
|
|
23
|
+
* and when the browser shows the page again from its back-forward cache. A call while pending
|
|
24
|
+
* runs nothing and returns the pending call's promise.
|
|
25
|
+
*
|
|
26
|
+
* @param leave - The work that starts the navigation. Call `end` inside it when it stays on the page.
|
|
27
|
+
* @returns What `leave` returns.
|
|
28
|
+
*/
|
|
29
|
+
start<T = void>(leave?: () => T | PromiseLike<T>): Promise<T>;
|
|
30
|
+
/** End the pending navigation, for a trigger that stayed on the page. */
|
|
31
|
+
end(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Pass as the `onClick` of a link, or the `onSubmit` of a form, that the browser follows itself,
|
|
34
|
+
* such as `/sign-in` or the `/_zappie/logout` form. It starts pending, and while pending it stops
|
|
35
|
+
* a second click. A click that opens a new tab or window, or that the page already handled,
|
|
36
|
+
* leaves it alone.
|
|
37
|
+
*/
|
|
38
|
+
follow(event: FollowEvent): void;
|
|
39
|
+
/**
|
|
40
|
+
* Call `listener` whenever `pending` changes.
|
|
41
|
+
*
|
|
42
|
+
* @returns A function that stops the calls.
|
|
43
|
+
*/
|
|
44
|
+
subscribe(listener: () => void): () => void;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Create the pending state for a trigger that leaves the page. Each trigger, or each group of
|
|
48
|
+
* triggers that must not run together, gets its own.
|
|
49
|
+
*
|
|
50
|
+
* @returns A pending navigation that is not pending.
|
|
51
|
+
*/
|
|
52
|
+
declare function createPendingNavigation(): PendingNavigation;
|
|
53
|
+
|
|
54
|
+
export { createPendingNavigation };
|
|
55
|
+
export type { FollowEvent, PendingNavigation };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/navigation/index.ts
|
|
2
|
+
var STARTING = Promise.resolve();
|
|
3
|
+
function createPendingNavigation() {
|
|
4
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
5
|
+
let run;
|
|
6
|
+
const set = (next) => {
|
|
7
|
+
run = next;
|
|
8
|
+
(next ? addEventListener : removeEventListener)("pageshow", end);
|
|
9
|
+
listeners.forEach((listener) => listener());
|
|
10
|
+
};
|
|
11
|
+
const end = (event) => {
|
|
12
|
+
if (run && event?.persisted !== false) {
|
|
13
|
+
set();
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const start = (leave) => {
|
|
17
|
+
if (run) {
|
|
18
|
+
return run;
|
|
19
|
+
}
|
|
20
|
+
set(STARTING);
|
|
21
|
+
const current = new Promise((resolve) => resolve(leave?.()));
|
|
22
|
+
if (run) {
|
|
23
|
+
run = current;
|
|
24
|
+
}
|
|
25
|
+
current.catch(() => {
|
|
26
|
+
if (run === current) {
|
|
27
|
+
end();
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return current;
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
get pending() {
|
|
34
|
+
return !!run;
|
|
35
|
+
},
|
|
36
|
+
start,
|
|
37
|
+
end: () => end(),
|
|
38
|
+
follow(event) {
|
|
39
|
+
if (run) {
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
} else if (!(event.defaultPrevented || event.button || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.currentTarget?.target === "_blank")) {
|
|
42
|
+
void start();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
subscribe(listener) {
|
|
46
|
+
listeners.add(listener);
|
|
47
|
+
return () => {
|
|
48
|
+
listeners.delete(listener);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
createPendingNavigation
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/navigation/index.ts"],
|
|
4
|
+
"mappings": ";AAgDA,IAAM,WAAW,QAAQ,QAAQ;AAQ1B,SAAS,0BAA6C;AAC3D,QAAM,YAAY,oBAAI,IAAgB;AACtC,MAAI;AACJ,QAAM,MAAM,CAAC,SAA4B;AACvC,UAAM;AAEN,KAAC,OAAO,mBAAmB,qBAAqB,YAAY,GAAG;AAC/D,cAAU,QAAQ,CAAC,aAAa,SAAS,CAAC;AAAA,EAC5C;AAEA,QAAM,MAAM,CAAC,UAAoC;AAC/C,QAAI,OAAO,OAAO,cAAc,OAAO;AACrC,UAAI;AAAA,IACN;AAAA,EACF;AACA,QAAM,QAAQ,CAAI,UAAqC;AACrD,QAAI,KAAK;AACP,aAAO;AAAA,IACT;AACA,QAAI,QAAQ;AACZ,UAAM,UAAU,IAAI,QAAuB,CAAC,YAAY,QAAQ,QAAQ,CAAC,CAAC;AAE1E,QAAI,KAAK;AACP,YAAM;AAAA,IACR;AACA,YAAQ,MAAM,MAAM;AAClB,UAAI,QAAQ,SAAS;AACnB,YAAI;AAAA,MACN;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AACA,SAAO;AAAA,IACL,IAAI,UAAU;AACZ,aAAO,CAAC,CAAC;AAAA,IACX;AAAA,IACA;AAAA,IACA,KAAK,MAAM,IAAI;AAAA,IACf,OAAO,OAAO;AACZ,UAAI,KAAK;AACP,cAAM,eAAe;AAAA,MACvB,WAAW,EACT,MAAM,oBACN,MAAM,UACN,MAAM,WACN,MAAM,WACN,MAAM,YACN,MAAM,UACL,MAAM,eAA4C,WAAW,WAC7D;AACD,aAAK,MAAM;AAAA,MACb;AAAA,IACF;AAAA,IACA,UAAU,UAAU;AAClB,gBAAU,IAAI,QAAQ;AACtB,aAAO,MAAM;AACX,kBAAU,OAAO,QAAQ;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AACF;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as _zappie_sdk_navigation from '@zappie/sdk/navigation';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Keep a trigger that leaves the page, such as Pay, sign-in or sign-out, busy from the click until
|
|
5
|
+
* the page unloads. Pass the work to `start`: it stays pending after a request and
|
|
6
|
+
* `location.assign`, and ends when the work fails, when you call `end`, or when the back-forward
|
|
7
|
+
* cache shows the page again. A second `start` while pending runs nothing. For a plain link or a
|
|
8
|
+
* form the browser follows itself, such as `/sign-in` or the `/_zappie/logout` form, pass `follow`
|
|
9
|
+
* as its `onClick` or `onSubmit`.
|
|
10
|
+
*
|
|
11
|
+
* @returns Whether the page is leaving, with `start`, `end` and `follow`.
|
|
12
|
+
*/
|
|
13
|
+
declare function usePendingNavigation(): {
|
|
14
|
+
pending: boolean;
|
|
15
|
+
start: <T = void>(leave?: () => T | PromiseLike<T>) => Promise<T>;
|
|
16
|
+
end: () => void;
|
|
17
|
+
follow: (event: _zappie_sdk_navigation.FollowEvent) => void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { usePendingNavigation };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/navigation/react.ts
|
|
4
|
+
import { createPendingNavigation } from "@zappie/sdk/navigation";
|
|
5
|
+
import { useState, useSyncExternalStore } from "react";
|
|
6
|
+
function usePendingNavigation() {
|
|
7
|
+
const [nav] = useState(createPendingNavigation);
|
|
8
|
+
const pending = useSyncExternalStore(
|
|
9
|
+
nav.subscribe,
|
|
10
|
+
() => nav.pending,
|
|
11
|
+
() => false
|
|
12
|
+
);
|
|
13
|
+
return { pending, start: nav.start, end: nav.end, follow: nav.follow };
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
usePendingNavigation
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/navigation/react.ts"],
|
|
4
|
+
"mappings": ";;;AACA,SAAS,+BAA+B;AACxC,SAAS,UAAU,4BAA4B;AAYxC,SAAS,uBAAuB;AACrC,QAAM,CAAC,GAAG,IAAI,SAAS,uBAAuB;AAC9C,QAAM,UAAU;AAAA,IACd,IAAI;AAAA,IACJ,MAAM,IAAI;AAAA,IACV,MAAM;AAAA,EACR;AACA,SAAO,EAAE,SAAS,OAAO,IAAI,OAAO,KAAK,IAAI,KAAK,QAAQ,IAAI,OAAO;AACvE;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { CartLineInput, OptionGroup } from '@zappie/sdk';
|
|
2
|
+
|
|
3
|
+
/** The longest note a shopper can add to an item, in characters. */
|
|
4
|
+
declare const ITEM_NOTE_MAX_LENGTH = 280;
|
|
5
|
+
/** The hint shown while a group is at its limit. */
|
|
6
|
+
declare const LIMIT_REACHED_HINT = "Limit reached. Remove one to choose another.";
|
|
7
|
+
/** The shopper's chosen options, as a cart line holds them. */
|
|
8
|
+
type OptionSelection = CartLineInput["choices"];
|
|
9
|
+
/** How a group's choices are picked: one dot from a required set, or tick boxes. */
|
|
10
|
+
type SelectionControl = "radio" | "checkbox";
|
|
11
|
+
/**
|
|
12
|
+
* The most units a group takes, or undefined when it has no limit. Zappie
|
|
13
|
+
* sends 0 for a group with no limit.
|
|
14
|
+
*/
|
|
15
|
+
declare function selectionMax(group: OptionGroup): number | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the group's choices are radios or checkboxes. Only a required group
|
|
18
|
+
* that takes exactly one choice is a radio group. Every other group, an
|
|
19
|
+
* optional single choice included, uses checkboxes, so the shopper can always
|
|
20
|
+
* untick an optional choice.
|
|
21
|
+
*/
|
|
22
|
+
declare function selectionControl(group: OptionGroup): SelectionControl;
|
|
23
|
+
/**
|
|
24
|
+
* How many units the shopper has chosen in a group. Each choice counts its
|
|
25
|
+
* quantity, and at least one, as Zappie counts them.
|
|
26
|
+
*/
|
|
27
|
+
declare function selectionCount(group: OptionGroup, choices: OptionSelection): number;
|
|
28
|
+
/**
|
|
29
|
+
* How many more units the group takes, never below 0, or undefined when the
|
|
30
|
+
* group has no limit.
|
|
31
|
+
*/
|
|
32
|
+
declare function groupRoom(group: OptionGroup, choices: OptionSelection): number | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Whether the shopper can pick this choice now. A chosen choice can always be
|
|
35
|
+
* unticked. In a group of one, picking another replaces the chosen one. In a
|
|
36
|
+
* larger group, a choice can be added only while the group has room for it.
|
|
37
|
+
* An unavailable choice can never be picked.
|
|
38
|
+
*/
|
|
39
|
+
declare function canChoose(group: OptionGroup, choices: OptionSelection, choiceId: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the group is full: it has a limit above one and the chosen units
|
|
42
|
+
* reach it. Unchosen choices in a full group can't be picked until the shopper
|
|
43
|
+
* removes one.
|
|
44
|
+
*/
|
|
45
|
+
declare function atLimit(group: OptionGroup, choices: OptionSelection): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The most of one choice the shopper can have: the choice's own maximum,
|
|
48
|
+
* capped by the room left in the group plus what the choice already holds.
|
|
49
|
+
*/
|
|
50
|
+
declare function choiceQuantityMax(group: OptionGroup, choices: OptionSelection, choiceId: string): number;
|
|
51
|
+
/**
|
|
52
|
+
* The group's rule as a short label for its heading, such as "(Exactly 2)",
|
|
53
|
+
* "(Max 3)" or "(Optional · up to 2)".
|
|
54
|
+
*/
|
|
55
|
+
declare function requirementLabel(group: OptionGroup): string;
|
|
56
|
+
/**
|
|
57
|
+
* What the shopper must do to make the group valid, such as "Choose 1 more
|
|
58
|
+
* item." or "Remove 2 items. Choose up to 3 items in total.", or how many the
|
|
59
|
+
* group takes when it is already valid.
|
|
60
|
+
*/
|
|
61
|
+
declare function boundMessage(group: OptionGroup, count: number): string;
|
|
62
|
+
/**
|
|
63
|
+
* Whether the group's chosen units break its rule. An optional group with
|
|
64
|
+
* nothing chosen is valid, whatever its minimum.
|
|
65
|
+
*/
|
|
66
|
+
declare function outOfBounds(group: OptionGroup, count: number): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* A live count of the group's chosen units: "2 of 2 chosen" for an exact
|
|
69
|
+
* group, "1 of up to 3 chosen" for a group with a limit, "1 chosen (at least
|
|
70
|
+
* 2)" for a group with only a minimum, and "3 chosen" otherwise.
|
|
71
|
+
*/
|
|
72
|
+
declare function countLabel(group: OptionGroup, count: number): string;
|
|
73
|
+
|
|
74
|
+
export { ITEM_NOTE_MAX_LENGTH, LIMIT_REACHED_HINT, atLimit, boundMessage, canChoose, choiceQuantityMax, countLabel, groupRoom, outOfBounds, requirementLabel, selectionControl, selectionCount, selectionMax };
|
|
75
|
+
export type { OptionSelection, SelectionControl };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/options/index.ts
|
|
2
|
+
var ITEM_NOTE_MAX_LENGTH = 280;
|
|
3
|
+
var LIMIT_REACHED_HINT = "Limit reached. Remove one to choose another.";
|
|
4
|
+
function selectionMax(group) {
|
|
5
|
+
return group.selection.max || void 0;
|
|
6
|
+
}
|
|
7
|
+
function selectionControl(group) {
|
|
8
|
+
return group.isAttribute || group.required && swaps(group) ? "radio" : "checkbox";
|
|
9
|
+
}
|
|
10
|
+
var swaps = (group) => group.selection.max === 1;
|
|
11
|
+
function selectionCount(group, choices) {
|
|
12
|
+
return (choices ?? []).reduce(
|
|
13
|
+
(count, choice) => choice.groupId === group.id ? count + Math.max(choice.quantity ?? 1, 1) : count,
|
|
14
|
+
0
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
var left = (group, choices) => (selectionMax(group) ?? Infinity) - selectionCount(group, choices);
|
|
18
|
+
function groupRoom(group, choices) {
|
|
19
|
+
const room = left(group, choices);
|
|
20
|
+
return room === Infinity ? void 0 : Math.max(0, room);
|
|
21
|
+
}
|
|
22
|
+
var held = (group, choices, choiceId) => selectionCount(
|
|
23
|
+
group,
|
|
24
|
+
choices?.filter((c) => c.choiceId === choiceId)
|
|
25
|
+
);
|
|
26
|
+
function canChoose(group, choices, choiceId) {
|
|
27
|
+
const choice = group.choices.find((c) => c.id === choiceId);
|
|
28
|
+
return Boolean(
|
|
29
|
+
choice?.isAvailable && (held(group, choices, choiceId) || swaps(group) || left(group, choices) >= (choice.quantity?.min ?? 1))
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
function atLimit(group, choices) {
|
|
33
|
+
return !swaps(group) && left(group, choices) <= 0;
|
|
34
|
+
}
|
|
35
|
+
function choiceQuantityMax(group, choices, choiceId) {
|
|
36
|
+
return Math.min(
|
|
37
|
+
group.choices.find((c) => c.id === choiceId)?.quantity?.max ?? 99,
|
|
38
|
+
swaps(group) ? 1 : Math.max(0, left(group, choices)) + held(group, choices, choiceId)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function requirementLabel(group) {
|
|
42
|
+
const { min } = group.selection;
|
|
43
|
+
const max = selectionMax(group);
|
|
44
|
+
if (!group.required) {
|
|
45
|
+
if (max !== void 0) {
|
|
46
|
+
return min > 1 ? `(Optional \xB7 ${min} to ${max})` : `(Optional \xB7 up to ${max})`;
|
|
47
|
+
}
|
|
48
|
+
return min > 1 ? `(Optional \xB7 at least ${min})` : "(Optional)";
|
|
49
|
+
}
|
|
50
|
+
if (max === void 0) {
|
|
51
|
+
return `(Min ${min})`;
|
|
52
|
+
}
|
|
53
|
+
return min === max ? `(Exactly ${min})` : min > 0 ? `(${min} to ${max})` : `(Max ${max})`;
|
|
54
|
+
}
|
|
55
|
+
var items = (n) => `${n} item${n === 1 ? "" : "s"}`;
|
|
56
|
+
function boundMessage(group, count) {
|
|
57
|
+
const { required } = group;
|
|
58
|
+
const { min } = group.selection;
|
|
59
|
+
const max = selectionMax(group);
|
|
60
|
+
if (max !== void 0 && count > max) {
|
|
61
|
+
return min === max ? `Remove ${items(count - max)}.` : `Remove ${items(count - max)}. Choose up to ${items(max)} in total.`;
|
|
62
|
+
}
|
|
63
|
+
if (!count && required && selectionControl(group) === "radio") {
|
|
64
|
+
return "Choose one option.";
|
|
65
|
+
}
|
|
66
|
+
if (!count && required && min <= 1 && max !== void 0) {
|
|
67
|
+
return `Choose at least one option. You can choose up to ${items(max)} in total.`;
|
|
68
|
+
}
|
|
69
|
+
const short = (required ? Math.max(1, min) : min) - count;
|
|
70
|
+
if (short > 0) {
|
|
71
|
+
return `Choose ${short} more item${short === 1 ? "" : "s"}.`;
|
|
72
|
+
}
|
|
73
|
+
return max !== void 0 ? `Choose up to ${items(max)} in total.` : `Choose at least ${items(min)}.`;
|
|
74
|
+
}
|
|
75
|
+
function outOfBounds(group, count) {
|
|
76
|
+
if (!count) {
|
|
77
|
+
return group.required;
|
|
78
|
+
}
|
|
79
|
+
return count < group.selection.min || count > (selectionMax(group) ?? Infinity);
|
|
80
|
+
}
|
|
81
|
+
function countLabel(group, count) {
|
|
82
|
+
const { min } = group.selection;
|
|
83
|
+
const max = selectionMax(group);
|
|
84
|
+
if (max !== void 0) {
|
|
85
|
+
return min === max ? `${count} of ${max} chosen` : `${count} of up to ${max} chosen`;
|
|
86
|
+
}
|
|
87
|
+
return min > 0 ? `${count} chosen (at least ${min})` : `${count} chosen`;
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
ITEM_NOTE_MAX_LENGTH,
|
|
91
|
+
LIMIT_REACHED_HINT,
|
|
92
|
+
atLimit,
|
|
93
|
+
boundMessage,
|
|
94
|
+
canChoose,
|
|
95
|
+
choiceQuantityMax,
|
|
96
|
+
countLabel,
|
|
97
|
+
groupRoom,
|
|
98
|
+
outOfBounds,
|
|
99
|
+
requirementLabel,
|
|
100
|
+
selectionControl,
|
|
101
|
+
selectionCount,
|
|
102
|
+
selectionMax
|
|
103
|
+
};
|
|
104
|
+
//# sourceMappingURL=index.js.map
|