@timbenniks/contentstack-platform-sdk 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.
Files changed (244) hide show
  1. package/dist/auth/index.cjs +607 -0
  2. package/dist/auth/index.cjs.map +1 -0
  3. package/dist/auth/index.d.cts +108 -0
  4. package/dist/auth/index.d.ts +108 -0
  5. package/dist/auth/index.mjs +25 -0
  6. package/dist/auth/index.mjs.map +1 -0
  7. package/dist/brandkit/index.cjs +525 -0
  8. package/dist/brandkit/index.cjs.map +1 -0
  9. package/dist/brandkit/index.d.cts +95 -0
  10. package/dist/brandkit/index.d.ts +95 -0
  11. package/dist/brandkit/index.mjs +11 -0
  12. package/dist/brandkit/index.mjs.map +1 -0
  13. package/dist/chunk-3C6J2BDB.mjs +84 -0
  14. package/dist/chunk-3C6J2BDB.mjs.map +1 -0
  15. package/dist/chunk-3KE63N3I.mjs +64 -0
  16. package/dist/chunk-3KE63N3I.mjs.map +1 -0
  17. package/dist/chunk-4CJ4IVPJ.mjs +212 -0
  18. package/dist/chunk-4CJ4IVPJ.mjs.map +1 -0
  19. package/dist/chunk-4JFUI7MJ.mjs +368 -0
  20. package/dist/chunk-4JFUI7MJ.mjs.map +1 -0
  21. package/dist/chunk-7VFGD32I.mjs +26 -0
  22. package/dist/chunk-7VFGD32I.mjs.map +1 -0
  23. package/dist/chunk-ARPJDW3A.mjs +44 -0
  24. package/dist/chunk-ARPJDW3A.mjs.map +1 -0
  25. package/dist/chunk-AVJHCFRK.mjs +52 -0
  26. package/dist/chunk-AVJHCFRK.mjs.map +1 -0
  27. package/dist/chunk-BK2IBTQS.mjs +131 -0
  28. package/dist/chunk-BK2IBTQS.mjs.map +1 -0
  29. package/dist/chunk-BUZ6CQHE.mjs +75 -0
  30. package/dist/chunk-BUZ6CQHE.mjs.map +1 -0
  31. package/dist/chunk-CKMAOWBQ.mjs +379 -0
  32. package/dist/chunk-CKMAOWBQ.mjs.map +1 -0
  33. package/dist/chunk-DJQLN4TR.mjs +1 -0
  34. package/dist/chunk-DJQLN4TR.mjs.map +1 -0
  35. package/dist/chunk-DMERADWM.mjs +67 -0
  36. package/dist/chunk-DMERADWM.mjs.map +1 -0
  37. package/dist/chunk-EREPKWTW.mjs +926 -0
  38. package/dist/chunk-EREPKWTW.mjs.map +1 -0
  39. package/dist/chunk-FQP4PB5X.mjs +88 -0
  40. package/dist/chunk-FQP4PB5X.mjs.map +1 -0
  41. package/dist/chunk-GNPQJBFX.mjs +144 -0
  42. package/dist/chunk-GNPQJBFX.mjs.map +1 -0
  43. package/dist/chunk-GOSB24M6.mjs +87 -0
  44. package/dist/chunk-GOSB24M6.mjs.map +1 -0
  45. package/dist/chunk-JL2E3EOT.mjs +255 -0
  46. package/dist/chunk-JL2E3EOT.mjs.map +1 -0
  47. package/dist/chunk-JYGZBKTH.mjs +222 -0
  48. package/dist/chunk-JYGZBKTH.mjs.map +1 -0
  49. package/dist/chunk-JZE2W7VW.mjs +52 -0
  50. package/dist/chunk-JZE2W7VW.mjs.map +1 -0
  51. package/dist/chunk-K76DKSHJ.mjs +18 -0
  52. package/dist/chunk-K76DKSHJ.mjs.map +1 -0
  53. package/dist/chunk-KLVIROVU.mjs +232 -0
  54. package/dist/chunk-KLVIROVU.mjs.map +1 -0
  55. package/dist/chunk-LPVVA5J3.mjs +18 -0
  56. package/dist/chunk-LPVVA5J3.mjs.map +1 -0
  57. package/dist/chunk-NKLOZ5VI.mjs +112 -0
  58. package/dist/chunk-NKLOZ5VI.mjs.map +1 -0
  59. package/dist/chunk-QGA4WBDC.mjs +7 -0
  60. package/dist/chunk-QGA4WBDC.mjs.map +1 -0
  61. package/dist/chunk-QW7TVYOA.mjs +56 -0
  62. package/dist/chunk-QW7TVYOA.mjs.map +1 -0
  63. package/dist/chunk-SU5QEKYW.mjs +83 -0
  64. package/dist/chunk-SU5QEKYW.mjs.map +1 -0
  65. package/dist/chunk-T5A2E2RI.mjs +654 -0
  66. package/dist/chunk-T5A2E2RI.mjs.map +1 -0
  67. package/dist/chunk-T5OIJQK7.mjs +116 -0
  68. package/dist/chunk-T5OIJQK7.mjs.map +1 -0
  69. package/dist/chunk-VW7DD6HV.mjs +253 -0
  70. package/dist/chunk-VW7DD6HV.mjs.map +1 -0
  71. package/dist/chunk-XH7NLHGW.mjs +133 -0
  72. package/dist/chunk-XH7NLHGW.mjs.map +1 -0
  73. package/dist/client-DJ5haQGd.d.cts +22 -0
  74. package/dist/client-DwVGVSQz.d.ts +22 -0
  75. package/dist/cma/index.cjs +1349 -0
  76. package/dist/cma/index.cjs.map +1 -0
  77. package/dist/cma/index.d.cts +22 -0
  78. package/dist/cma/index.d.ts +22 -0
  79. package/dist/cma/index.mjs +70 -0
  80. package/dist/cma/index.mjs.map +1 -0
  81. package/dist/errors-CAw-IRCP.d.cts +65 -0
  82. package/dist/errors-CAw-IRCP.d.ts +65 -0
  83. package/dist/generative-ai/index.cjs +401 -0
  84. package/dist/generative-ai/index.cjs.map +1 -0
  85. package/dist/generative-ai/index.d.cts +31 -0
  86. package/dist/generative-ai/index.d.ts +31 -0
  87. package/dist/generative-ai/index.mjs +10 -0
  88. package/dist/generative-ai/index.mjs.map +1 -0
  89. package/dist/images/index.cjs +185 -0
  90. package/dist/images/index.cjs.map +1 -0
  91. package/dist/images/index.d.cts +27 -0
  92. package/dist/images/index.d.ts +27 -0
  93. package/dist/images/index.mjs +8 -0
  94. package/dist/images/index.mjs.map +1 -0
  95. package/dist/index.cjs +2909 -0
  96. package/dist/index.cjs.map +1 -0
  97. package/dist/index.d.cts +30 -0
  98. package/dist/index.d.ts +30 -0
  99. package/dist/index.mjs +153 -0
  100. package/dist/index.mjs.map +1 -0
  101. package/dist/knowledge-vault/index.cjs +413 -0
  102. package/dist/knowledge-vault/index.cjs.map +1 -0
  103. package/dist/knowledge-vault/index.d.cts +49 -0
  104. package/dist/knowledge-vault/index.d.ts +49 -0
  105. package/dist/knowledge-vault/index.mjs +10 -0
  106. package/dist/knowledge-vault/index.mjs.map +1 -0
  107. package/dist/launch/index.cjs +624 -0
  108. package/dist/launch/index.cjs.map +1 -0
  109. package/dist/launch/index.d.cts +169 -0
  110. package/dist/launch/index.d.ts +169 -0
  111. package/dist/launch/index.mjs +11 -0
  112. package/dist/launch/index.mjs.map +1 -0
  113. package/dist/react/auth/index.cjs +113 -0
  114. package/dist/react/auth/index.cjs.map +1 -0
  115. package/dist/react/auth/index.d.cts +33 -0
  116. package/dist/react/auth/index.d.ts +33 -0
  117. package/dist/react/auth/index.mjs +13 -0
  118. package/dist/react/auth/index.mjs.map +1 -0
  119. package/dist/react/content/index.cjs +113 -0
  120. package/dist/react/content/index.cjs.map +1 -0
  121. package/dist/react/content/index.d.cts +25 -0
  122. package/dist/react/content/index.d.ts +25 -0
  123. package/dist/react/content/index.mjs +7 -0
  124. package/dist/react/content/index.mjs.map +1 -0
  125. package/dist/react/hooks/index.cjs +2097 -0
  126. package/dist/react/hooks/index.cjs.map +1 -0
  127. package/dist/react/hooks/index.d.cts +77 -0
  128. package/dist/react/hooks/index.d.ts +77 -0
  129. package/dist/react/hooks/index.mjs +46 -0
  130. package/dist/react/hooks/index.mjs.map +1 -0
  131. package/dist/react/index.cjs +2307 -0
  132. package/dist/react/index.cjs.map +1 -0
  133. package/dist/react/index.d.cts +16 -0
  134. package/dist/react/index.d.ts +16 -0
  135. package/dist/react/index.mjs +67 -0
  136. package/dist/react/index.mjs.map +1 -0
  137. package/dist/react/provider/index.cjs +83 -0
  138. package/dist/react/provider/index.cjs.map +1 -0
  139. package/dist/react/provider/index.d.cts +30 -0
  140. package/dist/react/provider/index.d.ts +30 -0
  141. package/dist/react/provider/index.mjs +11 -0
  142. package/dist/react/provider/index.mjs.map +1 -0
  143. package/dist/regions/index.cjs +171 -0
  144. package/dist/regions/index.cjs.map +1 -0
  145. package/dist/regions/index.d.cts +55 -0
  146. package/dist/regions/index.d.ts +55 -0
  147. package/dist/regions/index.mjs +13 -0
  148. package/dist/regions/index.mjs.map +1 -0
  149. package/dist/request-builders-BxeolQIw.d.ts +735 -0
  150. package/dist/request-builders-C2IG1LUo.d.cts +735 -0
  151. package/dist/rte/index.cjs +683 -0
  152. package/dist/rte/index.cjs.map +1 -0
  153. package/dist/rte/index.d.cts +33 -0
  154. package/dist/rte/index.d.ts +33 -0
  155. package/dist/rte/index.mjs +17 -0
  156. package/dist/rte/index.mjs.map +1 -0
  157. package/dist/server/index.cjs +917 -0
  158. package/dist/server/index.cjs.map +1 -0
  159. package/dist/server/index.d.cts +6 -0
  160. package/dist/server/index.d.ts +6 -0
  161. package/dist/server/index.mjs +45 -0
  162. package/dist/server/index.mjs.map +1 -0
  163. package/dist/server/middleware/index.cjs +614 -0
  164. package/dist/server/middleware/index.cjs.map +1 -0
  165. package/dist/server/middleware/index.d.cts +77 -0
  166. package/dist/server/middleware/index.d.ts +77 -0
  167. package/dist/server/middleware/index.mjs +27 -0
  168. package/dist/server/middleware/index.mjs.map +1 -0
  169. package/dist/server/proxy/index.cjs +329 -0
  170. package/dist/server/proxy/index.cjs.map +1 -0
  171. package/dist/server/proxy/index.d.cts +143 -0
  172. package/dist/server/proxy/index.d.ts +143 -0
  173. package/dist/server/proxy/index.mjs +29 -0
  174. package/dist/server/proxy/index.mjs.map +1 -0
  175. package/dist/server/webhooks/index.cjs +131 -0
  176. package/dist/server/webhooks/index.cjs.map +1 -0
  177. package/dist/server/webhooks/index.d.cts +230 -0
  178. package/dist/server/webhooks/index.d.ts +230 -0
  179. package/dist/server/webhooks/index.mjs +23 -0
  180. package/dist/server/webhooks/index.mjs.map +1 -0
  181. package/dist/types-6D9VR7pT.d.cts +26 -0
  182. package/dist/types-AelT0rFJ.d.cts +21 -0
  183. package/dist/types-AelT0rFJ.d.ts +21 -0
  184. package/dist/types-Bu5yCgmw.d.ts +26 -0
  185. package/dist/types-DgixK-ll.d.cts +23 -0
  186. package/dist/types-DgixK-ll.d.ts +23 -0
  187. package/dist/types-DrMwdlH9.d.cts +245 -0
  188. package/dist/types-DrMwdlH9.d.ts +245 -0
  189. package/dist/ui/css/index.cjs +31 -0
  190. package/dist/ui/css/index.cjs.map +1 -0
  191. package/dist/ui/css/index.d.cts +15 -0
  192. package/dist/ui/css/index.d.ts +15 -0
  193. package/dist/ui/css/index.mjs +7 -0
  194. package/dist/ui/css/index.mjs.map +1 -0
  195. package/dist/ui/index.cjs +368 -0
  196. package/dist/ui/index.cjs.map +1 -0
  197. package/dist/ui/index.d.cts +4 -0
  198. package/dist/ui/index.d.ts +4 -0
  199. package/dist/ui/index.mjs +33 -0
  200. package/dist/ui/index.mjs.map +1 -0
  201. package/dist/ui/theme/index.cjs +105 -0
  202. package/dist/ui/theme/index.cjs.map +1 -0
  203. package/dist/ui/theme/index.d.cts +33 -0
  204. package/dist/ui/theme/index.d.ts +33 -0
  205. package/dist/ui/theme/index.mjs +15 -0
  206. package/dist/ui/theme/index.mjs.map +1 -0
  207. package/dist/ui/tokens/index.cjs +286 -0
  208. package/dist/ui/tokens/index.cjs.map +1 -0
  209. package/dist/ui/tokens/index.d.cts +54 -0
  210. package/dist/ui/tokens/index.d.ts +54 -0
  211. package/dist/ui/tokens/index.mjs +17 -0
  212. package/dist/ui/tokens/index.mjs.map +1 -0
  213. package/dist/ui/tokens.css +408 -0
  214. package/dist/vue/auth/index.cjs +141 -0
  215. package/dist/vue/auth/index.cjs.map +1 -0
  216. package/dist/vue/auth/index.d.cts +78 -0
  217. package/dist/vue/auth/index.d.ts +78 -0
  218. package/dist/vue/auth/index.mjs +13 -0
  219. package/dist/vue/auth/index.mjs.map +1 -0
  220. package/dist/vue/composables/index.cjs +2108 -0
  221. package/dist/vue/composables/index.cjs.map +1 -0
  222. package/dist/vue/composables/index.d.cts +78 -0
  223. package/dist/vue/composables/index.d.ts +78 -0
  224. package/dist/vue/composables/index.mjs +46 -0
  225. package/dist/vue/composables/index.mjs.map +1 -0
  226. package/dist/vue/content/index.cjs +142 -0
  227. package/dist/vue/content/index.cjs.map +1 -0
  228. package/dist/vue/content/index.d.cts +47 -0
  229. package/dist/vue/content/index.d.ts +47 -0
  230. package/dist/vue/content/index.mjs +7 -0
  231. package/dist/vue/content/index.mjs.map +1 -0
  232. package/dist/vue/index.cjs +2389 -0
  233. package/dist/vue/index.cjs.map +1 -0
  234. package/dist/vue/index.d.cts +16 -0
  235. package/dist/vue/index.d.ts +16 -0
  236. package/dist/vue/index.mjs +69 -0
  237. package/dist/vue/index.mjs.map +1 -0
  238. package/dist/vue/provider/index.cjs +97 -0
  239. package/dist/vue/provider/index.cjs.map +1 -0
  240. package/dist/vue/provider/index.d.cts +90 -0
  241. package/dist/vue/provider/index.d.ts +90 -0
  242. package/dist/vue/provider/index.mjs +13 -0
  243. package/dist/vue/provider/index.mjs.map +1 -0
  244. package/package.json +195 -0
@@ -0,0 +1,607 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/auth/index.ts
21
+ var auth_exports = {};
22
+ __export(auth_exports, {
23
+ buildAuthorizationUrl: () => buildAuthorizationUrl,
24
+ contentstackAuthCallbacks: () => contentstackAuthCallbacks,
25
+ createAuthProvider: () => createAuthProvider,
26
+ exchangeCode: () => exchangeCode,
27
+ generateCodeChallenge: () => generateCodeChallenge,
28
+ generateCodeVerifier: () => generateCodeVerifier,
29
+ getUser: () => getUser,
30
+ refreshToken: () => refreshToken
31
+ });
32
+ module.exports = __toCommonJS(auth_exports);
33
+
34
+ // src/http/errors.ts
35
+ var ContentstackError = class extends Error {
36
+ name = "ContentstackError";
37
+ status;
38
+ errorCode;
39
+ errors;
40
+ requestPath;
41
+ constructor(message, options) {
42
+ super(message, options?.cause ? { cause: options.cause } : void 0);
43
+ this.status = options?.status;
44
+ this.errorCode = options?.errorCode;
45
+ this.errors = options?.errors;
46
+ this.requestPath = options?.requestPath;
47
+ }
48
+ };
49
+ var ContentstackAuthError = class extends ContentstackError {
50
+ name = "ContentstackAuthError";
51
+ status = 401;
52
+ };
53
+ var ContentstackForbiddenError = class extends ContentstackError {
54
+ name = "ContentstackForbiddenError";
55
+ status = 403;
56
+ };
57
+ var ContentstackNotFoundError = class extends ContentstackError {
58
+ name = "ContentstackNotFoundError";
59
+ status = 404;
60
+ };
61
+ var ContentstackValidationError = class extends ContentstackError {
62
+ name = "ContentstackValidationError";
63
+ status;
64
+ constructor(message, options) {
65
+ super(message, options);
66
+ this.status = options?.status ?? 400;
67
+ }
68
+ };
69
+ var ContentstackInvalidApiKeyError = class extends ContentstackError {
70
+ name = "ContentstackInvalidApiKeyError";
71
+ status = 412;
72
+ };
73
+ var ContentstackRateLimitError = class extends ContentstackError {
74
+ name = "ContentstackRateLimitError";
75
+ status = 429;
76
+ retryAfter;
77
+ constructor(message, options) {
78
+ super(message, { ...options, status: 429 });
79
+ this.retryAfter = options?.retryAfter;
80
+ }
81
+ };
82
+ var ContentstackServerError = class extends ContentstackError {
83
+ name = "ContentstackServerError";
84
+ };
85
+ var ContentstackConfigError = class extends ContentstackError {
86
+ name = "ContentstackConfigError";
87
+ };
88
+
89
+ // src/http/client.ts
90
+ var DEFAULT_TIMEOUT = 3e4;
91
+ var DEFAULT_RETRY_LIMIT = 5;
92
+ var DEFAULT_RETRY_DELAY = 300;
93
+ var MAX_JITTER = 100;
94
+ var ContentstackHttpClient = class _ContentstackHttpClient {
95
+ config;
96
+ constructor(config) {
97
+ this.config = {
98
+ baseUrl: config.baseUrl,
99
+ headers: config.headers ?? {},
100
+ timeout: config.timeout ?? DEFAULT_TIMEOUT,
101
+ retryOnError: config.retryOnError ?? true,
102
+ retryLimit: config.retryLimit ?? DEFAULT_RETRY_LIMIT,
103
+ retryDelay: config.retryDelay ?? DEFAULT_RETRY_DELAY,
104
+ fetch: config.fetch ?? globalThis.fetch.bind(globalThis)
105
+ };
106
+ }
107
+ async get(path, params) {
108
+ let url = `${this.config.baseUrl}${path}`;
109
+ if (params) {
110
+ const searchParams = new URLSearchParams(params);
111
+ url += `?${searchParams.toString()}`;
112
+ }
113
+ return this.request(url, { method: "GET" }, path);
114
+ }
115
+ async post(path, body) {
116
+ const url = `${this.config.baseUrl}${path}`;
117
+ return this.request(
118
+ url,
119
+ {
120
+ method: "POST",
121
+ headers: { "Content-Type": "application/json" },
122
+ body: body !== void 0 ? JSON.stringify(body) : void 0
123
+ },
124
+ path
125
+ );
126
+ }
127
+ async put(path, body) {
128
+ const url = `${this.config.baseUrl}${path}`;
129
+ return this.request(
130
+ url,
131
+ {
132
+ method: "PUT",
133
+ headers: { "Content-Type": "application/json" },
134
+ body: body !== void 0 ? JSON.stringify(body) : void 0
135
+ },
136
+ path
137
+ );
138
+ }
139
+ async patch(path, body) {
140
+ const url = `${this.config.baseUrl}${path}`;
141
+ return this.request(
142
+ url,
143
+ {
144
+ method: "PATCH",
145
+ headers: { "Content-Type": "application/json" },
146
+ body: body !== void 0 ? JSON.stringify(body) : void 0
147
+ },
148
+ path
149
+ );
150
+ }
151
+ async delete(path) {
152
+ const url = `${this.config.baseUrl}${path}`;
153
+ return this.request(url, { method: "DELETE" }, path);
154
+ }
155
+ async postForm(path, params) {
156
+ const url = `${this.config.baseUrl}${path}`;
157
+ return this.request(
158
+ url,
159
+ {
160
+ method: "POST",
161
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
162
+ body: params.toString()
163
+ },
164
+ path
165
+ );
166
+ }
167
+ async upload(path, form) {
168
+ const url = `${this.config.baseUrl}${path}`;
169
+ return this.request(url, { method: "POST", body: form }, path);
170
+ }
171
+ /** Return a new client with additional headers merged */
172
+ withHeaders(headers) {
173
+ return new _ContentstackHttpClient({
174
+ ...this.config,
175
+ headers: { ...this.config.headers, ...headers }
176
+ });
177
+ }
178
+ /** Return a new client with a different base URL */
179
+ withBaseUrl(baseUrl) {
180
+ return new _ContentstackHttpClient({
181
+ ...this.config,
182
+ baseUrl
183
+ });
184
+ }
185
+ async request(url, init, path) {
186
+ const headers = new Headers(this.config.headers);
187
+ if (init.headers) {
188
+ const initHeaders = init.headers instanceof Headers ? init.headers : new Headers(init.headers);
189
+ initHeaders.forEach((value, key) => headers.set(key, value));
190
+ }
191
+ let lastError;
192
+ const maxAttempts = this.config.retryOnError ? this.config.retryLimit + 1 : 1;
193
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
194
+ const controller = new AbortController();
195
+ const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
196
+ try {
197
+ const response = await this.config.fetch(url, {
198
+ ...init,
199
+ headers,
200
+ signal: controller.signal
201
+ });
202
+ if (response.ok) {
203
+ const data = await response.json().catch(() => ({}));
204
+ return { data, status: response.status, headers: response.headers };
205
+ }
206
+ const isRetryable = response.status === 429 || response.status >= 500;
207
+ if (isRetryable && this.config.retryOnError && attempt < maxAttempts - 1) {
208
+ const delay = this.calculateDelay(response, attempt);
209
+ await sleep(delay);
210
+ lastError = await this.createError(response, path);
211
+ continue;
212
+ }
213
+ throw await this.createError(response, path);
214
+ } catch (error) {
215
+ if (error instanceof ContentstackError) {
216
+ throw error;
217
+ }
218
+ if (error instanceof DOMException && error.name === "AbortError") {
219
+ throw new ContentstackError(`Request timed out after ${this.config.timeout}ms`, {
220
+ requestPath: path,
221
+ cause: error
222
+ });
223
+ }
224
+ throw new ContentstackError("Network request failed", {
225
+ requestPath: path,
226
+ cause: error instanceof Error ? error : new Error(String(error))
227
+ });
228
+ } finally {
229
+ clearTimeout(timeoutId);
230
+ }
231
+ }
232
+ throw lastError ?? new ContentstackError("Request failed after retries", { requestPath: path });
233
+ }
234
+ calculateDelay(response, attempt) {
235
+ const retryAfter = response.headers.get("Retry-After");
236
+ if (retryAfter) {
237
+ const seconds = Number.parseFloat(retryAfter);
238
+ if (!Number.isNaN(seconds)) {
239
+ return seconds * 1e3;
240
+ }
241
+ }
242
+ const jitter = Math.random() * MAX_JITTER;
243
+ return this.config.retryDelay * 2 ** attempt + jitter;
244
+ }
245
+ async createError(response, path) {
246
+ let body = {};
247
+ try {
248
+ body = await response.json();
249
+ } catch {
250
+ }
251
+ const message = body.error_message ?? body.error_description ?? body.message ?? `HTTP ${response.status} error`;
252
+ const errorCode = body.error_code;
253
+ const errors = body.errors;
254
+ const retryAfter = response.headers.get("Retry-After");
255
+ const opts = { status: response.status, errorCode, errors, requestPath: path };
256
+ switch (response.status) {
257
+ case 400:
258
+ return new ContentstackValidationError(message, { ...opts, status: 400 });
259
+ case 401:
260
+ return new ContentstackAuthError(message, opts);
261
+ case 403:
262
+ return new ContentstackForbiddenError(message, opts);
263
+ case 404:
264
+ return new ContentstackNotFoundError(message, opts);
265
+ case 412:
266
+ return new ContentstackInvalidApiKeyError(message, opts);
267
+ case 422:
268
+ return new ContentstackValidationError(message, { ...opts, status: 422 });
269
+ case 429:
270
+ return new ContentstackRateLimitError(message, {
271
+ ...opts,
272
+ retryAfter: retryAfter ? Number.parseFloat(retryAfter) : void 0
273
+ });
274
+ default:
275
+ if (response.status >= 500) {
276
+ return new ContentstackServerError(message, opts);
277
+ }
278
+ return new ContentstackError(message, opts);
279
+ }
280
+ }
281
+ };
282
+ function sleep(ms) {
283
+ return new Promise((resolve) => setTimeout(resolve, ms));
284
+ }
285
+
286
+ // src/regions/endpoints.ts
287
+ var import_contentstack_endpoints = require("@timbenniks/contentstack-endpoints");
288
+ var BRAND_KIT_URLS = {
289
+ us: {
290
+ brandKit: "https://brand-kits-api.contentstack.com",
291
+ brandKitAI: "https://ai.contentstack.com/brand-kits"
292
+ },
293
+ eu: {
294
+ brandKit: "https://eu-brand-kits-api.contentstack.com",
295
+ brandKitAI: "https://eu-ai.contentstack.com/brand-kits"
296
+ },
297
+ au: {
298
+ brandKit: "https://au-brand-kits-api.contentstack.com",
299
+ brandKitAI: "https://au-ai.contentstack.com/brand-kits"
300
+ },
301
+ "azure-na": {
302
+ brandKit: "https://azure-na-brand-kits-api.contentstack.com",
303
+ brandKitAI: "https://azure-na-ai.contentstack.com/brand-kits"
304
+ },
305
+ "azure-eu": {
306
+ brandKit: "https://azure-eu-brand-kits-api.contentstack.com",
307
+ brandKitAI: "https://azure-eu-ai.contentstack.com/brand-kits"
308
+ },
309
+ "gcp-na": {
310
+ brandKit: "https://gcp-na-brand-kits-api.contentstack.com",
311
+ brandKitAI: "https://gcp-na-ai.contentstack.com/brand-kits"
312
+ },
313
+ "gcp-eu": {
314
+ brandKit: "https://gcp-eu-brand-kits-api.contentstack.com",
315
+ brandKitAI: "https://gcp-eu-ai.contentstack.com/brand-kits"
316
+ }
317
+ };
318
+ function mapEndpoints(upstream, region, hostsOnly) {
319
+ const bk = BRAND_KIT_URLS[region];
320
+ const stripProtocol = (url) => url.replace(/^https?:\/\//, "");
321
+ return Object.freeze({
322
+ cma: upstream.contentManagement ?? "",
323
+ cda: upstream.contentDelivery ?? "",
324
+ graphql: upstream.graphqlDelivery ?? "",
325
+ images: upstream.images ?? "",
326
+ app: upstream.application ?? "",
327
+ preview: upstream.preview ?? "",
328
+ graphqlPreview: upstream.graphqlPreview ?? "",
329
+ launch: upstream.launch ?? "",
330
+ personalizeEdge: upstream.personalizeEdge ?? "",
331
+ brandKit: hostsOnly ? stripProtocol(bk.brandKit) : bk.brandKit,
332
+ brandKitAI: hostsOnly ? stripProtocol(bk.brandKitAI) : bk.brandKitAI
333
+ });
334
+ }
335
+ var ALL_REGIONS = [
336
+ "us",
337
+ "eu",
338
+ "au",
339
+ "azure-na",
340
+ "azure-eu",
341
+ "gcp-na",
342
+ "gcp-eu"
343
+ ];
344
+ function buildEndpointMap() {
345
+ const map = {};
346
+ for (const region of ALL_REGIONS) {
347
+ map[region] = mapEndpoints((0, import_contentstack_endpoints.getContentstackEndpoints)(region), region, false);
348
+ }
349
+ return Object.freeze(map);
350
+ }
351
+ function buildHostMap() {
352
+ const map = {};
353
+ for (const region of ALL_REGIONS) {
354
+ map[region] = mapEndpoints((0, import_contentstack_endpoints.getContentstackEndpoints)(region, true), region, true);
355
+ }
356
+ return Object.freeze(map);
357
+ }
358
+ var ENDPOINT_MAP = buildEndpointMap();
359
+ var HOST_MAP = buildHostMap();
360
+
361
+ // src/regions/resolver.ts
362
+ var VALID_REGIONS = new Set(Object.keys(ENDPOINT_MAP));
363
+ function resolveEndpoints(region) {
364
+ return ENDPOINT_MAP[region];
365
+ }
366
+
367
+ // src/auth/pkce.ts
368
+ var CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
369
+ function generateCodeVerifier() {
370
+ const bytes = new Uint8Array(43);
371
+ globalThis.crypto.getRandomValues(bytes);
372
+ let result = "";
373
+ for (const byte of bytes) {
374
+ result += CHARSET[byte % CHARSET.length];
375
+ }
376
+ return result;
377
+ }
378
+ async function generateCodeChallenge(verifier) {
379
+ const encoder = new TextEncoder();
380
+ const data = encoder.encode(verifier);
381
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", data);
382
+ const bytes = new Uint8Array(digest);
383
+ let binary = "";
384
+ for (const byte of bytes) {
385
+ binary += String.fromCharCode(byte);
386
+ }
387
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
388
+ }
389
+
390
+ // src/auth/oauth-client.ts
391
+ function validateConfig(config) {
392
+ if (!config.appId) {
393
+ throw new ContentstackConfigError(
394
+ "appId is required for OAuth. This is your Contentstack app's UID, not the client ID."
395
+ );
396
+ }
397
+ if (config.appId === config.clientId) {
398
+ throw new ContentstackConfigError(
399
+ "appId and clientId must be different. appId is your Contentstack app UID; clientId is the OAuth client identifier."
400
+ );
401
+ }
402
+ }
403
+ async function buildAuthorizationUrl(config, options) {
404
+ validateConfig(config);
405
+ const endpoints = resolveEndpoints(config.region);
406
+ const authorizationUrl = `${endpoints.app}/apps/${config.appId}/authorize`;
407
+ const state = options?.state ?? generateCodeVerifier();
408
+ const params = new URLSearchParams({
409
+ response_type: "code",
410
+ client_id: config.clientId,
411
+ redirect_uri: config.redirectUri,
412
+ scope: config.scopes.join(" "),
413
+ state
414
+ });
415
+ let codeVerifier;
416
+ if (options?.usePKCE) {
417
+ codeVerifier = generateCodeVerifier();
418
+ const codeChallenge = await generateCodeChallenge(codeVerifier);
419
+ params.set("code_challenge", codeChallenge);
420
+ params.set("code_challenge_method", "S256");
421
+ }
422
+ return {
423
+ url: `${authorizationUrl}?${params.toString()}`,
424
+ state,
425
+ codeVerifier
426
+ };
427
+ }
428
+ function mapTokenResponse(data) {
429
+ return {
430
+ accessToken: data.access_token,
431
+ refreshToken: data.refresh_token,
432
+ expiresIn: data.expires_in,
433
+ tokenType: data.token_type
434
+ };
435
+ }
436
+ async function makeTokenRequest(appBaseUrl, body, errorMessage) {
437
+ const client = new ContentstackHttpClient({ baseUrl: appBaseUrl });
438
+ try {
439
+ const { data } = await client.postForm("/apps-api/token", body);
440
+ return mapTokenResponse(data);
441
+ } catch (err) {
442
+ if (err instanceof ContentstackError) {
443
+ throw new ContentstackAuthError(err.message || errorMessage, {
444
+ status: err.status,
445
+ requestPath: "/apps-api/token",
446
+ cause: err
447
+ });
448
+ }
449
+ throw new ContentstackAuthError(errorMessage, {
450
+ requestPath: "/apps-api/token",
451
+ cause: err instanceof Error ? err : void 0
452
+ });
453
+ }
454
+ }
455
+ async function exchangeCode(config, code, options) {
456
+ const endpoints = resolveEndpoints(config.region);
457
+ const body = new URLSearchParams({
458
+ grant_type: "authorization_code",
459
+ code,
460
+ redirect_uri: config.redirectUri,
461
+ client_id: config.clientId,
462
+ client_secret: config.clientSecret
463
+ });
464
+ if (options?.codeVerifier) {
465
+ body.set("code_verifier", options.codeVerifier);
466
+ }
467
+ return makeTokenRequest(endpoints.app, body, "Failed to exchange authorization code");
468
+ }
469
+ async function refreshToken(config, token) {
470
+ const endpoints = resolveEndpoints(config.region);
471
+ const body = new URLSearchParams({
472
+ grant_type: "refresh_token",
473
+ refresh_token: token,
474
+ client_id: config.clientId,
475
+ client_secret: config.clientSecret
476
+ });
477
+ return makeTokenRequest(endpoints.app, body, "Failed to refresh token");
478
+ }
479
+ function mapUser(user) {
480
+ return {
481
+ uid: user.uid,
482
+ email: user.email,
483
+ firstName: user.first_name ?? void 0,
484
+ lastName: user.last_name ?? void 0,
485
+ username: user.username ?? void 0,
486
+ profileImage: user.profile_image ?? void 0
487
+ };
488
+ }
489
+ async function getUser(region, accessToken) {
490
+ const endpoints = resolveEndpoints(region);
491
+ const client = new ContentstackHttpClient({
492
+ baseUrl: `${endpoints.cma}/v3`,
493
+ headers: { Authorization: `Bearer ${accessToken}` }
494
+ });
495
+ try {
496
+ const { data } = await client.get("/user");
497
+ return mapUser(data.user);
498
+ } catch (err) {
499
+ if (err instanceof ContentstackError) {
500
+ throw new ContentstackAuthError(err.message || "Failed to fetch user profile", {
501
+ status: err.status,
502
+ requestPath: "/v3/user",
503
+ cause: err
504
+ });
505
+ }
506
+ throw new ContentstackAuthError("Failed to fetch user profile", {
507
+ requestPath: "/v3/user",
508
+ cause: err instanceof Error ? err : void 0
509
+ });
510
+ }
511
+ }
512
+ function createAuthProvider(config) {
513
+ validateConfig(config);
514
+ const endpoints = resolveEndpoints(config.region);
515
+ const userClient = new ContentstackHttpClient({ baseUrl: `${endpoints.cma}/v3` });
516
+ return {
517
+ id: "contentstack",
518
+ name: "Contentstack",
519
+ type: "oauth",
520
+ checks: ["state"],
521
+ authorization: {
522
+ url: `${endpoints.app}/apps/${config.appId}/authorize`,
523
+ params: {
524
+ response_type: "code",
525
+ scope: config.scopes.join(" ")
526
+ }
527
+ },
528
+ token: `${endpoints.app}/apps-api/token`,
529
+ userinfo: {
530
+ url: `${endpoints.cma}/v3/user`,
531
+ async request({ tokens }) {
532
+ const authedClient = userClient.withHeaders({
533
+ Authorization: `Bearer ${tokens.access_token}`
534
+ });
535
+ const { data } = await authedClient.get("/user");
536
+ return data;
537
+ }
538
+ },
539
+ profile(profile) {
540
+ const user = profile.user;
541
+ return {
542
+ id: user.uid,
543
+ name: [user.first_name, user.last_name].filter(Boolean).join(" ") || user.username,
544
+ email: user.email,
545
+ image: user.profile_image ?? null
546
+ };
547
+ },
548
+ clientId: config.clientId,
549
+ clientSecret: config.clientSecret
550
+ };
551
+ }
552
+ function contentstackAuthCallbacks(config) {
553
+ return {
554
+ async jwt({
555
+ token,
556
+ account
557
+ }) {
558
+ if (account) {
559
+ token.accessToken = account.access_token;
560
+ token.refreshToken = account.refresh_token;
561
+ token.accessTokenExpiresAt = Date.now() + (account.expires_in ?? 3600) * 1e3;
562
+ return token;
563
+ }
564
+ const expiresAt = token.accessTokenExpiresAt;
565
+ if (expiresAt && Date.now() < expiresAt - 6e4) {
566
+ return token;
567
+ }
568
+ const currentRefreshToken = token.refreshToken;
569
+ if (!currentRefreshToken) {
570
+ token.error = "RefreshAccessTokenError";
571
+ return token;
572
+ }
573
+ try {
574
+ const tokens = await refreshToken(config, currentRefreshToken);
575
+ token.accessToken = tokens.accessToken;
576
+ token.refreshToken = tokens.refreshToken;
577
+ token.accessTokenExpiresAt = Date.now() + tokens.expiresIn * 1e3;
578
+ token.error = void 0;
579
+ } catch {
580
+ token.error = "RefreshAccessTokenError";
581
+ }
582
+ return token;
583
+ },
584
+ async session({
585
+ session,
586
+ token
587
+ }) {
588
+ session.accessToken = token.accessToken;
589
+ if (token.error) {
590
+ session.error = token.error;
591
+ }
592
+ return session;
593
+ }
594
+ };
595
+ }
596
+ // Annotate the CommonJS export names for ESM import in node:
597
+ 0 && (module.exports = {
598
+ buildAuthorizationUrl,
599
+ contentstackAuthCallbacks,
600
+ createAuthProvider,
601
+ exchangeCode,
602
+ generateCodeChallenge,
603
+ generateCodeVerifier,
604
+ getUser,
605
+ refreshToken
606
+ });
607
+ //# sourceMappingURL=index.cjs.map