@sleekcms/client 2.3.0 → 2.4.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 +2 -2
- package/index.cjs +21 -23
- package/index.d.cts +2 -2
- package/index.d.ts +2 -2
- package/index.mjs +21 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -213,12 +213,12 @@ const logo = client.getImage('logo');
|
|
|
213
213
|
// { url: 'https://...', alt: '...', ... }
|
|
214
214
|
```
|
|
215
215
|
|
|
216
|
-
### `
|
|
216
|
+
### `getOptions(name)`
|
|
217
217
|
|
|
218
218
|
Get a option set (array of label/value pairs).
|
|
219
219
|
|
|
220
220
|
```typescript
|
|
221
|
-
const categories = client.
|
|
221
|
+
const categories = client.getOptions('categories');
|
|
222
222
|
// [{ label: 'Tech', value: 'tech' }, ...]
|
|
223
223
|
```
|
|
224
224
|
|
package/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
36
36
|
|
|
37
37
|
// src/lib.ts
|
|
38
38
|
var jmespath = __toESM(require("jmespath"), 1);
|
|
39
|
+
var envTagCache = /* @__PURE__ */ new Map();
|
|
39
40
|
function getBaseUrl(token, devEnv) {
|
|
40
41
|
let [env, siteId, ...rest] = token.split("-");
|
|
41
42
|
if (devEnv === "production") return `https://pub.sleekcms.com/${siteId}`;
|
|
@@ -78,8 +79,13 @@ async function fetchEnvTag({ siteToken, env }) {
|
|
|
78
79
|
async function fetchSiteContent(options) {
|
|
79
80
|
const { siteToken, env = "latest", cdn = false, search: search2, lang, cache, cacheMinutes } = options;
|
|
80
81
|
let url = getUrl({ siteToken, env, search: search2, lang });
|
|
81
|
-
if (cdn
|
|
82
|
-
|
|
82
|
+
if (!cdn) {
|
|
83
|
+
const cacheKey2 = `${siteToken}:${env}`;
|
|
84
|
+
let tag = envTagCache.get(cacheKey2);
|
|
85
|
+
if (!tag) {
|
|
86
|
+
tag = await fetchEnvTag({ siteToken, env });
|
|
87
|
+
envTagCache.set(cacheKey2, tag);
|
|
88
|
+
}
|
|
83
89
|
url = getUrl({ siteToken, env: tag, search: search2, lang });
|
|
84
90
|
}
|
|
85
91
|
const cacheKey = url;
|
|
@@ -209,7 +215,7 @@ async function createClient(options) {
|
|
|
209
215
|
if (!name) return null;
|
|
210
216
|
return data.images ? data.images[name] : null;
|
|
211
217
|
}
|
|
212
|
-
function
|
|
218
|
+
function getOptions(name) {
|
|
213
219
|
if (!name) return null;
|
|
214
220
|
const options2 = data.options ?? {};
|
|
215
221
|
const optionSet = options2[name];
|
|
@@ -222,34 +228,30 @@ async function createClient(options) {
|
|
|
222
228
|
getEntry,
|
|
223
229
|
getSlugs,
|
|
224
230
|
getImage,
|
|
225
|
-
|
|
231
|
+
getOptions
|
|
226
232
|
};
|
|
227
233
|
}
|
|
228
234
|
function createAsyncClient(options) {
|
|
229
235
|
const { siteToken, env = "latest", cdn, lang } = options;
|
|
230
236
|
const cache = options.cache ?? new MemoryCache();
|
|
231
237
|
let syncClient = null;
|
|
232
|
-
let tag = null;
|
|
233
238
|
async function getContent(search2) {
|
|
234
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
235
239
|
if (!search2 && !syncClient) {
|
|
236
|
-
syncClient = await createClient({ siteToken, env
|
|
240
|
+
syncClient = await createClient({ siteToken, env, cdn, lang, cache });
|
|
237
241
|
}
|
|
238
242
|
if (syncClient) return syncClient.getContent(search2);
|
|
239
243
|
if (!search2) return null;
|
|
240
|
-
return await fetchSiteContent({ siteToken, env
|
|
244
|
+
return await fetchSiteContent({ siteToken, env, search: search2, lang, cache, cdn });
|
|
241
245
|
}
|
|
242
246
|
async function getPages(path) {
|
|
243
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
244
247
|
if (syncClient) return syncClient.getPages(path);
|
|
245
|
-
const pages = await fetchSiteContent({ siteToken, env
|
|
248
|
+
const pages = await fetchSiteContent({ siteToken, env, search: "pages", lang, cache, cdn });
|
|
246
249
|
if (!path) return pages;
|
|
247
250
|
else return filterPagesByPath(pages, path);
|
|
248
251
|
}
|
|
249
252
|
async function getPage(path) {
|
|
250
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
251
253
|
if (syncClient) return syncClient.getPage(path);
|
|
252
|
-
const pages = await fetchSiteContent({ siteToken, env
|
|
254
|
+
const pages = await fetchSiteContent({ siteToken, env, search: "pages", lang, cache, cdn });
|
|
253
255
|
const page = pages == null ? void 0 : pages.find((p) => {
|
|
254
256
|
const pth = typeof p._path === "string" ? p._path : "";
|
|
255
257
|
return pth === path;
|
|
@@ -257,27 +259,23 @@ function createAsyncClient(options) {
|
|
|
257
259
|
return page ?? null;
|
|
258
260
|
}
|
|
259
261
|
async function getEntry(handle) {
|
|
260
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
261
262
|
if (syncClient) return syncClient.getEntry(handle);
|
|
262
263
|
let search2 = `entries.${handle}`;
|
|
263
|
-
return await fetchSiteContent({ siteToken, env
|
|
264
|
+
return await fetchSiteContent({ siteToken, env, search: search2, lang, cache, cdn });
|
|
264
265
|
}
|
|
265
266
|
async function getSlugs(path) {
|
|
266
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
267
267
|
if (syncClient) return syncClient.getSlugs(path);
|
|
268
|
-
const pages = await fetchSiteContent({ siteToken, env
|
|
268
|
+
const pages = await fetchSiteContent({ siteToken, env, search: "pages", lang, cache, cdn });
|
|
269
269
|
return extractSlugs(pages, path);
|
|
270
270
|
}
|
|
271
271
|
async function getImage(name) {
|
|
272
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
273
272
|
if (syncClient) return syncClient.getImage(name);
|
|
274
|
-
const images = await fetchSiteContent({ siteToken, env
|
|
273
|
+
const images = await fetchSiteContent({ siteToken, env, search: "images", lang, cache, cdn });
|
|
275
274
|
return images ? images[name] : null;
|
|
276
275
|
}
|
|
277
|
-
async function
|
|
278
|
-
if (
|
|
279
|
-
|
|
280
|
-
const options2 = await fetchSiteContent({ siteToken, env: tag ?? env, search: "options", lang, cache });
|
|
276
|
+
async function getOptions(name) {
|
|
277
|
+
if (syncClient) return syncClient.getOptions(name);
|
|
278
|
+
const options2 = await fetchSiteContent({ siteToken, env, search: "options", lang, cache, cdn });
|
|
281
279
|
const optionSet = options2[name];
|
|
282
280
|
return Array.isArray(optionSet) ? optionSet : null;
|
|
283
281
|
}
|
|
@@ -295,7 +293,7 @@ function createAsyncClient(options) {
|
|
|
295
293
|
getEntry,
|
|
296
294
|
getSlugs,
|
|
297
295
|
getImage,
|
|
298
|
-
|
|
296
|
+
getOptions,
|
|
299
297
|
_getFetchUrl,
|
|
300
298
|
_getEnvTag
|
|
301
299
|
};
|
package/index.d.cts
CHANGED
|
@@ -45,7 +45,7 @@ interface SleekClient {
|
|
|
45
45
|
getEntry(handle: string): Entry | Entry[] | null;
|
|
46
46
|
getSlugs(path: string): string[];
|
|
47
47
|
getImage(name: string): Image | null;
|
|
48
|
-
|
|
48
|
+
getOptions(name: string): Options | null;
|
|
49
49
|
}
|
|
50
50
|
interface SleekAsyncClient {
|
|
51
51
|
getContent(query?: string): Promise<SleekSiteContent>;
|
|
@@ -54,7 +54,7 @@ interface SleekAsyncClient {
|
|
|
54
54
|
getEntry(handle: string): Promise<Entry | Entry[] | null>;
|
|
55
55
|
getSlugs(path: string): Promise<string[]>;
|
|
56
56
|
getImage(name: string): Promise<Image | null>;
|
|
57
|
-
|
|
57
|
+
getOptions(name: string): Promise<Options | null>;
|
|
58
58
|
_getFetchUrl(): string;
|
|
59
59
|
_getEnvTag(): Promise<string>;
|
|
60
60
|
}
|
package/index.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ interface SleekClient {
|
|
|
45
45
|
getEntry(handle: string): Entry | Entry[] | null;
|
|
46
46
|
getSlugs(path: string): string[];
|
|
47
47
|
getImage(name: string): Image | null;
|
|
48
|
-
|
|
48
|
+
getOptions(name: string): Options | null;
|
|
49
49
|
}
|
|
50
50
|
interface SleekAsyncClient {
|
|
51
51
|
getContent(query?: string): Promise<SleekSiteContent>;
|
|
@@ -54,7 +54,7 @@ interface SleekAsyncClient {
|
|
|
54
54
|
getEntry(handle: string): Promise<Entry | Entry[] | null>;
|
|
55
55
|
getSlugs(path: string): Promise<string[]>;
|
|
56
56
|
getImage(name: string): Promise<Image | null>;
|
|
57
|
-
|
|
57
|
+
getOptions(name: string): Promise<Options | null>;
|
|
58
58
|
_getFetchUrl(): string;
|
|
59
59
|
_getEnvTag(): Promise<string>;
|
|
60
60
|
}
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/lib.ts
|
|
2
2
|
import * as jmespath from "jmespath";
|
|
3
|
+
var envTagCache = /* @__PURE__ */ new Map();
|
|
3
4
|
function getBaseUrl(token, devEnv) {
|
|
4
5
|
let [env, siteId, ...rest] = token.split("-");
|
|
5
6
|
if (devEnv === "production") return `https://pub.sleekcms.com/${siteId}`;
|
|
@@ -42,8 +43,13 @@ async function fetchEnvTag({ siteToken, env }) {
|
|
|
42
43
|
async function fetchSiteContent(options) {
|
|
43
44
|
const { siteToken, env = "latest", cdn = false, search: search2, lang, cache, cacheMinutes } = options;
|
|
44
45
|
let url = getUrl({ siteToken, env, search: search2, lang });
|
|
45
|
-
if (cdn
|
|
46
|
-
|
|
46
|
+
if (!cdn) {
|
|
47
|
+
const cacheKey2 = `${siteToken}:${env}`;
|
|
48
|
+
let tag = envTagCache.get(cacheKey2);
|
|
49
|
+
if (!tag) {
|
|
50
|
+
tag = await fetchEnvTag({ siteToken, env });
|
|
51
|
+
envTagCache.set(cacheKey2, tag);
|
|
52
|
+
}
|
|
47
53
|
url = getUrl({ siteToken, env: tag, search: search2, lang });
|
|
48
54
|
}
|
|
49
55
|
const cacheKey = url;
|
|
@@ -173,7 +179,7 @@ async function createClient(options) {
|
|
|
173
179
|
if (!name) return null;
|
|
174
180
|
return data.images ? data.images[name] : null;
|
|
175
181
|
}
|
|
176
|
-
function
|
|
182
|
+
function getOptions(name) {
|
|
177
183
|
if (!name) return null;
|
|
178
184
|
const options2 = data.options ?? {};
|
|
179
185
|
const optionSet = options2[name];
|
|
@@ -186,34 +192,30 @@ async function createClient(options) {
|
|
|
186
192
|
getEntry,
|
|
187
193
|
getSlugs,
|
|
188
194
|
getImage,
|
|
189
|
-
|
|
195
|
+
getOptions
|
|
190
196
|
};
|
|
191
197
|
}
|
|
192
198
|
function createAsyncClient(options) {
|
|
193
199
|
const { siteToken, env = "latest", cdn, lang } = options;
|
|
194
200
|
const cache = options.cache ?? new MemoryCache();
|
|
195
201
|
let syncClient = null;
|
|
196
|
-
let tag = null;
|
|
197
202
|
async function getContent(search2) {
|
|
198
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
199
203
|
if (!search2 && !syncClient) {
|
|
200
|
-
syncClient = await createClient({ siteToken, env
|
|
204
|
+
syncClient = await createClient({ siteToken, env, cdn, lang, cache });
|
|
201
205
|
}
|
|
202
206
|
if (syncClient) return syncClient.getContent(search2);
|
|
203
207
|
if (!search2) return null;
|
|
204
|
-
return await fetchSiteContent({ siteToken, env
|
|
208
|
+
return await fetchSiteContent({ siteToken, env, search: search2, lang, cache, cdn });
|
|
205
209
|
}
|
|
206
210
|
async function getPages(path) {
|
|
207
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
208
211
|
if (syncClient) return syncClient.getPages(path);
|
|
209
|
-
const pages = await fetchSiteContent({ siteToken, env
|
|
212
|
+
const pages = await fetchSiteContent({ siteToken, env, search: "pages", lang, cache, cdn });
|
|
210
213
|
if (!path) return pages;
|
|
211
214
|
else return filterPagesByPath(pages, path);
|
|
212
215
|
}
|
|
213
216
|
async function getPage(path) {
|
|
214
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
215
217
|
if (syncClient) return syncClient.getPage(path);
|
|
216
|
-
const pages = await fetchSiteContent({ siteToken, env
|
|
218
|
+
const pages = await fetchSiteContent({ siteToken, env, search: "pages", lang, cache, cdn });
|
|
217
219
|
const page = pages == null ? void 0 : pages.find((p) => {
|
|
218
220
|
const pth = typeof p._path === "string" ? p._path : "";
|
|
219
221
|
return pth === path;
|
|
@@ -221,27 +223,23 @@ function createAsyncClient(options) {
|
|
|
221
223
|
return page ?? null;
|
|
222
224
|
}
|
|
223
225
|
async function getEntry(handle) {
|
|
224
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
225
226
|
if (syncClient) return syncClient.getEntry(handle);
|
|
226
227
|
let search2 = `entries.${handle}`;
|
|
227
|
-
return await fetchSiteContent({ siteToken, env
|
|
228
|
+
return await fetchSiteContent({ siteToken, env, search: search2, lang, cache, cdn });
|
|
228
229
|
}
|
|
229
230
|
async function getSlugs(path) {
|
|
230
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
231
231
|
if (syncClient) return syncClient.getSlugs(path);
|
|
232
|
-
const pages = await fetchSiteContent({ siteToken, env
|
|
232
|
+
const pages = await fetchSiteContent({ siteToken, env, search: "pages", lang, cache, cdn });
|
|
233
233
|
return extractSlugs(pages, path);
|
|
234
234
|
}
|
|
235
235
|
async function getImage(name) {
|
|
236
|
-
if (cdn && !tag) tag = await fetchEnvTag({ siteToken, env });
|
|
237
236
|
if (syncClient) return syncClient.getImage(name);
|
|
238
|
-
const images = await fetchSiteContent({ siteToken, env
|
|
237
|
+
const images = await fetchSiteContent({ siteToken, env, search: "images", lang, cache, cdn });
|
|
239
238
|
return images ? images[name] : null;
|
|
240
239
|
}
|
|
241
|
-
async function
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
const options2 = await fetchSiteContent({ siteToken, env: tag ?? env, search: "options", lang, cache });
|
|
240
|
+
async function getOptions(name) {
|
|
241
|
+
if (syncClient) return syncClient.getOptions(name);
|
|
242
|
+
const options2 = await fetchSiteContent({ siteToken, env, search: "options", lang, cache, cdn });
|
|
245
243
|
const optionSet = options2[name];
|
|
246
244
|
return Array.isArray(optionSet) ? optionSet : null;
|
|
247
245
|
}
|
|
@@ -259,7 +257,7 @@ function createAsyncClient(options) {
|
|
|
259
257
|
getEntry,
|
|
260
258
|
getSlugs,
|
|
261
259
|
getImage,
|
|
262
|
-
|
|
260
|
+
getOptions,
|
|
263
261
|
_getFetchUrl,
|
|
264
262
|
_getEnvTag
|
|
265
263
|
};
|