@usequeek/theme-kit 0.1.29 → 0.1.31
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 +82 -0
- package/client.ts +4 -0
- package/components/cart/cart-panel-controller.tsx +9 -4
- package/components/cart/default-cart-shell.tsx +68 -7
- package/components/market-switcher.tsx +82 -0
- package/hooks/use-add-app-block-lines.ts +258 -0
- package/hooks/use-cart-server-pricing.tsx +33 -0
- package/hooks/use-markets.ts +55 -0
- package/hooks/use-products.ts +20 -5
- package/hooks/use-shop-cart.ts +15 -1
- package/index.ts +3 -0
- package/locales/en.default.json +6 -1
- package/package.json +1 -1
- package/provider.tsx +37 -2
- package/sdk/client.ts +6 -1
- package/sdk/queek-client.ts +17 -1
- package/sdk/server-store-client.ts +45 -4
- package/stores/cart-store.ts +59 -11
- package/types/app-block.ts +86 -0
- package/types/theme.ts +7 -0
- package/utils/cart-pricing.ts +115 -0
- package/utils/format.ts +36 -0
- package/utils/market-choice.ts +38 -0
- package/utils/market.ts +189 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the server charges for the cart on screen, as the host app read it
|
|
3
|
+
* (quantity-break and spend-gate discounts included). The kit only DISPLAYS it:
|
|
4
|
+
* every amount is a decimal string exactly as the server sent it ("4500.00"),
|
|
5
|
+
* and nothing here ever computes a price, a discount or a total.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** One cart line as the server prices it. Matched to a local line by `line_id` AND `quantity`. */
|
|
9
|
+
export interface CartLinePricing {
|
|
10
|
+
/** The local cart line id (`CartItem.id`). */
|
|
11
|
+
line_id: string;
|
|
12
|
+
/** The quantity the server priced; a line whose quantity differs is not covered. */
|
|
13
|
+
quantity: number;
|
|
14
|
+
/** One unit with its add-ons, as charged. */
|
|
15
|
+
unit_price: string;
|
|
16
|
+
/** The line total as charged. */
|
|
17
|
+
line_total: string;
|
|
18
|
+
/** The list (undiscounted) line total, only when the server charges less than it; otherwise null. */
|
|
19
|
+
list_line_total: string | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The server's pricing of one whole cart. */
|
|
23
|
+
export interface CartServerPricing {
|
|
24
|
+
lines: readonly CartLinePricing[];
|
|
25
|
+
/** The cart total as charged (before coupons and delivery). */
|
|
26
|
+
total: string;
|
|
27
|
+
/** The list (undiscounted) cart total, only when the server charges less than it; otherwise null. */
|
|
28
|
+
list_total: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DECIMAL = /^\d+(\.\d+)?$/;
|
|
32
|
+
|
|
33
|
+
function isDecimal(value: unknown): value is string {
|
|
34
|
+
return typeof value === 'string' && DECIMAL.test(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Digit-exact `a > b` for two non-negative decimal strings (no float round trip). */
|
|
38
|
+
function isGreater(a: string, b: string): boolean {
|
|
39
|
+
const [aWhole, aFraction = ''] = a.split('.');
|
|
40
|
+
const [bWhole, bFraction = ''] = b.split('.');
|
|
41
|
+
const left = aWhole.replace(/^0+(?=\d)/, '');
|
|
42
|
+
const right = bWhole.replace(/^0+(?=\d)/, '');
|
|
43
|
+
|
|
44
|
+
if (left.length !== right.length) {
|
|
45
|
+
return left.length > right.length;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Equal-width digit strings compare in numeric order; this only orders text, it computes nothing.
|
|
49
|
+
const width = aFraction.length > bFraction.length ? aFraction.length : bFraction.length;
|
|
50
|
+
const leftDigits = `${left}${aFraction.padEnd(width, '0')}`;
|
|
51
|
+
const rightDigits = `${right}${bFraction.padEnd(width, '0')}`;
|
|
52
|
+
|
|
53
|
+
return leftDigits > rightDigits;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A struck list amount is kept only when it is a well-formed decimal HIGHER than what is charged. */
|
|
57
|
+
function strikeOf(list: unknown, charged: string): string | null {
|
|
58
|
+
return isDecimal(list) && isGreater(list, charged) ? list : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The server pricing that may stand for these cart lines, or null.
|
|
63
|
+
*
|
|
64
|
+
* It stands only when it covers the lines one to one: same count, every line
|
|
65
|
+
* found by id with the same quantity, and every amount a plain
|
|
66
|
+
* non-negative decimal string. Anything else (no pricing, a line added or
|
|
67
|
+
* re-quantified since the server read, a malformed amount) is null, and the
|
|
68
|
+
* caller prints its own list prices exactly as it always has: nothing is
|
|
69
|
+
* guessed and nothing is mixed. A struck list amount that is not higher than
|
|
70
|
+
* the amount charged is dropped to null. Amounts are returned as received.
|
|
71
|
+
*/
|
|
72
|
+
export function resolveCartPricing(
|
|
73
|
+
pricing: CartServerPricing | null | undefined,
|
|
74
|
+
lines: readonly { id: string; quantity: number }[],
|
|
75
|
+
): CartServerPricing | null {
|
|
76
|
+
if (!pricing || !Array.isArray(pricing.lines) || !isDecimal(pricing.total)) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (lines.length === 0 || pricing.lines.length !== lines.length) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const byId = new Map<string, CartLinePricing>();
|
|
85
|
+
|
|
86
|
+
for (const entry of pricing.lines) {
|
|
87
|
+
if (!entry || typeof entry.line_id !== 'string' || !isDecimal(entry.unit_price) || !isDecimal(entry.line_total)) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
byId.set(entry.line_id, entry);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const covered: CartLinePricing[] = [];
|
|
95
|
+
|
|
96
|
+
for (const line of lines) {
|
|
97
|
+
const entry = byId.get(line.id);
|
|
98
|
+
|
|
99
|
+
if (!entry || entry.quantity !== line.quantity) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
covered.push({ ...entry, list_line_total: strikeOf(entry.list_line_total, entry.line_total) });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { lines: covered, total: pricing.total, list_total: strikeOf(pricing.list_total, pricing.total) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The server pricing of one cart line from a resolved overlay, or null when there is none for it. */
|
|
110
|
+
export function cartLinePricing(
|
|
111
|
+
pricing: CartServerPricing | null | undefined,
|
|
112
|
+
lineId: string,
|
|
113
|
+
): CartLinePricing | null {
|
|
114
|
+
return pricing?.lines.find((entry) => entry.line_id === lineId) ?? null;
|
|
115
|
+
}
|
package/utils/format.ts
CHANGED
|
@@ -171,6 +171,42 @@ export function formatMoney(
|
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Format a money amount the server sent as a decimal STRING ("4500.00",
|
|
176
|
+
* KWD "1.250") without a float round trip: the string reaches Intl untouched,
|
|
177
|
+
* at the currency's own precision, with decimals shown only when present (the
|
|
178
|
+
* same style as `formatMoney`, which caps at 2 digits). Returns an empty string
|
|
179
|
+
* for anything that is not a plain non-negative decimal. Never computes, never throws.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* formatWireMoney('4500.00', 'NGN'); // "₦4,500"
|
|
183
|
+
*/
|
|
184
|
+
export function formatWireMoney(
|
|
185
|
+
amount: string,
|
|
186
|
+
currency = PLATFORM_DEFAULT_CURRENCY,
|
|
187
|
+
locale?: string | null,
|
|
188
|
+
): string {
|
|
189
|
+
if (typeof amount !== 'string' || !/^\d+(\.\d+)?$/.test(amount)) {
|
|
190
|
+
return '';
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const code = normalizeCurrencyCode(currency);
|
|
194
|
+
const tag = displayTagFor(locale, (l) => Intl.NumberFormat.supportedLocalesOf(l));
|
|
195
|
+
try {
|
|
196
|
+
const precision = new Intl.NumberFormat(tag, { style: 'currency', currency: code }).resolvedOptions().maximumFractionDigits ?? 2;
|
|
197
|
+
|
|
198
|
+
// Intl parses a decimal string exactly; the cast only satisfies the lib typing.
|
|
199
|
+
return new Intl.NumberFormat(tag, {
|
|
200
|
+
style: 'currency',
|
|
201
|
+
currency: code,
|
|
202
|
+
minimumFractionDigits: 0,
|
|
203
|
+
maximumFractionDigits: precision,
|
|
204
|
+
}).format(amount as unknown as number);
|
|
205
|
+
} catch {
|
|
206
|
+
return `${code} ${amount}`;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
174
210
|
/** A whole-number percentage string, e.g. `toPercent(12.4)` is `"12%"`. */
|
|
175
211
|
/**
|
|
176
212
|
* The one rounding for an amount the kit computes (event values, line totals):
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { dropPersistedShopLines } from '../stores/cart-store';
|
|
2
|
+
import { marketCookieWrite, readMarketFromCookieString, type StoreMarket } from './market';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Browser-only: records the shopper's market for this store and reloads.
|
|
6
|
+
*
|
|
7
|
+
* A market changes every price on the page, the cart and the checkout, and the
|
|
8
|
+
* server render reads the same cookie, so the one correct way to apply it is a
|
|
9
|
+
* full load: no price from the previous currency survives in a client cache,
|
|
10
|
+
* a store or an already rendered page. The store's cart lines are dropped from
|
|
11
|
+
* the persisted cart first, because a line keeps the unit price it was added at
|
|
12
|
+
* (see `dropPersistedShopLines`); the server cart re-prices them on the reload.
|
|
13
|
+
*
|
|
14
|
+
* `null` returns the shopper to the store's own currency.
|
|
15
|
+
*/
|
|
16
|
+
export function chooseMarket(vendorSlug: string, vendorId: string, marketId: number | null): void {
|
|
17
|
+
if (typeof window === 'undefined') return;
|
|
18
|
+
|
|
19
|
+
document.cookie = marketCookieWrite(vendorSlug, marketId);
|
|
20
|
+
dropPersistedShopLines(vendorId);
|
|
21
|
+
window.location.reload();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Clears a choice the store no longer offers (its market was unpublished or
|
|
26
|
+
* deleted), once. Runs only when the host resolved the store's market list
|
|
27
|
+
* (a failed read passes none, and a shopper's choice is never dropped on a blip).
|
|
28
|
+
*/
|
|
29
|
+
export function reconcileMarketChoice(vendorSlug: string, vendorId: string, published: readonly StoreMarket[] | null): void {
|
|
30
|
+
if (typeof window === 'undefined' || published === null || !vendorSlug) return;
|
|
31
|
+
|
|
32
|
+
const chosen = readMarketFromCookieString(document.cookie, vendorSlug);
|
|
33
|
+
if (chosen === null || published.some((market) => market.id === chosen)) return;
|
|
34
|
+
|
|
35
|
+
document.cookie = marketCookieWrite(vendorSlug, null);
|
|
36
|
+
dropPersistedShopLines(vendorId);
|
|
37
|
+
window.location.reload();
|
|
38
|
+
}
|
package/utils/market.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storefront market transport (markets.currency).
|
|
3
|
+
*
|
|
4
|
+
* A store may publish markets: a set of countries plus one currency. The shopper
|
|
5
|
+
* picks one explicitly (no geo switch), the API then answers prices in that
|
|
6
|
+
* currency when a read carries `?market=<id>`, and an order carries the rate pin
|
|
7
|
+
* the cart or quote issued (`market_pin` in the order body).
|
|
8
|
+
*
|
|
9
|
+
* The choice lives in a cookie, so the server render and the browser agree on
|
|
10
|
+
* it. It is per store: the cookie name carries the store slug, because
|
|
11
|
+
* path-based stores share one host. Like `utils/locale.ts` this module is pure
|
|
12
|
+
* (no `next/*`, no React) and every function returns its input untouched when no
|
|
13
|
+
* market is chosen, so a store without a published market behaves exactly as
|
|
14
|
+
* before.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** One published market of the store, as `GET /client/store/markets` lists it. */
|
|
18
|
+
export interface StoreMarket {
|
|
19
|
+
/** The integer id a read sends back as `?market=`. */
|
|
20
|
+
id: number;
|
|
21
|
+
name: string;
|
|
22
|
+
/** ISO 3166-1 alpha-2 codes the market sells to. */
|
|
23
|
+
countries: string[];
|
|
24
|
+
/** ISO 4217 code prices are shown and charged in. */
|
|
25
|
+
currency: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
import { getActivePlatform } from '../sdk/platform';
|
|
29
|
+
|
|
30
|
+
const MARKET_ID_MAX_LENGTH = 18;
|
|
31
|
+
const COOKIE_PREFIX = 'queek_market_';
|
|
32
|
+
export const MARKET_COOKIE_MAX_AGE_SECONDS = 60 * 60 * 24 * 365;
|
|
33
|
+
|
|
34
|
+
/** The market id a request names, or null. Only short digit strings qualify, so nothing odd reaches the API or a cache key. */
|
|
35
|
+
export function parseMarketId(value: unknown): number | null {
|
|
36
|
+
const text = typeof value === 'number' && Number.isInteger(value) ? String(value) : value;
|
|
37
|
+
if (typeof text !== 'string' || text.length === 0 || text.length > MARKET_ID_MAX_LENGTH || !/^\d+$/.test(text)) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const id = Number(text);
|
|
41
|
+
return Number.isSafeInteger(id) && id > 0 ? id : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Cookie holding the shopper's market for one store. Slug characters outside the cookie-name set become `_`. */
|
|
45
|
+
export function marketCookieName(vendorSlug: string): string {
|
|
46
|
+
return `${COOKIE_PREFIX}${vendorSlug.replace(/[^A-Za-z0-9_-]/g, '_')}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The market id in a `Cookie` header / `document.cookie` string for this store, or null. */
|
|
50
|
+
export function readMarketFromCookieString(cookieString: string | null | undefined, vendorSlug: string): number | null {
|
|
51
|
+
if (!cookieString) return null;
|
|
52
|
+
const name = marketCookieName(vendorSlug);
|
|
53
|
+
for (const part of cookieString.split(';')) {
|
|
54
|
+
const [key, ...rest] = part.trim().split('=');
|
|
55
|
+
if (key === name) return parseMarketId(rest.join('='));
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The `Set-Cookie`-style value a browser write uses; `null` clears the choice. */
|
|
61
|
+
export function marketCookieWrite(vendorSlug: string, marketId: number | null): string {
|
|
62
|
+
const name = marketCookieName(vendorSlug);
|
|
63
|
+
return marketId === null
|
|
64
|
+
? `${name}=; Path=/; Max-Age=0; SameSite=Lax`
|
|
65
|
+
: `${name}=${marketId}; Path=/; Max-Age=${MARKET_COOKIE_MAX_AGE_SECONDS}; SameSite=Lax`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type QueryRecord = Record<string, string | number | boolean | string[] | null | undefined>;
|
|
69
|
+
|
|
70
|
+
/** Adds `?market=<id>` to a backend query. No market: the input is returned untouched (same reference). */
|
|
71
|
+
export function withMarketQuery<T extends QueryRecord>(
|
|
72
|
+
query: T | undefined,
|
|
73
|
+
marketId: number | null | undefined,
|
|
74
|
+
): T | (T & { market: string }) | undefined {
|
|
75
|
+
const id = parseMarketId(marketId);
|
|
76
|
+
if (id === null) return query;
|
|
77
|
+
return { ...(query ?? ({} as T)), market: String(id) };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Appends `market=<id>` to a built URL or path. No market: the input is returned untouched. */
|
|
81
|
+
export function withMarketPath(path: string, marketId: number | null | undefined): string {
|
|
82
|
+
const id = parseMarketId(marketId);
|
|
83
|
+
if (id === null || /[?&]market=/.test(path)) return path;
|
|
84
|
+
const hash = path.indexOf('#');
|
|
85
|
+
const head = hash === -1 ? path : path.slice(0, hash);
|
|
86
|
+
const tail = hash === -1 ? '' : path.slice(hash);
|
|
87
|
+
return `${head}${head.includes('?') ? '&' : '?'}market=${id}${tail}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether a store read carries prices (or a price-dependent quote): the only
|
|
92
|
+
* calls that take `?market`. The same list decides which server reads leave the
|
|
93
|
+
* shared data cache for a shopper in a market.
|
|
94
|
+
*/
|
|
95
|
+
export function isMarketPricedPath(path: string): boolean {
|
|
96
|
+
const clean = (path.split(/[?#]/)[0] ?? '').replace(/^(?:\/api\/v1)?(?:\/client\/store)?/, '');
|
|
97
|
+
// Anchored at the first segment: a page called "cart", a product's reviews or an order's pay link are not priced reads.
|
|
98
|
+
return (
|
|
99
|
+
/^\/products(?:\/[^/]+(?:\/related)?)?\/?$/.test(clean) ||
|
|
100
|
+
/^\/(?:collections|categories|promotions|search|shipping-zones|fulfillment|shipping|checkout|payment-methods|cart|wishlist-items)(?:\/|$)/.test(clean) ||
|
|
101
|
+
/^\/order\/checkout\/?$/.test(clean)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The market of the running browser page. `Provider` sets it from what the host
|
|
107
|
+
* validated; the browser transports read it per request. Server code never
|
|
108
|
+
* touches it (module state is shared between requests there). Markets are a
|
|
109
|
+
* storefront feature: the in-store QR channel never carries one.
|
|
110
|
+
*/
|
|
111
|
+
let browserMarketId: number | null = null;
|
|
112
|
+
|
|
113
|
+
export function setBrowserMarketId(marketId: number | null): void {
|
|
114
|
+
if (typeof window === 'undefined') return;
|
|
115
|
+
browserMarketId = parseMarketId(marketId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function getBrowserMarketId(): number | null {
|
|
119
|
+
return typeof window === 'undefined' || getActivePlatform() !== 'storefront' ? null : browserMarketId;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Normalises the `store/markets` payload; anything malformed is dropped, so a bad row never reaches a picker. */
|
|
123
|
+
export function normalizeStoreMarkets(input: unknown): StoreMarket[] {
|
|
124
|
+
if (!Array.isArray(input)) return [];
|
|
125
|
+
const markets: StoreMarket[] = [];
|
|
126
|
+
for (const row of input as Array<Record<string, unknown> | null>) {
|
|
127
|
+
const id = parseMarketId(row?.id);
|
|
128
|
+
const currency = typeof row?.currency === 'string' ? row.currency.trim().toUpperCase() : '';
|
|
129
|
+
if (id === null || !/^[A-Z]{3}$/.test(currency) || typeof row?.name !== 'string' || row.name === '') continue;
|
|
130
|
+
markets.push({
|
|
131
|
+
id,
|
|
132
|
+
name: row.name,
|
|
133
|
+
countries: Array.isArray(row.countries) ? row.countries.filter((c): c is string => typeof c === 'string') : [],
|
|
134
|
+
currency,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return markets;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The refusal codes the market gates answer with, to branch on instead of a message. */
|
|
141
|
+
export const MARKET_ERROR_CODES = [
|
|
142
|
+
'market_rate_changed',
|
|
143
|
+
'market_unavailable',
|
|
144
|
+
'market_payment_unavailable',
|
|
145
|
+
'market_price_unusable',
|
|
146
|
+
'market_method_unavailable',
|
|
147
|
+
'market_delivery_unavailable',
|
|
148
|
+
'market_cart_required',
|
|
149
|
+
// A market order older than markets.order_rate_ttl_hours: GET orders/{id}/pay answers 422, no link, order again.
|
|
150
|
+
'market_rate_expired',
|
|
151
|
+
// 422 at the quote and the order (and cart `market.unavailable`): the market total is zero or below the provider minimum.
|
|
152
|
+
'market_total_unusable',
|
|
153
|
+
] as const;
|
|
154
|
+
|
|
155
|
+
export type MarketErrorCode = (typeof MARKET_ERROR_CODES)[number];
|
|
156
|
+
|
|
157
|
+
/** The market refusal an API failure carries, with its body data (`market` block, fresh `quote`), or null. */
|
|
158
|
+
export interface MarketRefusal {
|
|
159
|
+
code: MarketErrorCode;
|
|
160
|
+
status: number;
|
|
161
|
+
message: string;
|
|
162
|
+
/** `data.market`: the market block with a NEW pin on a 409 `market_rate_changed`; null on `market_unavailable`. */
|
|
163
|
+
market: { id: number; name: string; currency: string; pin: string } | null;
|
|
164
|
+
/** `data.quote`: the cart priced at the current rate, when the API could price it. */
|
|
165
|
+
quote: Record<string, unknown> | null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function asMarketRefusal(error: unknown): MarketRefusal | null {
|
|
169
|
+
const failure = error as { status?: unknown; code?: unknown; message?: unknown; details?: unknown; data?: unknown } | null;
|
|
170
|
+
if (!failure || typeof failure !== 'object') return null;
|
|
171
|
+
|
|
172
|
+
const code = MARKET_ERROR_CODES.find((candidate) => candidate === failure.code);
|
|
173
|
+
if (!code || typeof failure.status !== 'number') return null;
|
|
174
|
+
|
|
175
|
+
const body = (failure.details ?? failure.data ?? null) as { market?: unknown; quote?: unknown } | null;
|
|
176
|
+
const market = body?.market as { id?: unknown; name?: unknown; currency?: unknown; pin?: unknown } | null | undefined;
|
|
177
|
+
const marketId = parseMarketId(market?.id);
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
code,
|
|
181
|
+
status: failure.status,
|
|
182
|
+
message: typeof failure.message === 'string' ? failure.message : '',
|
|
183
|
+
market:
|
|
184
|
+
marketId !== null && typeof market?.name === 'string' && typeof market?.currency === 'string' && typeof market?.pin === 'string'
|
|
185
|
+
? { id: marketId, name: market.name, currency: market.currency, pin: market.pin }
|
|
186
|
+
: null,
|
|
187
|
+
quote: body?.quote && typeof body.quote === 'object' ? (body.quote as Record<string, unknown>) : null,
|
|
188
|
+
};
|
|
189
|
+
}
|