@eldrajs/sdk 0.4.0 → 0.4.2
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 +18 -0
- package/README.md +37 -0
- package/dist/{client-DLMV5fcx.js → client-C7hMOeGA.js} +4 -3
- package/dist/date-DzIUeT6Y.js +12215 -0
- package/dist/format/date.js +1 -12210
- package/dist/format/intl.d.ts +21 -0
- package/dist/format/intl.js +159 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/{types-CiA-DRIp.d.ts → types-BN7Y-C9T.d.ts} +11 -8
- package/dist/vite.d.ts +2 -1
- package/dist/vite.js +14 -4
- package/package.json +6 -2
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type Restore = () => void;
|
|
2
|
+
export interface InstallIcelandicIntlOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Patch even where the runtime has Icelandic data (Node, Safari, Firefox), so every runtime prints
|
|
5
|
+
* the same Icelandic. Off by default: there the runtime's own Icelandic is left alone.
|
|
6
|
+
*/
|
|
7
|
+
force?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Formats Icelandic from this package's data wherever the runtime has none, and leaves every other
|
|
11
|
+
* locale to the runtime. Call it once, before anything formats (a Nuxt client plugin, the top of
|
|
12
|
+
* `main.ts`). Returns a function that puts the runtime's own `Intl` back.
|
|
13
|
+
*
|
|
14
|
+
* Covers numbers and dates. Dates in a zone other than UTC (Iceland's) stay with the runtime, since
|
|
15
|
+
* the bundled data carries no time zones. Lists, relative times, names and sorting are not patched:
|
|
16
|
+
* use `listFormat`, `relativeTimeFormat`, `displayNames` and `comparator` for those.
|
|
17
|
+
*
|
|
18
|
+
* Only the call that installs the patch can remove it, once; a call made while it is installed
|
|
19
|
+
* changes nothing and returns a function that does nothing.
|
|
20
|
+
*/
|
|
21
|
+
export declare function installIcelandicIntl(options?: InstallIcelandicIntlOptions): Restore;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { n as isUtcZone, t as dateTimeFormat } from "../date-DzIUeT6Y.js";
|
|
2
|
+
import { n as numberFormat } from "../number-nkpzAc71.js";
|
|
3
|
+
// src/format/intl.ts
|
|
4
|
+
/**
|
|
5
|
+
* `installIcelandicIntl()` — Icelandic for code you do not control.
|
|
6
|
+
*
|
|
7
|
+
* `@eldrajs/sdk/format`'s functions are the way to format text you write. A date picker, vue-i18n's
|
|
8
|
+
* `n()`/`d()` or a chart library calls `Intl` itself, and where the runtime has no Icelandic data
|
|
9
|
+
* (Chrome, Edge, Cloudflare Workers) it prints English. This patches `Intl.NumberFormat`,
|
|
10
|
+
* `Intl.DateTimeFormat`, `Number.prototype.toLocaleString` and the three `Date.prototype.toLocale*`
|
|
11
|
+
* methods so that a request for Icelandic is formatted from this package's own data, and every other
|
|
12
|
+
* locale is left to the runtime exactly as before. Unlike a polyfill that replaces `Intl`, nothing
|
|
13
|
+
* changes for a browser on `en-GB` or `de-DE`, and there is no default locale to get wrong.
|
|
14
|
+
*/
|
|
15
|
+
const INSTALLED = Symbol.for("@eldrajs/sdk/format/intl");
|
|
16
|
+
/** Whether the runtime would format `locales` as Icelandic if it could: an Icelandic tag before any tag it supports. */
|
|
17
|
+
function wantsIcelandic(Runtime, locales) {
|
|
18
|
+
if (locales === void 0) return false;
|
|
19
|
+
for (const tag of Intl.getCanonicalLocales(locales)) {
|
|
20
|
+
if (/^is(-|$)/i.test(tag)) return true;
|
|
21
|
+
if (Runtime.supportedLocalesOf([tag]).length > 0) return false;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
function supportedLocalesOf(Runtime) {
|
|
26
|
+
return (locales, options) => {
|
|
27
|
+
const supported = new Set(Runtime.supportedLocalesOf(locales, options));
|
|
28
|
+
return Intl.getCanonicalLocales(locales).filter((tag) => supported.has(tag) || /^is(-|$)/i.test(tag));
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const DATE_FIELDS = [
|
|
32
|
+
"weekday",
|
|
33
|
+
"year",
|
|
34
|
+
"month",
|
|
35
|
+
"day"
|
|
36
|
+
];
|
|
37
|
+
const TIME_FIELDS = [
|
|
38
|
+
"dayPeriod",
|
|
39
|
+
"hour",
|
|
40
|
+
"minute",
|
|
41
|
+
"second",
|
|
42
|
+
"fractionalSecondDigits"
|
|
43
|
+
];
|
|
44
|
+
/** ECMA-402's ToDateTimeOptions: the fields `toLocale{Date,Time,}String` add when none are asked for. */
|
|
45
|
+
function withDefaults(options, required, defaults) {
|
|
46
|
+
const resolved = { ...options };
|
|
47
|
+
if (required === "date" && resolved.timeStyle !== void 0) throw new TypeError("toLocaleDateString does not accept timeStyle");
|
|
48
|
+
if (required === "time" && resolved.dateStyle !== void 0) throw new TypeError("toLocaleTimeString does not accept dateStyle");
|
|
49
|
+
const has = (fields) => fields.some((field) => resolved[field] !== void 0);
|
|
50
|
+
let needDefaults = true;
|
|
51
|
+
if ((required === "date" || required === "any") && has(DATE_FIELDS)) needDefaults = false;
|
|
52
|
+
if ((required === "time" || required === "any") && has(TIME_FIELDS)) needDefaults = false;
|
|
53
|
+
if (resolved.dateStyle !== void 0 || resolved.timeStyle !== void 0) needDefaults = false;
|
|
54
|
+
if (!needDefaults) return resolved;
|
|
55
|
+
if (defaults === "date" || defaults === "all") Object.assign(resolved, {
|
|
56
|
+
year: "numeric",
|
|
57
|
+
month: "numeric",
|
|
58
|
+
day: "numeric"
|
|
59
|
+
});
|
|
60
|
+
if (defaults === "time" || defaults === "all") Object.assign(resolved, {
|
|
61
|
+
hour: "numeric",
|
|
62
|
+
minute: "numeric",
|
|
63
|
+
second: "numeric"
|
|
64
|
+
});
|
|
65
|
+
return resolved;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Formats Icelandic from this package's data wherever the runtime has none, and leaves every other
|
|
69
|
+
* locale to the runtime. Call it once, before anything formats (a Nuxt client plugin, the top of
|
|
70
|
+
* `main.ts`). Returns a function that puts the runtime's own `Intl` back.
|
|
71
|
+
*
|
|
72
|
+
* Covers numbers and dates. Dates in a zone other than UTC (Iceland's) stay with the runtime, since
|
|
73
|
+
* the bundled data carries no time zones. Lists, relative times, names and sorting are not patched:
|
|
74
|
+
* use `listFormat`, `relativeTimeFormat`, `displayNames` and `comparator` for those.
|
|
75
|
+
*
|
|
76
|
+
* Only the call that installs the patch can remove it, once; a call made while it is installed
|
|
77
|
+
* changes nothing and returns a function that does nothing.
|
|
78
|
+
*/
|
|
79
|
+
function installIcelandicIntl(options = {}) {
|
|
80
|
+
const intl = Intl;
|
|
81
|
+
const RuntimeNumberFormat = Intl.NumberFormat;
|
|
82
|
+
const RuntimeDateTimeFormat = Intl.DateTimeFormat;
|
|
83
|
+
if (RuntimeNumberFormat[INSTALLED]) return () => {};
|
|
84
|
+
if (RuntimeNumberFormat.supportedLocalesOf(["is"]).length > 0 && RuntimeDateTimeFormat.supportedLocalesOf(["is"]).length > 0 && !options.force) return () => {};
|
|
85
|
+
/**
|
|
86
|
+
* The zone a date formatter would use: the one asked for, else the runtime's own, read on every
|
|
87
|
+
* call because it can change while the page is open.
|
|
88
|
+
*/
|
|
89
|
+
const zoneOf = (dateOptions) => dateOptions?.timeZone ?? new RuntimeDateTimeFormat().resolvedOptions().timeZone;
|
|
90
|
+
const icelandicNumbers = (locales) => wantsIcelandic(RuntimeNumberFormat, locales);
|
|
91
|
+
/** The UTC zone to format an Icelandic date in, or `undefined` when the runtime keeps it. */
|
|
92
|
+
const icelandicZone = (locales, dateOptions) => {
|
|
93
|
+
if (!wantsIcelandic(RuntimeDateTimeFormat, locales)) return void 0;
|
|
94
|
+
const zone = zoneOf(dateOptions);
|
|
95
|
+
return isUtcZone(zone) ? zone : void 0;
|
|
96
|
+
};
|
|
97
|
+
/** The formatters this patch hands out, so `instanceof` recognises them as well as native ones. */
|
|
98
|
+
const bundled = /* @__PURE__ */ new WeakSet();
|
|
99
|
+
const handOut = (formatter) => {
|
|
100
|
+
bundled.add(formatter);
|
|
101
|
+
return formatter;
|
|
102
|
+
};
|
|
103
|
+
const icelandicDateFormat = (dateOptions, zone) => handOut(dateTimeFormat("is", {
|
|
104
|
+
...dateOptions,
|
|
105
|
+
timeZone: zone
|
|
106
|
+
}));
|
|
107
|
+
const NumberFormat = function NumberFormat(locales, numberOptions) {
|
|
108
|
+
if (icelandicNumbers(locales)) return handOut(numberFormat("is", numberOptions));
|
|
109
|
+
return Reflect.construct(RuntimeNumberFormat, [locales, numberOptions], new.target ?? RuntimeNumberFormat);
|
|
110
|
+
};
|
|
111
|
+
Object.defineProperties(NumberFormat, {
|
|
112
|
+
prototype: { value: RuntimeNumberFormat.prototype },
|
|
113
|
+
supportedLocalesOf: { value: supportedLocalesOf(RuntimeNumberFormat) },
|
|
114
|
+
[Symbol.hasInstance]: { value: (value) => value instanceof RuntimeNumberFormat || typeof value === "object" && value !== null && bundled.has(value) }
|
|
115
|
+
});
|
|
116
|
+
const DateTimeFormat = function DateTimeFormat(locales, dateOptions) {
|
|
117
|
+
const zone = icelandicZone(locales, dateOptions);
|
|
118
|
+
if (zone !== void 0) return icelandicDateFormat(dateOptions, zone);
|
|
119
|
+
return Reflect.construct(RuntimeDateTimeFormat, [locales, dateOptions], new.target ?? RuntimeDateTimeFormat);
|
|
120
|
+
};
|
|
121
|
+
Object.defineProperties(DateTimeFormat, {
|
|
122
|
+
prototype: { value: RuntimeDateTimeFormat.prototype },
|
|
123
|
+
supportedLocalesOf: { value: supportedLocalesOf(RuntimeDateTimeFormat) },
|
|
124
|
+
[Symbol.hasInstance]: { value: (value) => value instanceof RuntimeDateTimeFormat || typeof value === "object" && value !== null && bundled.has(value) }
|
|
125
|
+
});
|
|
126
|
+
NumberFormat[INSTALLED] = true;
|
|
127
|
+
const numberToLocaleString = Number.prototype.toLocaleString;
|
|
128
|
+
const numberValueOf = Number.prototype.valueOf;
|
|
129
|
+
const dateGetTime = Date.prototype.getTime;
|
|
130
|
+
const dateToLocaleString = Date.prototype.toLocaleString;
|
|
131
|
+
const dateToLocaleDateString = Date.prototype.toLocaleDateString;
|
|
132
|
+
const dateToLocaleTimeString = Date.prototype.toLocaleTimeString;
|
|
133
|
+
Number.prototype.toLocaleString = function toLocaleString(locales, numberOptions) {
|
|
134
|
+
const value = numberValueOf.call(this);
|
|
135
|
+
return icelandicNumbers(locales) ? numberFormat("is", numberOptions).format(value) : numberToLocaleString.call(this, locales, numberOptions);
|
|
136
|
+
};
|
|
137
|
+
const patchDate = (original, required, defaults) => function(locales, dateOptions) {
|
|
138
|
+
const zone = Number.isNaN(dateGetTime.call(this)) ? void 0 : icelandicZone(locales, dateOptions);
|
|
139
|
+
if (zone === void 0) return original.call(this, locales, dateOptions);
|
|
140
|
+
return icelandicDateFormat(withDefaults(dateOptions, required, defaults), zone).format(this);
|
|
141
|
+
};
|
|
142
|
+
Date.prototype.toLocaleString = patchDate(dateToLocaleString, "any", "all");
|
|
143
|
+
Date.prototype.toLocaleDateString = patchDate(dateToLocaleDateString, "date", "date");
|
|
144
|
+
Date.prototype.toLocaleTimeString = patchDate(dateToLocaleTimeString, "time", "time");
|
|
145
|
+
intl.NumberFormat = NumberFormat;
|
|
146
|
+
intl.DateTimeFormat = DateTimeFormat;
|
|
147
|
+
let restored = false;
|
|
148
|
+
return () => {
|
|
149
|
+
if (restored) return;
|
|
150
|
+
restored = true;
|
|
151
|
+
intl.NumberFormat = RuntimeNumberFormat;
|
|
152
|
+
intl.DateTimeFormat = RuntimeDateTimeFormat;
|
|
153
|
+
Number.prototype.toLocaleString = numberToLocaleString;
|
|
154
|
+
Date.prototype.toLocaleString = dateToLocaleString;
|
|
155
|
+
Date.prototype.toLocaleDateString = dateToLocaleDateString;
|
|
156
|
+
Date.prototype.toLocaleTimeString = dateToLocaleTimeString;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export { installIcelandicIntl };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ as EldraOrganizationFeature, A as EldraFeature, At as
|
|
1
|
+
import { $ as EldraOrganizationFeature, A as EldraFeature, At as EldraContractItem, B as EldraFormSubmitResult, Bt as clearCampaignAttribution, C as EldraCmsResolveEntryListResponse, Ct as EldraStockAvailability, D as EldraCollectionListOptions, Dt as RuntimeValue, E as EldraCollectionList, Et as RuntimeEnv, F as EldraFormField, Ft as EldraContractQuery, G as EldraInventoryClient, Gt as EldraCartSessionOptions, H as EldraFormsClient, Ht as readCampaignAttribution, I as EldraFormFieldOption, It as EldraContractResponse, J as EldraOrderLine, Jt as EldraOrderAccessTokens, K as EldraLocaleOptions, Kt as EldraKeyValueStorage, L as EldraFormFieldType, Lt as EldraCampaignAttribution, M as EldraFeatureClient, Mt as EldraContractPath, N as EldraFormActionKind, Nt as EldraContractPaths, O as EldraCollectionProductsOptions, Ot as EldraContract, P as EldraFormDefinition, Pt as EldraContractProp, Q as EldraOrganizationDetails, R as EldraFormLocalized, Rt as EldraCampaignAttributionOptions, S as EldraCmsResolveEntryListOptions, St as EldraSearchResponse, T as EldraCollection, Tt as EldraStockAvailabilityItem, U as EldraHttpClient, Ut as withdrawCampaignAttributionConsent, V as EldraFormTokenResult, Vt as grantCampaignAttributionConsent, W as EldraHttpRequest, Wt as EldraCartSession, X as EldraOrdersClient, Xt as createOrderAccessTokens, Y as EldraOrderReadOptions, Yt as createCartSession, Z as EldraOrganizationCommerce, _ as EldraCmsEntryListEntry, _t as EldraRecoveredBasketItem, a as EldraCartTotals, at as EldraPlatformConfig, b as EldraCmsGetOptions, bt as EldraRetryOptions, c as EldraCatalogListProductsOptions, ct as EldraProductExtensions, d as EldraCheckoutUrlOptions, dt as EldraProductMediaLink, et as EldraOrganizationLocales, f as EldraClient, ft as EldraProductOption, g as EldraCmsEntryListDisplayMode, gt as EldraRecoveredBasket, h as EldraCmsEntryList, ht as EldraProductVariant, i as EldraCartItem, it as EldraPlatformClient, j as EldraFeatureCapabilities, jt as EldraContractMethod, k as EldraDiscountResult, kt as EldraContractBody, l as EldraCategory, lt as EldraProductList, m as EldraCmsClient, mt as EldraProductThumbnail, n as EldraCart, nt as EldraPageMeta, o as EldraCatalogClient, ot as EldraPlatformReadOptions, p as EldraClientOptions, pt as EldraProductOptionValue, q as EldraOrder, qt as EldraOrderAccessTokenOptions, r as EldraCartClient, rt as EldraPaginated, s as EldraCatalogGetProductOptions, st as EldraProductDetails, t as EldraAddCartItemInput, tt as EldraOrganizationOptions, u as EldraCheckoutClient, ut as EldraProductListItem, v as EldraCmsEntryListFilterCriterion, vt as EldraRequestContext, w as EldraCmsResolvedEntryList, wt as EldraStockAvailabilityInput, x as EldraCmsListOptions, xt as EldraSearchOptions, y as EldraCmsGetEntryByUniqueFieldOptions, yt as EldraRequestOptions, z as EldraFormSubmitInput, zt as captureCampaignAttribution } from "./types-BN7Y-C9T.js";
|
|
2
2
|
export declare const DEFAULT_ELDRA_API_BASE_URL = "https://web.eldra.app/api";
|
|
3
3
|
export declare class EldraHttpError extends Error {
|
|
4
4
|
readonly status: number;
|
|
@@ -114,4 +114,4 @@ interface EldraResponsiveImage {
|
|
|
114
114
|
* as `src` and no `srcset`. Returns `undefined` when there is no URL to render.
|
|
115
115
|
*/
|
|
116
116
|
export declare function responsiveImage(source: EldraImageSource, options: EldraResponsiveImageOptions): EldraResponsiveImage | undefined;
|
|
117
|
-
export { type EldraAddCartItemInput, type EldraCampaignAttribution, type EldraCampaignAttributionOptions, type EldraCart, type EldraCartClient, type EldraCartItem, type EldraCartSession, type EldraCartSessionOptions, type EldraCartTotals, type EldraCatalogClient, type EldraCatalogGetProductOptions, type EldraCatalogListProductsOptions, type EldraCategory, type EldraCheckoutClient, type EldraCheckoutUrlOptions, type EldraClient, type EldraClientOptions, type EldraCmsClient, type EldraCmsEntryList, type EldraCmsEntryListDisplayMode, type EldraCmsEntryListEntry, type EldraCmsEntryListFilterCriterion, type EldraCmsGetEntryByUniqueFieldOptions, type EldraCmsGetOptions, type EldraCmsListOptions, type EldraCmsResolveEntryListOptions, type EldraCmsResolveEntryListResponse, type EldraCmsResolvedEntryList, type EldraCollection, type EldraCollectionList, type EldraCollectionListOptions, type EldraCollectionProductsOptions, type EldraContract, type EldraContractBody, type EldraContractItem, type EldraContractMethod, type EldraContractPath, type EldraContractPaths, type EldraContractProp, type EldraContractQuery, type EldraContractResponse, type EldraDiscountResult, type EldraFeature, type EldraFeatureCapabilities, type EldraFeatureClient, type EldraFormActionKind, type EldraFormDefinition, type EldraFormField, type EldraFormFieldOption, type EldraFormFieldType, type EldraFormLocalized, type EldraFormSubmitInput, type EldraFormSubmitResult, type EldraFormTokenResult, type EldraFormsClient, type EldraHttpClient, type EldraHttpRequest, type EldraImageSource, type EldraImageSrcsetOptions, type EldraImageVariant, type EldraInventoryClient, type EldraKeyValueStorage, type EldraLocaleOptions, type EldraOrder, type EldraOrderAccessTokenOptions, type EldraOrderAccessTokens, type EldraOrderLine, type EldraOrderReadOptions, type EldraOrdersClient, type EldraOrganizationCommerce, type EldraOrganizationDetails, type EldraOrganizationFeature, type EldraOrganizationLocales, type EldraOrganizationOptions, type EldraPageMeta, type EldraPaginated, type EldraPlatformClient, type EldraPlatformConfig, type EldraPlatformReadOptions, type EldraProductDetails, type EldraProductList, type EldraProductListItem, type EldraProductMediaLink, type EldraProductOption, type EldraProductOptionValue, type EldraProductThumbnail, type EldraProductVariant, type EldraRecoveredBasket, type EldraRecoveredBasketItem, type EldraRequestContext, type EldraRequestOptions, type EldraResponsiveImage, type EldraResponsiveImageOptions, type EldraRetryOptions, type EldraSearchOptions, type EldraSearchResponse, type EldraSizedImageVariant, type EldraStockAvailability, type EldraStockAvailabilityInput, type EldraStockAvailabilityItem, type EldraTrackerScript, type EldraTrackerScriptOptions, type RuntimeEnv, type RuntimeValue, captureCampaignAttribution, clearCampaignAttribution, createCartSession, createOrderAccessTokens, grantCampaignAttributionConsent, readCampaignAttribution, withdrawCampaignAttributionConsent };
|
|
117
|
+
export { type EldraAddCartItemInput, type EldraCampaignAttribution, type EldraCampaignAttributionOptions, type EldraCart, type EldraCartClient, type EldraCartItem, type EldraCartSession, type EldraCartSessionOptions, type EldraCartTotals, type EldraCatalogClient, type EldraCatalogGetProductOptions, type EldraCatalogListProductsOptions, type EldraCategory, type EldraCheckoutClient, type EldraCheckoutUrlOptions, type EldraClient, type EldraClientOptions, type EldraCmsClient, type EldraCmsEntryList, type EldraCmsEntryListDisplayMode, type EldraCmsEntryListEntry, type EldraCmsEntryListFilterCriterion, type EldraCmsGetEntryByUniqueFieldOptions, type EldraCmsGetOptions, type EldraCmsListOptions, type EldraCmsResolveEntryListOptions, type EldraCmsResolveEntryListResponse, type EldraCmsResolvedEntryList, type EldraCollection, type EldraCollectionList, type EldraCollectionListOptions, type EldraCollectionProductsOptions, type EldraContract, type EldraContractBody, type EldraContractItem, type EldraContractMethod, type EldraContractPath, type EldraContractPaths, type EldraContractProp, type EldraContractQuery, type EldraContractResponse, type EldraDiscountResult, type EldraFeature, type EldraFeatureCapabilities, type EldraFeatureClient, type EldraFormActionKind, type EldraFormDefinition, type EldraFormField, type EldraFormFieldOption, type EldraFormFieldType, type EldraFormLocalized, type EldraFormSubmitInput, type EldraFormSubmitResult, type EldraFormTokenResult, type EldraFormsClient, type EldraHttpClient, type EldraHttpRequest, type EldraImageSource, type EldraImageSrcsetOptions, type EldraImageVariant, type EldraInventoryClient, type EldraKeyValueStorage, type EldraLocaleOptions, type EldraOrder, type EldraOrderAccessTokenOptions, type EldraOrderAccessTokens, type EldraOrderLine, type EldraOrderReadOptions, type EldraOrdersClient, type EldraOrganizationCommerce, type EldraOrganizationDetails, type EldraOrganizationFeature, type EldraOrganizationLocales, type EldraOrganizationOptions, type EldraPageMeta, type EldraPaginated, type EldraPlatformClient, type EldraPlatformConfig, type EldraPlatformReadOptions, type EldraProductDetails, type EldraProductExtensions, type EldraProductList, type EldraProductListItem, type EldraProductMediaLink, type EldraProductOption, type EldraProductOptionValue, type EldraProductThumbnail, type EldraProductVariant, type EldraRecoveredBasket, type EldraRecoveredBasketItem, type EldraRequestContext, type EldraRequestOptions, type EldraResponsiveImage, type EldraResponsiveImageOptions, type EldraRetryOptions, type EldraSearchOptions, type EldraSearchResponse, type EldraSizedImageVariant, type EldraStockAvailability, type EldraStockAvailabilityInput, type EldraStockAvailabilityItem, type EldraTrackerScript, type EldraTrackerScriptOptions, type RuntimeEnv, type RuntimeValue, captureCampaignAttribution, clearCampaignAttribution, createCartSession, createOrderAccessTokens, grantCampaignAttributionConsent, readCampaignAttribution, withdrawCampaignAttributionConsent };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as initEldraClient, c as captureCampaignAttribution, d as readCampaignAttribution, f as withdrawCampaignAttributionConsent, i as getEldraClient, l as clearCampaignAttribution, m as createOrderAccessTokens, n as EldraHttpError, o as resolveApiBaseUrl, p as createCartSession, r as createEldraClient, s as resolveOrgId, t as DEFAULT_ELDRA_API_BASE_URL, u as grantCampaignAttributionConsent } from "./client-
|
|
1
|
+
import { a as initEldraClient, c as captureCampaignAttribution, d as readCampaignAttribution, f as withdrawCampaignAttributionConsent, i as getEldraClient, l as clearCampaignAttribution, m as createOrderAccessTokens, n as EldraHttpError, o as resolveApiBaseUrl, p as createCartSession, r as createEldraClient, s as resolveOrgId, t as DEFAULT_ELDRA_API_BASE_URL, u as grantCampaignAttributionConsent } from "./client-C7hMOeGA.js";
|
|
2
2
|
// src/tracker.ts
|
|
3
3
|
/**
|
|
4
4
|
* Builds the tracker `<script>` attributes from the same configuration the client uses, so the
|
|
@@ -62,10 +62,10 @@ interface EldraContract {}
|
|
|
62
62
|
type Paths = EldraContract extends {
|
|
63
63
|
paths: infer P;
|
|
64
64
|
} ? P : never;
|
|
65
|
-
type Generated = [Paths] extends [never] ? false : true;
|
|
65
|
+
type Generated$1 = [Paths] extends [never] ? false : true;
|
|
66
66
|
type EldraContractPaths = Paths;
|
|
67
|
-
type EldraContractPath = Generated extends true ? keyof Paths : string;
|
|
68
|
-
type EldraContractMethod<Path extends EldraContractPath> = Generated extends true ? keyof Paths[Path & keyof Paths] : string;
|
|
67
|
+
type EldraContractPath = Generated$1 extends true ? keyof Paths : string;
|
|
68
|
+
type EldraContractMethod<Path extends EldraContractPath> = Generated$1 extends true ? keyof Paths[Path & keyof Paths] : string;
|
|
69
69
|
type Operation<Path, Method> = Path extends keyof Paths ? Method extends keyof Paths[Path] ? Paths[Path][Method] : never : never;
|
|
70
70
|
type JsonContent<Response> = Response extends {
|
|
71
71
|
content: {
|
|
@@ -77,13 +77,13 @@ type SuccessResponse<Responses> = Responses extends {
|
|
|
77
77
|
} ? JsonContent<Response> : Responses extends {
|
|
78
78
|
201: infer Response;
|
|
79
79
|
} ? JsonContent<Response> : never;
|
|
80
|
-
type EldraContractResponse<Path extends EldraContractPath, Method extends EldraContractMethod<Path>> = Generated extends true ? Operation<Path, Method> extends {
|
|
80
|
+
type EldraContractResponse<Path extends EldraContractPath, Method extends EldraContractMethod<Path>> = Generated$1 extends true ? Operation<Path, Method> extends {
|
|
81
81
|
responses: infer Responses;
|
|
82
82
|
} ? SuccessResponse<Responses> : never : unknown;
|
|
83
|
-
type EldraContractBody<Path extends EldraContractPath, Method extends EldraContractMethod<Path>> = Generated extends true ? Operation<Path, Method> extends {
|
|
83
|
+
type EldraContractBody<Path extends EldraContractPath, Method extends EldraContractMethod<Path>> = Generated$1 extends true ? Operation<Path, Method> extends {
|
|
84
84
|
requestBody?: infer Body;
|
|
85
85
|
} ? JsonContent<NonNullable<Body>> : never : Record<string, unknown>;
|
|
86
|
-
type EldraContractQuery<Path extends EldraContractPath, Method extends EldraContractMethod<Path>> = Generated extends true ? Operation<Path, Method> extends {
|
|
86
|
+
type EldraContractQuery<Path extends EldraContractPath, Method extends EldraContractMethod<Path>> = Generated$1 extends true ? Operation<Path, Method> extends {
|
|
87
87
|
parameters: {
|
|
88
88
|
query?: infer Query;
|
|
89
89
|
};
|
|
@@ -301,7 +301,10 @@ interface EldraCmsClient {
|
|
|
301
301
|
}
|
|
302
302
|
type EldraProductList = EldraContractResponse<'/catalog/v1/products/list', 'get'>;
|
|
303
303
|
type EldraProductListItem = EldraContractItem<EldraContractProp<EldraProductList, 'data'>>;
|
|
304
|
-
|
|
304
|
+
interface EldraProductExtensions {}
|
|
305
|
+
type EldraProductDetails = EldraContractResponse<'/catalog/v1/products/{productId}', 'get'> extends (infer Generated) ? unknown extends Generated ? unknown : Omit<Generated, 'extensions'> & {
|
|
306
|
+
extensions?: EldraProductExtensions;
|
|
307
|
+
} : never;
|
|
305
308
|
type EldraProductVariant = EldraContractItem<EldraContractProp<EldraProductDetails, 'variants'>>;
|
|
306
309
|
type EldraProductOption = EldraContractItem<EldraContractProp<EldraProductDetails, 'options'>>;
|
|
307
310
|
type EldraProductOptionValue = EldraContractItem<EldraContractProp<EldraProductOption, 'values'>>;
|
|
@@ -547,4 +550,4 @@ interface EldraFormsClient {
|
|
|
547
550
|
/** `POST /site/v1/forms/subscriptions/unsubscribe?token=` (a GET of the link is only a page). */
|
|
548
551
|
unsubscribe(token: string, context?: EldraRequestContext): Promise<EldraFormTokenResult>;
|
|
549
552
|
}
|
|
550
|
-
export { EldraOrganizationFeature as $, EldraFeature as A,
|
|
553
|
+
export { EldraOrganizationFeature as $, EldraFeature as A, EldraContractItem as At, EldraFormSubmitResult as B, clearCampaignAttribution as Bt, EldraCmsResolveEntryListResponse as C, EldraStockAvailability as Ct, EldraCollectionListOptions as D, RuntimeValue as Dt, EldraCollectionList as E, RuntimeEnv as Et, EldraFormField as F, EldraContractQuery as Ft, EldraInventoryClient as G, EldraCartSessionOptions as Gt, EldraFormsClient as H, readCampaignAttribution as Ht, EldraFormFieldOption as I, EldraContractResponse as It, EldraOrderLine as J, EldraOrderAccessTokens as Jt, EldraLocaleOptions as K, EldraKeyValueStorage as Kt, EldraFormFieldType as L, EldraCampaignAttribution as Lt, EldraFeatureClient as M, EldraContractPath as Mt, EldraFormActionKind as N, EldraContractPaths as Nt, EldraCollectionProductsOptions as O, EldraContract as Ot, EldraFormDefinition as P, EldraContractProp as Pt, EldraOrganizationDetails as Q, EldraFormLocalized as R, EldraCampaignAttributionOptions as Rt, EldraCmsResolveEntryListOptions as S, EldraSearchResponse as St, EldraCollection as T, EldraStockAvailabilityItem as Tt, EldraHttpClient as U, withdrawCampaignAttributionConsent as Ut, EldraFormTokenResult as V, grantCampaignAttributionConsent as Vt, EldraHttpRequest as W, EldraCartSession as Wt, EldraOrdersClient as X, createOrderAccessTokens as Xt, EldraOrderReadOptions as Y, createCartSession as Yt, EldraOrganizationCommerce as Z, EldraCmsEntryListEntry as _, EldraRecoveredBasketItem as _t, EldraCartTotals as a, EldraPlatformConfig as at, EldraCmsGetOptions as b, EldraRetryOptions as bt, EldraCatalogListProductsOptions as c, EldraProductExtensions as ct, EldraCheckoutUrlOptions as d, EldraProductMediaLink as dt, EldraOrganizationLocales as et, EldraClient as f, EldraProductOption as ft, EldraCmsEntryListDisplayMode as g, EldraRecoveredBasket as gt, EldraCmsEntryList as h, EldraProductVariant as ht, EldraCartItem as i, EldraPlatformClient as it, EldraFeatureCapabilities as j, EldraContractMethod as jt, EldraDiscountResult as k, EldraContractBody as kt, EldraCategory as l, EldraProductList as lt, EldraCmsClient as m, EldraProductThumbnail as mt, EldraCart as n, EldraPageMeta as nt, EldraCatalogClient as o, EldraPlatformReadOptions as ot, EldraClientOptions as p, EldraProductOptionValue as pt, EldraOrder as q, EldraOrderAccessTokenOptions as qt, EldraCartClient as r, EldraPaginated as rt, EldraCatalogGetProductOptions as s, EldraProductDetails as st, EldraAddCartItemInput as t, EldraOrganizationOptions as tt, EldraCheckoutClient as u, EldraProductListItem as ut, EldraCmsEntryListFilterCriterion as v, EldraRequestContext as vt, EldraCmsResolvedEntryList as w, EldraStockAvailabilityInput as wt, EldraCmsListOptions as x, EldraSearchOptions as xt, EldraCmsGetEntryByUniqueFieldOptions as y, EldraRequestOptions as yt, EldraFormSubmitInput as z, captureCampaignAttribution as zt };
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Dt as RuntimeValue, Et as RuntimeEnv } from "./types-BN7Y-C9T.js";
|
|
2
2
|
import { Plugin } from "vite";
|
|
3
3
|
export interface EldraVitePluginOptions {
|
|
4
4
|
apiBaseUrl?: RuntimeValue<string>;
|
|
@@ -32,6 +32,7 @@ export interface EldraGenerationResult {
|
|
|
32
32
|
}
|
|
33
33
|
export declare function eldra(options?: EldraVitePluginOptions): Plugin;
|
|
34
34
|
export declare function generateEldraFiles(root: string, options: EldraVitePluginOptions, mode?: string): Promise<EldraGenerationResult>;
|
|
35
|
+
export declare function retargetSdkAugmentation(source: string, sdkImport: string): string;
|
|
35
36
|
export declare function createContractSource(document: unknown, options?: {
|
|
36
37
|
sdkImport?: string;
|
|
37
38
|
source?: URL | string;
|
package/dist/vite.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./client-
|
|
1
|
+
import "./client-C7hMOeGA.js";
|
|
2
2
|
import { access, mkdir, writeFile } from "node:fs/promises";
|
|
3
3
|
import { dirname, resolve } from "node:path";
|
|
4
4
|
import openapiTS, { astToString } from "openapi-typescript";
|
|
@@ -80,7 +80,8 @@ async function generateEldraFiles(root, options, mode = process.env.NODE_ENV ??
|
|
|
80
80
|
...fetchOptions,
|
|
81
81
|
maxDepth: options.maxDepth,
|
|
82
82
|
moduleName,
|
|
83
|
-
schemas: options.schemas
|
|
83
|
+
schemas: options.schemas,
|
|
84
|
+
sdkImport
|
|
84
85
|
}));
|
|
85
86
|
const contractSource = options.contract === false ? void 0 : await attempt("Gateway contract", result, () => fetchContract(apiBaseUrl, orgId, {
|
|
86
87
|
...fetchOptions,
|
|
@@ -110,13 +111,22 @@ async function generateEldraFiles(root, options, mode = process.env.NODE_ENV ??
|
|
|
110
111
|
async function fetchCmsTypes(apiBaseUrl, orgId, options) {
|
|
111
112
|
const url = new URL(`${trimSlash(apiBaseUrl)}/cms/v1/typescript-definitions`);
|
|
112
113
|
url.searchParams.set("moduleName", options.moduleName);
|
|
114
|
+
url.searchParams.set("sdkAugmentation", "true");
|
|
113
115
|
if (options.maxDepth !== void 0) url.searchParams.set("maxDepth", String(options.maxDepth));
|
|
114
116
|
if (options.schemas && options.schemas.length > 0) url.searchParams.set("schemas", options.schemas.join(","));
|
|
115
117
|
const body = await fetchText(url, orgId, {
|
|
116
118
|
...options,
|
|
117
119
|
accept: "text/plain"
|
|
118
120
|
});
|
|
119
|
-
return body.endsWith("\n") ? body : `${body}\n
|
|
121
|
+
return retargetSdkAugmentation(body.endsWith("\n") ? body : `${body}\n`, options.sdkImport);
|
|
122
|
+
}
|
|
123
|
+
function retargetSdkAugmentation(source, sdkImport) {
|
|
124
|
+
if (sdkImport === defaultSdkImport) return source;
|
|
125
|
+
const declaration = new RegExp(`declare\\s+module\\s+(['"])${escapeRegExp(defaultSdkImport)}\\1`, "g");
|
|
126
|
+
return source.replace(declaration, (_match, quote) => `declare module ${quote}${sdkImport}${quote}`);
|
|
127
|
+
}
|
|
128
|
+
function escapeRegExp(value) {
|
|
129
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
120
130
|
}
|
|
121
131
|
async function fetchContract(apiBaseUrl, orgId, options) {
|
|
122
132
|
const url = new URL(`${trimSlash(apiBaseUrl)}/public/openapi.json`);
|
|
@@ -243,4 +253,4 @@ function stripTSExtension(path) {
|
|
|
243
253
|
function pascalTypeName(value) {
|
|
244
254
|
return value.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
245
255
|
}
|
|
246
|
-
export { createContractSource, eldra, generateEldraFiles };
|
|
256
|
+
export { createContractSource, eldra, generateEldraFiles, retargetSdkAugmentation };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eldrajs/sdk",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Framework-free client for the Eldra Web Studio public storefront API: CMS, catalog, cart, checkout handoff, orders, inventory. Types generated from your gateway's OpenAPI document and CMS schemas at build time.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cms",
|
|
@@ -55,6 +55,10 @@
|
|
|
55
55
|
"types": "./dist/format/display-names.d.ts",
|
|
56
56
|
"import": "./dist/format/display-names.js"
|
|
57
57
|
},
|
|
58
|
+
"./format/intl": {
|
|
59
|
+
"types": "./dist/format/intl.d.ts",
|
|
60
|
+
"import": "./dist/format/intl.js"
|
|
61
|
+
},
|
|
58
62
|
"./package.json": "./package.json"
|
|
59
63
|
},
|
|
60
64
|
"publishConfig": {
|
|
@@ -86,7 +90,7 @@
|
|
|
86
90
|
},
|
|
87
91
|
"scripts": {
|
|
88
92
|
"build": "tsdown",
|
|
89
|
-
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.generated.json",
|
|
93
|
+
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.generated.json && tsc -p tsconfig.generated-bare.json",
|
|
90
94
|
"test": "vitest run",
|
|
91
95
|
"fixture": "node scripts/fixture.mjs",
|
|
92
96
|
"format-data": "node scripts/extract-format-data.mjs",
|