@eldrajs/sdk 0.2.0 → 0.2.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 CHANGED
@@ -9,6 +9,19 @@ platform repository.
9
9
 
10
10
  ## Unreleased
11
11
 
12
+ - Restore `previewToken` on the SDK client and Vite generator after the move from
13
+ `vue-ui-components`. Accepts a string or callback and sends `X-Preview-Token`; the generator
14
+ forwards it to both type endpoints without embedding it in generated files.
15
+
16
+ - The Vite plugin writes `.eldra/web-studio/` beside the nearest `package.json` instead of Vite's
17
+ root; on Nuxt 4 the root is `app/`, and the folder landed there. A relative `outDir` is resolved
18
+ the same way.
19
+ - A fetch the plugin skips — gateway down, unknown organisation id, wrong URL — is now a build
20
+ warning naming what was skipped and why; it was silent. `generateEldraFiles` returns what it
21
+ wrote and what it skipped.
22
+
23
+ ## 0.2.0 — 2026-09-16
24
+
12
25
  - **Breaking.** The SDK no longer ships response types. The Vite plugin — now `eldra()`, was
13
26
  `eldraCms()` — generates `contract.ts` from your gateway's `/api/public/openapi.json` next to the
14
27
  CMS types, and it augments the SDK's `EldraContract` so every method is typed against the gateway
package/README.md CHANGED
@@ -35,6 +35,13 @@ call site.
35
35
 
36
36
  The full walkthrough is [docs/getting-started.md](../../docs/getting-started.md).
37
37
 
38
+ ## Preview mode
39
+
40
+ Both `createEldraClient` and the Vite plugin accept `previewToken` as a string or callback and send
41
+ it as `X-Preview-Token`. Configure each separately; the generator never embeds the token in its
42
+ output. See [preview mode](../../docs/getting-started.md#preview-mode) for examples and header
43
+ precedence.
44
+
38
45
  ## What is in the client
39
46
 
40
47
  | Group | Methods |
@@ -254,6 +254,8 @@ function createHttpRequest(clientOptions, requestOptions) {
254
254
  const headers = mergeHeaders(resolveRuntimeValue(clientOptions.headers), requestOptions.headers);
255
255
  const orgId = requestOptions.orgId ?? resolveOrgId(clientOptions);
256
256
  if (orgId) headers.set("X-Org-Id", orgId);
257
+ const previewToken = resolveRuntimeValue(clientOptions.previewToken);
258
+ if (previewToken) headers.set("X-Preview-Token", previewToken);
257
259
  let body;
258
260
  if (requestOptions.body !== void 0) {
259
261
  headers.set("Content-Type", headers.get("Content-Type") ?? "application/json");
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
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, _t as EldraContractProp, a as EldraCartTotals, at as EldraSearchResponse, b as EldraCmsGetOptions, c as EldraCatalogListProductsOptions, ct as EldraStockAvailabilityItem, d as EldraCheckoutHandoffOptions, dt as EldraContract, et as EldraRecoveredBasket, f as EldraClient, ft as EldraContractBody, g as EldraCmsEntryListDisplayMode, gt as EldraContractPaths, h as EldraCmsEntryList, ht as EldraContractPath, i as EldraCartItem, it as EldraSearchOptions, j as EldraFeatureCapabilities, k as EldraDiscountResult, l as EldraCategory, lt as RuntimeEnv, m as EldraCmsClient, mt as EldraContractMethod, n as EldraCart, nt as EldraRequestContext, o as EldraCatalogClient, ot as EldraStockAvailability, p as EldraClientOptions, pt as EldraContractItem, 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, vt as EldraContractQuery, w as EldraCmsResolvedEntryList, x as EldraCmsListOptions, y as EldraCmsGetEntryByUniqueFieldOptions, yt as EldraContractResponse, z as EldraOrderReadOptions } from "./types-Bq1jUVcz.js";
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, _t as EldraContractProp, a as EldraCartTotals, at as EldraSearchResponse, b as EldraCmsGetOptions, c as EldraCatalogListProductsOptions, ct as EldraStockAvailabilityItem, d as EldraCheckoutHandoffOptions, dt as EldraContract, et as EldraRecoveredBasket, f as EldraClient, ft as EldraContractBody, g as EldraCmsEntryListDisplayMode, gt as EldraContractPaths, h as EldraCmsEntryList, ht as EldraContractPath, i as EldraCartItem, it as EldraSearchOptions, j as EldraFeatureCapabilities, k as EldraDiscountResult, l as EldraCategory, lt as RuntimeEnv, m as EldraCmsClient, mt as EldraContractMethod, n as EldraCart, nt as EldraRequestContext, o as EldraCatalogClient, ot as EldraStockAvailability, p as EldraClientOptions, pt as EldraContractItem, 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, vt as EldraContractQuery, w as EldraCmsResolvedEntryList, x as EldraCmsListOptions, y as EldraCmsGetEntryByUniqueFieldOptions, yt as EldraContractResponse, z as EldraOrderReadOptions } from "./types-CPUs_QPj.js";
2
2
  //#region src/client.d.ts
3
3
  export declare const DEFAULT_ELDRA_API_BASE_URL = "https://web.eldra.app/api";
4
4
  export declare class EldraHttpError extends Error {
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { a as initEldraClient, i as getEldraClient, n as EldraHttpError, r as createEldraClient, t as DEFAULT_ELDRA_API_BASE_URL } from "./client-BREAy8rQ.js";
1
+ import { a as initEldraClient, i as getEldraClient, n as EldraHttpError, r as createEldraClient, t as DEFAULT_ELDRA_API_BASE_URL } from "./client-CfkktZmj.js";
2
2
  //#region src/storage.ts
3
3
  const DEFAULT_CART_KEY = "eldra.cartId";
4
4
  const DEFAULT_ORDER_TOKEN_PREFIX = "eldra.orderAccessToken.";
@@ -57,6 +57,8 @@ type EldraHttpClient = <T = unknown>(request: EldraHttpRequest) => Promise<T>;
57
57
  interface EldraClientOptions {
58
58
  apiBaseUrl?: RuntimeValue<string>;
59
59
  orgId?: RuntimeValue<string>;
60
+ /** Sends X-Preview-Token on requests, overriding the same header in headers/context. */
61
+ previewToken?: RuntimeValue<string>;
60
62
  env?: RuntimeValue<RuntimeEnv>;
61
63
  headers?: RuntimeValue<HeadersInit>;
62
64
  httpClient?: EldraHttpClient;
package/dist/vite.d.ts CHANGED
@@ -1,9 +1,11 @@
1
- import { lt as RuntimeEnv, ut as RuntimeValue } from "./types-Bq1jUVcz.js";
1
+ import { lt as RuntimeEnv, ut as RuntimeValue } from "./types-CPUs_QPj.js";
2
2
  import { Plugin } from "vite";
3
3
  //#region src/vite-plugin.d.ts
4
4
  export interface EldraVitePluginOptions {
5
5
  apiBaseUrl?: RuntimeValue<string>;
6
6
  orgId?: RuntimeValue<string>;
7
+ /** Sends X-Preview-Token when fetching types/contracts; never embedded in generated files. */
8
+ previewToken?: RuntimeValue<string>;
7
9
  env?: RuntimeValue<RuntimeEnv>;
8
10
  headers?: RuntimeValue<HeadersInit>;
9
11
  schemas?: string[];
@@ -20,8 +22,17 @@ export interface EldraVitePluginOptions {
20
22
  fetch?: typeof fetch;
21
23
  skipOnMissingConfig?: boolean;
22
24
  }
25
+ export interface EldraGenerationResult {
26
+ /** Absolute paths of the files written. */
27
+ written: string[];
28
+ /** What was not generated, and why; the previously generated file, if any, is left in place. */
29
+ skipped: Array<{
30
+ what: string;
31
+ reason: string;
32
+ }>;
33
+ }
23
34
  export declare function eldra(options?: EldraVitePluginOptions): Plugin;
24
- export declare function generateEldraFiles(root: string, options: EldraVitePluginOptions, mode?: string): Promise<void>;
35
+ export declare function generateEldraFiles(root: string, options: EldraVitePluginOptions, mode?: string): Promise<EldraGenerationResult>;
25
36
  export declare function createContractSource(document: unknown, options?: {
26
37
  sdkImport?: string;
27
38
  source?: URL | string;
package/dist/vite.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./client-BREAy8rQ.js";
1
+ import "./client-CfkktZmj.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";
@@ -37,53 +37,75 @@ function eldra(options = {}) {
37
37
  async buildStart() {
38
38
  if (generated) return;
39
39
  generated = true;
40
- await generateEldraFiles(config?.root ?? process.cwd(), options, config?.mode);
40
+ const result = await generateEldraFiles(config?.root ?? process.cwd(), options, config?.mode);
41
+ for (const skip of result.skipped) this.warn(`${skip.what} not generated: ${skip.reason}`);
41
42
  }
42
43
  };
43
44
  }
44
45
  async function generateEldraFiles(root, options, mode = process.env.NODE_ENV ?? "development") {
45
- const env = resolvePluginEnv(root, mode, options);
46
+ const projectRoot = await findProjectRoot(root);
47
+ const env = resolvePluginEnv(projectRoot, mode, options);
46
48
  const apiBaseUrl = resolveRuntimeValue(options.apiBaseUrl) ?? readFirstEnvValue(env, apiBaseUrlEnvKeys) ?? "https://web.eldra.app/api";
47
49
  const orgId = resolveRuntimeValue(options.orgId) ?? readFirstEnvValue(env, orgIdEnvKeys);
48
50
  if (!orgId) {
49
- if (options.skipOnMissingConfig) return;
50
- throw new Error("Missing Eldra generation config. Set orgId or ELDRA_ORG_ID. Set apiBaseUrl only for staging, local, or test gateways.");
51
+ const reason = "no organisation id. Set orgId or ELDRA_ORG_ID; set apiBaseUrl only for staging, local, or test gateways.";
52
+ if (options.skipOnMissingConfig) return {
53
+ written: [],
54
+ skipped: [{
55
+ what: "Eldra types",
56
+ reason
57
+ }]
58
+ };
59
+ throw new Error(`Missing Eldra generation config: ${reason}`);
51
60
  }
52
61
  const moduleName = options.moduleName ?? defaultModuleName;
53
62
  const sdkImport = options.sdkImport ?? defaultSdkImport;
54
- const outDir = resolve(root, options.outDir ?? defaultOutDir);
63
+ const outDir = resolve(projectRoot, options.outDir ?? defaultOutDir);
55
64
  const typesFileName = options.typesFileName ?? defaultTypesFileName;
56
65
  const contractFileName = options.contractFileName ?? defaultContractFileName;
57
66
  const clientFileName = options.clientFileName ?? defaultClientFileName;
58
67
  const indexFileName = options.indexFileName ?? defaultIndexFileName;
68
+ const headers = new Headers(resolveRuntimeValue(options.headers));
69
+ const previewToken = resolveRuntimeValue(options.previewToken);
70
+ if (previewToken) headers.set("X-Preview-Token", previewToken);
59
71
  const fetchOptions = {
60
72
  fetch: options.fetch,
61
- headers: resolveRuntimeValue(options.headers)
73
+ headers
62
74
  };
63
- const typesSource = await orSkip(() => fetchCmsTypes(apiBaseUrl, orgId, {
75
+ const result = {
76
+ written: [],
77
+ skipped: []
78
+ };
79
+ const typesSource = await attempt("CMS types", result, () => fetchCmsTypes(apiBaseUrl, orgId, {
64
80
  ...fetchOptions,
65
81
  maxDepth: options.maxDepth,
66
82
  moduleName,
67
83
  schemas: options.schemas
68
84
  }));
69
- const contractSource = options.contract === false ? void 0 : await orSkip(() => fetchContract(apiBaseUrl, orgId, {
85
+ const contractSource = options.contract === false ? void 0 : await attempt("Gateway contract", result, () => fetchContract(apiBaseUrl, orgId, {
70
86
  ...fetchOptions,
71
87
  sdkImport
72
88
  }));
73
- if (!typesSource && !contractSource) return;
89
+ if (!typesSource && !contractSource) return result;
74
90
  await mkdir(outDir, { recursive: true });
91
+ const write = async (fileName, source) => {
92
+ const path = resolve(outDir, fileName);
93
+ await writeGeneratedFile(path, source);
94
+ result.written.push(path);
95
+ };
75
96
  if (typesSource) {
76
- await writeGeneratedFile(resolve(outDir, typesFileName), typesSource);
77
- await writeGeneratedFile(resolve(outDir, clientFileName), createGeneratedClientSource({
97
+ await write(typesFileName, typesSource);
98
+ await write(clientFileName, createGeneratedClientSource({
78
99
  moduleName,
79
100
  sdkImport,
80
101
  typesImport: `./${stripTSExtension(typesFileName)}`
81
102
  }));
82
103
  }
83
- if (contractSource) await writeGeneratedFile(resolve(outDir, contractFileName), contractSource);
104
+ if (contractSource) await write(contractFileName, contractSource);
84
105
  const hasClient = typesSource !== void 0 || await exists(resolve(outDir, clientFileName));
85
106
  const hasContract = contractSource !== void 0 || await exists(resolve(outDir, contractFileName));
86
- await writeGeneratedFile(resolve(outDir, indexFileName), [hasClient ? `export * from './${stripTSExtension(clientFileName)}';` : void 0, hasContract ? `export * from './${stripTSExtension(contractFileName)}';` : void 0].filter(Boolean).join("\n") + "\n");
107
+ await write(indexFileName, [hasClient ? `export * from './${stripTSExtension(clientFileName)}';` : void 0, hasContract ? `export * from './${stripTSExtension(contractFileName)}';` : void 0].filter(Boolean).join("\n") + "\n");
108
+ return result;
87
109
  }
88
110
  async function fetchCmsTypes(apiBaseUrl, orgId, options) {
89
111
  const url = new URL(`${trimSlash(apiBaseUrl)}/cms/v1/typescript-definitions`);
@@ -132,13 +154,26 @@ async function fetchText(url, orgId, options) {
132
154
  if (!response.ok) throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}\n${body}`);
133
155
  return body;
134
156
  }
135
- async function orSkip(run) {
157
+ async function attempt(what, result, run) {
136
158
  try {
137
159
  return await run();
138
- } catch {
160
+ } catch (error) {
161
+ result.skipped.push({
162
+ what,
163
+ reason: error instanceof Error ? error.message : String(error)
164
+ });
139
165
  return;
140
166
  }
141
167
  }
168
+ async function findProjectRoot(start) {
169
+ let dir = resolve(start);
170
+ while (true) {
171
+ if (await exists(resolve(dir, "package.json"))) return dir;
172
+ const parent = dirname(dir);
173
+ if (parent === dir) return resolve(start);
174
+ dir = parent;
175
+ }
176
+ }
142
177
  function createGeneratedClientSource(options) {
143
178
  const prefix = pascalTypeName(options.moduleName) || defaultModuleName;
144
179
  return `import {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eldrajs/sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.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",