@vard-app/sdk 0.1.0 → 0.1.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/README.md +30 -26
- package/dist/index.d.mts +30 -4
- package/dist/index.d.ts +30 -4
- package/dist/index.js +120 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +118 -10
- package/dist/index.mjs.map +1 -1
- package/dist/next.d.mts +13 -19
- package/dist/next.d.ts +13 -19
- package/dist/next.js +185 -16
- package/dist/next.js.map +1 -1
- package/dist/next.mjs +183 -17
- package/dist/next.mjs.map +1 -1
- package/dist/{types-DLM7i_Qr.d.mts → types-eJuYa65b.d.mts} +33 -2
- package/dist/{types-DLM7i_Qr.d.ts → types-eJuYa65b.d.ts} +33 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,34 +14,34 @@ pnpm add @vard-app/sdk
|
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
16
|
// lib/vard.ts
|
|
17
|
-
import { createVard } from "@vard-app/sdk"
|
|
18
|
-
import { createVardNextAdapter } from "@vard-app/sdk/next"
|
|
17
|
+
import { createVard } from "@vard-app/sdk";
|
|
18
|
+
import { createVardNextAdapter } from "@vard-app/sdk/next";
|
|
19
19
|
|
|
20
20
|
export const vard = createVard({
|
|
21
21
|
workspaceId: process.env.VARD_WORKSPACE_ID,
|
|
22
22
|
store: createVardNextAdapter(),
|
|
23
|
-
})
|
|
23
|
+
});
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
### 2. Use variables anywhere in your site
|
|
27
27
|
|
|
28
28
|
```tsx
|
|
29
29
|
// app/page.tsx
|
|
30
|
-
import { vard } from "@/lib/vard"
|
|
30
|
+
import { vard } from "@/lib/vard";
|
|
31
31
|
|
|
32
32
|
export default function HomePage() {
|
|
33
|
-
const heroTitle
|
|
34
|
-
const heroCopy
|
|
35
|
-
const primaryColor = vard.color("theme.primary", "#2563eb")
|
|
36
|
-
const heroImage
|
|
37
|
-
const showBanner
|
|
33
|
+
const heroTitle = vard.string("hero.title", "Welcome to our site");
|
|
34
|
+
const heroCopy = vard.richtext("hero.copy", "We build great things.");
|
|
35
|
+
const primaryColor = vard.color("theme.primary", "#2563eb");
|
|
36
|
+
const heroImage = vard.image("hero.image", "/default-hero.jpg");
|
|
37
|
+
const showBanner = vard.boolean("banner.show", false);
|
|
38
38
|
|
|
39
39
|
const team = vard.list(
|
|
40
40
|
"team.members",
|
|
41
41
|
{ name: "string", role: "string", photo: "image" },
|
|
42
42
|
[{ name: "Jane Doe", role: "Founder", photo: "/jane.jpg" }],
|
|
43
43
|
{ label: "Team Members" }
|
|
44
|
-
)
|
|
44
|
+
);
|
|
45
45
|
|
|
46
46
|
return (
|
|
47
47
|
<main style={{ "--primary": primaryColor } as React.CSSProperties}>
|
|
@@ -52,7 +52,7 @@ export default function HomePage() {
|
|
|
52
52
|
<TeamCard key={member.name} {...member} />
|
|
53
53
|
))}
|
|
54
54
|
</main>
|
|
55
|
-
)
|
|
55
|
+
);
|
|
56
56
|
}
|
|
57
57
|
```
|
|
58
58
|
|
|
@@ -60,25 +60,29 @@ export default function HomePage() {
|
|
|
60
60
|
|
|
61
61
|
```tsx
|
|
62
62
|
// app/layout.tsx
|
|
63
|
-
import { createVardNextAdapter, prefetchVardValues } from "@vard/sdk/next"
|
|
64
|
-
import { vard } from "@/lib/vard"
|
|
63
|
+
import { createVardNextAdapter, prefetchVardValues } from "@vard/sdk/next";
|
|
64
|
+
import { vard } from "@/lib/vard";
|
|
65
65
|
|
|
66
66
|
export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
67
|
-
await prefetchVardValues(vard.store as ReturnType<typeof createVardNextAdapter>)
|
|
68
|
-
return
|
|
67
|
+
await prefetchVardValues(vard.store as ReturnType<typeof createVardNextAdapter>);
|
|
68
|
+
return (
|
|
69
|
+
<html>
|
|
70
|
+
<body>{children}</body>
|
|
71
|
+
</html>
|
|
72
|
+
);
|
|
69
73
|
}
|
|
70
74
|
```
|
|
71
75
|
|
|
72
76
|
## Variable types
|
|
73
77
|
|
|
74
|
-
| Method
|
|
75
|
-
|
|
76
|
-
| `vard.string()`
|
|
77
|
-
| `vard.richtext()` | Markdown
|
|
78
|
-
| `vard.color()`
|
|
79
|
-
| `vard.image()`
|
|
80
|
-
| `vard.boolean()`
|
|
81
|
-
| `vard.list()`
|
|
78
|
+
| Method | Type | Client UI |
|
|
79
|
+
| ----------------- | ---------------------- | ---------------- |
|
|
80
|
+
| `vard.string()` | Plain text | Text input |
|
|
81
|
+
| `vard.richtext()` | Markdown | Rich text editor |
|
|
82
|
+
| `vard.color()` | CSS color string | Color picker |
|
|
83
|
+
| `vard.image()` | Asset URL | Image uploader |
|
|
84
|
+
| `vard.boolean()` | `true` / `false` | Toggle switch |
|
|
85
|
+
| `vard.list()` | Array of typed objects | Repeater |
|
|
82
86
|
|
|
83
87
|
## Permissions
|
|
84
88
|
|
|
@@ -88,7 +92,7 @@ Each variable can restrict which role can edit it:
|
|
|
88
92
|
vard.color("theme.primary", "#2563eb", {
|
|
89
93
|
label: "Brand Color",
|
|
90
94
|
editableBy: "owner", // only owners can change this
|
|
91
|
-
})
|
|
95
|
+
});
|
|
92
96
|
```
|
|
93
97
|
|
|
94
98
|
Roles: `owner` > `developer` > `member` > `viewer`
|
|
@@ -99,6 +103,6 @@ If `VARD_WORKSPACE_ID` is not set, the SDK returns default values for every vari
|
|
|
99
103
|
|
|
100
104
|
## Environment variables
|
|
101
105
|
|
|
102
|
-
| Variable
|
|
103
|
-
|
|
106
|
+
| Variable | Description |
|
|
107
|
+
| ------------------- | ---------------------- |
|
|
104
108
|
| `VARD_WORKSPACE_ID` | Your Vard workspace ID |
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { V as VardOptions, a as VardClient } from './types-
|
|
2
|
-
export { I as InferListItem,
|
|
1
|
+
import { V as VardOptions, a as VardClient, b as VardStore } from './types-eJuYa65b.mjs';
|
|
2
|
+
export { I as InferListItem, c as VardListItemSchema, d as VardRole, e as VardVariableDefinition, f as VardVariableOptions, g as VardVariableType } from './types-eJuYa65b.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Creates a Vard client instance. Call this once at the top of your site
|
|
@@ -8,7 +8,7 @@ export { I as InferListItem, b as VardListItemSchema, c as VardRole, d as VardSt
|
|
|
8
8
|
* @example
|
|
9
9
|
* // lib/vard.ts
|
|
10
10
|
* import { createVard } from "@vard/sdk"
|
|
11
|
-
* export const vard = createVard()
|
|
11
|
+
* export const vard = createVard({ apiKey: "..." })
|
|
12
12
|
*
|
|
13
13
|
* // app/page.tsx
|
|
14
14
|
* import { vard } from "@/lib/vard"
|
|
@@ -16,4 +16,30 @@ export { I as InferListItem, b as VardListItemSchema, c as VardRole, d as VardSt
|
|
|
16
16
|
*/
|
|
17
17
|
declare function createVard(options?: VardOptions): VardClient;
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
interface VardFetchStoreOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Base URL of the Vard API. Defaults to https://api.vard.app
|
|
22
|
+
*/
|
|
23
|
+
apiBase?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Workspace ID (Legacy). Defaults to process.env.VARD_WORKSPACE_ID.
|
|
26
|
+
*/
|
|
27
|
+
workspaceId?: string;
|
|
28
|
+
/**
|
|
29
|
+
* API Key for authentication. Defaults to process.env.VARD_API_KEY.
|
|
30
|
+
*/
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Custom fetch options (headers, etc.)
|
|
34
|
+
*/
|
|
35
|
+
fetchOptions?: RequestInit;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a universal VardStore that fetches variable values from the Vard API.
|
|
39
|
+
* Works in any environment with a global `fetch` (Browsers, Node 18+, Bun, Deno).
|
|
40
|
+
*/
|
|
41
|
+
declare function createVardFetchStore(options?: VardFetchStoreOptions): VardStore & {
|
|
42
|
+
prefetch(): Promise<void>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { VardClient, VardOptions, VardStore, createVard, createVardFetchStore };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { V as VardOptions, a as VardClient } from './types-
|
|
2
|
-
export { I as InferListItem,
|
|
1
|
+
import { V as VardOptions, a as VardClient, b as VardStore } from './types-eJuYa65b.js';
|
|
2
|
+
export { I as InferListItem, c as VardListItemSchema, d as VardRole, e as VardVariableDefinition, f as VardVariableOptions, g as VardVariableType } from './types-eJuYa65b.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Creates a Vard client instance. Call this once at the top of your site
|
|
@@ -8,7 +8,7 @@ export { I as InferListItem, b as VardListItemSchema, c as VardRole, d as VardSt
|
|
|
8
8
|
* @example
|
|
9
9
|
* // lib/vard.ts
|
|
10
10
|
* import { createVard } from "@vard/sdk"
|
|
11
|
-
* export const vard = createVard()
|
|
11
|
+
* export const vard = createVard({ apiKey: "..." })
|
|
12
12
|
*
|
|
13
13
|
* // app/page.tsx
|
|
14
14
|
* import { vard } from "@/lib/vard"
|
|
@@ -16,4 +16,30 @@ export { I as InferListItem, b as VardListItemSchema, c as VardRole, d as VardSt
|
|
|
16
16
|
*/
|
|
17
17
|
declare function createVard(options?: VardOptions): VardClient;
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
interface VardFetchStoreOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Base URL of the Vard API. Defaults to https://api.vard.app
|
|
22
|
+
*/
|
|
23
|
+
apiBase?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Workspace ID (Legacy). Defaults to process.env.VARD_WORKSPACE_ID.
|
|
26
|
+
*/
|
|
27
|
+
workspaceId?: string;
|
|
28
|
+
/**
|
|
29
|
+
* API Key for authentication. Defaults to process.env.VARD_API_KEY.
|
|
30
|
+
*/
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Custom fetch options (headers, etc.)
|
|
34
|
+
*/
|
|
35
|
+
fetchOptions?: RequestInit;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a universal VardStore that fetches variable values from the Vard API.
|
|
39
|
+
* Works in any environment with a global `fetch` (Browsers, Node 18+, Bun, Deno).
|
|
40
|
+
*/
|
|
41
|
+
declare function createVardFetchStore(options?: VardFetchStoreOptions): VardStore & {
|
|
42
|
+
prefetch(): Promise<void>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { VardClient, VardOptions, VardStore, createVard, createVardFetchStore };
|
package/dist/index.js
CHANGED
|
@@ -20,16 +20,84 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
createVard: () => createVard
|
|
23
|
+
createVard: () => createVard,
|
|
24
|
+
createVardFetchStore: () => createVardFetchStore
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(src_exports);
|
|
26
27
|
|
|
28
|
+
// src/fetch-store.ts
|
|
29
|
+
function createVardFetchStore(options = {}) {
|
|
30
|
+
const {
|
|
31
|
+
apiBase = "https://api.vard.app",
|
|
32
|
+
workspaceId = options.workspaceId ?? process.env.VARD_WORKSPACE_ID,
|
|
33
|
+
apiKey = options.apiKey ?? process.env.VARD_API_KEY,
|
|
34
|
+
fetchOptions = {}
|
|
35
|
+
} = options;
|
|
36
|
+
let resolvedValues = null;
|
|
37
|
+
let fetchPromise = null;
|
|
38
|
+
async function fetchValues() {
|
|
39
|
+
const isDevelopment = process.env.NODE_ENV === "development";
|
|
40
|
+
if (!apiKey && !workspaceId) {
|
|
41
|
+
if (!isDevelopment) {
|
|
42
|
+
console.error(
|
|
43
|
+
"\x1B[31m[vard] Missing configuration! VARD_API_KEY is not set.\x1B[0m\nPlease set this environment variable in your production environment."
|
|
44
|
+
);
|
|
45
|
+
} else {
|
|
46
|
+
console.warn(
|
|
47
|
+
"[vard] No API Key found. Running in local fallback mode (using default values)."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return /* @__PURE__ */ new Map();
|
|
51
|
+
}
|
|
52
|
+
const url = apiKey ? `${apiBase}/api/content/variables` : `${apiBase}/v1/workspaces/${workspaceId}/variables`;
|
|
53
|
+
try {
|
|
54
|
+
const res = await fetch(url, {
|
|
55
|
+
...fetchOptions,
|
|
56
|
+
headers: {
|
|
57
|
+
...fetchOptions.headers,
|
|
58
|
+
...apiKey ? { "X-Vard-API-Key": apiKey } : {}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (!res.ok) {
|
|
62
|
+
console.warn(`[vard] Failed to fetch variables: ${res.status}`);
|
|
63
|
+
return /* @__PURE__ */ new Map();
|
|
64
|
+
}
|
|
65
|
+
const data = await res.json();
|
|
66
|
+
return new Map(data.map((v) => [v.key, v.value]));
|
|
67
|
+
} catch (err) {
|
|
68
|
+
console.warn("[vard] Error fetching variable values:", err);
|
|
69
|
+
return /* @__PURE__ */ new Map();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
get(key) {
|
|
74
|
+
return resolvedValues?.get(key);
|
|
75
|
+
},
|
|
76
|
+
async prefetch() {
|
|
77
|
+
if (resolvedValues) return;
|
|
78
|
+
if (!fetchPromise) {
|
|
79
|
+
fetchPromise = fetchValues();
|
|
80
|
+
}
|
|
81
|
+
resolvedValues = await fetchPromise;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
27
86
|
// src/client.ts
|
|
87
|
+
var hasLoggedNoop = false;
|
|
28
88
|
var noopStore = {
|
|
29
|
-
get: () =>
|
|
89
|
+
get: () => {
|
|
90
|
+
if (!hasLoggedNoop && process.env.NODE_ENV !== "test") {
|
|
91
|
+
console.warn(
|
|
92
|
+
"[vard] Using default values for all variables because no Store was provided to createVard()."
|
|
93
|
+
);
|
|
94
|
+
hasLoggedNoop = true;
|
|
95
|
+
}
|
|
96
|
+
return void 0;
|
|
97
|
+
}
|
|
30
98
|
};
|
|
31
99
|
function createVard(options = {}) {
|
|
32
|
-
const store = options.store ?? noopStore;
|
|
100
|
+
const store = options.store ?? (options.apiKey || options.workspaceId ? createVardFetchStore(options) : noopStore);
|
|
33
101
|
const registry = [];
|
|
34
102
|
function register(def) {
|
|
35
103
|
if (!registry.find((d) => d.key === def.key)) {
|
|
@@ -49,7 +117,8 @@ function createVard(options = {}) {
|
|
|
49
117
|
description: opts.description,
|
|
50
118
|
type: "string",
|
|
51
119
|
defaultValue: fallback,
|
|
52
|
-
editableBy: opts.editableBy ?? "member"
|
|
120
|
+
editableBy: opts.editableBy ?? "member",
|
|
121
|
+
group: opts.group
|
|
53
122
|
});
|
|
54
123
|
},
|
|
55
124
|
richtext(key, fallback, opts = {}) {
|
|
@@ -59,7 +128,8 @@ function createVard(options = {}) {
|
|
|
59
128
|
description: opts.description,
|
|
60
129
|
type: "richtext",
|
|
61
130
|
defaultValue: fallback,
|
|
62
|
-
editableBy: opts.editableBy ?? "member"
|
|
131
|
+
editableBy: opts.editableBy ?? "member",
|
|
132
|
+
group: opts.group
|
|
63
133
|
});
|
|
64
134
|
},
|
|
65
135
|
color(key, fallback, opts = {}) {
|
|
@@ -69,7 +139,8 @@ function createVard(options = {}) {
|
|
|
69
139
|
description: opts.description,
|
|
70
140
|
type: "color",
|
|
71
141
|
defaultValue: fallback,
|
|
72
|
-
editableBy: opts.editableBy ?? "member"
|
|
142
|
+
editableBy: opts.editableBy ?? "member",
|
|
143
|
+
group: opts.group
|
|
73
144
|
});
|
|
74
145
|
},
|
|
75
146
|
image(key, fallback, opts = {}) {
|
|
@@ -79,7 +150,8 @@ function createVard(options = {}) {
|
|
|
79
150
|
description: opts.description,
|
|
80
151
|
type: "image",
|
|
81
152
|
defaultValue: fallback,
|
|
82
|
-
editableBy: opts.editableBy ?? "member"
|
|
153
|
+
editableBy: opts.editableBy ?? "member",
|
|
154
|
+
group: opts.group
|
|
83
155
|
});
|
|
84
156
|
},
|
|
85
157
|
boolean(key, fallback, opts = {}) {
|
|
@@ -89,7 +161,8 @@ function createVard(options = {}) {
|
|
|
89
161
|
description: opts.description,
|
|
90
162
|
type: "boolean",
|
|
91
163
|
defaultValue: fallback,
|
|
92
|
-
editableBy: opts.editableBy ?? "member"
|
|
164
|
+
editableBy: opts.editableBy ?? "member",
|
|
165
|
+
group: opts.group
|
|
93
166
|
});
|
|
94
167
|
},
|
|
95
168
|
list(key, schema, fallback, opts = {}) {
|
|
@@ -100,12 +173,47 @@ function createVard(options = {}) {
|
|
|
100
173
|
type: "list",
|
|
101
174
|
defaultValue: fallback,
|
|
102
175
|
editableBy: opts.editableBy ?? "member",
|
|
103
|
-
listItemSchema: schema
|
|
176
|
+
listItemSchema: schema,
|
|
177
|
+
group: opts.group
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
collection(key, schema, fallback, opts = {}) {
|
|
181
|
+
return register({
|
|
182
|
+
key,
|
|
183
|
+
label: opts.label ?? labelFromKey(key),
|
|
184
|
+
description: opts.description,
|
|
185
|
+
type: "list",
|
|
186
|
+
defaultValue: fallback,
|
|
187
|
+
editableBy: opts.editableBy ?? "member",
|
|
188
|
+
listItemSchema: schema,
|
|
189
|
+
group: opts.group,
|
|
190
|
+
isCollection: true
|
|
104
191
|
});
|
|
105
192
|
},
|
|
193
|
+
global: {
|
|
194
|
+
string(key, fallback, opts = {}) {
|
|
195
|
+
return client.string(key, fallback, { ...opts, group: "Global" });
|
|
196
|
+
},
|
|
197
|
+
richtext(key, fallback, opts = {}) {
|
|
198
|
+
return client.richtext(key, fallback, { ...opts, group: "Global" });
|
|
199
|
+
},
|
|
200
|
+
color(key, fallback, opts = {}) {
|
|
201
|
+
return client.color(key, fallback, { ...opts, group: "Global" });
|
|
202
|
+
},
|
|
203
|
+
image(key, fallback, opts = {}) {
|
|
204
|
+
return client.image(key, fallback, { ...opts, group: "Global" });
|
|
205
|
+
},
|
|
206
|
+
boolean(key, fallback, opts = {}) {
|
|
207
|
+
return client.boolean(key, fallback, { ...opts, group: "Global" });
|
|
208
|
+
},
|
|
209
|
+
list(key, schema, fallback, opts = {}) {
|
|
210
|
+
return client.list(key, schema, fallback, { ...opts, group: "Global" });
|
|
211
|
+
}
|
|
212
|
+
},
|
|
106
213
|
getDefinitions() {
|
|
107
214
|
return [...registry];
|
|
108
|
-
}
|
|
215
|
+
},
|
|
216
|
+
store
|
|
109
217
|
};
|
|
110
218
|
return client;
|
|
111
219
|
}
|
|
@@ -115,6 +223,7 @@ function labelFromKey(key) {
|
|
|
115
223
|
}
|
|
116
224
|
// Annotate the CommonJS export names for ESM import in node:
|
|
117
225
|
0 && (module.exports = {
|
|
118
|
-
createVard
|
|
226
|
+
createVard,
|
|
227
|
+
createVardFetchStore
|
|
119
228
|
});
|
|
120
229
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/client.ts"],"sourcesContent":["// Public entrypoint for @vard/sdk\nexport { createVard } from \"./client\";\nexport type {\n VardClient,\n VardOptions,\n VardStore,\n VardVariableDefinition,\n VardVariableType,\n VardVariableOptions,\n VardListItemSchema,\n InferListItem,\n VardRole,\n} from \"./types\";\n","import type {\n VardClient,\n VardOptions,\n VardStore,\n VardVariableDefinition,\n VardVariableOptions,\n VardListItemSchema,\n InferListItem,\n VardRole,\n} from \"./types\";\n\n// ─────────────────────────────────────────────\n// No-op store (used in local dev when no workspaceId is set)\n// ─────────────────────────────────────────────\n\nconst noopStore: VardStore = {\n get: () => undefined,\n};\n\n// ─────────────────────────────────────────────\n// createVard — the main SDK factory\n// ─────────────────────────────────────────────\n\n/**\n * Creates a Vard client instance. Call this once at the top of your site\n * (e.g. in `lib/vard.ts`) and export the result.\n *\n * @example\n * // lib/vard.ts\n * import { createVard } from \"@vard/sdk\"\n * export const vard = createVard()\n *\n * // app/page.tsx\n * import { vard } from \"@/lib/vard\"\n * const title = vard.string(\"hero.title\", \"Hello, world\")\n */\nexport function createVard(options: VardOptions = {}): VardClient {\n const store: VardStore = options.store ?? noopStore;\n\n // Registry of all declared variables — used by CLI + build pipeline\n const registry: VardVariableDefinition[] = [];\n\n function register<T>(def: VardVariableDefinition<T>): T {\n // Avoid double-registering the same key (e.g. in hot-reload scenarios)\n if (!registry.find((d) => d.key === def.key)) {\n registry.push(def as VardVariableDefinition);\n }\n\n // Resolve value: stored client value takes precedence over the default\n const stored = store.get(def.key);\n if (stored !== undefined && stored !== null) {\n return stored as T;\n }\n\n return def.defaultValue;\n }\n\n const client: VardClient = {\n string(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"string\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n });\n },\n\n richtext(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"richtext\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n });\n },\n\n color(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"color\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n });\n },\n\n image(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"image\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n });\n },\n\n boolean(key, fallback, opts: VardVariableOptions = {}) {\n return register<boolean>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"boolean\",\n defaultValue: fallback,\n editableBy: (opts.editableBy as VardRole) ?? \"member\",\n });\n },\n\n list<S extends VardListItemSchema>(\n key: string,\n schema: S,\n fallback: InferListItem<S>[],\n opts: VardVariableOptions = {}\n ): InferListItem<S>[] {\n return register<InferListItem<S>[]>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"list\",\n defaultValue: fallback,\n editableBy: (opts.editableBy as VardRole) ?? \"member\",\n listItemSchema: schema,\n });\n },\n\n getDefinitions() {\n return [...registry];\n },\n };\n\n return client;\n}\n\n// ─────────────────────────────────────────────\n// Helpers\n// ─────────────────────────────────────────────\n\n/**\n * Converts a dot-notation key to a human-readable label.\n * e.g. \"hero.primaryTitle\" → \"Hero Primary Title\"\n */\nfunction labelFromKey(key: string): string {\n const lastSegment = key.split(\".\").pop() ?? key;\n // Split on camelCase boundaries and capitalise\n return lastSegment\n .replace(/([A-Z])/g, \" $1\")\n .replace(/^./, (c) => c.toUpperCase())\n .trim();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACeA,IAAM,YAAuB;AAAA,EAC3B,KAAK,MAAM;AACb;AAmBO,SAAS,WAAW,UAAuB,CAAC,GAAe;AAChE,QAAM,QAAmB,QAAQ,SAAS;AAG1C,QAAM,WAAqC,CAAC;AAE5C,WAAS,SAAY,KAAmC;AAEtD,QAAI,CAAC,SAAS,KAAK,CAAC,MAAM,EAAE,QAAQ,IAAI,GAAG,GAAG;AAC5C,eAAS,KAAK,GAA6B;AAAA,IAC7C;AAGA,UAAM,SAAS,MAAM,IAAI,IAAI,GAAG;AAChC,QAAI,WAAW,UAAa,WAAW,MAAM;AAC3C,aAAO;AAAA,IACT;AAEA,WAAO,IAAI;AAAA,EACb;AAEA,QAAM,SAAqB;AAAA,IACzB,OAAO,KAAK,UAAU,OAA4B,CAAC,GAAG;AACpD,aAAO,SAAiB;AAAA,QACtB;AAAA,QACA,OAAO,KAAK,SAAS,aAAa,GAAG;AAAA,QACrC,aAAa,KAAK;AAAA,QAClB,MAAM;AAAA,QACN,cAAc;AAAA,QACd,YAAY,KAAK,cAAc;AAAA,MACjC,CAAC;AAAA,IACH;AAAA,IAEA,SAAS,KAAK,UAAU,OAA4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;AAAA,IACrB;AAAA,EACF;AAEA,SAAO;AACT;AAUA,SAAS,aAAa,KAAqB;AACzC,QAAM,cAAc,IAAI,MAAM,GAAG,EAAE,IAAI,KAAK;AAE5C,SAAO,YACJ,QAAQ,YAAY,KAAK,EACzB,QAAQ,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,EACpC,KAAK;AACV;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/fetch-store.ts","../src/client.ts"],"sourcesContent":["// Public entrypoint for @vard/sdk\nexport { createVard } from \"./client\";\nexport { createVardFetchStore } from \"./fetch-store\";\nexport type {\n VardClient,\n VardOptions,\n VardStore,\n VardVariableDefinition,\n VardVariableType,\n VardVariableOptions,\n VardListItemSchema,\n InferListItem,\n VardRole,\n} from \"./types\";\n","import type { VardStore } from \"./types\";\n\nexport interface VardFetchStoreOptions {\n /**\n * Base URL of the Vard API. Defaults to https://api.vard.app\n */\n apiBase?: string;\n\n /**\n * Workspace ID (Legacy). Defaults to process.env.VARD_WORKSPACE_ID.\n */\n workspaceId?: string;\n\n /**\n * API Key for authentication. Defaults to process.env.VARD_API_KEY.\n */\n apiKey?: string;\n\n /**\n * Custom fetch options (headers, etc.)\n */\n fetchOptions?: RequestInit;\n}\n\n/**\n * Creates a universal VardStore that fetches variable values from the Vard API.\n * Works in any environment with a global `fetch` (Browsers, Node 18+, Bun, Deno).\n */\nexport function createVardFetchStore(\n options: VardFetchStoreOptions = {}\n): VardStore & { prefetch(): Promise<void> } {\n const {\n apiBase = \"https://api.vard.app\",\n workspaceId = options.workspaceId ?? process.env.VARD_WORKSPACE_ID,\n apiKey = options.apiKey ?? process.env.VARD_API_KEY,\n fetchOptions = {},\n } = options;\n\n let resolvedValues: Map<string, unknown> | null = null;\n let fetchPromise: Promise<Map<string, unknown>> | null = null;\n\n async function fetchValues(): Promise<Map<string, unknown>> {\n const isDevelopment = process.env.NODE_ENV === \"development\";\n\n if (!apiKey && !workspaceId) {\n if (!isDevelopment) {\n console.error(\n \"\\x1b[31m[vard] Missing configuration! VARD_API_KEY is not set.\\x1b[0m\\n\" +\n \"Please set this environment variable in your production environment.\"\n );\n } else {\n console.warn(\n \"[vard] No API Key found. Running in local fallback mode (using default values).\"\n );\n }\n return new Map();\n }\n\n const url = apiKey\n ? `${apiBase}/api/content/variables`\n : `${apiBase}/v1/workspaces/${workspaceId}/variables`;\n\n try {\n const res = await fetch(url, {\n ...fetchOptions,\n headers: {\n ...fetchOptions.headers,\n ...(apiKey ? { \"X-Vard-API-Key\": apiKey } : {}),\n },\n });\n\n if (!res.ok) {\n console.warn(`[vard] Failed to fetch variables: ${res.status}`);\n return new Map();\n }\n\n const data = (await res.json()) as { key: string; value: unknown }[];\n return new Map(data.map((v) => [v.key, v.value]));\n } catch (err) {\n console.warn(\"[vard] Error fetching variable values:\", err);\n return new Map();\n }\n }\n\n return {\n get(key: string): unknown {\n return resolvedValues?.get(key);\n },\n\n async prefetch(): Promise<void> {\n if (resolvedValues) return;\n if (!fetchPromise) {\n fetchPromise = fetchValues();\n }\n resolvedValues = await fetchPromise;\n },\n };\n}\n","import type {\n VardClient,\n VardOptions,\n VardStore,\n VardVariableDefinition,\n VardVariableOptions,\n VardListItemSchema,\n InferListItem,\n VardRole,\n} from \"./types\";\n\n// ─────────────────────────────────────────────\n// No-op store (used in local dev when no workspaceId is set)\n// ─────────────────────────────────────────────\n\nlet hasLoggedNoop = false;\nconst noopStore: VardStore = {\n get: () => {\n if (!hasLoggedNoop && process.env.NODE_ENV !== \"test\") {\n console.warn(\n \"[vard] Using default values for all variables because no Store was provided to createVard().\"\n );\n hasLoggedNoop = true;\n }\n return undefined;\n },\n};\n\n// ─────────────────────────────────────────────\n// createVard — the main SDK factory\n// ─────────────────────────────────────────────\n\nimport { createVardFetchStore } from \"./fetch-store\";\n\n/**\n * Creates a Vard client instance. Call this once at the top of your site\n * (e.g. in `lib/vard.ts`) and export the result.\n *\n * @example\n * // lib/vard.ts\n * import { createVard } from \"@vard/sdk\"\n * export const vard = createVard({ apiKey: \"...\" })\n *\n * // app/page.tsx\n * import { vard } from \"@/lib/vard\"\n * const title = vard.string(\"hero.title\", \"Hello, world\")\n */\nexport function createVard(options: VardOptions = {}): VardClient {\n const store: VardStore =\n options.store ??\n (options.apiKey || options.workspaceId ? createVardFetchStore(options) : noopStore);\n\n // Registry of all declared variables — used by CLI + build pipeline\n const registry: VardVariableDefinition[] = [];\n\n function register<T>(def: VardVariableDefinition<T>): T {\n // Avoid double-registering the same key (e.g. in hot-reload scenarios)\n if (!registry.find((d) => d.key === def.key)) {\n registry.push(def as VardVariableDefinition);\n }\n\n // Resolve value: stored client value takes precedence over the default\n const stored = store.get(def.key);\n if (stored !== undefined && stored !== null) {\n return stored as T;\n }\n\n return def.defaultValue;\n }\n\n const client: VardClient = {\n string(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"string\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n group: opts.group,\n });\n },\n\n richtext(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"richtext\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n group: opts.group,\n });\n },\n\n color(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"color\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n group: opts.group,\n });\n },\n\n image(key, fallback, opts: VardVariableOptions = {}) {\n return register<string>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"image\",\n defaultValue: fallback,\n editableBy: opts.editableBy ?? \"member\",\n group: opts.group,\n });\n },\n\n boolean(key, fallback, opts: VardVariableOptions = {}) {\n return register<boolean>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"boolean\",\n defaultValue: fallback,\n editableBy: (opts.editableBy as VardRole) ?? \"member\",\n group: opts.group,\n });\n },\n\n list<S extends VardListItemSchema>(\n key: string,\n schema: S,\n fallback: InferListItem<S>[],\n opts: VardVariableOptions = {}\n ): InferListItem<S>[] {\n return register<InferListItem<S>[]>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"list\",\n defaultValue: fallback,\n editableBy: (opts.editableBy as VardRole) ?? \"member\",\n listItemSchema: schema,\n group: opts.group,\n });\n },\n\n collection<S extends VardListItemSchema>(\n key: string,\n schema: S,\n fallback: InferListItem<S>[],\n opts: Omit<VardVariableOptions, \"type\"> = {}\n ): InferListItem<S>[] {\n return register<InferListItem<S>[]>({\n key,\n label: opts.label ?? labelFromKey(key),\n description: opts.description,\n type: \"list\",\n defaultValue: fallback,\n editableBy: (opts.editableBy as VardRole) ?? \"member\",\n listItemSchema: schema,\n group: opts.group,\n isCollection: true,\n });\n },\n\n global: {\n string(key, fallback, opts = {}) {\n return client.string(key, fallback, { ...opts, group: \"Global\" } as VardVariableOptions);\n },\n richtext(key, fallback, opts = {}) {\n return client.richtext(key, fallback, { ...opts, group: \"Global\" } as VardVariableOptions);\n },\n color(key, fallback, opts = {}) {\n return client.color(key, fallback, { ...opts, group: \"Global\" } as VardVariableOptions);\n },\n image(key, fallback, opts = {}) {\n return client.image(key, fallback, { ...opts, group: \"Global\" } as VardVariableOptions);\n },\n boolean(key, fallback, opts = {}) {\n return client.boolean(key, fallback, { ...opts, group: \"Global\" } as Omit<\n VardVariableOptions,\n \"type\"\n >);\n },\n list(key, schema, fallback, opts = {}) {\n return client.list(key, schema, fallback, { ...opts, group: \"Global\" } as Omit<\n VardVariableOptions,\n \"type\"\n >);\n },\n },\n\n getDefinitions() {\n return [...registry];\n },\n store,\n };\n\n return client;\n}\n\n// ─────────────────────────────────────────────\n// Helpers\n// ─────────────────────────────────────────────\n\n/**\n * Converts a dot-notation key to a human-readable label.\n * e.g. \"hero.primaryTitle\" → \"Hero Primary Title\"\n */\nfunction labelFromKey(key: string): string {\n const lastSegment = key.split(\".\").pop() ?? key;\n // Split on camelCase boundaries and capitalise\n return lastSegment\n .replace(/([A-Z])/g, \" $1\")\n .replace(/^./, (c) => c.toUpperCase())\n .trim();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC4BO,SAAS,qBACd,UAAiC,CAAC,GACS;AAC3C,QAAM;AAAA,IACJ,UAAU;AAAA,IACV,cAAc,QAAQ,eAAe,QAAQ,IAAI;AAAA,IACjD,SAAS,QAAQ,UAAU,QAAQ,IAAI;AAAA,IACvC,eAAe,CAAC;AAAA,EAClB,IAAI;AAEJ,MAAI,iBAA8C;AAClD,MAAI,eAAqD;AAEzD,iBAAe,cAA6C;AAC1D,UAAM,gBAAgB,QAAQ,IAAI,aAAa;AAE/C,QAAI,CAAC,UAAU,CAAC,aAAa;AAC3B,UAAI,CAAC,eAAe;AAClB,gBAAQ;AAAA,UACN;AAAA,QAEF;AAAA,MACF,OAAO;AACL,gBAAQ;AAAA,UACN;AAAA,QACF;AAAA,MACF;AACA,aAAO,oBAAI,IAAI;AAAA,IACjB;AAEA,UAAM,MAAM,SACR,GAAG,OAAO,2BACV,GAAG,OAAO,kBAAkB,WAAW;AAE3C,QAAI;AACF,YAAM,MAAM,MAAM,MAAM,KAAK;AAAA,QAC3B,GAAG;AAAA,QACH,SAAS;AAAA,UACP,GAAG,aAAa;AAAA,UAChB,GAAI,SAAS,EAAE,kBAAkB,OAAO,IAAI,CAAC;AAAA,QAC/C;AAAA,MACF,CAAC;AAED,UAAI,CAAC,IAAI,IAAI;AACX,gBAAQ,KAAK,qCAAqC,IAAI,MAAM,EAAE;AAC9D,eAAO,oBAAI,IAAI;AAAA,MACjB;AAEA,YAAM,OAAQ,MAAM,IAAI,KAAK;AAC7B,aAAO,IAAI,IAAI,KAAK,IAAI,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAAA,IAClD,SA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AAAA,IACF;AAAA,IAEA,iBAAiB;AACf,aAAO,CAAC,GAAG,QAAQ;AAAA,IACrB;AAAA,IACA;AAAA,EACF;AAEA,SAAO;AACT;AAUA,SAAS,aAAa,KAAqB;AACzC,QAAM,cAAc,IAAI,MAAM,GAAG,EAAE,IAAI,KAAK;AAE5C,SAAO,YACJ,QAAQ,YAAY,KAAK,EACzB,QAAQ,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,EACpC,KAAK;AACV;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,76 @@
|
|
|
1
|
+
// src/fetch-store.ts
|
|
2
|
+
function createVardFetchStore(options = {}) {
|
|
3
|
+
const {
|
|
4
|
+
apiBase = "https://api.vard.app",
|
|
5
|
+
workspaceId = options.workspaceId ?? process.env.VARD_WORKSPACE_ID,
|
|
6
|
+
apiKey = options.apiKey ?? process.env.VARD_API_KEY,
|
|
7
|
+
fetchOptions = {}
|
|
8
|
+
} = options;
|
|
9
|
+
let resolvedValues = null;
|
|
10
|
+
let fetchPromise = null;
|
|
11
|
+
async function fetchValues() {
|
|
12
|
+
const isDevelopment = process.env.NODE_ENV === "development";
|
|
13
|
+
if (!apiKey && !workspaceId) {
|
|
14
|
+
if (!isDevelopment) {
|
|
15
|
+
console.error(
|
|
16
|
+
"\x1B[31m[vard] Missing configuration! VARD_API_KEY is not set.\x1B[0m\nPlease set this environment variable in your production environment."
|
|
17
|
+
);
|
|
18
|
+
} else {
|
|
19
|
+
console.warn(
|
|
20
|
+
"[vard] No API Key found. Running in local fallback mode (using default values)."
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return /* @__PURE__ */ new Map();
|
|
24
|
+
}
|
|
25
|
+
const url = apiKey ? `${apiBase}/api/content/variables` : `${apiBase}/v1/workspaces/${workspaceId}/variables`;
|
|
26
|
+
try {
|
|
27
|
+
const res = await fetch(url, {
|
|
28
|
+
...fetchOptions,
|
|
29
|
+
headers: {
|
|
30
|
+
...fetchOptions.headers,
|
|
31
|
+
...apiKey ? { "X-Vard-API-Key": apiKey } : {}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
if (!res.ok) {
|
|
35
|
+
console.warn(`[vard] Failed to fetch variables: ${res.status}`);
|
|
36
|
+
return /* @__PURE__ */ new Map();
|
|
37
|
+
}
|
|
38
|
+
const data = await res.json();
|
|
39
|
+
return new Map(data.map((v) => [v.key, v.value]));
|
|
40
|
+
} catch (err) {
|
|
41
|
+
console.warn("[vard] Error fetching variable values:", err);
|
|
42
|
+
return /* @__PURE__ */ new Map();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
get(key) {
|
|
47
|
+
return resolvedValues?.get(key);
|
|
48
|
+
},
|
|
49
|
+
async prefetch() {
|
|
50
|
+
if (resolvedValues) return;
|
|
51
|
+
if (!fetchPromise) {
|
|
52
|
+
fetchPromise = fetchValues();
|
|
53
|
+
}
|
|
54
|
+
resolvedValues = await fetchPromise;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
1
59
|
// src/client.ts
|
|
60
|
+
var hasLoggedNoop = false;
|
|
2
61
|
var noopStore = {
|
|
3
|
-
get: () =>
|
|
62
|
+
get: () => {
|
|
63
|
+
if (!hasLoggedNoop && process.env.NODE_ENV !== "test") {
|
|
64
|
+
console.warn(
|
|
65
|
+
"[vard] Using default values for all variables because no Store was provided to createVard()."
|
|
66
|
+
);
|
|
67
|
+
hasLoggedNoop = true;
|
|
68
|
+
}
|
|
69
|
+
return void 0;
|
|
70
|
+
}
|
|
4
71
|
};
|
|
5
72
|
function createVard(options = {}) {
|
|
6
|
-
const store = options.store ?? noopStore;
|
|
73
|
+
const store = options.store ?? (options.apiKey || options.workspaceId ? createVardFetchStore(options) : noopStore);
|
|
7
74
|
const registry = [];
|
|
8
75
|
function register(def) {
|
|
9
76
|
if (!registry.find((d) => d.key === def.key)) {
|
|
@@ -23,7 +90,8 @@ function createVard(options = {}) {
|
|
|
23
90
|
description: opts.description,
|
|
24
91
|
type: "string",
|
|
25
92
|
defaultValue: fallback,
|
|
26
|
-
editableBy: opts.editableBy ?? "member"
|
|
93
|
+
editableBy: opts.editableBy ?? "member",
|
|
94
|
+
group: opts.group
|
|
27
95
|
});
|
|
28
96
|
},
|
|
29
97
|
richtext(key, fallback, opts = {}) {
|
|
@@ -33,7 +101,8 @@ function createVard(options = {}) {
|
|
|
33
101
|
description: opts.description,
|
|
34
102
|
type: "richtext",
|
|
35
103
|
defaultValue: fallback,
|
|
36
|
-
editableBy: opts.editableBy ?? "member"
|
|
104
|
+
editableBy: opts.editableBy ?? "member",
|
|
105
|
+
group: opts.group
|
|
37
106
|
});
|
|
38
107
|
},
|
|
39
108
|
color(key, fallback, opts = {}) {
|
|
@@ -43,7 +112,8 @@ function createVard(options = {}) {
|
|
|
43
112
|
description: opts.description,
|
|
44
113
|
type: "color",
|
|
45
114
|
defaultValue: fallback,
|
|
46
|
-
editableBy: opts.editableBy ?? "member"
|
|
115
|
+
editableBy: opts.editableBy ?? "member",
|
|
116
|
+
group: opts.group
|
|
47
117
|
});
|
|
48
118
|
},
|
|
49
119
|
image(key, fallback, opts = {}) {
|
|
@@ -53,7 +123,8 @@ function createVard(options = {}) {
|
|
|
53
123
|
description: opts.description,
|
|
54
124
|
type: "image",
|
|
55
125
|
defaultValue: fallback,
|
|
56
|
-
editableBy: opts.editableBy ?? "member"
|
|
126
|
+
editableBy: opts.editableBy ?? "member",
|
|
127
|
+
group: opts.group
|
|
57
128
|
});
|
|
58
129
|
},
|
|
59
130
|
boolean(key, fallback, opts = {}) {
|
|
@@ -63,7 +134,8 @@ function createVard(options = {}) {
|
|
|
63
134
|
description: opts.description,
|
|
64
135
|
type: "boolean",
|
|
65
136
|
defaultValue: fallback,
|
|
66
|
-
editableBy: opts.editableBy ?? "member"
|
|
137
|
+
editableBy: opts.editableBy ?? "member",
|
|
138
|
+
group: opts.group
|
|
67
139
|
});
|
|
68
140
|
},
|
|
69
141
|
list(key, schema, fallback, opts = {}) {
|
|
@@ -74,12 +146,47 @@ function createVard(options = {}) {
|
|
|
74
146
|
type: "list",
|
|
75
147
|
defaultValue: fallback,
|
|
76
148
|
editableBy: opts.editableBy ?? "member",
|
|
77
|
-
listItemSchema: schema
|
|
149
|
+
listItemSchema: schema,
|
|
150
|
+
group: opts.group
|
|
78
151
|
});
|
|
79
152
|
},
|
|
153
|
+
collection(key, schema, fallback, opts = {}) {
|
|
154
|
+
return register({
|
|
155
|
+
key,
|
|
156
|
+
label: opts.label ?? labelFromKey(key),
|
|
157
|
+
description: opts.description,
|
|
158
|
+
type: "list",
|
|
159
|
+
defaultValue: fallback,
|
|
160
|
+
editableBy: opts.editableBy ?? "member",
|
|
161
|
+
listItemSchema: schema,
|
|
162
|
+
group: opts.group,
|
|
163
|
+
isCollection: true
|
|
164
|
+
});
|
|
165
|
+
},
|
|
166
|
+
global: {
|
|
167
|
+
string(key, fallback, opts = {}) {
|
|
168
|
+
return client.string(key, fallback, { ...opts, group: "Global" });
|
|
169
|
+
},
|
|
170
|
+
richtext(key, fallback, opts = {}) {
|
|
171
|
+
return client.richtext(key, fallback, { ...opts, group: "Global" });
|
|
172
|
+
},
|
|
173
|
+
color(key, fallback, opts = {}) {
|
|
174
|
+
return client.color(key, fallback, { ...opts, group: "Global" });
|
|
175
|
+
},
|
|
176
|
+
image(key, fallback, opts = {}) {
|
|
177
|
+
return client.image(key, fallback, { ...opts, group: "Global" });
|
|
178
|
+
},
|
|
179
|
+
boolean(key, fallback, opts = {}) {
|
|
180
|
+
return client.boolean(key, fallback, { ...opts, group: "Global" });
|
|
181
|
+
},
|
|
182
|
+
list(key, schema, fallback, opts = {}) {
|
|
183
|
+
return client.list(key, schema, fallback, { ...opts, group: "Global" });
|
|
184
|
+
}
|
|
185
|
+
},
|
|
80
186
|
getDefinitions() {
|
|
81
187
|
return [...registry];
|
|
82
|
-
}
|
|
188
|
+
},
|
|
189
|
+
store
|
|
83
190
|
};
|
|
84
191
|
return client;
|
|
85
192
|
}
|
|
@@ -88,6 +195,7 @@ function labelFromKey(key) {
|
|
|
88
195
|
return lastSegment.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase()).trim();
|
|
89
196
|
}
|
|
90
197
|
export {
|
|
91
|
-
createVard
|
|
198
|
+
createVard,
|
|
199
|
+
createVardFetchStore
|
|
92
200
|
};
|
|
93
201
|
//# sourceMappingURL=index.mjs.map
|