@inneropen/marvin-sdk 2.0.0-next.18 → 2.0.0-next.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.
@@ -0,0 +1,396 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __esm = (fn, res) => function __init() {
6
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
7
+ };
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // src/core/errors/MarvinError.ts
23
+ var MarvinError, MarvinApiError, MarvinAuthError, MarvinConfigError, MarvinValidationError;
24
+ var init_MarvinError = __esm({
25
+ "src/core/errors/MarvinError.ts"() {
26
+ "use strict";
27
+ MarvinError = class _MarvinError extends Error {
28
+ constructor(message, code, statusCode) {
29
+ super(message);
30
+ this.code = code;
31
+ this.statusCode = statusCode;
32
+ this.name = "MarvinError";
33
+ Object.setPrototypeOf(this, _MarvinError.prototype);
34
+ }
35
+ code;
36
+ statusCode;
37
+ };
38
+ MarvinApiError = class _MarvinApiError extends MarvinError {
39
+ constructor(message, statusCode, endpoint, responseBody) {
40
+ super(message, "API_ERROR", statusCode);
41
+ this.statusCode = statusCode;
42
+ this.endpoint = endpoint;
43
+ this.responseBody = responseBody;
44
+ this.name = "MarvinApiError";
45
+ Object.setPrototypeOf(this, _MarvinApiError.prototype);
46
+ }
47
+ statusCode;
48
+ endpoint;
49
+ responseBody;
50
+ static fromResponse(status, statusText, endpoint, body) {
51
+ const message = `Marvin API error: ${status} ${statusText} at ${endpoint}${body ? `
52
+ ${body}` : ""}`;
53
+ return new _MarvinApiError(message, status, endpoint, body);
54
+ }
55
+ };
56
+ MarvinAuthError = class _MarvinAuthError extends MarvinError {
57
+ constructor(message, statusCode) {
58
+ super(message, "AUTH_ERROR", statusCode);
59
+ this.name = "MarvinAuthError";
60
+ Object.setPrototypeOf(this, _MarvinAuthError.prototype);
61
+ }
62
+ };
63
+ MarvinConfigError = class _MarvinConfigError extends MarvinError {
64
+ constructor(message) {
65
+ super(message, "CONFIG_ERROR");
66
+ this.name = "MarvinConfigError";
67
+ Object.setPrototypeOf(this, _MarvinConfigError.prototype);
68
+ }
69
+ };
70
+ MarvinValidationError = class _MarvinValidationError extends MarvinError {
71
+ constructor(message, field) {
72
+ super(message, "VALIDATION_ERROR");
73
+ this.field = field;
74
+ this.name = "MarvinValidationError";
75
+ Object.setPrototypeOf(this, _MarvinValidationError.prototype);
76
+ }
77
+ field;
78
+ };
79
+ }
80
+ });
81
+
82
+ // src/core/errors/index.ts
83
+ var init_errors = __esm({
84
+ "src/core/errors/index.ts"() {
85
+ "use strict";
86
+ init_MarvinError();
87
+ }
88
+ });
89
+
90
+ // src/core/http/HttpClient.ts
91
+ var HttpClient;
92
+ var init_HttpClient = __esm({
93
+ "src/core/http/HttpClient.ts"() {
94
+ "use strict";
95
+ init_errors();
96
+ HttpClient = class {
97
+ baseUrl;
98
+ auth;
99
+ defaultHeaders;
100
+ credentials;
101
+ timeout;
102
+ constructor(config) {
103
+ this.baseUrl = config.baseUrl;
104
+ this.auth = config.auth;
105
+ this.defaultHeaders = config.defaultHeaders || {};
106
+ this.credentials = config.credentials || "same-origin";
107
+ this.timeout = config.timeout || 3e4;
108
+ }
109
+ /**
110
+ * Build full URL from endpoint
111
+ */
112
+ buildUrl(endpoint) {
113
+ const base = this.baseUrl.replace(/\/$/, "");
114
+ const path = endpoint.replace(/^\//, "");
115
+ return `${base}/${path}`;
116
+ }
117
+ /**
118
+ * Build headers for request
119
+ */
120
+ buildHeaders(additionalHeaders, isFormData = false) {
121
+ const headerInit = {};
122
+ if (!isFormData) {
123
+ headerInit["Content-Type"] = "application/json";
124
+ }
125
+ const headers = new Headers({
126
+ ...headerInit,
127
+ ...this.defaultHeaders,
128
+ ...additionalHeaders
129
+ });
130
+ this.auth.injectAuth(headers);
131
+ return headers;
132
+ }
133
+ /**
134
+ * Build query string from params
135
+ */
136
+ buildQueryString(params) {
137
+ const filtered = Object.entries(params).filter(([_, value]) => value !== void 0).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
138
+ return filtered.length > 0 ? `?${filtered.join("&")}` : "";
139
+ }
140
+ /**
141
+ * Perform HTTP request
142
+ */
143
+ async request(method, endpoint, options) {
144
+ const url = this.buildUrl(endpoint) + (options?.params ? this.buildQueryString(options.params) : "");
145
+ const isFormData = options?.body instanceof FormData;
146
+ const headers = this.buildHeaders(options?.headers, isFormData);
147
+ const controller = new AbortController();
148
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
149
+ try {
150
+ const response = await fetch(url, {
151
+ method,
152
+ headers,
153
+ body: isFormData ? options.body : options?.body ? JSON.stringify(options.body) : void 0,
154
+ credentials: this.credentials,
155
+ signal: controller.signal
156
+ });
157
+ clearTimeout(timeoutId);
158
+ if (response.status === 401 && this.auth.handleUnauthorized) {
159
+ await this.auth.handleUnauthorized();
160
+ }
161
+ if (!response.ok) {
162
+ const errorText = await response.text().catch(() => "");
163
+ throw MarvinApiError.fromResponse(
164
+ response.status,
165
+ response.statusText,
166
+ endpoint,
167
+ errorText
168
+ );
169
+ }
170
+ const contentType = response.headers.get("content-type");
171
+ if (!contentType || !contentType.includes("application/json")) {
172
+ return void 0;
173
+ }
174
+ return await response.json();
175
+ } catch (error) {
176
+ clearTimeout(timeoutId);
177
+ if (error instanceof MarvinApiError) {
178
+ throw error;
179
+ }
180
+ if (error instanceof Error) {
181
+ if (error.name === "AbortError") {
182
+ throw new MarvinApiError(
183
+ `Request timeout after ${this.timeout}ms`,
184
+ 408,
185
+ endpoint
186
+ );
187
+ }
188
+ throw new MarvinApiError(error.message, 0, endpoint);
189
+ }
190
+ throw error;
191
+ }
192
+ }
193
+ /**
194
+ * GET request
195
+ */
196
+ async get(endpoint, params) {
197
+ return this.request("GET", endpoint, { params });
198
+ }
199
+ /**
200
+ * POST request
201
+ */
202
+ async post(endpoint, body) {
203
+ return this.request("POST", endpoint, { body });
204
+ }
205
+ /**
206
+ * PUT request
207
+ */
208
+ async put(endpoint, body) {
209
+ return this.request("PUT", endpoint, { body });
210
+ }
211
+ /**
212
+ * PATCH request
213
+ */
214
+ async patch(endpoint, body) {
215
+ return this.request("PATCH", endpoint, { body });
216
+ }
217
+ /**
218
+ * DELETE request
219
+ */
220
+ async delete(endpoint) {
221
+ return this.request("DELETE", endpoint);
222
+ }
223
+ };
224
+ }
225
+ });
226
+
227
+ // src/core/http/index.ts
228
+ var init_http = __esm({
229
+ "src/core/http/index.ts"() {
230
+ "use strict";
231
+ init_HttpClient();
232
+ }
233
+ });
234
+
235
+ // src/core/auth/AuthStrategy.ts
236
+ var BearerTokenAuth, SessionAuth, NoAuth;
237
+ var init_AuthStrategy = __esm({
238
+ "src/core/auth/AuthStrategy.ts"() {
239
+ "use strict";
240
+ BearerTokenAuth = class {
241
+ constructor(token) {
242
+ this.token = token;
243
+ }
244
+ token;
245
+ injectAuth(headers) {
246
+ headers.set("Authorization", `Bearer ${this.token}`);
247
+ }
248
+ getDescription() {
249
+ return "Bearer token authentication";
250
+ }
251
+ };
252
+ SessionAuth = class {
253
+ constructor(sessionToken, onUnauthorized) {
254
+ this.sessionToken = sessionToken;
255
+ this.onUnauthorized = onUnauthorized;
256
+ }
257
+ sessionToken;
258
+ onUnauthorized;
259
+ injectAuth(headers) {
260
+ if (this.sessionToken) {
261
+ headers.set("Authorization", `Bearer ${this.sessionToken}`);
262
+ }
263
+ }
264
+ async handleUnauthorized() {
265
+ if (this.onUnauthorized) {
266
+ await this.onUnauthorized();
267
+ }
268
+ }
269
+ getDescription() {
270
+ return this.sessionToken ? "Session authentication (explicit token)" : "Session authentication (cookies)";
271
+ }
272
+ };
273
+ NoAuth = class {
274
+ injectAuth(_headers) {
275
+ }
276
+ getDescription() {
277
+ return "No authentication";
278
+ }
279
+ };
280
+ }
281
+ });
282
+
283
+ // src/core/auth/index.ts
284
+ var init_auth = __esm({
285
+ "src/core/auth/index.ts"() {
286
+ "use strict";
287
+ init_AuthStrategy();
288
+ }
289
+ });
290
+
291
+ // src/core/pagination/PaginatedResponse.ts
292
+ function createPaginatedResponse(data, meta) {
293
+ return { data, meta };
294
+ }
295
+ function extractData(response) {
296
+ if (Array.isArray(response)) {
297
+ return response;
298
+ }
299
+ return response.data;
300
+ }
301
+ function isPaginatedResponse(response) {
302
+ return !Array.isArray(response) && "data" in response;
303
+ }
304
+ function pageToOffset(page, limit) {
305
+ return (page - 1) * limit;
306
+ }
307
+ function offsetToPage(offset, limit) {
308
+ return Math.floor(offset / limit) + 1;
309
+ }
310
+ var init_PaginatedResponse = __esm({
311
+ "src/core/pagination/PaginatedResponse.ts"() {
312
+ "use strict";
313
+ }
314
+ });
315
+
316
+ // src/core/pagination/index.ts
317
+ var init_pagination = __esm({
318
+ "src/core/pagination/index.ts"() {
319
+ "use strict";
320
+ init_PaginatedResponse();
321
+ }
322
+ });
323
+
324
+ // src/core/cache.ts
325
+ var MarvinCache;
326
+ var init_cache = __esm({
327
+ "src/core/cache.ts"() {
328
+ "use strict";
329
+ MarvinCache = class {
330
+ constructor(duration = 5 * 60 * 1e3) {
331
+ this.duration = duration;
332
+ }
333
+ duration;
334
+ cache = /* @__PURE__ */ new Map();
335
+ get(key) {
336
+ const entry = this.cache.get(key);
337
+ if (!entry) return void 0;
338
+ const age = Date.now() - entry.timestamp;
339
+ if (age > this.duration) {
340
+ this.cache.delete(key);
341
+ return void 0;
342
+ }
343
+ return entry.data;
344
+ }
345
+ set(key, data) {
346
+ this.cache.set(key, {
347
+ data,
348
+ timestamp: Date.now()
349
+ });
350
+ }
351
+ has(key) {
352
+ return this.get(key) !== void 0;
353
+ }
354
+ clear() {
355
+ this.cache.clear();
356
+ }
357
+ delete(key) {
358
+ this.cache.delete(key);
359
+ }
360
+ };
361
+ }
362
+ });
363
+
364
+ // src/core/index.ts
365
+ var init_core = __esm({
366
+ "src/core/index.ts"() {
367
+ "use strict";
368
+ init_http();
369
+ init_auth();
370
+ init_errors();
371
+ init_pagination();
372
+ init_cache();
373
+ }
374
+ });
375
+
376
+ export {
377
+ __esm,
378
+ __export,
379
+ __toCommonJS,
380
+ MarvinError,
381
+ MarvinApiError,
382
+ MarvinAuthError,
383
+ MarvinConfigError,
384
+ MarvinValidationError,
385
+ HttpClient,
386
+ BearerTokenAuth,
387
+ SessionAuth,
388
+ NoAuth,
389
+ createPaginatedResponse,
390
+ extractData,
391
+ isPaginatedResponse,
392
+ pageToOffset,
393
+ offsetToPage,
394
+ MarvinCache,
395
+ init_core
396
+ };