@blockforgecms/cms-core 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/README.md +10 -0
- package/dist/index.cjs +202 -0
- package/dist/index.d.cts +66 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +170 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# @blockforgecms/cms-core
|
|
2
|
+
|
|
3
|
+
Shared core utilities for BlockForge contract-driven CMS:
|
|
4
|
+
|
|
5
|
+
- content model contract types
|
|
6
|
+
- model key normalization helpers
|
|
7
|
+
- default content builders from contracts
|
|
8
|
+
- storage adapter interface + resolvers (`local`, `file`)
|
|
9
|
+
|
|
10
|
+
This package is framework-agnostic and intended to be reused by adapters and tooling.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
EMPTY_RICH_TEXT_DOCUMENT: () => EMPTY_RICH_TEXT_DOCUMENT,
|
|
23
|
+
buildDefaultContentFromContract: () => buildDefaultContentFromContract,
|
|
24
|
+
createFileStorageAdapter: () => createFileStorageAdapter,
|
|
25
|
+
createLocalStorageAdapter: () => createLocalStorageAdapter,
|
|
26
|
+
modelKeyToDisplay: () => modelKeyToDisplay,
|
|
27
|
+
normalizeModelKey: () => normalizeModelKey,
|
|
28
|
+
resolveStorageAdapter: () => resolveStorageAdapter
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/model-key.ts
|
|
33
|
+
var normalizeModelKey = (value) => value.trim().replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[^A-Za-z0-9]+/g, "-").replace(/^-+|-+$/g, "").replace(/-+/g, "-").toUpperCase();
|
|
34
|
+
var modelKeyToDisplay = (value) => normalizeModelKey(value).toLowerCase().split("-").filter(Boolean).map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
|
35
|
+
|
|
36
|
+
// src/defaults.ts
|
|
37
|
+
var clone = (value) => JSON.parse(JSON.stringify(value));
|
|
38
|
+
var EMPTY_RICH_TEXT_DOCUMENT = {
|
|
39
|
+
type: "doc",
|
|
40
|
+
content: []
|
|
41
|
+
};
|
|
42
|
+
var getDefaultArrayObjectItemFieldValue = (field) => {
|
|
43
|
+
switch (field.kind) {
|
|
44
|
+
case "number":
|
|
45
|
+
return 0;
|
|
46
|
+
case "toggle":
|
|
47
|
+
return false;
|
|
48
|
+
case "select":
|
|
49
|
+
return field.options?.[0] || "";
|
|
50
|
+
case "json":
|
|
51
|
+
return null;
|
|
52
|
+
case "text":
|
|
53
|
+
default:
|
|
54
|
+
return "";
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var getDefaultValueForField = (field) => {
|
|
58
|
+
switch (field.kind) {
|
|
59
|
+
case "text":
|
|
60
|
+
return "";
|
|
61
|
+
case "number":
|
|
62
|
+
return 0;
|
|
63
|
+
case "toggle":
|
|
64
|
+
return false;
|
|
65
|
+
case "select":
|
|
66
|
+
return field.options?.[0] || "";
|
|
67
|
+
case "richtext":
|
|
68
|
+
return clone(EMPTY_RICH_TEXT_DOCUMENT);
|
|
69
|
+
case "array-text":
|
|
70
|
+
case "array-number":
|
|
71
|
+
case "array-boolean":
|
|
72
|
+
case "array-object":
|
|
73
|
+
return [];
|
|
74
|
+
case "json":
|
|
75
|
+
default:
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var normalizeArrayObjectField = (field) => {
|
|
80
|
+
if (field.kind !== "array-object" || !Array.isArray(field.itemFields)) {
|
|
81
|
+
return field;
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
...field,
|
|
85
|
+
itemFields: field.itemFields.map((itemField) => ({
|
|
86
|
+
...itemField,
|
|
87
|
+
required: itemField.required ?? false
|
|
88
|
+
}))
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
var buildDefaultContentFromContract = (contract) => {
|
|
92
|
+
const content = {};
|
|
93
|
+
for (const rawField of contract.fields || []) {
|
|
94
|
+
const field = normalizeArrayObjectField(rawField);
|
|
95
|
+
if (field.kind === "array-object" && field.required && field.itemFields?.length) {
|
|
96
|
+
const firstItem = field.itemFields.reduce((acc, itemField) => {
|
|
97
|
+
acc[itemField.key] = getDefaultArrayObjectItemFieldValue(itemField);
|
|
98
|
+
return acc;
|
|
99
|
+
}, {});
|
|
100
|
+
content[field.key] = [firstItem];
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
content[field.key] = getDefaultValueForField(field);
|
|
104
|
+
}
|
|
105
|
+
return content;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// src/storage/local-storage.ts
|
|
109
|
+
var getKey = (namespace, projectId, bucket) => `${namespace}:${projectId}:${bucket}`;
|
|
110
|
+
var safeParse = (value) => {
|
|
111
|
+
if (!value) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return JSON.parse(value);
|
|
116
|
+
} catch (_error) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
var createLocalStorageAdapter = (namespace = "blockforge") => ({
|
|
121
|
+
mode: "local",
|
|
122
|
+
async read(projectId, bucket) {
|
|
123
|
+
if (typeof window === "undefined") {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return safeParse(window.localStorage.getItem(getKey(namespace, projectId, bucket)));
|
|
127
|
+
},
|
|
128
|
+
async write(projectId, bucket, value) {
|
|
129
|
+
if (typeof window === "undefined") {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
window.localStorage.setItem(getKey(namespace, projectId, bucket), JSON.stringify(value));
|
|
133
|
+
},
|
|
134
|
+
async remove(projectId, bucket) {
|
|
135
|
+
if (typeof window === "undefined") {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
window.localStorage.removeItem(getKey(namespace, projectId, bucket));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// src/storage/file-storage.ts
|
|
143
|
+
var toQuery = (projectId, bucket) => {
|
|
144
|
+
const params = new URLSearchParams();
|
|
145
|
+
params.set("projectId", projectId);
|
|
146
|
+
params.set("bucket", bucket);
|
|
147
|
+
return params.toString();
|
|
148
|
+
};
|
|
149
|
+
var createFileStorageAdapter = (endpoint = "/api/blockforge/content") => ({
|
|
150
|
+
mode: "file",
|
|
151
|
+
async read(projectId, bucket) {
|
|
152
|
+
const response = await fetch(`${endpoint}?${toQuery(projectId, bucket)}`, {
|
|
153
|
+
method: "GET",
|
|
154
|
+
headers: { Accept: "application/json" },
|
|
155
|
+
credentials: "same-origin"
|
|
156
|
+
});
|
|
157
|
+
if (!response.ok) {
|
|
158
|
+
throw new Error(`File storage read failed (${response.status})`);
|
|
159
|
+
}
|
|
160
|
+
const payload = await response.json();
|
|
161
|
+
return payload.value ?? null;
|
|
162
|
+
},
|
|
163
|
+
async write(projectId, bucket, value) {
|
|
164
|
+
const response = await fetch(endpoint, {
|
|
165
|
+
method: "POST",
|
|
166
|
+
headers: { "Content-Type": "application/json" },
|
|
167
|
+
credentials: "same-origin",
|
|
168
|
+
body: JSON.stringify({ projectId, bucket, value })
|
|
169
|
+
});
|
|
170
|
+
if (!response.ok) {
|
|
171
|
+
throw new Error(`File storage write failed (${response.status})`);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
async remove(projectId, bucket) {
|
|
175
|
+
const response = await fetch(`${endpoint}?${toQuery(projectId, bucket)}`, {
|
|
176
|
+
method: "DELETE",
|
|
177
|
+
credentials: "same-origin"
|
|
178
|
+
});
|
|
179
|
+
if (!response.ok) {
|
|
180
|
+
throw new Error(`File storage delete failed (${response.status})`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// src/storage/resolve.ts
|
|
186
|
+
var resolveStorageAdapter = (options = {}) => {
|
|
187
|
+
const mode = options.mode || "local";
|
|
188
|
+
if (mode === "file") {
|
|
189
|
+
return createFileStorageAdapter(options.fileEndpoint);
|
|
190
|
+
}
|
|
191
|
+
return createLocalStorageAdapter(options.namespace);
|
|
192
|
+
};
|
|
193
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
194
|
+
0 && (module.exports = {
|
|
195
|
+
EMPTY_RICH_TEXT_DOCUMENT,
|
|
196
|
+
buildDefaultContentFromContract,
|
|
197
|
+
createFileStorageAdapter,
|
|
198
|
+
createLocalStorageAdapter,
|
|
199
|
+
modelKeyToDisplay,
|
|
200
|
+
normalizeModelKey,
|
|
201
|
+
resolveStorageAdapter
|
|
202
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
type ContractFieldKind = 'text' | 'number' | 'toggle' | 'select' | 'richtext' | 'array-text' | 'array-number' | 'array-boolean' | 'array-object' | 'json';
|
|
2
|
+
interface ContractArrayObjectItemField {
|
|
3
|
+
key: string;
|
|
4
|
+
label: string;
|
|
5
|
+
kind: 'text' | 'number' | 'toggle' | 'select' | 'json';
|
|
6
|
+
required: boolean;
|
|
7
|
+
options?: string[];
|
|
8
|
+
}
|
|
9
|
+
interface ContractField {
|
|
10
|
+
key: string;
|
|
11
|
+
label: string;
|
|
12
|
+
kind: ContractFieldKind;
|
|
13
|
+
required: boolean;
|
|
14
|
+
options?: string[];
|
|
15
|
+
itemFields?: ContractArrayObjectItemField[];
|
|
16
|
+
}
|
|
17
|
+
interface ContentModelContract {
|
|
18
|
+
modelKey: string;
|
|
19
|
+
displayName: string;
|
|
20
|
+
sourceKind: 'typescript';
|
|
21
|
+
sourceRef: string;
|
|
22
|
+
fields: ContractField[];
|
|
23
|
+
}
|
|
24
|
+
interface ActiveContentModelContract extends ContentModelContract {
|
|
25
|
+
version: number;
|
|
26
|
+
contractHash: string;
|
|
27
|
+
}
|
|
28
|
+
interface GeneratedContentModelContract extends ContentModelContract {
|
|
29
|
+
contractHash: string;
|
|
30
|
+
}
|
|
31
|
+
interface GeneratedContentModelDocument {
|
|
32
|
+
generatedAt: string;
|
|
33
|
+
projectId: string;
|
|
34
|
+
models: GeneratedContentModelContract[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const normalizeModelKey: (value: string) => string;
|
|
38
|
+
declare const modelKeyToDisplay: (value: string) => string;
|
|
39
|
+
|
|
40
|
+
declare const EMPTY_RICH_TEXT_DOCUMENT: {
|
|
41
|
+
type: string;
|
|
42
|
+
content: any[];
|
|
43
|
+
};
|
|
44
|
+
declare const buildDefaultContentFromContract: (contract: ActiveContentModelContract | ContentModelContract) => Record<string, unknown>;
|
|
45
|
+
|
|
46
|
+
type CMSStorageMode = 'local' | 'file';
|
|
47
|
+
type CMSStorageBucket = 'pages' | 'settings' | 'reusableSections' | 'contentModels' | 'projectApiKeys' | 'projects';
|
|
48
|
+
interface CMSStorageAdapter {
|
|
49
|
+
mode: CMSStorageMode;
|
|
50
|
+
read<T>(projectId: string, bucket: CMSStorageBucket): Promise<T | null>;
|
|
51
|
+
write<T>(projectId: string, bucket: CMSStorageBucket, value: T): Promise<void>;
|
|
52
|
+
remove(projectId: string, bucket: CMSStorageBucket): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
interface ResolveStorageAdapterOptions {
|
|
55
|
+
mode?: CMSStorageMode;
|
|
56
|
+
namespace?: string;
|
|
57
|
+
fileEndpoint?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare const createLocalStorageAdapter: (namespace?: string) => CMSStorageAdapter;
|
|
61
|
+
|
|
62
|
+
declare const createFileStorageAdapter: (endpoint?: string) => CMSStorageAdapter;
|
|
63
|
+
|
|
64
|
+
declare const resolveStorageAdapter: (options?: ResolveStorageAdapterOptions) => CMSStorageAdapter;
|
|
65
|
+
|
|
66
|
+
export { type ActiveContentModelContract, type CMSStorageAdapter, type CMSStorageBucket, type CMSStorageMode, type ContentModelContract, type ContractArrayObjectItemField, type ContractField, type ContractFieldKind, EMPTY_RICH_TEXT_DOCUMENT, type GeneratedContentModelContract, type GeneratedContentModelDocument, type ResolveStorageAdapterOptions, buildDefaultContentFromContract, createFileStorageAdapter, createLocalStorageAdapter, modelKeyToDisplay, normalizeModelKey, resolveStorageAdapter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
type ContractFieldKind = 'text' | 'number' | 'toggle' | 'select' | 'richtext' | 'array-text' | 'array-number' | 'array-boolean' | 'array-object' | 'json';
|
|
2
|
+
interface ContractArrayObjectItemField {
|
|
3
|
+
key: string;
|
|
4
|
+
label: string;
|
|
5
|
+
kind: 'text' | 'number' | 'toggle' | 'select' | 'json';
|
|
6
|
+
required: boolean;
|
|
7
|
+
options?: string[];
|
|
8
|
+
}
|
|
9
|
+
interface ContractField {
|
|
10
|
+
key: string;
|
|
11
|
+
label: string;
|
|
12
|
+
kind: ContractFieldKind;
|
|
13
|
+
required: boolean;
|
|
14
|
+
options?: string[];
|
|
15
|
+
itemFields?: ContractArrayObjectItemField[];
|
|
16
|
+
}
|
|
17
|
+
interface ContentModelContract {
|
|
18
|
+
modelKey: string;
|
|
19
|
+
displayName: string;
|
|
20
|
+
sourceKind: 'typescript';
|
|
21
|
+
sourceRef: string;
|
|
22
|
+
fields: ContractField[];
|
|
23
|
+
}
|
|
24
|
+
interface ActiveContentModelContract extends ContentModelContract {
|
|
25
|
+
version: number;
|
|
26
|
+
contractHash: string;
|
|
27
|
+
}
|
|
28
|
+
interface GeneratedContentModelContract extends ContentModelContract {
|
|
29
|
+
contractHash: string;
|
|
30
|
+
}
|
|
31
|
+
interface GeneratedContentModelDocument {
|
|
32
|
+
generatedAt: string;
|
|
33
|
+
projectId: string;
|
|
34
|
+
models: GeneratedContentModelContract[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const normalizeModelKey: (value: string) => string;
|
|
38
|
+
declare const modelKeyToDisplay: (value: string) => string;
|
|
39
|
+
|
|
40
|
+
declare const EMPTY_RICH_TEXT_DOCUMENT: {
|
|
41
|
+
type: string;
|
|
42
|
+
content: any[];
|
|
43
|
+
};
|
|
44
|
+
declare const buildDefaultContentFromContract: (contract: ActiveContentModelContract | ContentModelContract) => Record<string, unknown>;
|
|
45
|
+
|
|
46
|
+
type CMSStorageMode = 'local' | 'file';
|
|
47
|
+
type CMSStorageBucket = 'pages' | 'settings' | 'reusableSections' | 'contentModels' | 'projectApiKeys' | 'projects';
|
|
48
|
+
interface CMSStorageAdapter {
|
|
49
|
+
mode: CMSStorageMode;
|
|
50
|
+
read<T>(projectId: string, bucket: CMSStorageBucket): Promise<T | null>;
|
|
51
|
+
write<T>(projectId: string, bucket: CMSStorageBucket, value: T): Promise<void>;
|
|
52
|
+
remove(projectId: string, bucket: CMSStorageBucket): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
interface ResolveStorageAdapterOptions {
|
|
55
|
+
mode?: CMSStorageMode;
|
|
56
|
+
namespace?: string;
|
|
57
|
+
fileEndpoint?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare const createLocalStorageAdapter: (namespace?: string) => CMSStorageAdapter;
|
|
61
|
+
|
|
62
|
+
declare const createFileStorageAdapter: (endpoint?: string) => CMSStorageAdapter;
|
|
63
|
+
|
|
64
|
+
declare const resolveStorageAdapter: (options?: ResolveStorageAdapterOptions) => CMSStorageAdapter;
|
|
65
|
+
|
|
66
|
+
export { type ActiveContentModelContract, type CMSStorageAdapter, type CMSStorageBucket, type CMSStorageMode, type ContentModelContract, type ContractArrayObjectItemField, type ContractField, type ContractFieldKind, EMPTY_RICH_TEXT_DOCUMENT, type GeneratedContentModelContract, type GeneratedContentModelDocument, type ResolveStorageAdapterOptions, buildDefaultContentFromContract, createFileStorageAdapter, createLocalStorageAdapter, modelKeyToDisplay, normalizeModelKey, resolveStorageAdapter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// src/model-key.ts
|
|
2
|
+
var normalizeModelKey = (value) => value.trim().replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[^A-Za-z0-9]+/g, "-").replace(/^-+|-+$/g, "").replace(/-+/g, "-").toUpperCase();
|
|
3
|
+
var modelKeyToDisplay = (value) => normalizeModelKey(value).toLowerCase().split("-").filter(Boolean).map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
|
4
|
+
|
|
5
|
+
// src/defaults.ts
|
|
6
|
+
var clone = (value) => JSON.parse(JSON.stringify(value));
|
|
7
|
+
var EMPTY_RICH_TEXT_DOCUMENT = {
|
|
8
|
+
type: "doc",
|
|
9
|
+
content: []
|
|
10
|
+
};
|
|
11
|
+
var getDefaultArrayObjectItemFieldValue = (field) => {
|
|
12
|
+
switch (field.kind) {
|
|
13
|
+
case "number":
|
|
14
|
+
return 0;
|
|
15
|
+
case "toggle":
|
|
16
|
+
return false;
|
|
17
|
+
case "select":
|
|
18
|
+
return field.options?.[0] || "";
|
|
19
|
+
case "json":
|
|
20
|
+
return null;
|
|
21
|
+
case "text":
|
|
22
|
+
default:
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var getDefaultValueForField = (field) => {
|
|
27
|
+
switch (field.kind) {
|
|
28
|
+
case "text":
|
|
29
|
+
return "";
|
|
30
|
+
case "number":
|
|
31
|
+
return 0;
|
|
32
|
+
case "toggle":
|
|
33
|
+
return false;
|
|
34
|
+
case "select":
|
|
35
|
+
return field.options?.[0] || "";
|
|
36
|
+
case "richtext":
|
|
37
|
+
return clone(EMPTY_RICH_TEXT_DOCUMENT);
|
|
38
|
+
case "array-text":
|
|
39
|
+
case "array-number":
|
|
40
|
+
case "array-boolean":
|
|
41
|
+
case "array-object":
|
|
42
|
+
return [];
|
|
43
|
+
case "json":
|
|
44
|
+
default:
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var normalizeArrayObjectField = (field) => {
|
|
49
|
+
if (field.kind !== "array-object" || !Array.isArray(field.itemFields)) {
|
|
50
|
+
return field;
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
...field,
|
|
54
|
+
itemFields: field.itemFields.map((itemField) => ({
|
|
55
|
+
...itemField,
|
|
56
|
+
required: itemField.required ?? false
|
|
57
|
+
}))
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
var buildDefaultContentFromContract = (contract) => {
|
|
61
|
+
const content = {};
|
|
62
|
+
for (const rawField of contract.fields || []) {
|
|
63
|
+
const field = normalizeArrayObjectField(rawField);
|
|
64
|
+
if (field.kind === "array-object" && field.required && field.itemFields?.length) {
|
|
65
|
+
const firstItem = field.itemFields.reduce((acc, itemField) => {
|
|
66
|
+
acc[itemField.key] = getDefaultArrayObjectItemFieldValue(itemField);
|
|
67
|
+
return acc;
|
|
68
|
+
}, {});
|
|
69
|
+
content[field.key] = [firstItem];
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
content[field.key] = getDefaultValueForField(field);
|
|
73
|
+
}
|
|
74
|
+
return content;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/storage/local-storage.ts
|
|
78
|
+
var getKey = (namespace, projectId, bucket) => `${namespace}:${projectId}:${bucket}`;
|
|
79
|
+
var safeParse = (value) => {
|
|
80
|
+
if (!value) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
return JSON.parse(value);
|
|
85
|
+
} catch (_error) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var createLocalStorageAdapter = (namespace = "blockforge") => ({
|
|
90
|
+
mode: "local",
|
|
91
|
+
async read(projectId, bucket) {
|
|
92
|
+
if (typeof window === "undefined") {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
return safeParse(window.localStorage.getItem(getKey(namespace, projectId, bucket)));
|
|
96
|
+
},
|
|
97
|
+
async write(projectId, bucket, value) {
|
|
98
|
+
if (typeof window === "undefined") {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
window.localStorage.setItem(getKey(namespace, projectId, bucket), JSON.stringify(value));
|
|
102
|
+
},
|
|
103
|
+
async remove(projectId, bucket) {
|
|
104
|
+
if (typeof window === "undefined") {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
window.localStorage.removeItem(getKey(namespace, projectId, bucket));
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// src/storage/file-storage.ts
|
|
112
|
+
var toQuery = (projectId, bucket) => {
|
|
113
|
+
const params = new URLSearchParams();
|
|
114
|
+
params.set("projectId", projectId);
|
|
115
|
+
params.set("bucket", bucket);
|
|
116
|
+
return params.toString();
|
|
117
|
+
};
|
|
118
|
+
var createFileStorageAdapter = (endpoint = "/api/blockforge/content") => ({
|
|
119
|
+
mode: "file",
|
|
120
|
+
async read(projectId, bucket) {
|
|
121
|
+
const response = await fetch(`${endpoint}?${toQuery(projectId, bucket)}`, {
|
|
122
|
+
method: "GET",
|
|
123
|
+
headers: { Accept: "application/json" },
|
|
124
|
+
credentials: "same-origin"
|
|
125
|
+
});
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
throw new Error(`File storage read failed (${response.status})`);
|
|
128
|
+
}
|
|
129
|
+
const payload = await response.json();
|
|
130
|
+
return payload.value ?? null;
|
|
131
|
+
},
|
|
132
|
+
async write(projectId, bucket, value) {
|
|
133
|
+
const response = await fetch(endpoint, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
headers: { "Content-Type": "application/json" },
|
|
136
|
+
credentials: "same-origin",
|
|
137
|
+
body: JSON.stringify({ projectId, bucket, value })
|
|
138
|
+
});
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
throw new Error(`File storage write failed (${response.status})`);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
async remove(projectId, bucket) {
|
|
144
|
+
const response = await fetch(`${endpoint}?${toQuery(projectId, bucket)}`, {
|
|
145
|
+
method: "DELETE",
|
|
146
|
+
credentials: "same-origin"
|
|
147
|
+
});
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
throw new Error(`File storage delete failed (${response.status})`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// src/storage/resolve.ts
|
|
155
|
+
var resolveStorageAdapter = (options = {}) => {
|
|
156
|
+
const mode = options.mode || "local";
|
|
157
|
+
if (mode === "file") {
|
|
158
|
+
return createFileStorageAdapter(options.fileEndpoint);
|
|
159
|
+
}
|
|
160
|
+
return createLocalStorageAdapter(options.namespace);
|
|
161
|
+
};
|
|
162
|
+
export {
|
|
163
|
+
EMPTY_RICH_TEXT_DOCUMENT,
|
|
164
|
+
buildDefaultContentFromContract,
|
|
165
|
+
createFileStorageAdapter,
|
|
166
|
+
createLocalStorageAdapter,
|
|
167
|
+
modelKeyToDisplay,
|
|
168
|
+
normalizeModelKey,
|
|
169
|
+
resolveStorageAdapter
|
|
170
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blockforgecms/cms-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
24
|
+
"clean": "rm -rf dist"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"tsup": "^8.5.0"
|
|
28
|
+
}
|
|
29
|
+
}
|