@sleekcms/client 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/dist/index.cjs ADDED
@@ -0,0 +1,213 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ createClient: () => createClient,
33
+ createSyncClient: () => createSyncClient
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var jmespath = __toESM(require("jmespath"), 1);
37
+ function isDevToken(token) {
38
+ return token.startsWith("dev-");
39
+ }
40
+ function getBaseUrl(token) {
41
+ let [env, siteId, ...rest] = token.split("-");
42
+ return `https://${env}.sleekcms.com/${siteId}`;
43
+ }
44
+ async function fetchSiteContent(options, searchQuery) {
45
+ const { siteToken, env = "latest", mock } = options;
46
+ if (!siteToken) {
47
+ throw new Error("[SleekCMS] siteToken is required");
48
+ }
49
+ const baseUrl = getBaseUrl(siteToken).replace(/\/$/, "");
50
+ const url = new URL(`${baseUrl}/${env}`);
51
+ if (searchQuery) {
52
+ url.searchParams.set("search", searchQuery);
53
+ }
54
+ if (mock && isDevToken(siteToken)) {
55
+ url.searchParams.set("mock", "true");
56
+ }
57
+ const res = await fetch(url.toString(), {
58
+ method: "GET",
59
+ headers: {
60
+ "Content-Type": "application/json",
61
+ Authorization: siteToken
62
+ }
63
+ });
64
+ if (!res.ok) {
65
+ let message = res.statusText;
66
+ try {
67
+ const data = await res.json();
68
+ if (data && data.message) message = data.message;
69
+ } catch {
70
+ }
71
+ throw new Error(`[SleekCMS] Request failed (${res.status}): ${message}`);
72
+ }
73
+ return res.json();
74
+ }
75
+ function applyJmes(data, query) {
76
+ if (!query) return data;
77
+ return jmespath.search(data, query);
78
+ }
79
+ function createClient(options) {
80
+ const dev = isDevToken(options.siteToken);
81
+ let cacheMode = !!options.cache || !!options.mock && dev;
82
+ let cachedContent = null;
83
+ async function ensureCacheLoaded() {
84
+ if (cachedContent) return cachedContent;
85
+ const data = await fetchSiteContent(options);
86
+ cachedContent = data;
87
+ return data;
88
+ }
89
+ async function getContent(query) {
90
+ if (cacheMode) {
91
+ const data2 = await ensureCacheLoaded();
92
+ return applyJmes(data2, query);
93
+ }
94
+ if (!query) {
95
+ const data2 = await fetchSiteContent(options);
96
+ cachedContent = data2;
97
+ cacheMode = true;
98
+ return data2;
99
+ }
100
+ const data = await fetchSiteContent(options, query);
101
+ return data;
102
+ }
103
+ async function findPages(path, query) {
104
+ if (!path) {
105
+ throw new Error("[SleekCMS] path is required for findPages");
106
+ }
107
+ if (cacheMode) {
108
+ const data = await ensureCacheLoaded();
109
+ const pages2 = data.pages ?? [];
110
+ const filtered2 = pages2.filter((p) => {
111
+ const pth = typeof p._path === "string" ? p._path : "";
112
+ return pth.startsWith(path);
113
+ });
114
+ return applyJmes(filtered2, query);
115
+ }
116
+ const pages = await fetchSiteContent(
117
+ options,
118
+ "pages"
119
+ );
120
+ const filtered = (pages ?? []).filter((p) => {
121
+ const pth = typeof p._path === "string" ? p._path : "";
122
+ return pth.startsWith(path);
123
+ });
124
+ return applyJmes(filtered, query);
125
+ }
126
+ async function getImages() {
127
+ if (cacheMode) {
128
+ const data = await ensureCacheLoaded();
129
+ return data.images ?? {};
130
+ }
131
+ const images = await fetchSiteContent(
132
+ options,
133
+ "images"
134
+ );
135
+ return images ?? {};
136
+ }
137
+ async function getImage(name) {
138
+ if (!name) return void 0;
139
+ if (cacheMode) {
140
+ const data = await ensureCacheLoaded();
141
+ return data.images ? data.images[name] : void 0;
142
+ }
143
+ const images = await fetchSiteContent(
144
+ options,
145
+ "images"
146
+ );
147
+ return images ? images[name] : void 0;
148
+ }
149
+ async function getList(name) {
150
+ if (!name) return void 0;
151
+ if (cacheMode) {
152
+ const data = await ensureCacheLoaded();
153
+ const lists2 = data.lists ?? {};
154
+ const list2 = lists2[name];
155
+ return Array.isArray(list2) ? list2 : void 0;
156
+ }
157
+ const lists = await fetchSiteContent(
158
+ options,
159
+ "lists"
160
+ );
161
+ const list = lists ? lists[name] : void 0;
162
+ return Array.isArray(list) ? list : void 0;
163
+ }
164
+ return {
165
+ getContent,
166
+ findPages,
167
+ getImages,
168
+ getImage,
169
+ getList
170
+ };
171
+ }
172
+ async function createSyncClient(options) {
173
+ const data = await fetchSiteContent(options);
174
+ function getContent(query) {
175
+ return applyJmes(data, query);
176
+ }
177
+ function findPages(path, query) {
178
+ if (!path) {
179
+ throw new Error("[SleekCMS] path is required for findPages");
180
+ }
181
+ const pages = data.pages ?? [];
182
+ const filtered = pages.filter((p) => {
183
+ const pth = typeof p._path === "string" ? p._path : "";
184
+ return pth.startsWith(path);
185
+ });
186
+ return applyJmes(filtered, query);
187
+ }
188
+ function getImages() {
189
+ return data.images ?? {};
190
+ }
191
+ function getImage(name) {
192
+ if (!name) return void 0;
193
+ return data.images ? data.images[name] : void 0;
194
+ }
195
+ function getList(name) {
196
+ if (!name) return void 0;
197
+ const lists = data.lists ?? {};
198
+ const list = lists[name];
199
+ return Array.isArray(list) ? list : void 0;
200
+ }
201
+ return {
202
+ getContent,
203
+ findPages,
204
+ getImages,
205
+ getImage,
206
+ getList
207
+ };
208
+ }
209
+ // Annotate the CommonJS export names for ESM import in node:
210
+ 0 && (module.exports = {
211
+ createClient,
212
+ createSyncClient
213
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleekcms/client",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Official SleekCMS content client for Node 18+ and browser",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -17,7 +17,8 @@
17
17
  "build": "tsup src/index.ts --dts --format esm,cjs",
18
18
  "clean": "rimraf dist || true",
19
19
  "test": "vitest run",
20
- "test:watch": "vitest"
20
+ "test:watch": "vitest",
21
+ "prepublishOnly": "npm run build"
21
22
  },
22
23
  "dependencies": {
23
24
  "jmespath": "^0.16.0"
@@ -30,6 +31,8 @@
30
31
  "devDependencies": {
31
32
  "@types/jmespath": "^0.15.2",
32
33
  "@vitest/ui": "^4.0.15",
34
+ "tsup": "^8.5.1",
35
+ "typescript": "^5.4.0",
33
36
  "vitest": "^4.0.15"
34
37
  }
35
38
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import jmespath from "jmespath";
1
+ import * as jmespath from "jmespath";
2
2
  import type { SleekSiteContent, ClientOptions } from "./types";
3
3
 
4
4
  function isDevToken(token: string): boolean {