@c15t/backend 0.0.1-rc.15 → 0.0.1-rc.17

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.
@@ -1,23 +1,24 @@
1
1
 
2
2
  
3
- > @c15t/backend@0.0.1-rc.15 build /Users/christopherburns/glados/k/c15t/packages/backend
3
+ > @c15t/backend@0.0.1-rc.17 build /Users/christopherburns/glados/k/c15t/packages/backend
4
4
  > rslib build
5
5
 
6
6
    Rslib v0.5.3
7
7
  
8
- ready  built in 0.34 s (esm)
9
- ready  built in 0.34 s (cjs)
8
+ ready  built in 0.36 s (esm)
9
+ ready  built in 0.36 s (cjs)
10
10
  start  Generating DTS... (esm)
11
11
  start  Generating DTS... (cjs)
12
- ready  DTS generated in 3.23 s (esm)
13
- ready  DTS generated in 3.19 s (cjs)
12
+ ready  DTS generated in 3.61 s (esm)
13
+ ready  DTS generated in 3.57 s (cjs)
14
14
 
15
15
   File (esm) Size 
16
16
  dist/pkgs/types/index.js 0.00 kB
17
17
  dist/pkgs/data-model/fields/index.js 5.6 kB
18
18
  dist/pkgs/logger/index.js 7.2 kB
19
- dist/integrations/index.js 9.3 kB
20
- dist/integrations/next.js 9.3 kB
19
+ dist/integrations/node.js 9.1 kB
20
+ dist/integrations/next.js 9.6 kB
21
+ dist/integrations/cloudflare.js 11.2 kB
21
22
  dist/pkgs/results/index.js 22.2 kB
22
23
  dist/pkgs/db-adapters/adapters/memory-adapter/index.js 49.4 kB
23
24
  dist/pkgs/db-adapters/adapters/prisma-adapter/index.js 52.6 kB
@@ -28,9 +29,9 @@
28
29
  dist/schema/index.js 74.9 kB
29
30
  dist/pkgs/data-model/index.js 75.6 kB
30
31
  dist/pkgs/db-adapters/index.js 94.3 kB
31
- dist/index.js 154.5 kB
32
+ dist/index.js 154.6 kB
32
33
 
33
- Total: 800.7 kB
34
+ Total: 812.0 kB
34
35
 
35
36
   -----
36
37
 
@@ -38,8 +39,9 @@
38
39
  dist/pkgs/types/index.cjs 0.72 kB
39
40
  dist/pkgs/data-model/fields/index.cjs 7.0 kB
40
41
  dist/pkgs/logger/index.cjs 8.1 kB
41
- dist/integrations/index.cjs 10.4 kB
42
- dist/integrations/next.cjs 10.4 kB
42
+ dist/integrations/node.cjs 10.2 kB
43
+ dist/integrations/next.cjs 10.7 kB
44
+ dist/integrations/cloudflare.cjs 12.3 kB
43
45
  dist/pkgs/results/index.cjs 23.1 kB
44
46
  dist/pkgs/db-adapters/adapters/memory-adapter/index.cjs 50.1 kB
45
47
  dist/pkgs/db-adapters/adapters/prisma-adapter/index.cjs 53.3 kB
@@ -50,7 +52,7 @@
50
52
  dist/schema/index.cjs 82.6 kB
51
53
  dist/pkgs/data-model/index.cjs 83.5 kB
52
54
  dist/pkgs/db-adapters/index.cjs 95.0 kB
53
- dist/index.cjs 168.2 kB
55
+ dist/index.cjs 168.3 kB
54
56
 
55
- Total: 848.0 kB
57
+ Total: 860.4 kB
56
58
 
@@ -1,7 +1,7 @@
1
1
 
2
2
  
3
- > @c15t/backend@0.0.1-rc.14 fmt /Users/christopherburns/glados/k/c15t/packages/backend
3
+ > @c15t/backend@0.0.1-rc.15 fmt /Users/christopherburns/glados/k/c15t/packages/backend
4
4
  > pnpm biome format --write . && biome check --formatter-enabled=false --linter-enabled=false --organize-imports-enabled=true --write
5
5
 
6
- Formatted 166 files in 60ms. No fixes applied.
7
- Checked 358 files in 84ms. Fixed 27 files.
6
+ Formatted 167 files in 42ms. Fixed 2 files.
7
+ Checked 362 files in 49ms. Fixed 28 files.
package/dist/index.cjs CHANGED
@@ -3300,13 +3300,16 @@ var __webpack_exports__ = {};
3300
3300
  message
3301
3301
  };
3302
3302
  }
3303
+ var package_namespaceObject = {
3304
+ i8: "0.0.1-rc.17"
3305
+ };
3303
3306
  const status_status = defineRoute({
3304
3307
  path: '/status',
3305
3308
  method: 'get',
3306
3309
  handler: async (event)=>{
3307
3310
  const response = {
3308
3311
  status: 'ok',
3309
- version: '2.0.0',
3312
+ version: package_namespaceObject.i8,
3310
3313
  timestamp: new Date().toISOString(),
3311
3314
  storage: {
3312
3315
  type: event.context.adapter?.id ?? 'Unavailable',
package/dist/index.js CHANGED
@@ -3270,13 +3270,16 @@ function checkJurisdiction(countryCode) {
3270
3270
  message
3271
3271
  };
3272
3272
  }
3273
+ var package_namespaceObject = {
3274
+ i8: "0.0.1-rc.17"
3275
+ };
3273
3276
  const status_status = defineRoute({
3274
3277
  path: '/status',
3275
3278
  method: 'get',
3276
3279
  handler: async (event)=>{
3277
3280
  const response = {
3278
3281
  status: 'ok',
3279
- version: '2.0.0',
3282
+ version: package_namespaceObject.i8,
3280
3283
  timestamp: new Date().toISOString(),
3281
3284
  storage: {
3282
3285
  type: event.context.adapter?.id ?? 'Unavailable',
@@ -0,0 +1,291 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = function(exports1) {
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ toCloudflareHandler: ()=>toCloudflareHandler
28
+ });
29
+ require("neverthrow");
30
+ const external_h3_namespaceObject = require("h3");
31
+ const error_codes_ERROR_CODES = Object.freeze({
32
+ NOT_FOUND: 'Resource not found',
33
+ BAD_REQUEST: 'Bad request',
34
+ CONFLICT: 'Conflict with current state',
35
+ MISSING_REQUIRED_PARAMETER: 'Missing required parameter',
36
+ UNAUTHORIZED: 'Unauthorized',
37
+ FORBIDDEN: 'Forbidden',
38
+ INTERNAL_SERVER_ERROR: 'Internal server error',
39
+ INITIALIZATION_FAILED: 'Initialization failed',
40
+ DATABASE_CONNECTION_ERROR: 'Database connection error',
41
+ DATABASE_QUERY_ERROR: 'Database query error',
42
+ INVALID_CONFIGURATION: 'Invalid configuration',
43
+ REQUEST_HANDLER_ERROR: 'Request handler error',
44
+ INVALID_REQUEST: 'Invalid request',
45
+ UNKNOWN_ERROR: 'Unknown error',
46
+ NETWORK_ERROR: 'Network error',
47
+ PLUGIN_INITIALIZATION_FAILED: 'Plugin initialization failed',
48
+ API_RETRIEVAL_ERROR: 'API retrieval error',
49
+ VALIDATION_ERROR: 'Validation error',
50
+ UNEXPECTED: 'Unexpected error'
51
+ });
52
+ const ERROR_CATEGORIES = Object.freeze({
53
+ VALIDATION: 'validation',
54
+ AUTHORIZATION: 'authorization',
55
+ STORAGE: 'storage',
56
+ NETWORK: 'network',
57
+ PLUGIN: 'plugin',
58
+ CONFIGURATION: 'configuration',
59
+ UNEXPECTED: 'unexpected'
60
+ });
61
+ const api_namespaceObject = require("@opentelemetry/api");
62
+ api_namespaceObject.trace.getTracer('@doubletie/results');
63
+ class error_class_DoubleTieError extends external_h3_namespaceObject.H3Error {
64
+ code;
65
+ category;
66
+ meta;
67
+ constructor(message, options = {
68
+ code: error_codes_ERROR_CODES.UNKNOWN_ERROR,
69
+ status: 500,
70
+ category: ERROR_CATEGORIES.UNEXPECTED,
71
+ cause: void 0,
72
+ meta: {}
73
+ }){
74
+ super(message, {
75
+ cause: options.cause
76
+ });
77
+ this.name = this.constructor.name;
78
+ this.code = options.code ?? error_codes_ERROR_CODES.UNKNOWN_ERROR;
79
+ this.statusCode = options.status ?? 500;
80
+ this.category = options.category ?? ERROR_CATEGORIES.UNEXPECTED;
81
+ this.meta = options.meta ?? {};
82
+ this.data = {
83
+ code: this.code,
84
+ category: this.category,
85
+ meta: this.meta
86
+ };
87
+ tracing_withSpan('create_doubletie_error', async (span)=>{
88
+ span.setAttributes({
89
+ 'error.name': this.name,
90
+ 'error.message': message,
91
+ 'error.code': this.code,
92
+ 'error.status': this.statusCode,
93
+ 'error.category': this.category,
94
+ 'error.has_cause': !!this.cause,
95
+ 'error.cause_type': this.cause instanceof Error ? this.cause.constructor.name : typeof this.cause,
96
+ 'error.has_meta': !!this.meta
97
+ });
98
+ if (this.cause instanceof Error) span.recordException(this.cause);
99
+ });
100
+ if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor);
101
+ }
102
+ static isDoubleTieError(error) {
103
+ return error instanceof error_class_DoubleTieError;
104
+ }
105
+ toJSON() {
106
+ const validationErrorMessage = this.meta?.validationErrors ? String(this.meta.validationErrors) : void 0;
107
+ const stackTrace = this.stack ? this.stack.split('\n').map((line)=>line.trim()).filter((line)=>line && !line.includes('Error: ')) : [];
108
+ return {
109
+ statusCode: this.statusCode,
110
+ message: validationErrorMessage || this.message,
111
+ statusMessage: this.statusMessage,
112
+ data: {
113
+ code: this.code,
114
+ category: this.category,
115
+ meta: this.meta,
116
+ ...'production' === process.env.NODE_ENV ? {} : {
117
+ stack: stackTrace
118
+ },
119
+ ...validationErrorMessage && this.message ? {
120
+ originalMessage: this.message
121
+ } : {},
122
+ ...this.cause ? {
123
+ cause: this.cause instanceof Error ? {
124
+ name: this.cause.name,
125
+ message: this.cause.message,
126
+ stack: this.cause.stack ? this.cause.stack.split('\n').map((line)=>line.trim()) : void 0
127
+ } : this.cause
128
+ } : {}
129
+ }
130
+ };
131
+ }
132
+ static fromResponse(response, data) {
133
+ let message = `HTTP error ${response.status}`;
134
+ let errorCode = `HTTP ${response.status}`;
135
+ let errorMeta = {};
136
+ if (data && 'object' == typeof data && null !== data) {
137
+ const errorObj = data;
138
+ if ('string' == typeof errorObj.message) message = errorObj.message;
139
+ if ('string' == typeof errorObj.code) errorCode = errorObj.code;
140
+ if ('object' == typeof errorObj.data && null !== errorObj.data) errorMeta = errorObj.data;
141
+ }
142
+ return new error_class_DoubleTieError(message, {
143
+ code: errorCode,
144
+ status: response.status,
145
+ meta: errorMeta
146
+ });
147
+ }
148
+ withMeta(additionalMeta) {
149
+ return new error_class_DoubleTieError(this.message, {
150
+ code: this.code,
151
+ status: this.statusCode,
152
+ category: this.category,
153
+ cause: this.cause instanceof Error ? this.cause : void 0,
154
+ meta: {
155
+ ...this.meta,
156
+ ...additionalMeta
157
+ }
158
+ });
159
+ }
160
+ static createSubclass(name) {
161
+ const ErrorSubclass = class extends error_class_DoubleTieError {
162
+ constructor(message, options){
163
+ super(message, options);
164
+ this.name = name;
165
+ }
166
+ };
167
+ Object.defineProperty(ErrorSubclass, 'name', {
168
+ value: name
169
+ });
170
+ return ErrorSubclass;
171
+ }
172
+ static formatValidationError(error) {
173
+ if (!error.meta) return error.message;
174
+ let formattedMessage = `${error.message} (${error.code})`;
175
+ if (error.meta.validationErrors) formattedMessage += `\nValidation Errors: ${JSON.stringify(error.meta.validationErrors, null, 2)}`;
176
+ const otherMeta = Object.fromEntries(Object.entries(error.meta).filter(([key])=>'validationErrors' !== key));
177
+ if (Object.keys(otherMeta).length > 0) formattedMessage += `\nAdditional Context: ${JSON.stringify(otherMeta, null, 2)}`;
178
+ return formattedMessage;
179
+ }
180
+ }
181
+ function toCloudflareHandler(instance) {
182
+ return async (request)=>{
183
+ try {
184
+ const basePath = instance.options?.basePath || '/api/c15t';
185
+ const originalUrl = new URL(request.url);
186
+ let pathWithoutBase = originalUrl.pathname;
187
+ if ('OPTIONS' === request.method) return handleCorsPreflightRequest(request, instance);
188
+ if (pathWithoutBase.startsWith(basePath)) {
189
+ pathWithoutBase = pathWithoutBase.substring(basePath.length);
190
+ if (!pathWithoutBase.startsWith('/')) pathWithoutBase = `/${pathWithoutBase}`;
191
+ }
192
+ const rewrittenUrl = new URL(originalUrl.toString());
193
+ rewrittenUrl.pathname = pathWithoutBase;
194
+ const headers = new Headers();
195
+ for (const [key, value] of request.headers.entries())headers.set(key, value);
196
+ const rewrittenRequest = new Request(rewrittenUrl.toString(), {
197
+ method: request.method,
198
+ headers,
199
+ body: [
200
+ 'GET',
201
+ 'HEAD'
202
+ ].includes(request.method) ? void 0 : request.body
203
+ });
204
+ await updateBaseUrl(request, basePath);
205
+ const result = await instance.handler(rewrittenRequest);
206
+ return await result.match((response)=>addCorsHeaders(response, request, instance), (error)=>{
207
+ const status = error.statusCode || 500;
208
+ const message = error.message || error_codes_ERROR_CODES.INTERNAL_SERVER_ERROR;
209
+ const errorResponse = new Response(JSON.stringify({
210
+ error: true,
211
+ code: error.code || error_codes_ERROR_CODES.INTERNAL_SERVER_ERROR,
212
+ message,
213
+ meta: error.meta
214
+ }), {
215
+ status,
216
+ headers: {
217
+ 'Content-Type': 'application/json'
218
+ }
219
+ });
220
+ return addCorsHeaders(errorResponse, request, instance);
221
+ });
222
+ } catch (error) {
223
+ const errorResponse = new Response(JSON.stringify({
224
+ error: true,
225
+ code: error_codes_ERROR_CODES.INTERNAL_SERVER_ERROR,
226
+ message: 'An unexpected error occurred',
227
+ meta: {
228
+ error: String(error)
229
+ }
230
+ }), {
231
+ status: 500,
232
+ headers: {
233
+ 'Content-Type': 'application/json'
234
+ }
235
+ });
236
+ return addCorsHeaders(errorResponse, request, instance);
237
+ }
238
+ };
239
+ function handleCorsPreflightRequest(request, instance) {
240
+ const origin = request.headers.get('Origin');
241
+ const isTrusted = isTrustedOrigin(origin, instance);
242
+ const headers = new Headers({
243
+ 'Access-Control-Allow-Origin': isTrusted && origin ? origin : '*',
244
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
245
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization, x-request-id',
246
+ 'Access-Control-Max-Age': '86400'
247
+ });
248
+ if (isTrusted) headers.set('Access-Control-Allow-Credentials', 'true');
249
+ return new Response(null, {
250
+ status: 204,
251
+ headers
252
+ });
253
+ }
254
+ function addCorsHeaders(response, request, instance) {
255
+ const origin = request.headers.get('Origin');
256
+ const isTrusted = isTrustedOrigin(origin, instance);
257
+ const newResponse = new Response(response.body, {
258
+ status: response.status,
259
+ statusText: response.statusText,
260
+ headers: response.headers
261
+ });
262
+ newResponse.headers.set('Access-Control-Allow-Origin', isTrusted && origin ? origin : '*');
263
+ if (isTrusted) newResponse.headers.set('Access-Control-Allow-Credentials', 'true');
264
+ return newResponse;
265
+ }
266
+ function isTrustedOrigin(origin, instance) {
267
+ if (!origin) return false;
268
+ const { trustedOrigins = [] } = instance.options || {};
269
+ if (trustedOrigins.includes('*')) return true;
270
+ return trustedOrigins.includes(origin);
271
+ }
272
+ async function updateBaseUrl(request, basePath) {
273
+ if (!instance.$context) return;
274
+ try {
275
+ const contextResult = await instance.$context;
276
+ contextResult.match((context)=>{
277
+ const url = new URL(request.url);
278
+ const baseURL = `${url.protocol}//${url.host}${basePath}`;
279
+ if (!context.baseURL || context.baseURL !== baseURL) {
280
+ context.baseURL = baseURL;
281
+ if (context.options) context.options.baseURL = baseURL;
282
+ }
283
+ }, ()=>{});
284
+ } catch {}
285
+ }
286
+ }
287
+ var __webpack_export_target__ = exports;
288
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
289
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
290
+ value: true
291
+ });
@@ -0,0 +1,32 @@
1
+ import type { C15TInstance } from '../core';
2
+ /**
3
+ * Convert a c15t handler to a Cloudflare Worker handler.
4
+ *
5
+ * This adapter is specifically designed for Cloudflare Workers environments,
6
+ * avoiding Node.js-specific dependencies and APIs.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { toCloudflareHandler } from '@c15t/backend/integrations';
11
+ * import { c15tInstance } from '@c15t/backend';
12
+ *
13
+ * // Create c15t instance
14
+ * const c15t = c15tInstance({
15
+ * basePath: '/',
16
+ * cors: true,
17
+ * // other options...
18
+ * });
19
+ *
20
+ * // Export the worker handler
21
+ * export default {
22
+ * async fetch(request, env, ctx) {
23
+ * return await toCloudflareHandler(c15t)(request);
24
+ * }
25
+ * };
26
+ * ```
27
+ *
28
+ * @param instance - The c15t instance to adapt
29
+ * @returns A handler function suitable for Cloudflare Workers
30
+ */
31
+ export declare function toCloudflareHandler(instance: C15TInstance): (request: Request) => Promise<Response>;
32
+ //# sourceMappingURL=cloudflare.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare.d.ts","sourceRoot":"","sources":["../../src/integrations/cloudflare.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAI3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,YAAY,IAC3C,SAAS,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC,CAiNlD"}