@sleekcms/client 1.0.1 → 2.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 +134 -325
- package/index.cjs +123 -104
- package/index.d.cts +22 -34
- package/index.d.ts +22 -34
- package/index.mjs +121 -102
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
// src/lib.ts
|
|
2
2
|
import * as jmespath from "jmespath";
|
|
3
|
-
function isDevToken(token) {
|
|
4
|
-
return token.startsWith("dev-");
|
|
5
|
-
}
|
|
6
3
|
function getBaseUrl(token, devEnv) {
|
|
7
4
|
let [env, siteId, ...rest] = token.split("-");
|
|
8
5
|
if (devEnv === "production") return `https://${env}.sleekcms.com/${siteId}`;
|
|
@@ -14,52 +11,59 @@ function applyJmes(data, query) {
|
|
|
14
11
|
if (!query) return data;
|
|
15
12
|
return jmespath.search(data, query);
|
|
16
13
|
}
|
|
17
|
-
function
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const baseUrl = getBaseUrl(siteToken, devEnv).replace(/\/$/, "");
|
|
30
|
-
const url = new URL(`${baseUrl}/${env}`);
|
|
31
|
-
if (searchQuery && !cacheMode && !cachedContent) {
|
|
32
|
-
url.searchParams.set("search", searchQuery);
|
|
33
|
-
}
|
|
34
|
-
if (mock && dev) {
|
|
35
|
-
url.searchParams.set("mock", "true");
|
|
36
|
-
}
|
|
37
|
-
const res = await fetch(url.toString(), {
|
|
38
|
-
method: "GET",
|
|
14
|
+
function getUrl({ siteToken, env, search: search2, lang, devEnv = "production" }) {
|
|
15
|
+
const baseUrl = getBaseUrl(siteToken, devEnv).replace(/\/$/, "");
|
|
16
|
+
const url = new URL(`${baseUrl}/${env}`);
|
|
17
|
+
if (search2) url.searchParams.append("search", search2);
|
|
18
|
+
if (lang) url.searchParams.append("lang", lang);
|
|
19
|
+
return url.toString();
|
|
20
|
+
}
|
|
21
|
+
async function fetchEnvTag({ siteToken, env }) {
|
|
22
|
+
const url = getUrl({ siteToken, env });
|
|
23
|
+
try {
|
|
24
|
+
let res = await fetch(url, {
|
|
25
|
+
method: "POST",
|
|
39
26
|
headers: {
|
|
40
27
|
"Content-Type": "application/json",
|
|
41
28
|
Authorization: siteToken
|
|
42
29
|
}
|
|
43
30
|
});
|
|
44
|
-
if (
|
|
45
|
-
let
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (data2 && data2.message) message = data2.message;
|
|
49
|
-
} catch {
|
|
31
|
+
if (res.ok) {
|
|
32
|
+
let data = await res.json();
|
|
33
|
+
if (data.tag) {
|
|
34
|
+
return data.tag;
|
|
50
35
|
}
|
|
51
|
-
throw new Error(`[SleekCMS] Request failed (${res.status}): ${message}`);
|
|
52
36
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.error("[SleekCMS] Unable to resolve env tag.");
|
|
39
|
+
}
|
|
40
|
+
return env;
|
|
41
|
+
}
|
|
42
|
+
async function fetchSiteContent(options) {
|
|
43
|
+
const { siteToken, env = "latest", cdn = false, search: search2, lang } = options;
|
|
44
|
+
let url = getUrl({ siteToken, env, search: search2, lang });
|
|
45
|
+
if (cdn && !search2) {
|
|
46
|
+
let tag = await fetchEnvTag({ siteToken, env });
|
|
47
|
+
url = getUrl({ siteToken, env: tag, search: search2, lang });
|
|
48
|
+
}
|
|
49
|
+
const res = await fetch(url, {
|
|
50
|
+
method: "GET",
|
|
51
|
+
headers: {
|
|
52
|
+
"Content-Type": "application/json",
|
|
53
|
+
Authorization: siteToken
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
if (!res.ok) {
|
|
57
|
+
let message = res.statusText;
|
|
58
|
+
try {
|
|
59
|
+
const data = await res.json();
|
|
60
|
+
if (data && data.message) message = data.message;
|
|
61
|
+
} catch {
|
|
57
62
|
}
|
|
58
|
-
|
|
59
|
-
}
|
|
63
|
+
throw new Error(`[SleekCMS] Request failed (${res.status}): ${message}`);
|
|
64
|
+
}
|
|
65
|
+
return await res.json();
|
|
60
66
|
}
|
|
61
|
-
|
|
62
|
-
// src/index.ts
|
|
63
67
|
function extractSlugs(pages, path) {
|
|
64
68
|
const slugs = [];
|
|
65
69
|
const pagesList = pages ?? [];
|
|
@@ -78,121 +82,136 @@ function filterPagesByPath(pages, path) {
|
|
|
78
82
|
return pth.startsWith(path);
|
|
79
83
|
});
|
|
80
84
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
|
|
86
|
+
// src/index.ts
|
|
87
|
+
async function createClient(options) {
|
|
88
|
+
const data = await fetchSiteContent(options);
|
|
89
|
+
function getContent(query) {
|
|
90
|
+
return applyJmes(data, query);
|
|
85
91
|
}
|
|
86
|
-
|
|
92
|
+
function getPages(path) {
|
|
87
93
|
if (!path) {
|
|
88
94
|
throw new Error("[SleekCMS] path is required for getPages");
|
|
89
95
|
}
|
|
90
|
-
|
|
91
|
-
const filtered = filterPagesByPath(data.pages, path);
|
|
92
|
-
return applyJmes(filtered, query);
|
|
96
|
+
return filterPagesByPath(data.pages, path);
|
|
93
97
|
}
|
|
94
|
-
|
|
98
|
+
function getPage(path) {
|
|
95
99
|
if (!path) {
|
|
96
100
|
throw new Error("[SleekCMS] path is required for getPage");
|
|
97
101
|
}
|
|
98
|
-
const data = await fetchSiteContent();
|
|
99
102
|
const pages = data.pages ?? [];
|
|
100
103
|
const page = pages.find((p) => {
|
|
101
104
|
const pth = typeof p._path === "string" ? p._path : "";
|
|
102
105
|
return pth === path;
|
|
103
106
|
});
|
|
104
|
-
|
|
105
|
-
|
|
107
|
+
return page ?? null;
|
|
108
|
+
}
|
|
109
|
+
function getEntry(handle) {
|
|
110
|
+
if (!handle) {
|
|
111
|
+
throw new Error("[SleekCMS] handle is required for getEntry");
|
|
106
112
|
}
|
|
107
|
-
|
|
113
|
+
const entries = data.entries ?? {};
|
|
114
|
+
const entry = entries[handle] ?? null;
|
|
115
|
+
return entry;
|
|
108
116
|
}
|
|
109
|
-
|
|
117
|
+
function getSlugs(path) {
|
|
110
118
|
if (!path) {
|
|
111
119
|
throw new Error("[SleekCMS] path is required for getSlugs");
|
|
112
120
|
}
|
|
113
|
-
const data = await fetchSiteContent();
|
|
114
121
|
return extractSlugs(data.pages, path);
|
|
115
122
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return data.images
|
|
119
|
-
}
|
|
120
|
-
async function getImage(name) {
|
|
121
|
-
if (!name) return void 0;
|
|
122
|
-
const data = await fetchSiteContent();
|
|
123
|
-
return data.images ? data.images[name] : void 0;
|
|
123
|
+
function getImage(name) {
|
|
124
|
+
if (!name) return null;
|
|
125
|
+
return data.images ? data.images[name] : null;
|
|
124
126
|
}
|
|
125
|
-
|
|
126
|
-
if (!name) return
|
|
127
|
-
const data = await fetchSiteContent();
|
|
127
|
+
function getList(name) {
|
|
128
|
+
if (!name) return null;
|
|
128
129
|
const lists = data.lists ?? {};
|
|
129
130
|
const list = lists[name];
|
|
130
|
-
return Array.isArray(list) ? list :
|
|
131
|
+
return Array.isArray(list) ? list : null;
|
|
131
132
|
}
|
|
132
133
|
return {
|
|
133
134
|
getContent,
|
|
134
135
|
getPages,
|
|
135
136
|
getPage,
|
|
137
|
+
getEntry,
|
|
136
138
|
getSlugs,
|
|
137
|
-
getImages,
|
|
138
139
|
getImage,
|
|
139
140
|
getList
|
|
140
141
|
};
|
|
141
142
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
143
|
+
function createAsyncClient({ siteToken, env = "latest", cdn, lang }) {
|
|
144
|
+
let syncClient = null;
|
|
145
|
+
let tag = null;
|
|
146
|
+
let cache = {};
|
|
147
|
+
async function getContent(search2) {
|
|
148
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
149
|
+
if (!search2 && !syncClient) {
|
|
150
|
+
syncClient = await createClient({ siteToken, env: tag ?? env, cdn, lang });
|
|
151
|
+
cache = {};
|
|
151
152
|
}
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
if (syncClient) return syncClient.getContent(search2);
|
|
154
|
+
if (!search2) return null;
|
|
155
|
+
if (!cache[search2]) cache[search2] = await fetchSiteContent({ siteToken, env: tag ?? env, search: search2, lang });
|
|
156
|
+
return cache[search2] ?? null;
|
|
154
157
|
}
|
|
155
|
-
function
|
|
156
|
-
if (!
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
158
|
+
async function getPages(path) {
|
|
159
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
160
|
+
if (syncClient) return syncClient.getPages(path);
|
|
161
|
+
if (!cache.pages) cache.pages = await fetchSiteContent({ siteToken, env: tag ?? env, search: "pages", lang });
|
|
162
|
+
if (!path) return cache.pages;
|
|
163
|
+
else return filterPagesByPath(cache.pages, path);
|
|
164
|
+
}
|
|
165
|
+
async function getPage(path) {
|
|
166
|
+
var _a;
|
|
167
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
168
|
+
if (syncClient) return syncClient.getPage(path);
|
|
169
|
+
if (!cache.pages) cache.pages = await fetchSiteContent({ siteToken, env: tag ?? env, search: "pages", lang });
|
|
170
|
+
const page = (_a = cache.pages) == null ? void 0 : _a.find((p) => {
|
|
161
171
|
const pth = typeof p._path === "string" ? p._path : "";
|
|
162
172
|
return pth === path;
|
|
163
173
|
});
|
|
164
174
|
return page ?? null;
|
|
165
175
|
}
|
|
166
|
-
function
|
|
167
|
-
if (!
|
|
168
|
-
|
|
176
|
+
async function getEntry(handle) {
|
|
177
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
178
|
+
if (syncClient) return syncClient.getEntry(handle);
|
|
179
|
+
let search2 = `entries.${handle}`;
|
|
180
|
+
if (cache[search2] === void 0) {
|
|
181
|
+
cache[search2] = await fetchSiteContent({ siteToken, env: tag ?? env, search: search2, lang });
|
|
169
182
|
}
|
|
170
|
-
return
|
|
183
|
+
return cache[search2];
|
|
171
184
|
}
|
|
172
|
-
function
|
|
173
|
-
|
|
185
|
+
async function getSlugs(path) {
|
|
186
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
187
|
+
if (syncClient) return syncClient.getSlugs(path);
|
|
188
|
+
if (!cache.pages) cache.pages = await fetchSiteContent({ siteToken, env: tag ?? env, search: "pages", lang });
|
|
189
|
+
return extractSlugs(cache.pages, path);
|
|
174
190
|
}
|
|
175
|
-
function getImage(name) {
|
|
176
|
-
if (!
|
|
177
|
-
|
|
191
|
+
async function getImage(name) {
|
|
192
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
193
|
+
if (syncClient) return syncClient.getImage(name);
|
|
194
|
+
if (!cache.images) cache.images = await fetchSiteContent({ siteToken, env: tag ?? env, search: "images", lang });
|
|
195
|
+
return cache.images ? cache.images[name] : null;
|
|
178
196
|
}
|
|
179
|
-
function getList(name) {
|
|
180
|
-
if (!
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
197
|
+
async function getList(name) {
|
|
198
|
+
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
199
|
+
if (syncClient) return syncClient.getList(name);
|
|
200
|
+
if (!cache.lists) cache.lists = await fetchSiteContent({ siteToken, env: tag ?? env, search: "lists", lang });
|
|
201
|
+
const list = cache.lists[name];
|
|
202
|
+
return Array.isArray(list) ? list : null;
|
|
184
203
|
}
|
|
185
204
|
return {
|
|
186
205
|
getContent,
|
|
187
206
|
getPages,
|
|
188
207
|
getPage,
|
|
208
|
+
getEntry,
|
|
189
209
|
getSlugs,
|
|
190
|
-
getImages,
|
|
191
210
|
getImage,
|
|
192
211
|
getList
|
|
193
212
|
};
|
|
194
213
|
}
|
|
195
214
|
export {
|
|
196
|
-
|
|
197
|
-
|
|
215
|
+
createAsyncClient,
|
|
216
|
+
createClient
|
|
198
217
|
};
|