@zappie/sdk 0.10.7 → 0.10.8
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 +7 -0
- package/README.md +13 -3
- package/dist/checkout-defaults/index.d.ts +22 -10
- package/dist/checkout-defaults/index.js +44 -45
- package/dist/checkout-defaults/index.js.map +1 -1
- package/dist/customer/client.js +19 -2
- package/dist/customer/client.js.map +1 -1
- package/dist/customer/index.d.ts +1 -1
- package/dist/debug.js +1 -1
- package/dist/index.js +1 -1
- package/dist/internal/remembered-address.js +63 -0
- package/dist/internal/remembered-address.js.map +6 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @zappie/sdk
|
|
2
2
|
|
|
3
|
+
## 0.10.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0d3b8e8: A remembered delivery address is now offered only to the shopper who entered it. `rememberAddress()` stores whether a guest or which customer used it. Pass the signed-in customer's ID, or `null` for a guest. Without one, it uses the shopper the page's customer client last saw, and keeps nothing before the session answers. `rememberedAddress()` and `defaultAddress()` read it back the same way. An address stored before this release names no shopper, so it is dropped rather than offered to anyone. The customer client forgets the remembered address on `signOut()`, and whenever the session shows a guest or a different customer, so a sign-out on another page or an expired session counts too.
|
|
8
|
+
- 2feeb9a: `signOut()` takes the delivery address out of each bag saved on this device. The bag keeps its items and promo code and holds no fulfilment, so choosing delivery asks for an address again. A pickup bag is left as it is, and blocked storage still signs out.
|
|
9
|
+
|
|
3
10
|
## 0.10.7
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -228,10 +228,20 @@ default storefront does, so every template follows one rule.
|
|
|
228
228
|
type is `"fixed"`, an address without a place ID counts as none. The cart's
|
|
229
229
|
own delivery address always wins in `checkoutDefaults()`.
|
|
230
230
|
- **Remembering.** `rememberAddress(address, storage, customerId)` keeps the
|
|
231
|
-
address line, place ID, coordinates and
|
|
232
|
-
|
|
233
|
-
`
|
|
231
|
+
address line, place ID, coordinates and who used it in `localStorage`, on
|
|
232
|
+
this device only, for that shopper alone. Pass the signed-in customer's ID,
|
|
233
|
+
or `null` for a guest. Leave it out and the address belongs to whoever the
|
|
234
|
+
page's customer client last saw signed in, and nothing is kept before the
|
|
235
|
+
session answers. `rememberedAddress()` reads it back the same way. An
|
|
236
|
+
address stored before it named its shopper is dropped, not offered.
|
|
237
|
+
`rememberAddress(null)` forgets it. Both do nothing where storage is
|
|
234
238
|
blocked.
|
|
239
|
+
- **Sign-out.** The customer client forgets the remembered address on
|
|
240
|
+
`signOut()`, and whenever the session shows a guest or another customer,
|
|
241
|
+
so a sign-out on another page or an expired session counts too.
|
|
242
|
+
`signOut()` also takes the delivery address out of each bag saved on this
|
|
243
|
+
device. The bag keeps its items and promo code and holds no fulfilment, so
|
|
244
|
+
choosing delivery asks for an address again. A pickup bag is left as it is.
|
|
235
245
|
- **Region.** `defaultRegion(branch, chosen)` keeps a chosen region and
|
|
236
246
|
otherwise picks the branch's only one.
|
|
237
247
|
- **Time.** `restoreSchedule(fulfilment, branchId)` keeps the cart's scheduled
|
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { DeliveryAddress, Branch, CartFulfilment } from '@zappie/sdk';
|
|
2
2
|
import { SessionCustomer, SavedAddress, CustomerClient } from '@zappie/sdk/customer';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* How the branch prices delivery. Only `"fixed"` lets an address without a place ID stand.
|
|
6
|
-
*/
|
|
7
|
-
type DeliveryFeeType = "region" | "distance" | "fixed";
|
|
8
4
|
/**
|
|
9
5
|
* The browser storage the remembered address uses. Pass `null` to remember nothing.
|
|
10
6
|
*/
|
|
11
7
|
type AddressStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;
|
|
8
|
+
declare global {
|
|
9
|
+
var __zappieShopper: {
|
|
10
|
+
id: string | null;
|
|
11
|
+
} | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* How the branch prices delivery. Only `"fixed"` lets an address without a place ID stand.
|
|
16
|
+
*/
|
|
17
|
+
type DeliveryFeeType = "region" | "distance" | "fixed";
|
|
18
|
+
|
|
12
19
|
/**
|
|
13
20
|
* The phone a checkout shows, and the `contact_phone` to send.
|
|
14
21
|
*/
|
|
@@ -60,9 +67,12 @@ declare function defaultPhone(typed: string | undefined, customer: SessionCustom
|
|
|
60
67
|
/**
|
|
61
68
|
* The last delivery address this shopper used on this device, or undefined. An address a guest
|
|
62
69
|
* used is offered only to a guest, and one a customer used only to that customer. Under
|
|
63
|
-
* distance or region fees, an entry without a place ID counts as none and is removed.
|
|
70
|
+
* distance or region fees, an entry without a place ID counts as none and is removed. So is an
|
|
71
|
+
* entry that names no shopper, written before addresses had one.
|
|
64
72
|
*
|
|
65
|
-
* @param options - The fee type, storage and signed-in customer's ID.
|
|
73
|
+
* @param options - The fee type, storage and signed-in customer's ID. Pass `customerId: null`
|
|
74
|
+
* for a guest. Without it, the shopper is the one the page's customer client last saw, and
|
|
75
|
+
* nothing is offered before it has seen one.
|
|
66
76
|
*/
|
|
67
77
|
declare function rememberedAddress(options?: {
|
|
68
78
|
feeType?: DeliveryFeeType;
|
|
@@ -70,13 +80,15 @@ declare function rememberedAddress(options?: {
|
|
|
70
80
|
customerId?: string | null;
|
|
71
81
|
}): DeliveryAddress | undefined;
|
|
72
82
|
/**
|
|
73
|
-
* Remember the delivery address the shopper used, on this device only
|
|
74
|
-
* line, place ID, coordinates and
|
|
75
|
-
* is blocked.
|
|
83
|
+
* Remember the delivery address the shopper used, on this device only, for that shopper alone.
|
|
84
|
+
* Stores the address line, place ID, coordinates and who used it, nothing else. Does nothing
|
|
85
|
+
* where storage is blocked, or when the shopper is unknown. The customer client forgets the
|
|
86
|
+
* address when its customer signs out.
|
|
76
87
|
*
|
|
77
88
|
* @param value - The address, or null to forget it.
|
|
78
89
|
* @param storage - Defaults to `localStorage`.
|
|
79
|
-
* @param customerId - The signed-in customer's ID
|
|
90
|
+
* @param customerId - The signed-in customer's ID, or null for a guest. Without it, the
|
|
91
|
+
* shopper is the one the page's customer client last saw.
|
|
80
92
|
*/
|
|
81
93
|
declare function rememberAddress(value: DeliveryAddress | null, storage?: AddressStorage | null, customerId?: string | null): void;
|
|
82
94
|
/**
|
|
@@ -1,22 +1,13 @@
|
|
|
1
1
|
// src/checkout-defaults/index.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
function attempt(fn) {
|
|
14
|
-
try {
|
|
15
|
-
return fn();
|
|
16
|
-
} catch {
|
|
17
|
-
return void 0;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
2
|
+
import {
|
|
3
|
+
ADDRESS_KEY,
|
|
4
|
+
addressStorage,
|
|
5
|
+
forgetRememberedAddress,
|
|
6
|
+
knownShopper,
|
|
7
|
+
ownerOf,
|
|
8
|
+
readStored,
|
|
9
|
+
settleShopper
|
|
10
|
+
} from "../internal/remembered-address.js";
|
|
20
11
|
function text(value) {
|
|
21
12
|
return typeof value === "string" && value.trim() ? value : void 0;
|
|
22
13
|
}
|
|
@@ -34,56 +25,64 @@ function defaultPhone(typed, customer) {
|
|
|
34
25
|
const masked = customer?.phone_masked;
|
|
35
26
|
return masked ? { display: masked, fromAccount: true } : { display: "", fromAccount: false };
|
|
36
27
|
}
|
|
28
|
+
function shopper(customerId) {
|
|
29
|
+
return customerId === null ? null : customerId || knownShopper();
|
|
30
|
+
}
|
|
37
31
|
function rememberedAddress(options = {}) {
|
|
38
|
-
const storage =
|
|
39
|
-
const
|
|
40
|
-
if (!
|
|
32
|
+
const storage = addressStorage(options.storage);
|
|
33
|
+
const stored = readStored(storage);
|
|
34
|
+
if (!stored) {
|
|
41
35
|
return void 0;
|
|
42
36
|
}
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
37
|
+
const owner = ownerOf(stored);
|
|
38
|
+
if (owner === void 0) {
|
|
39
|
+
forgetRememberedAddress(storage);
|
|
45
40
|
return void 0;
|
|
46
41
|
}
|
|
47
|
-
const
|
|
42
|
+
const reader = shopper(options.customerId);
|
|
43
|
+
if (reader === void 0 || owner !== reader) {
|
|
44
|
+
if (typeof reader === "string" && typeof owner === "string") {
|
|
45
|
+
forgetRememberedAddress(storage);
|
|
46
|
+
}
|
|
47
|
+
return void 0;
|
|
48
|
+
}
|
|
49
|
+
const placeId = text(stored.placeId);
|
|
48
50
|
if (!placeId && options.feeType !== "fixed") {
|
|
49
|
-
|
|
51
|
+
forgetRememberedAddress(storage);
|
|
50
52
|
return void 0;
|
|
51
53
|
}
|
|
52
54
|
return address(
|
|
53
|
-
text(stored
|
|
55
|
+
text(stored.address),
|
|
54
56
|
placeId,
|
|
55
|
-
stored
|
|
56
|
-
stored
|
|
57
|
+
stored.latitude,
|
|
58
|
+
stored.longitude
|
|
57
59
|
);
|
|
58
60
|
}
|
|
59
61
|
function rememberAddress(value, storage, customerId) {
|
|
60
|
-
const target =
|
|
62
|
+
const target = addressStorage(storage);
|
|
61
63
|
if (!target) {
|
|
62
64
|
return;
|
|
63
65
|
}
|
|
64
66
|
if (!value?.line1.trim()) {
|
|
65
|
-
|
|
67
|
+
forgetRememberedAddress(target);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const owner = shopper(customerId);
|
|
71
|
+
if (owner === void 0) {
|
|
66
72
|
return;
|
|
67
73
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
try {
|
|
75
|
+
target.setItem(
|
|
76
|
+
ADDRESS_KEY,
|
|
71
77
|
JSON.stringify({
|
|
72
|
-
...
|
|
78
|
+
...owner ? { customerId: owner } : { guest: true },
|
|
73
79
|
address: value.line1,
|
|
74
80
|
placeId: value.placeId ?? "",
|
|
75
81
|
latitude: value.coordinates?.lat,
|
|
76
82
|
longitude: value.coordinates?.lng
|
|
77
83
|
})
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
function forget(customerId, storage) {
|
|
82
|
-
const target = local(storage);
|
|
83
|
-
const raw = target ? attempt(() => target.getItem(KEY)) : void 0;
|
|
84
|
-
const stored = raw ? attempt(() => JSON.parse(raw)) : void 0;
|
|
85
|
-
if (text(stored?.customerId) === customerId) {
|
|
86
|
-
attempt(() => target?.removeItem(KEY));
|
|
84
|
+
);
|
|
85
|
+
} catch {
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
88
|
function defaultAddress(options = {}) {
|
|
@@ -160,8 +159,8 @@ function watchCheckoutDefaults(customer, read, listener) {
|
|
|
160
159
|
const refresh = () => {
|
|
161
160
|
const current = ++run;
|
|
162
161
|
const id = customer.state === "authenticated" ? customer.customer?.id : void 0;
|
|
163
|
-
if (
|
|
164
|
-
|
|
162
|
+
if (customer.state !== "unknown") {
|
|
163
|
+
settleShopper(id ?? null, read().storage);
|
|
165
164
|
}
|
|
166
165
|
signedIn = id;
|
|
167
166
|
if (!id) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/checkout-defaults/index.ts"],
|
|
4
|
-
"mappings": ";
|
|
4
|
+
"mappings": ";AASA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AAoDP,SAAS,KAAK,OAAoC;AAChD,SAAO,OAAO,UAAU,YAAY,MAAM,KAAK,IAAI,QAAQ;AAC7D;AAEA,SAAS,MAAM,KAAc,KAAc;AACzC,SAAO,OAAO,QAAQ,YACpB,OAAO,QAAQ,YACf,OAAO,SAAS,GAAG,KACnB,OAAO,SAAS,GAAG,IACjB,EAAE,aAAa,EAAE,KAAK,IAAI,EAAE,IAC5B,CAAC;AACP;AAEA,SAAS,QACP,OACA,SACA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,UAAU,MAAM;AACpB,UAAM,UAAU,EAAE;AAClB,UAAM,KACJ,SAAS,UAAU,kBAAkB,SAAS,UAAU,KAAK;AAC/D,QAAI,SAAS,UAAU,WAAW;AAChC,oBAAc,MAAM,MAAM,KAAK,EAAE,OAAO;AAAA,IAC1C;AACA,eAAW;AACX,QAAI,CAAC,IAAI;AACP,cAAQ,MAAS;AACjB;AAAA,IACF;AACA,SAAK,SAAS,UAAU,EAAE;AAAA,MACxB,CAAC,UAAU;AACT,YAAI,CAAC,WAAW,YAAY,KAAK;AAC/B,kBAAQ,KAAK;AAAA,QACf;AAAA,MACF;AAAA,MACA,MAAM;AACJ,YAAI,CAAC,WAAW,YAAY,KAAK;AAC/B,kBAAQ,MAAS;AAAA,QACnB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,QAAM,MAAM,SAAS,SAAS,CAAC,OAAO,SAAS;AAC7C,UAAM,KAAK,UAAU,kBAAkB,MAAM,KAAK;AAClD,QAAI,OAAO,UAAU;AACnB,cAAQ;AAAA,IACV;AAAA,EACF,CAAC;AACD,UAAQ;AACR,SAAO,MAAM;AACX,cAAU;AACV,QAAI;AAAA,EACN;AACF;",
|
|
5
5
|
"names": ["address"]
|
|
6
6
|
}
|
package/dist/customer/client.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
// src/customer/client.ts
|
|
2
2
|
import { createPendingNavigation } from "@zappie/sdk/navigation";
|
|
3
3
|
import { requestWithRetry } from "../internal/customer-http.js";
|
|
4
|
+
import {
|
|
5
|
+
forgetBagAddresses,
|
|
6
|
+
forgetRememberedAddress,
|
|
7
|
+
settleShopper
|
|
8
|
+
} from "../internal/remembered-address.js";
|
|
4
9
|
import { currentPath, leaveTo, safeReturnTo } from "./navigation.js";
|
|
5
10
|
import { createCustomerRequest, readCustomerResponse } from "./request.js";
|
|
6
11
|
import { watchOrder } from "./watch-order.js";
|
|
@@ -18,6 +23,12 @@ function createCustomer(options = {}) {
|
|
|
18
23
|
const fetcher = options.fetch ?? globalThis.fetch;
|
|
19
24
|
let customer = options.initialCustomer ?? null;
|
|
20
25
|
let state = options.initialCustomer === void 0 ? "unknown" : customer ? "authenticated" : "anonymous";
|
|
26
|
+
const settle = () => {
|
|
27
|
+
if (state !== "unknown") {
|
|
28
|
+
settleShopper(customer?.id ?? null);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
settle();
|
|
21
32
|
const listeners = /* @__PURE__ */ new Set();
|
|
22
33
|
const notify = () => {
|
|
23
34
|
for (const listener of listeners) {
|
|
@@ -30,6 +41,7 @@ function createCustomer(options = {}) {
|
|
|
30
41
|
}
|
|
31
42
|
state = nextState;
|
|
32
43
|
customer = next;
|
|
44
|
+
settle();
|
|
33
45
|
globalThis.__zappieEmit?.("auth:changed", { state, customer });
|
|
34
46
|
notify();
|
|
35
47
|
};
|
|
@@ -101,9 +113,14 @@ function createCustomer(options = {}) {
|
|
|
101
113
|
)
|
|
102
114
|
);
|
|
103
115
|
},
|
|
104
|
-
/** Sign out through Zappie hosting. A call while pending does nothing. */
|
|
116
|
+
/** Sign out through Zappie hosting, forgetting the delivery address remembered on this device and the one in each saved bag, whose items stay. A call while pending does nothing. */
|
|
105
117
|
signOut() {
|
|
106
|
-
void leaving.start(() =>
|
|
118
|
+
void leaving.start(() => {
|
|
119
|
+
globalThis.__zappieShopper = void 0;
|
|
120
|
+
forgetRememberedAddress();
|
|
121
|
+
forgetBagAddresses();
|
|
122
|
+
return leaveTo("/_zappie/logout", true);
|
|
123
|
+
});
|
|
107
124
|
},
|
|
108
125
|
// Every read is a GET, fetch's default.
|
|
109
126
|
profile: () => request("/me"),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/customer/client.ts"],
|
|
4
|
-
"mappings": ";AAEA,SAAS,+BAA+B;AACxC,SAAS,wBAAwB;AACjC,SAAS,aAAa,SAAS,oBAAoB;AACnD,SAAS,uBAAuB,4BAA4B;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AAIA,SAAQ,GAAG,MAAM,QAAW;AAC9B,gBAAM,IAAI,KAAK,OAAOA,SAAQ,GAAG,CAAC,CAAC;AAAA,QACrC;AAAA,MACF;AACA,aAAO,QAAoB,aAAa,MAAM,OAAO,IAAI,KAAK,KAAK,EAAE,EAAE;AAAA,IACzE;AAAA,IACA,OAAO,CAAC,OAAe,UAAU,EAAE;AAAA,IACnC,WAAW,IAAY,cAAiC;AACtD,aAAO,WAAW,MAAM,IAAI,WAAW,YAAY;AAAA,IACrD;AAAA,EACF;AACF;",
|
|
5
5
|
"names": ["options"]
|
|
6
6
|
}
|
package/dist/customer/index.d.ts
CHANGED
|
@@ -148,7 +148,7 @@ declare function createCustomer(options?: CustomerClientOptions): {
|
|
|
148
148
|
onChange(listener: CustomerListener): () => void;
|
|
149
149
|
/** Go to sign-in and come back to `returnTo`, this page by default. A call while pending does nothing. */
|
|
150
150
|
signIn(returnTo?: string): void;
|
|
151
|
-
/** Sign out through Zappie hosting. A call while pending does nothing. */
|
|
151
|
+
/** Sign out through Zappie hosting, forgetting the delivery address remembered on this device and the one in each saved bag, whose items stay. A call while pending does nothing. */
|
|
152
152
|
signOut(): void;
|
|
153
153
|
profile: () => Promise<CustomerProfile>;
|
|
154
154
|
addresses(): Promise<SavedAddress[]>;
|
package/dist/debug.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -11,7 +11,7 @@ import { ZappieError, isZappieError } from "./errors.js";
|
|
|
11
11
|
export * from "./models.js";
|
|
12
12
|
import { format, money } from "./money.js";
|
|
13
13
|
import { RestTransport } from "./transport/rest.js";
|
|
14
|
-
var VERSION = true ? "0.10.
|
|
14
|
+
var VERSION = true ? "0.10.8" : "0.0.0-development";
|
|
15
15
|
export {
|
|
16
16
|
API_VERSION,
|
|
17
17
|
RestTransport,
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/internal/remembered-address.ts
|
|
2
|
+
var ADDRESS_KEY = "zappie_delivery_address";
|
|
3
|
+
function ownerOf(stored) {
|
|
4
|
+
const id = stored?.customerId;
|
|
5
|
+
return typeof id === "string" && id ? id : stored?.guest === true ? null : void 0;
|
|
6
|
+
}
|
|
7
|
+
function addressStorage(storage) {
|
|
8
|
+
try {
|
|
9
|
+
return storage !== void 0 ? storage : typeof window === "undefined" ? null : window.localStorage;
|
|
10
|
+
} catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function readStored(storage) {
|
|
15
|
+
try {
|
|
16
|
+
const stored = JSON.parse(storage?.getItem(ADDRESS_KEY) ?? "0");
|
|
17
|
+
return stored && typeof stored === "object" ? stored : void 0;
|
|
18
|
+
} catch {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function forgetRememberedAddress(storage) {
|
|
23
|
+
try {
|
|
24
|
+
addressStorage(storage)?.removeItem(ADDRESS_KEY);
|
|
25
|
+
} catch {
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function knownShopper() {
|
|
29
|
+
return globalThis.__zappieShopper?.id;
|
|
30
|
+
}
|
|
31
|
+
function settleShopper(id, storage) {
|
|
32
|
+
globalThis.__zappieShopper = { id };
|
|
33
|
+
const owner = ownerOf(readStored(addressStorage(storage)));
|
|
34
|
+
if (owner && owner !== id) {
|
|
35
|
+
forgetRememberedAddress(storage);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function forgetBagAddresses() {
|
|
39
|
+
try {
|
|
40
|
+
for (const key of Object.keys(localStorage)) {
|
|
41
|
+
try {
|
|
42
|
+
const bag = key.startsWith("zappie:cart:v1:") ? JSON.parse(localStorage[key]) : void 0;
|
|
43
|
+
if (bag?.fulfilment?.type == "delivery") {
|
|
44
|
+
bag.fulfilment = null;
|
|
45
|
+
localStorage[key] = JSON.stringify(bag);
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
ADDRESS_KEY,
|
|
55
|
+
addressStorage,
|
|
56
|
+
forgetBagAddresses,
|
|
57
|
+
forgetRememberedAddress,
|
|
58
|
+
knownShopper,
|
|
59
|
+
ownerOf,
|
|
60
|
+
readStored,
|
|
61
|
+
settleShopper
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=remembered-address.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/internal/remembered-address.ts"],
|
|
4
|
+
"mappings": ";AAiBO,IAAM,cAAc;AAcpB,SAAS,QACd,QAC2B;AAC3B,QAAM,KAAK,QAAQ;AACnB,SAAO,OAAO,OAAO,YAAY,KAC7B,KACA,QAAQ,UAAU,OAChB,OACA;AACR;AAGO,SAAS,eACd,SACuB;AACvB,MAAI;AACF,WAAO,YAAY,SACf,UACA,OAAO,WAAW,cAChB,OACA,OAAO;AAAA,EACf,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGO,SAAS,WACd,SAC2B;AAC3B,MAAI;AACF,UAAM,SAAS,KAAK,MAAM,SAAS,QAAQ,WAAW,KAAK,GAAG;AAC9D,WAAO,UAAU,OAAO,WAAW,WAC9B,SACD;AAAA,EACN,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGO,SAAS,wBAAwB,SAAiC;AACvE,MAAI;AACF,mBAAe,OAAO,GAAG,WAAW,WAAW;AAAA,EACjD,QAAQ;AAAA,EAER;AACF;AAGO,SAAS,eAA0C;AACxD,SAAO,WAAW,iBAAiB;AACrC;AAMO,SAAS,cACd,IACA,SACA;AACA,aAAW,kBAAkB,EAAE,GAAG;AAClC,QAAM,QAAQ,QAAQ,WAAW,eAAe,OAAO,CAAC,CAAC;AACzD,MAAI,SAAS,UAAU,IAAI;AACzB,4BAAwB,OAAO;AAAA,EACjC;AACF;AAMO,SAAS,qBAAqB;AACnC,MAAI;AACF,eAAW,OAAO,OAAO,KAAK,YAAY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