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