@eldrajs/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/LICENSE +21 -0
- package/dist/client-CP-xWxRN.js +330 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +53 -0
- package/dist/types-DNiJpmSn.d.ts +6006 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +153 -0
- package/package.json +65 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Web Studio SDK changelog
|
|
2
|
+
|
|
3
|
+
`@eldrajs/sdk` is its own package from 0.1.0; entries before that are the versions of
|
|
4
|
+
`@eldra-is/vue-ui-components`, where it shipped as the `./sdk` and `./sdk/vite` entry points. This file is
|
|
5
|
+
hand-maintained: every change a consumer of the SDK can see gets a line under Unreleased in the same
|
|
6
|
+
change, naming the contract version when the contract moved. Release-please writes `RELEASE-NOTES.md`
|
|
7
|
+
from commit messages and does not describe the SDK. The contract's own history is `CONTRACT.md` in the
|
|
8
|
+
platform repository.
|
|
9
|
+
|
|
10
|
+
## 0.1.0 — 2026-09-15
|
|
11
|
+
|
|
12
|
+
- Contract snapshot 2.4.0. `inventory.availability` items no longer need a `locationId`: `EldraStockAvailabilityInput.locationId` is optional, and an omitted one is answered by the organisation's default inventory location, the same resolution the cart's reservation uses; the response still names the location that answered. A storefront needs no location id in its configuration any more.
|
|
13
|
+
- Contract snapshot 2.3.0. `PUT /shopping-cart/v1/cart/{cartID}/discount` refuses a code whose redemption limit is reached with 409 `CART_DISCOUNT_EXHAUSTED`, distinguishable from the 404 every other refusal keeps; a code that runs out while on a cart stops reducing it on the next read. No path or field changed, so no type changed; `ELDRA_CONTRACT_VERSION` is the only visible difference.
|
|
14
|
+
- Contract snapshot 2.2.0. The gateway renamed the thirty schemas whose names began with `._`: `._WebCart` is now `shoppingcartweb_WebCart`, `._CreateOrderResponse` is `orderweb_CreateOrderResponse`, `._OrderResponse` is `orderweb_OrderResponse`, `._PayOrderInBody` is `PayOrderInBody`, and the duplicated `._CollectionItem` and `._WebGuestBooking` folded into `dto_CollectionItem` and `handler_WebGuestBooking`. Nothing on the wire changed. The SDK's exported types (`EldraCart`, `EldraOrder`, `EldraContractPaths`, …) are derived from paths and resolve to the same shapes; only code that imports `contract/v1.ts` directly and names a schema sees the new names.
|
|
15
|
+
|
|
16
|
+
## 1.67.0 — 2026-09-10
|
|
17
|
+
|
|
18
|
+
- Contract snapshot 2.1.0 (6bae4dc). `orders.recover` now also answers `cartId`, absent when nothing could be added, and `unavailable`, the lines the shop no longer sells; orders carry `recoveredFromOrderId`; the organisation's settings carry `checkoutRecoveryUrl`.
|
|
19
|
+
|
|
20
|
+
## 1.66.1 — 2026-09-09
|
|
21
|
+
|
|
22
|
+
- Contract snapshot 2.0.0 (ddfbe5c). `POST /payment/v1/pay-order/{orderId}` requires the `X-Order-Token` header, the only type change; the SDK does not call that route, the hosted checkout does. The contract's behavioural changes in 2.0.0 (request limits on order creation, `ORDER_IDEMPOTENCY_CONFLICT`, `CART_NOT_FOUND` for an unknown `cartId`, `CART_DISCOUNT_TOO_MANY_ATTEMPTS`, rate limits) are in `CONTRACT.md`, not in the types.
|
|
23
|
+
- Contract snapshot 1.2.0 (7ed087e). Products, product list items and the cart carry `requiresShipping`, the only type change; order creation refuses `ORDER_SHIPPING_REQUIRED` for a cart that needs delivery and has none.
|
|
24
|
+
- Both snapshots landed as `chore(sdk)` commits, so release-please's notes for 1.66.1 do not mention them.
|
|
25
|
+
|
|
26
|
+
## 1.66.0 — 2026-09-09
|
|
27
|
+
|
|
28
|
+
- Contract snapshot 1.1.0 (e4185ed). Public delivery provider accounts carry `shippingPrice`, `freeShippingThreshold` and `currency`; organisation details carry `commerce` with `currency`, `taxInclusivePricing` and `defaultTaxRate`; orders carry `shippingAmount`.
|
|
29
|
+
|
|
30
|
+
## 1.65.0 — 2026-09-09
|
|
31
|
+
|
|
32
|
+
- Types come from the public storefront contract (snapshot 1.0.0, c4be48e): `contract/web-gateway.v1.json`, the generated `contract/v1.ts`, `ELDRA_CONTRACT_VERSION`, and the `EldraContractResponse`, `EldraContractBody`, `EldraContractQuery` and `EldraContractPaths` helpers. Catalog methods default their response type to the contract type and keep the generic override.
|
|
33
|
+
- New: `client.cart` (`addItem`, `get`, `updateItem`, `removeItem`, `applyDiscount`, `removeDiscount`), `client.orders` (`get` with the one-time access token, `recover` from a link token), `client.checkout.handoffUrl`, `client.inventory.availability`, and on `client.catalog`: `listCategories`, `listCollections`, `getCollection`, `listCollectionProducts`, `search`.
|
|
34
|
+
- `EldraHttpError.code` carries the gateway's problem code, such as `INSUFFICIENT_STOCK`.
|
|
35
|
+
- `createCartSession` and `createOrderAccessTokens` persist the cart id and the order tokens without throwing when storage is unavailable.
|
|
36
|
+
- `pnpm sdk:contract`, `sdk:contract:check` and `sdk:typecheck`; the pr-check workflow runs lint, the contract check, the type check and the tests on every pull request.
|
|
37
|
+
|
|
38
|
+
## 1.49.0 — 2026-06-14
|
|
39
|
+
|
|
40
|
+
- Vite plugin: a failed fetch of the CMS TypeScript definitions no longer fails the build; generation is skipped (366e720).
|
|
41
|
+
|
|
42
|
+
## 1.45.0 — 2026-06-13
|
|
43
|
+
|
|
44
|
+
- `client.cms.resolveEntryList` and the entry-list types `EldraCmsEntryList`, `EldraCmsResolveEntryListOptions`, `EldraCmsResolveEntryListResponse`, `EldraCmsResolvedEntryList`, `EldraPaginated` and `EldraPageMeta` (d6cd911).
|
|
45
|
+
|
|
46
|
+
## 1.42.1 — 2026-06-06
|
|
47
|
+
|
|
48
|
+
- The default API base URL ends in `/api`: `https://web.eldra.app/api` (b1feed0).
|
|
49
|
+
|
|
50
|
+
## 1.42.0 — 2026-06-06
|
|
51
|
+
|
|
52
|
+
- First release of the SDK (4649539): `createEldraClient`, `initEldraClient`, `getEldraClient`, `EldraHttpError`, `client.cms` (`list`, `get`, `getEntryByUniqueField`), `client.catalog` (`listProducts`, `getProduct`), `client.features` (`list`, `isEnabled`, `getCapabilities`), and the `eldraCms` Vite plugin that generates the typed CMS client files.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eldra Tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
//#region src/client.ts
|
|
2
|
+
const apiBaseUrlEnvKeys = [
|
|
3
|
+
"ELDRA_API_BASE_URL",
|
|
4
|
+
"VITE_ELDRA_API_BASE_URL",
|
|
5
|
+
"NEXT_PUBLIC_ELDRA_API_BASE_URL",
|
|
6
|
+
"NUXT_PUBLIC_ELDRA_API_BASE_URL",
|
|
7
|
+
"PUBLIC_ELDRA_API_BASE_URL"
|
|
8
|
+
];
|
|
9
|
+
const DEFAULT_ELDRA_API_BASE_URL = "https://web.eldra.app/api";
|
|
10
|
+
const orgIdEnvKeys = [
|
|
11
|
+
"ELDRA_ORG_ID",
|
|
12
|
+
"VITE_ELDRA_ORG_ID",
|
|
13
|
+
"NEXT_PUBLIC_ELDRA_ORG_ID",
|
|
14
|
+
"NUXT_PUBLIC_ELDRA_ORG_ID",
|
|
15
|
+
"PUBLIC_ELDRA_ORG_ID"
|
|
16
|
+
];
|
|
17
|
+
let defaultClient;
|
|
18
|
+
var EldraHttpError = class extends Error {
|
|
19
|
+
status;
|
|
20
|
+
statusText;
|
|
21
|
+
body;
|
|
22
|
+
/** The gateway's problem `code`, such as `INSUFFICIENT_STOCK`, when the body carried one. */
|
|
23
|
+
code;
|
|
24
|
+
constructor(response, body) {
|
|
25
|
+
super(`Web Studio request failed with ${response.status} ${response.statusText}`);
|
|
26
|
+
this.name = "EldraHttpError";
|
|
27
|
+
this.status = response.status;
|
|
28
|
+
this.statusText = response.statusText;
|
|
29
|
+
this.body = body;
|
|
30
|
+
this.code = problemCode(body);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
function problemCode(body) {
|
|
34
|
+
if (body && typeof body === "object" && "code" in body) {
|
|
35
|
+
const code = body.code;
|
|
36
|
+
return typeof code === "string" ? code : void 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function initEldraClient(options) {
|
|
40
|
+
defaultClient = createEldraClient(options);
|
|
41
|
+
return defaultClient;
|
|
42
|
+
}
|
|
43
|
+
function getEldraClient() {
|
|
44
|
+
if (!defaultClient) throw new Error("Eldra client has not been initialized. Call initEldraClient() first.");
|
|
45
|
+
return defaultClient;
|
|
46
|
+
}
|
|
47
|
+
function createEldraClient(options) {
|
|
48
|
+
const httpClient = options.httpClient ?? createFetchHttpClient(options.fetch);
|
|
49
|
+
const request = (requestOptions) => httpClient(createHttpRequest(options, requestOptions));
|
|
50
|
+
const getOrganization = (organizationOptions, context) => {
|
|
51
|
+
const orgId = resolveRequestOrgId(options, organizationOptions, context);
|
|
52
|
+
return request({
|
|
53
|
+
...context,
|
|
54
|
+
orgId,
|
|
55
|
+
path: `/organization/v1/${encodeURIComponent(orgId)}`
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
const getOrganizationFeatureMap = async (organizationOptions, context) => {
|
|
59
|
+
return mapOrganizationFeatures((await getOrganization(organizationOptions, context)).features ?? []);
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
request,
|
|
63
|
+
cms: {
|
|
64
|
+
list: (schemaApiId, listOptions, context) => request({
|
|
65
|
+
...context,
|
|
66
|
+
path: `/cms/v1/schema/${encodeURIComponent(schemaApiId)}/entry`,
|
|
67
|
+
query: listOptions
|
|
68
|
+
}),
|
|
69
|
+
get: (schemaApiId, entryId, getOptions, context) => request({
|
|
70
|
+
...context,
|
|
71
|
+
path: `/cms/v1/schema/${encodeURIComponent(schemaApiId)}/entry/${encodeURIComponent(entryId)}`,
|
|
72
|
+
query: getOptions
|
|
73
|
+
}),
|
|
74
|
+
getEntryByUniqueField: (schemaApiId, fieldId, entryIdentifier, getOptions, context) => request({
|
|
75
|
+
...context,
|
|
76
|
+
path: `/cms/v1/schema/${encodeURIComponent(schemaApiId)}/entry/unique/${encodeURIComponent(fieldId)}/${encodeURIComponent(entryIdentifier)}`,
|
|
77
|
+
query: getOptions
|
|
78
|
+
}),
|
|
79
|
+
resolveEntryList: (entryList, resolveOptions, context) => request({
|
|
80
|
+
...context,
|
|
81
|
+
method: "POST",
|
|
82
|
+
path: "/cms/v1/entry-list/resolve",
|
|
83
|
+
query: resolveEntryListQuery(resolveOptions),
|
|
84
|
+
body: resolveEntryListBody(entryList)
|
|
85
|
+
})
|
|
86
|
+
},
|
|
87
|
+
features: {
|
|
88
|
+
getOrganization,
|
|
89
|
+
list: async (organizationOptions, context) => {
|
|
90
|
+
return (await getOrganization(organizationOptions, context)).features ?? [];
|
|
91
|
+
},
|
|
92
|
+
isEnabled: async (feature, organizationOptions, context) => {
|
|
93
|
+
return (await getOrganizationFeatureMap(organizationOptions, context))[feature] === true;
|
|
94
|
+
},
|
|
95
|
+
getCapabilities: async (organizationOptions, context) => {
|
|
96
|
+
const features = await getOrganizationFeatureMap(organizationOptions, context);
|
|
97
|
+
const cms = features.CMS === true;
|
|
98
|
+
const ecommerce = features.ECOMMERCE === true;
|
|
99
|
+
return {
|
|
100
|
+
cms,
|
|
101
|
+
catalog: ecommerce,
|
|
102
|
+
products: ecommerce,
|
|
103
|
+
ecommerce,
|
|
104
|
+
features
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
catalog: {
|
|
109
|
+
listProducts: (listOptions, context) => request({
|
|
110
|
+
...context,
|
|
111
|
+
path: "/catalog/v1/products/list",
|
|
112
|
+
query: listOptions
|
|
113
|
+
}),
|
|
114
|
+
getProduct: (productId, getOptions, context) => request({
|
|
115
|
+
...context,
|
|
116
|
+
path: `/catalog/v1/products/${encodeURIComponent(productId)}`,
|
|
117
|
+
query: getOptions
|
|
118
|
+
}),
|
|
119
|
+
listCategories: async (localeOptions, context) => await request({
|
|
120
|
+
...context,
|
|
121
|
+
path: "/catalog/v1/categories",
|
|
122
|
+
query: localeOptions
|
|
123
|
+
}) ?? [],
|
|
124
|
+
listCollections: (listOptions, context) => request({
|
|
125
|
+
...context,
|
|
126
|
+
path: "/catalog/v1/collections",
|
|
127
|
+
query: listOptions
|
|
128
|
+
}),
|
|
129
|
+
getCollection: (slug, localeOptions, context) => request({
|
|
130
|
+
...context,
|
|
131
|
+
path: `/catalog/v1/collections/${encodeURIComponent(slug)}`,
|
|
132
|
+
query: localeOptions
|
|
133
|
+
}),
|
|
134
|
+
listCollectionProducts: (slug, listOptions, context) => request({
|
|
135
|
+
...context,
|
|
136
|
+
path: `/catalog/v1/collections/${encodeURIComponent(slug)}/products`,
|
|
137
|
+
query: listOptions
|
|
138
|
+
}),
|
|
139
|
+
search: (query, searchOptions, context) => request({
|
|
140
|
+
...context,
|
|
141
|
+
path: "/search/v1",
|
|
142
|
+
query: {
|
|
143
|
+
...searchOptions,
|
|
144
|
+
q: query
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
},
|
|
148
|
+
cart: {
|
|
149
|
+
addItem: (input, context) => request({
|
|
150
|
+
...context,
|
|
151
|
+
method: "POST",
|
|
152
|
+
path: "/shopping-cart/v1/cart/items",
|
|
153
|
+
body: input
|
|
154
|
+
}),
|
|
155
|
+
get: (cartId, localeOptions, context) => request({
|
|
156
|
+
...context,
|
|
157
|
+
path: `/shopping-cart/v1/cart/${encodeURIComponent(cartId)}`,
|
|
158
|
+
query: localeOptions
|
|
159
|
+
}),
|
|
160
|
+
updateItem: (cartId, itemId, input, context) => request({
|
|
161
|
+
...context,
|
|
162
|
+
method: "PATCH",
|
|
163
|
+
path: `/shopping-cart/v1/cart/${encodeURIComponent(cartId)}/items/${encodeURIComponent(itemId)}`,
|
|
164
|
+
body: input
|
|
165
|
+
}),
|
|
166
|
+
removeItem: (cartId, itemId, context) => request({
|
|
167
|
+
...context,
|
|
168
|
+
method: "DELETE",
|
|
169
|
+
path: `/shopping-cart/v1/cart/${encodeURIComponent(cartId)}/items/${encodeURIComponent(itemId)}`
|
|
170
|
+
}),
|
|
171
|
+
applyDiscount: async (cartId, code, localeOptions, context) => {
|
|
172
|
+
const trimmed = code.trim();
|
|
173
|
+
const cart = await request({
|
|
174
|
+
...context,
|
|
175
|
+
method: "PUT",
|
|
176
|
+
path: `/shopping-cart/v1/cart/${encodeURIComponent(cartId)}/discount`,
|
|
177
|
+
query: localeOptions,
|
|
178
|
+
body: { code: trimmed }
|
|
179
|
+
});
|
|
180
|
+
return {
|
|
181
|
+
cart,
|
|
182
|
+
applied: typeof cart.discountCode === "string" && cart.discountCode.trim().toUpperCase() === trimmed.toUpperCase()
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
removeDiscount: (cartId, localeOptions, context) => request({
|
|
186
|
+
...context,
|
|
187
|
+
method: "DELETE",
|
|
188
|
+
path: `/shopping-cart/v1/cart/${encodeURIComponent(cartId)}/discount`,
|
|
189
|
+
query: localeOptions
|
|
190
|
+
})
|
|
191
|
+
},
|
|
192
|
+
orders: {
|
|
193
|
+
get: (orderId, readOptions, context) => request({
|
|
194
|
+
...context,
|
|
195
|
+
path: `/order/v1/${encodeURIComponent(orderId)}`,
|
|
196
|
+
headers: mergeHeaders(context?.headers, readOptions?.accessToken ? { "X-Order-Token": readOptions.accessToken } : void 0)
|
|
197
|
+
}),
|
|
198
|
+
recover: (token, context) => request({
|
|
199
|
+
...context,
|
|
200
|
+
method: "POST",
|
|
201
|
+
path: "/order/v1/recover",
|
|
202
|
+
body: { token: token.trim() }
|
|
203
|
+
})
|
|
204
|
+
},
|
|
205
|
+
checkout: { handoffUrl: (handoff) => {
|
|
206
|
+
const checkoutUrl = handoff.checkoutUrl ?? resolveRuntimeValue(options.checkoutUrl);
|
|
207
|
+
if (!checkoutUrl) throw new Error("Missing checkout URL. Pass checkoutUrl to createEldraClient() or to handoffUrl().");
|
|
208
|
+
const orgId = handoff.orgId ?? resolveOrgId(options);
|
|
209
|
+
if (!orgId) throw new Error("Missing Web Studio organization ID.");
|
|
210
|
+
const url = new URL(`${checkoutUrl.replace(/\/$/, "")}/checkout/${encodeURIComponent(orgId)}/${encodeURIComponent(handoff.cartId)}`);
|
|
211
|
+
if (handoff.locale) url.searchParams.set("lang", handoff.locale);
|
|
212
|
+
return url.toString();
|
|
213
|
+
} },
|
|
214
|
+
inventory: { availability: (items, context) => request({
|
|
215
|
+
...context,
|
|
216
|
+
method: "POST",
|
|
217
|
+
path: "/inventory/v1/stock/availability",
|
|
218
|
+
body: { items }
|
|
219
|
+
}) }
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function resolveRequestOrgId(clientOptions, organizationOptions, context) {
|
|
223
|
+
const orgId = organizationOptions?.orgId ?? context?.orgId ?? resolveOrgId(clientOptions);
|
|
224
|
+
if (!orgId) throw new Error("Missing Web Studio organization ID.");
|
|
225
|
+
return orgId;
|
|
226
|
+
}
|
|
227
|
+
function mapOrganizationFeatures(features) {
|
|
228
|
+
const mapped = {};
|
|
229
|
+
for (const row of features) if (row.feature === "CMS" || row.feature === "ECOMMERCE") mapped[row.feature] = row.enabled;
|
|
230
|
+
return mapped;
|
|
231
|
+
}
|
|
232
|
+
function resolveEntryListQuery(options) {
|
|
233
|
+
return {
|
|
234
|
+
locale: options?.locale,
|
|
235
|
+
page: options?.page,
|
|
236
|
+
pageSize: options?.pageSize,
|
|
237
|
+
limit: options?.limit,
|
|
238
|
+
depth: options?.depth,
|
|
239
|
+
deep: options?.deep
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function resolveEntryListBody(entryList) {
|
|
243
|
+
return {
|
|
244
|
+
schemas: entryList.schemas,
|
|
245
|
+
filters: entryList.filters,
|
|
246
|
+
displayAs: entryList.displayAs,
|
|
247
|
+
orderBy: entryList.orderBy,
|
|
248
|
+
pageSize: entryList.pageSize
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function createHttpRequest(clientOptions, requestOptions) {
|
|
252
|
+
const method = requestOptions.method ?? (requestOptions.body === void 0 ? "GET" : "POST");
|
|
253
|
+
const headers = mergeHeaders(resolveRuntimeValue(clientOptions.headers), requestOptions.headers);
|
|
254
|
+
const orgId = requestOptions.orgId ?? resolveOrgId(clientOptions);
|
|
255
|
+
if (orgId) headers.set("X-Org-Id", orgId);
|
|
256
|
+
let body;
|
|
257
|
+
if (requestOptions.body !== void 0) {
|
|
258
|
+
headers.set("Content-Type", headers.get("Content-Type") ?? "application/json");
|
|
259
|
+
body = isRawBody(requestOptions.body) ? requestOptions.body : JSON.stringify(requestOptions.body);
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
url: buildUrl(resolveApiBaseUrl(clientOptions), requestOptions.path, requestOptions.query),
|
|
263
|
+
method,
|
|
264
|
+
headers,
|
|
265
|
+
body,
|
|
266
|
+
signal: requestOptions.signal
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
function createFetchHttpClient(fetchImpl) {
|
|
270
|
+
return async (request) => {
|
|
271
|
+
const resolvedFetch = fetchImpl ?? globalThis.fetch;
|
|
272
|
+
if (!resolvedFetch) throw new Error("No fetch implementation is available. Pass fetch or httpClient to createEldraClient().");
|
|
273
|
+
const response = await resolvedFetch(request.url, {
|
|
274
|
+
method: request.method,
|
|
275
|
+
headers: request.headers,
|
|
276
|
+
body: request.body,
|
|
277
|
+
signal: request.signal
|
|
278
|
+
});
|
|
279
|
+
const body = await readResponseBody(response);
|
|
280
|
+
if (!response.ok) throw new EldraHttpError(response, body);
|
|
281
|
+
return body;
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function readResponseBody(response) {
|
|
285
|
+
if (response.status === 204) return Promise.resolve(void 0);
|
|
286
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
287
|
+
if (contentType.includes("application/json") || contentType.includes("application/problem+json")) return response.json();
|
|
288
|
+
return response.text();
|
|
289
|
+
}
|
|
290
|
+
function resolveApiBaseUrl(options) {
|
|
291
|
+
return resolveRuntimeValue(options.apiBaseUrl) ?? readFirstEnvValue(resolveRuntimeValue(options.env), apiBaseUrlEnvKeys) ?? "https://web.eldra.app/api";
|
|
292
|
+
}
|
|
293
|
+
function resolveOrgId(options) {
|
|
294
|
+
return resolveRuntimeValue(options.orgId) ?? readFirstEnvValue(resolveRuntimeValue(options.env), orgIdEnvKeys);
|
|
295
|
+
}
|
|
296
|
+
function readFirstEnvValue(env, keys) {
|
|
297
|
+
for (const key of keys) {
|
|
298
|
+
const value = env?.[key];
|
|
299
|
+
if (typeof value === "string" && value.trim() !== "") return value;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function resolveRuntimeValue(value) {
|
|
303
|
+
return typeof value === "function" ? value() : value;
|
|
304
|
+
}
|
|
305
|
+
function mergeHeaders(...sources) {
|
|
306
|
+
const headers = new Headers();
|
|
307
|
+
for (const source of sources) {
|
|
308
|
+
if (!source) continue;
|
|
309
|
+
new Headers(source).forEach((value, key) => headers.set(key, value));
|
|
310
|
+
}
|
|
311
|
+
return headers;
|
|
312
|
+
}
|
|
313
|
+
function buildUrl(apiBaseUrl, path, query) {
|
|
314
|
+
const url = new URL(`${apiBaseUrl.replace(/\/$/, "")}/${path.replace(/^\//, "")}`);
|
|
315
|
+
for (const [key, value] of Object.entries(query ?? {})) appendQueryValue(url.searchParams, key, value);
|
|
316
|
+
return url.toString();
|
|
317
|
+
}
|
|
318
|
+
function appendQueryValue(searchParams, key, value) {
|
|
319
|
+
if (value === void 0 || value === null || value === "") return;
|
|
320
|
+
if (Array.isArray(value)) {
|
|
321
|
+
searchParams.set(key, value.map(String).join(","));
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
searchParams.set(key, String(value));
|
|
325
|
+
}
|
|
326
|
+
function isRawBody(body) {
|
|
327
|
+
return typeof body === "string" || typeof FormData !== "undefined" && body instanceof FormData || typeof URLSearchParams !== "undefined" && body instanceof URLSearchParams || typeof Blob !== "undefined" && body instanceof Blob || body instanceof ArrayBuffer;
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
export { initEldraClient as a, getEldraClient as i, EldraHttpError as n, createEldraClient as r, DEFAULT_ELDRA_API_BASE_URL as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { $ as EldraProductVariant, A as EldraFeature, B as EldraOrdersClient, C as EldraCmsResolveEntryListResponse, D as EldraCollectionListOptions, E as EldraCollectionList, F as EldraInventoryClient, G as EldraPaginated, H as EldraOrganizationFeature, I as EldraLocaleOptions, J as EldraProductListItem, K as EldraProductDetails, L as EldraOrder, M as EldraFeatureClient, N as EldraHttpClient, O as EldraCollectionProductsOptions, P as EldraHttpRequest, Q as EldraProductThumbnail, R as EldraOrderLine, S as EldraCmsResolveEntryListOptions, T as EldraCollection, U as EldraOrganizationOptions, V as EldraOrganizationDetails, W as EldraPageMeta, X as EldraProductOption, Y as EldraProductMediaLink, Z as EldraProductOptionValue, _ as EldraCmsEntryListEntry, a as EldraCartTotals, at as EldraSearchResponse, b as EldraCmsGetOptions, c as EldraCatalogListProductsOptions, ct as EldraStockAvailabilityItem, d as EldraCheckoutHandoffOptions, dt as EldraContractBody, et as EldraRecoveredBasket, f as EldraClient, ft as EldraContractMethod, g as EldraCmsEntryListDisplayMode, gt as paths, h as EldraCmsEntryList, ht as EldraContractResponse, i as EldraCartItem, it as EldraSearchOptions, j as EldraFeatureCapabilities, k as EldraDiscountResult, l as EldraCategory, lt as RuntimeEnv, m as EldraCmsClient, mt as EldraContractQuery, n as EldraCart, nt as EldraRequestContext, o as EldraCatalogClient, ot as EldraStockAvailability, p as EldraClientOptions, pt as EldraContractPath, q as EldraProductList, r as EldraCartClient, rt as EldraRequestOptions, s as EldraCatalogGetProductOptions, st as EldraStockAvailabilityInput, t as EldraAddCartItemInput, tt as EldraRecoveredBasketItem, u as EldraCheckoutClient, ut as RuntimeValue, v as EldraCmsEntryListFilterCriterion, w as EldraCmsResolvedEntryList, x as EldraCmsListOptions, y as EldraCmsGetEntryByUniqueFieldOptions, z as EldraOrderReadOptions } from "./types-DNiJpmSn.js";
|
|
2
|
+
//#region src/contract/version.d.ts
|
|
3
|
+
export declare const ELDRA_CONTRACT_VERSION = "2.4.0";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/client.d.ts
|
|
6
|
+
export declare const DEFAULT_ELDRA_API_BASE_URL = "https://web.eldra.app/api";
|
|
7
|
+
export declare class EldraHttpError extends Error {
|
|
8
|
+
readonly status: number;
|
|
9
|
+
readonly statusText: string;
|
|
10
|
+
readonly body: unknown;
|
|
11
|
+
/** The gateway's problem `code`, such as `INSUFFICIENT_STOCK`, when the body carried one. */
|
|
12
|
+
readonly code: string | undefined;
|
|
13
|
+
constructor(response: Response, body: unknown);
|
|
14
|
+
}
|
|
15
|
+
export declare function initEldraClient(options: EldraClientOptions): EldraClient;
|
|
16
|
+
export declare function getEldraClient(): EldraClient;
|
|
17
|
+
export declare function createEldraClient(options: EldraClientOptions): EldraClient;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/storage.d.ts
|
|
20
|
+
interface EldraKeyValueStorage {
|
|
21
|
+
getItem(key: string): string | null;
|
|
22
|
+
setItem(key: string, value: string): void;
|
|
23
|
+
removeItem(key: string): void;
|
|
24
|
+
}
|
|
25
|
+
interface EldraCartSession {
|
|
26
|
+
read(): string | null;
|
|
27
|
+
remember(cartId: string): void;
|
|
28
|
+
forget(): void;
|
|
29
|
+
}
|
|
30
|
+
interface EldraOrderAccessTokens {
|
|
31
|
+
remember(orderId: string, token: string | undefined): void;
|
|
32
|
+
read(orderId: string): string | null;
|
|
33
|
+
}
|
|
34
|
+
interface EldraCartSessionOptions {
|
|
35
|
+
storage?: EldraKeyValueStorage | null;
|
|
36
|
+
key?: string;
|
|
37
|
+
}
|
|
38
|
+
interface EldraOrderAccessTokenOptions {
|
|
39
|
+
storage?: EldraKeyValueStorage | null;
|
|
40
|
+
keyPrefix?: string;
|
|
41
|
+
}
|
|
42
|
+
/** The cart id a browser holds between visits. Lives in localStorage so the basket survives a closed tab. */
|
|
43
|
+
export declare function createCartSession(options?: EldraCartSessionOptions): EldraCartSession;
|
|
44
|
+
/**
|
|
45
|
+
* The one-time order access token, kept in sessionStorage rather than the return URL so it
|
|
46
|
+
* survives the payment provider's redirect without landing in a URL, a log or a Referer.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createOrderAccessTokens(options?: EldraOrderAccessTokenOptions): EldraOrderAccessTokens;
|
|
49
|
+
//#endregion
|
|
50
|
+
export type { EldraAddCartItemInput, EldraCart, EldraCartClient, EldraCartItem, EldraCartSession, EldraCartSessionOptions, EldraCartTotals, EldraCatalogClient, EldraCatalogGetProductOptions, EldraCatalogListProductsOptions, EldraCategory, EldraCheckoutClient, EldraCheckoutHandoffOptions, EldraClient, EldraClientOptions, EldraCmsClient, EldraCmsEntryList, EldraCmsEntryListDisplayMode, EldraCmsEntryListEntry, EldraCmsEntryListFilterCriterion, EldraCmsGetEntryByUniqueFieldOptions, EldraCmsGetOptions, EldraCmsListOptions, EldraCmsResolveEntryListOptions, EldraCmsResolveEntryListResponse, EldraCmsResolvedEntryList, EldraCollection, EldraCollectionList, EldraCollectionListOptions, EldraCollectionProductsOptions, EldraContractBody, EldraContractMethod, EldraContractPath, paths as EldraContractPaths, EldraContractQuery, EldraContractResponse, EldraDiscountResult, EldraFeature, EldraFeatureCapabilities, EldraFeatureClient, EldraHttpClient, EldraHttpRequest, EldraInventoryClient, EldraKeyValueStorage, EldraLocaleOptions, EldraOrder, EldraOrderAccessTokenOptions, EldraOrderAccessTokens, EldraOrderLine, EldraOrderReadOptions, EldraOrdersClient, EldraOrganizationDetails, EldraOrganizationFeature, EldraOrganizationOptions, EldraPageMeta, EldraPaginated, EldraProductDetails, EldraProductList, EldraProductListItem, EldraProductMediaLink, EldraProductOption, EldraProductOptionValue, EldraProductThumbnail, EldraProductVariant, EldraRecoveredBasket, EldraRecoveredBasketItem, EldraRequestContext, EldraRequestOptions, EldraSearchOptions, EldraSearchResponse, EldraStockAvailability, EldraStockAvailabilityInput, EldraStockAvailabilityItem, RuntimeEnv, RuntimeValue };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { a as initEldraClient, i as getEldraClient, n as EldraHttpError, r as createEldraClient, t as DEFAULT_ELDRA_API_BASE_URL } from "./client-CP-xWxRN.js";
|
|
2
|
+
//#region src/contract/version.ts
|
|
3
|
+
const ELDRA_CONTRACT_VERSION = "2.4.0";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/storage.ts
|
|
6
|
+
const DEFAULT_CART_KEY = "eldra.cartId";
|
|
7
|
+
const DEFAULT_ORDER_TOKEN_PREFIX = "eldra.orderAccessToken.";
|
|
8
|
+
function browserStorage(name) {
|
|
9
|
+
try {
|
|
10
|
+
return globalThis[name] ?? null;
|
|
11
|
+
} catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function safeRead(storage, key) {
|
|
16
|
+
try {
|
|
17
|
+
return storage?.getItem(key) ?? null;
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function safeWrite(storage, key, value) {
|
|
23
|
+
try {
|
|
24
|
+
if (value === null) storage?.removeItem(key);
|
|
25
|
+
else storage?.setItem(key, value);
|
|
26
|
+
} catch {}
|
|
27
|
+
}
|
|
28
|
+
/** The cart id a browser holds between visits. Lives in localStorage so the basket survives a closed tab. */
|
|
29
|
+
function createCartSession(options = {}) {
|
|
30
|
+
const storage = options.storage === void 0 ? browserStorage("localStorage") : options.storage;
|
|
31
|
+
const key = options.key ?? DEFAULT_CART_KEY;
|
|
32
|
+
return {
|
|
33
|
+
read: () => safeRead(storage, key),
|
|
34
|
+
remember: (cartId) => safeWrite(storage, key, cartId),
|
|
35
|
+
forget: () => safeWrite(storage, key, null)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The one-time order access token, kept in sessionStorage rather than the return URL so it
|
|
40
|
+
* survives the payment provider's redirect without landing in a URL, a log or a Referer.
|
|
41
|
+
*/
|
|
42
|
+
function createOrderAccessTokens(options = {}) {
|
|
43
|
+
const storage = options.storage === void 0 ? browserStorage("sessionStorage") : options.storage;
|
|
44
|
+
const prefix = options.keyPrefix ?? DEFAULT_ORDER_TOKEN_PREFIX;
|
|
45
|
+
return {
|
|
46
|
+
remember: (orderId, token) => {
|
|
47
|
+
if (token) safeWrite(storage, prefix + orderId, token);
|
|
48
|
+
},
|
|
49
|
+
read: (orderId) => safeRead(storage, prefix + orderId)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { DEFAULT_ELDRA_API_BASE_URL, ELDRA_CONTRACT_VERSION, EldraHttpError, createCartSession, createEldraClient, createOrderAccessTokens, getEldraClient, initEldraClient };
|