@sleekcms/client 0.1.3 → 0.1.5

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/index.d.cts CHANGED
@@ -25,6 +25,7 @@ interface ClientOptions {
25
25
  env?: string;
26
26
  cache?: boolean;
27
27
  mock?: boolean;
28
+ resolveEnv?: boolean;
28
29
  }
29
30
 
30
31
  /**
package/index.d.ts CHANGED
@@ -25,6 +25,7 @@ interface ClientOptions {
25
25
  env?: string;
26
26
  cache?: boolean;
27
27
  mock?: boolean;
28
+ resolveEnv?: boolean;
28
29
  }
29
30
 
30
31
  /**
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@sleekcms/client",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Official SleekCMS content client for Node 18+ and browser",
5
5
  "type": "module",
6
6
  "main": "index.cjs",
7
7
  "module": "index.mjs",
8
8
  "types": "index.d.ts",
9
+ "private": false,
9
10
  "exports": {
10
11
  ".": {
11
12
  "types": "./index.d.ts",
@@ -14,11 +15,11 @@
14
15
  }
15
16
  },
16
17
  "scripts": {
17
- "build": "npm run clean && tsup src/index.ts --dts --format esm,cjs && cp package.json README.md dist/",
18
+ "build": "npm run clean && tsup src/index.ts --dts --format esm,cjs && cp README.md dist/ && jq '.private = false | .files = [\"index.cjs\", \"index.mjs\", \"index.d.ts\", \"index.d.cts\", \"README.md\"]' package.json > dist/package.json",
18
19
  "clean": "rimraf dist || true",
19
20
  "test": "vitest run",
20
21
  "test:watch": "vitest",
21
- "publish:dist": "cd dist && npm publish"
22
+ "publish:dist": "cd dist && npm publish --access public"
22
23
  },
23
24
  "dependencies": {
24
25
  "jmespath": "^0.16.0"
@@ -34,5 +35,12 @@
34
35
  "tsup": "^8.5.1",
35
36
  "typescript": "^5.4.0",
36
37
  "vitest": "^4.0.15"
37
- }
38
+ },
39
+ "files": [
40
+ "index.cjs",
41
+ "index.mjs",
42
+ "index.d.ts",
43
+ "index.d.cts",
44
+ "README.md"
45
+ ]
38
46
  }
package/index.js DELETED
@@ -1,178 +0,0 @@
1
- // src/index.ts
2
- import * as jmespath from "jmespath";
3
- function isDevToken(token) {
4
- return token.startsWith("dev-");
5
- }
6
- function getBaseUrl(token) {
7
- let [env, siteId, ...rest] = token.split("-");
8
- return `https://${env}.sleekcms.com/${siteId}`;
9
- }
10
- async function fetchSiteContent(options, searchQuery) {
11
- const { siteToken, env = "latest", mock } = options;
12
- if (!siteToken) {
13
- throw new Error("[SleekCMS] siteToken is required");
14
- }
15
- const baseUrl = getBaseUrl(siteToken).replace(/\/$/, "");
16
- const url = new URL(`${baseUrl}/${env}`);
17
- if (searchQuery) {
18
- url.searchParams.set("search", searchQuery);
19
- }
20
- if (mock && isDevToken(siteToken)) {
21
- url.searchParams.set("mock", "true");
22
- }
23
- const res = await fetch(url.toString(), {
24
- method: "GET",
25
- headers: {
26
- "Content-Type": "application/json",
27
- Authorization: siteToken
28
- }
29
- });
30
- if (!res.ok) {
31
- let message = res.statusText;
32
- try {
33
- const data = await res.json();
34
- if (data && data.message) message = data.message;
35
- } catch {
36
- }
37
- throw new Error(`[SleekCMS] Request failed (${res.status}): ${message}`);
38
- }
39
- return res.json();
40
- }
41
- function applyJmes(data, query) {
42
- if (!query) return data;
43
- return jmespath.search(data, query);
44
- }
45
- function createClient(options) {
46
- const dev = isDevToken(options.siteToken);
47
- let cacheMode = !!options.cache || !!options.mock && dev;
48
- let cachedContent = null;
49
- async function ensureCacheLoaded() {
50
- if (cachedContent) return cachedContent;
51
- const data = await fetchSiteContent(options);
52
- cachedContent = data;
53
- return data;
54
- }
55
- async function getContent(query) {
56
- if (cacheMode) {
57
- const data2 = await ensureCacheLoaded();
58
- return applyJmes(data2, query);
59
- }
60
- if (!query) {
61
- const data2 = await fetchSiteContent(options);
62
- cachedContent = data2;
63
- cacheMode = true;
64
- return data2;
65
- }
66
- const data = await fetchSiteContent(options, query);
67
- return data;
68
- }
69
- async function findPages(path, query) {
70
- if (!path) {
71
- throw new Error("[SleekCMS] path is required for findPages");
72
- }
73
- if (cacheMode) {
74
- const data = await ensureCacheLoaded();
75
- const pages2 = data.pages ?? [];
76
- const filtered2 = pages2.filter((p) => {
77
- const pth = typeof p._path === "string" ? p._path : "";
78
- return pth.startsWith(path);
79
- });
80
- return applyJmes(filtered2, query);
81
- }
82
- const pages = await fetchSiteContent(
83
- options,
84
- "pages"
85
- );
86
- const filtered = (pages ?? []).filter((p) => {
87
- const pth = typeof p._path === "string" ? p._path : "";
88
- return pth.startsWith(path);
89
- });
90
- return applyJmes(filtered, query);
91
- }
92
- async function getImages() {
93
- if (cacheMode) {
94
- const data = await ensureCacheLoaded();
95
- return data.images ?? {};
96
- }
97
- const images = await fetchSiteContent(
98
- options,
99
- "images"
100
- );
101
- return images ?? {};
102
- }
103
- async function getImage(name) {
104
- if (!name) return void 0;
105
- if (cacheMode) {
106
- const data = await ensureCacheLoaded();
107
- return data.images ? data.images[name] : void 0;
108
- }
109
- const images = await fetchSiteContent(
110
- options,
111
- "images"
112
- );
113
- return images ? images[name] : void 0;
114
- }
115
- async function getList(name) {
116
- if (!name) return void 0;
117
- if (cacheMode) {
118
- const data = await ensureCacheLoaded();
119
- const lists2 = data.lists ?? {};
120
- const list2 = lists2[name];
121
- return Array.isArray(list2) ? list2 : void 0;
122
- }
123
- const lists = await fetchSiteContent(
124
- options,
125
- "lists"
126
- );
127
- const list = lists ? lists[name] : void 0;
128
- return Array.isArray(list) ? list : void 0;
129
- }
130
- return {
131
- getContent,
132
- findPages,
133
- getImages,
134
- getImage,
135
- getList
136
- };
137
- }
138
- async function createSyncClient(options) {
139
- const data = await fetchSiteContent(options);
140
- function getContent(query) {
141
- return applyJmes(data, query);
142
- }
143
- function findPages(path, query) {
144
- if (!path) {
145
- throw new Error("[SleekCMS] path is required for findPages");
146
- }
147
- const pages = data.pages ?? [];
148
- const filtered = pages.filter((p) => {
149
- const pth = typeof p._path === "string" ? p._path : "";
150
- return pth.startsWith(path);
151
- });
152
- return applyJmes(filtered, query);
153
- }
154
- function getImages() {
155
- return data.images ?? {};
156
- }
157
- function getImage(name) {
158
- if (!name) return void 0;
159
- return data.images ? data.images[name] : void 0;
160
- }
161
- function getList(name) {
162
- if (!name) return void 0;
163
- const lists = data.lists ?? {};
164
- const list = lists[name];
165
- return Array.isArray(list) ? list : void 0;
166
- }
167
- return {
168
- getContent,
169
- findPages,
170
- getImages,
171
- getImage,
172
- getList
173
- };
174
- }
175
- export {
176
- createClient,
177
- createSyncClient
178
- };