@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/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { lt as RuntimeEnv, ut as RuntimeValue } from "./types-DNiJpmSn.js";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
//#region src/vite-plugin.d.ts
|
|
4
|
+
export interface EldraCmsVitePluginOptions {
|
|
5
|
+
apiBaseUrl?: RuntimeValue<string>;
|
|
6
|
+
orgId?: RuntimeValue<string>;
|
|
7
|
+
env?: RuntimeValue<RuntimeEnv>;
|
|
8
|
+
headers?: RuntimeValue<HeadersInit>;
|
|
9
|
+
schemas?: string[];
|
|
10
|
+
maxDepth?: number;
|
|
11
|
+
moduleName?: string;
|
|
12
|
+
outDir?: string;
|
|
13
|
+
typesFileName?: string;
|
|
14
|
+
clientFileName?: string;
|
|
15
|
+
indexFileName?: string;
|
|
16
|
+
sdkImport?: string;
|
|
17
|
+
fetch?: typeof fetch;
|
|
18
|
+
skipOnMissingConfig?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare function eldraCms(options?: EldraCmsVitePluginOptions): Plugin;
|
|
21
|
+
export declare function generateEldraCmsFiles(root: string, options: EldraCmsVitePluginOptions, mode?: string): Promise<void>;
|
|
22
|
+
//#endregion
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import "./client-CP-xWxRN.js";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { loadEnv } from "vite";
|
|
5
|
+
//#region src/vite-plugin.ts
|
|
6
|
+
const defaultOutDir = ".eldra/web-studio";
|
|
7
|
+
const defaultTypesFileName = "cms-types.ts";
|
|
8
|
+
const defaultClientFileName = "client.ts";
|
|
9
|
+
const defaultIndexFileName = "index.ts";
|
|
10
|
+
const defaultModuleName = "EldraCMS";
|
|
11
|
+
const defaultSdkImport = "@eldrajs/sdk";
|
|
12
|
+
const apiBaseUrlEnvKeys = [
|
|
13
|
+
"ELDRA_API_BASE_URL",
|
|
14
|
+
"VITE_ELDRA_API_BASE_URL",
|
|
15
|
+
"NEXT_PUBLIC_ELDRA_API_BASE_URL",
|
|
16
|
+
"NUXT_PUBLIC_ELDRA_API_BASE_URL",
|
|
17
|
+
"PUBLIC_ELDRA_API_BASE_URL"
|
|
18
|
+
];
|
|
19
|
+
const orgIdEnvKeys = [
|
|
20
|
+
"ELDRA_ORG_ID",
|
|
21
|
+
"VITE_ELDRA_ORG_ID",
|
|
22
|
+
"NEXT_PUBLIC_ELDRA_ORG_ID",
|
|
23
|
+
"NUXT_PUBLIC_ELDRA_ORG_ID",
|
|
24
|
+
"PUBLIC_ELDRA_ORG_ID"
|
|
25
|
+
];
|
|
26
|
+
function eldraCms(options = {}) {
|
|
27
|
+
let config;
|
|
28
|
+
let generated = false;
|
|
29
|
+
return {
|
|
30
|
+
name: "eldra-cms",
|
|
31
|
+
enforce: "pre",
|
|
32
|
+
configResolved(resolvedConfig) {
|
|
33
|
+
config = resolvedConfig;
|
|
34
|
+
},
|
|
35
|
+
async buildStart() {
|
|
36
|
+
if (generated) return;
|
|
37
|
+
generated = true;
|
|
38
|
+
await generateEldraCmsFiles(config?.root ?? process.cwd(), options, config?.mode);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function generateEldraCmsFiles(root, options, mode = process.env.NODE_ENV ?? "development") {
|
|
43
|
+
const env = resolvePluginEnv(root, mode, options);
|
|
44
|
+
const apiBaseUrl = resolveRuntimeValue(options.apiBaseUrl) ?? readFirstEnvValue(env, apiBaseUrlEnvKeys) ?? "https://web.eldra.app/api";
|
|
45
|
+
const orgId = resolveRuntimeValue(options.orgId) ?? readFirstEnvValue(env, orgIdEnvKeys);
|
|
46
|
+
if (!orgId) {
|
|
47
|
+
if (options.skipOnMissingConfig) return;
|
|
48
|
+
throw new Error("Missing Eldra CMS generation config. Set orgId or ELDRA_ORG_ID. Set apiBaseUrl only for staging, local, or test gateways.");
|
|
49
|
+
}
|
|
50
|
+
const moduleName = options.moduleName ?? defaultModuleName;
|
|
51
|
+
const outDir = resolve(root, options.outDir ?? defaultOutDir);
|
|
52
|
+
const typesFileName = options.typesFileName ?? defaultTypesFileName;
|
|
53
|
+
const clientFileName = options.clientFileName ?? defaultClientFileName;
|
|
54
|
+
const indexFileName = options.indexFileName ?? defaultIndexFileName;
|
|
55
|
+
const typesSource = await fetchCmsTypesOrSkip(apiBaseUrl, orgId, {
|
|
56
|
+
fetch: options.fetch,
|
|
57
|
+
headers: resolveRuntimeValue(options.headers),
|
|
58
|
+
maxDepth: options.maxDepth,
|
|
59
|
+
moduleName,
|
|
60
|
+
schemas: options.schemas
|
|
61
|
+
});
|
|
62
|
+
if (!typesSource) return;
|
|
63
|
+
await mkdir(outDir, { recursive: true });
|
|
64
|
+
await writeGeneratedFile(resolve(outDir, typesFileName), typesSource);
|
|
65
|
+
await writeGeneratedFile(resolve(outDir, clientFileName), createGeneratedClientSource({
|
|
66
|
+
moduleName,
|
|
67
|
+
sdkImport: options.sdkImport ?? defaultSdkImport,
|
|
68
|
+
typesImport: `./${stripTSExtension(typesFileName)}`
|
|
69
|
+
}));
|
|
70
|
+
await writeGeneratedFile(resolve(outDir, indexFileName), `export * from './${stripTSExtension(clientFileName)}';\n`);
|
|
71
|
+
}
|
|
72
|
+
async function fetchCmsTypes(apiBaseUrl, orgId, options) {
|
|
73
|
+
const url = new URL(`${apiBaseUrl.replace(/\/$/, "")}/cms/v1/typescript-definitions`);
|
|
74
|
+
url.searchParams.set("moduleName", options.moduleName);
|
|
75
|
+
if (options.maxDepth !== void 0) url.searchParams.set("maxDepth", String(options.maxDepth));
|
|
76
|
+
if (options.schemas && options.schemas.length > 0) url.searchParams.set("schemas", options.schemas.join(","));
|
|
77
|
+
const headers = new Headers(options.headers);
|
|
78
|
+
headers.set("Accept", "text/plain");
|
|
79
|
+
headers.set("X-Org-Id", orgId);
|
|
80
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
81
|
+
if (!fetchImpl) throw new Error("No fetch implementation is available for Eldra CMS generation.");
|
|
82
|
+
const response = await fetchImpl(url, { headers });
|
|
83
|
+
const body = await response.text();
|
|
84
|
+
if (!response.ok) throw new Error(`Failed to generate Eldra CMS types: ${response.status} ${response.statusText}\n${body}`);
|
|
85
|
+
return body.endsWith("\n") ? body : `${body}\n`;
|
|
86
|
+
}
|
|
87
|
+
async function fetchCmsTypesOrSkip(apiBaseUrl, orgId, options) {
|
|
88
|
+
try {
|
|
89
|
+
return await fetchCmsTypes(apiBaseUrl, orgId, options);
|
|
90
|
+
} catch {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function createGeneratedClientSource(options) {
|
|
95
|
+
const prefix = pascalTypeName(options.moduleName) || defaultModuleName;
|
|
96
|
+
return `import {
|
|
97
|
+
createEldraClient,
|
|
98
|
+
getEldraClient,
|
|
99
|
+
initEldraClient,
|
|
100
|
+
type EldraClient,
|
|
101
|
+
type EldraClientOptions,
|
|
102
|
+
} from '${options.sdkImport}';
|
|
103
|
+
import type { ${prefix}Client } from '${options.typesImport}';
|
|
104
|
+
|
|
105
|
+
export * from '${options.typesImport}';
|
|
106
|
+
export type WebStudioCmsClient = ${prefix}Client & Pick<EldraClient['cms'], 'resolveEntryList'>;
|
|
107
|
+
export type WebStudioClientOptions = EldraClientOptions;
|
|
108
|
+
|
|
109
|
+
export interface WebStudioClient extends Omit<EldraClient, 'cms'> {
|
|
110
|
+
cms: WebStudioCmsClient;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function createWebStudioClient(options: WebStudioClientOptions): WebStudioClient {
|
|
114
|
+
return createEldraClient(options) as unknown as WebStudioClient;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function initWebStudioClient(options: WebStudioClientOptions): WebStudioClient {
|
|
118
|
+
return initEldraClient(options) as unknown as WebStudioClient;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function getWebStudioClient(): WebStudioClient {
|
|
122
|
+
return getEldraClient() as unknown as WebStudioClient;
|
|
123
|
+
}
|
|
124
|
+
`;
|
|
125
|
+
}
|
|
126
|
+
function resolvePluginEnv(root, mode, options) {
|
|
127
|
+
return {
|
|
128
|
+
...loadEnv(mode, root, ""),
|
|
129
|
+
...process.env,
|
|
130
|
+
...resolveRuntimeValue(options.env)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function readFirstEnvValue(env, keys) {
|
|
134
|
+
for (const key of keys) {
|
|
135
|
+
const value = env[key];
|
|
136
|
+
if (typeof value === "string" && value.trim() !== "") return value;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function resolveRuntimeValue(value) {
|
|
140
|
+
return typeof value === "function" ? value() : value;
|
|
141
|
+
}
|
|
142
|
+
async function writeGeneratedFile(path, source) {
|
|
143
|
+
await mkdir(dirname(path), { recursive: true });
|
|
144
|
+
await writeFile(path, source, "utf8");
|
|
145
|
+
}
|
|
146
|
+
function stripTSExtension(path) {
|
|
147
|
+
return path.replace(/\.ts$/, "");
|
|
148
|
+
}
|
|
149
|
+
function pascalTypeName(value) {
|
|
150
|
+
return value.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
export { eldraCms, generateEldraCmsFiles };
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eldrajs/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-free client for the Eldra Web Studio public storefront API: CMS, catalog, cart, checkout handoff, orders, inventory. Types generated from the versioned public contract.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cms",
|
|
7
|
+
"commerce",
|
|
8
|
+
"eldra",
|
|
9
|
+
"headless",
|
|
10
|
+
"sdk",
|
|
11
|
+
"storefront"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/eldra-is/headless-kit/tree/main/packages/sdk#readme",
|
|
14
|
+
"bugs": "https://github.com/eldra-is/headless-kit/issues",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/eldra-is/headless-kit.git",
|
|
19
|
+
"directory": "packages/sdk"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"CHANGELOG.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./vite": {
|
|
34
|
+
"types": "./dist/vite.d.ts",
|
|
35
|
+
"import": "./dist/vite.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"provenance": true
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"openapi-typescript": "^7.13.0",
|
|
45
|
+
"vite": "^8.0.8"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"vite": ">=5"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"vite": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=20"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsdown",
|
|
60
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"contract": "node scripts/contract.mjs",
|
|
63
|
+
"contract:check": "node scripts/contract.mjs --check"
|
|
64
|
+
}
|
|
65
|
+
}
|