balda-js 0.0.39 → 0.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs CHANGED
@@ -5,7 +5,7 @@ var pino = require('pino');
5
5
  var path = require('path');
6
6
  var Ajv = require('ajv');
7
7
  var addFormats = require('ajv-formats');
8
- var typebox = require('@sinclair/typebox');
8
+ var zod = require('zod');
9
9
  var fs = require('fs/promises');
10
10
  var http = require('http');
11
11
 
@@ -948,7 +948,9 @@ var ajv = addFormats__default.default(new Ajv__default.default(), [
948
948
  "iso-time"
949
949
  ]);
950
950
  var validateSchema = (inputSchema, data, safe = false) => {
951
- const validate2 = ajv.compile(inputSchema);
951
+ const jsonSchema = zod.z.toJSONSchema(inputSchema);
952
+ const { $schema, ...schemaWithoutMeta } = jsonSchema;
953
+ const validate2 = ajv.compile(schemaWithoutMeta);
952
954
  if (!validate2(data)) {
953
955
  if (safe) {
954
956
  return data;
@@ -984,16 +986,17 @@ var serialize = (schema, options) => {
984
986
  descriptor.value[SERIALIZE_METADATA] = {};
985
987
  }
986
988
  descriptor.value[SERIALIZE_METADATA][status] = {
989
+ name: propertyKey,
987
990
  schema,
988
991
  safe: options?.safe ?? true
989
992
  };
990
993
  if (!descriptor.value[SERIALIZE_WRAPPED]) {
991
994
  const originalMethod = descriptor.value;
992
- descriptor.value = async function(...args2) {
995
+ const wrappedFunction = async function(...args2) {
993
996
  const res = args2[1];
994
997
  await originalMethod.apply(this, args2);
995
998
  const actualStatus = res.responseStatus;
996
- const serializeMetadata = originalMethod[SERIALIZE_METADATA];
999
+ const serializeMetadata = wrappedFunction[SERIALIZE_METADATA];
997
1000
  const schema2 = serializeMetadata?.[actualStatus]?.schema;
998
1001
  const safe = serializeMetadata?.[actualStatus]?.safe ?? true;
999
1002
  if (schema2 && !safe) {
@@ -1013,8 +1016,9 @@ var serialize = (schema, options) => {
1013
1016
  }
1014
1017
  }
1015
1018
  };
1016
- descriptor.value[SERIALIZE_WRAPPED] = true;
1017
- descriptor.value[SERIALIZE_METADATA] = originalMethod[SERIALIZE_METADATA];
1019
+ wrappedFunction[SERIALIZE_WRAPPED] = true;
1020
+ wrappedFunction[SERIALIZE_METADATA] = originalMethod[SERIALIZE_METADATA];
1021
+ descriptor.value = wrappedFunction;
1018
1022
  }
1019
1023
  };
1020
1024
  };
@@ -2220,8 +2224,6 @@ var commandRegistry = CommandRegistry.getInstance();
2220
2224
  // src/runtime/native_request.ts
2221
2225
  var NativeRequest = class extends Request {
2222
2226
  };
2223
-
2224
- // src/server/http/request.ts
2225
2227
  var Request2 = class _Request extends NativeRequest {
2226
2228
  static fromRequest(request) {
2227
2229
  return new _Request(request.url, {
@@ -2236,19 +2238,19 @@ var Request2 = class _Request extends NativeRequest {
2236
2238
  static enrichRequest(request) {
2237
2239
  request.validate = (inputSchema, safe = false) => {
2238
2240
  if (typeof inputSchema === "function") {
2239
- inputSchema = inputSchema(typebox.Type);
2241
+ inputSchema = inputSchema(zod.z);
2240
2242
  }
2241
2243
  return validateSchema(inputSchema, request.body || {}, safe);
2242
2244
  };
2243
2245
  request.validateQuery = (inputSchema, safe = false) => {
2244
2246
  if (typeof inputSchema === "function") {
2245
- inputSchema = inputSchema(typebox.Type);
2247
+ inputSchema = inputSchema(zod.z);
2246
2248
  }
2247
2249
  return validateSchema(inputSchema, request.query || {}, safe);
2248
2250
  };
2249
2251
  request.validateAll = (inputSchema, safe = false) => {
2250
2252
  if (typeof inputSchema === "function") {
2251
- inputSchema = inputSchema(typebox.Type);
2253
+ inputSchema = inputSchema(zod.z);
2252
2254
  }
2253
2255
  return validateSchema(
2254
2256
  inputSchema,
@@ -2355,7 +2357,7 @@ var Request2 = class _Request extends NativeRequest {
2355
2357
  */
2356
2358
  validate(inputSchema, safe = false) {
2357
2359
  if (typeof inputSchema === "function") {
2358
- inputSchema = inputSchema(typebox.Type);
2360
+ inputSchema = inputSchema(zod.z);
2359
2361
  }
2360
2362
  return validateSchema(inputSchema, this.body || {}, safe);
2361
2363
  }
@@ -2364,7 +2366,7 @@ var Request2 = class _Request extends NativeRequest {
2364
2366
  */
2365
2367
  validateQuery(inputSchema, safe = false) {
2366
2368
  if (typeof inputSchema === "function") {
2367
- inputSchema = inputSchema(typebox.Type);
2369
+ inputSchema = inputSchema(zod.z);
2368
2370
  }
2369
2371
  return validateSchema(inputSchema, this.query || {}, safe);
2370
2372
  }
@@ -2373,7 +2375,7 @@ var Request2 = class _Request extends NativeRequest {
2373
2375
  */
2374
2376
  validateAll(inputSchema, safe = false) {
2375
2377
  if (typeof inputSchema === "function") {
2376
- inputSchema = inputSchema(typebox.Type);
2378
+ inputSchema = inputSchema(zod.z);
2377
2379
  }
2378
2380
  return validateSchema(
2379
2381
  inputSchema,
@@ -4134,8 +4136,6 @@ function parseMimeType(contentType) {
4134
4136
  }
4135
4137
  return trimmed.substring(0, semicolonIndex).trim().toLowerCase();
4136
4138
  }
4137
-
4138
- // src/plugins/swagger/swagger.ts
4139
4139
  var swagger = (globalOptions) => {
4140
4140
  let swaggerOptions = {
4141
4141
  type: "standard",
@@ -4167,9 +4167,54 @@ var swagger = (globalOptions) => {
4167
4167
  res.json(spec);
4168
4168
  });
4169
4169
  };
4170
+ function safeToJSONSchema(schema) {
4171
+ try {
4172
+ return zod.z.toJSONSchema(schema);
4173
+ } catch (error) {
4174
+ if (error instanceof Error && error.message.includes(
4175
+ "Custom types cannot be represented in JSON Schema"
4176
+ )) {
4177
+ const def = schema._def;
4178
+ if (def?.typeName === "ZodInstanceof") {
4179
+ const testFile = new File([], "test");
4180
+ if (schema.safeParse(testFile).success) {
4181
+ return { type: "string", format: "binary" };
4182
+ }
4183
+ }
4184
+ if (def?.typeName === "ZodObject" && def?.shape) {
4185
+ const properties = {};
4186
+ const required = [];
4187
+ for (const [key, fieldSchema] of Object.entries(def.shape)) {
4188
+ try {
4189
+ properties[key] = safeToJSONSchema(fieldSchema);
4190
+ const fieldDef = fieldSchema._def;
4191
+ if (fieldDef?.typeName !== "ZodOptional" && fieldDef?.typeName !== "ZodDefault") {
4192
+ required.push(key);
4193
+ }
4194
+ } catch {
4195
+ properties[key] = { type: "string", format: "binary" };
4196
+ }
4197
+ }
4198
+ return {
4199
+ type: "object",
4200
+ properties,
4201
+ ...required.length > 0 ? { required } : {}
4202
+ };
4203
+ }
4204
+ return { type: "object", description: "Custom type" };
4205
+ }
4206
+ throw error;
4207
+ }
4208
+ }
4170
4209
  function generateOpenAPISpec(globalOptions) {
4171
4210
  const routes = router.getRoutes();
4172
4211
  const paths = {};
4212
+ if (Array.isArray(globalOptions.models)) {
4213
+ globalOptions.models = globalOptions.models.reduce((acc, model) => {
4214
+ acc[model.$id || "name"] = model;
4215
+ return acc;
4216
+ }, {});
4217
+ }
4173
4218
  const components = {
4174
4219
  ...globalOptions.components,
4175
4220
  securitySchemes: globalOptions.securitySchemes || {},
@@ -4191,15 +4236,17 @@ function generateOpenAPISpec(globalOptions) {
4191
4236
  };
4192
4237
  let parameters = [];
4193
4238
  if (swaggerOptions?.query) {
4194
- if (swaggerOptions.query.type === "object" && swaggerOptions.query.properties) {
4239
+ if (swaggerOptions.query.type === "object" && swaggerOptions.query.shape) {
4195
4240
  for (const [name, schema] of Object.entries(
4196
- swaggerOptions.query.properties
4241
+ swaggerOptions.query.shape
4197
4242
  )) {
4198
4243
  parameters.push({
4199
4244
  name,
4200
4245
  in: "query",
4201
- required: Array.isArray(swaggerOptions.query.required) ? swaggerOptions.query.required.includes(name) : false,
4202
- schema: typeboxToOpenAPI(schema)
4246
+ required: Array.isArray(
4247
+ swaggerOptions.query.shape[name].required
4248
+ ) ? swaggerOptions.query.shape[name].required.includes(name) : false,
4249
+ schema: safeToJSONSchema(schema)
4203
4250
  });
4204
4251
  }
4205
4252
  }
@@ -4224,7 +4271,7 @@ function generateOpenAPISpec(globalOptions) {
4224
4271
  operation.requestBody = {
4225
4272
  content: {
4226
4273
  [routeBodyContentType]: {
4227
- schema: typeboxToOpenAPI(swaggerOptions.requestBody)
4274
+ schema: safeToJSONSchema(swaggerOptions.requestBody)
4228
4275
  }
4229
4276
  },
4230
4277
  required: true
@@ -4248,7 +4295,7 @@ function generateOpenAPISpec(globalOptions) {
4248
4295
  description: `Response for ${statusCode}`,
4249
4296
  content: {
4250
4297
  "application/json": {
4251
- schema: typeboxToOpenAPI(schema)
4298
+ schema: safeToJSONSchema(schema)
4252
4299
  }
4253
4300
  }
4254
4301
  };
@@ -4262,7 +4309,7 @@ function generateOpenAPISpec(globalOptions) {
4262
4309
  description: `Error response for ${statusCode}`,
4263
4310
  content: {
4264
4311
  "application/json": {
4265
- schema: typeboxToOpenAPI(schema)
4312
+ schema: safeToJSONSchema(schema)
4266
4313
  }
4267
4314
  }
4268
4315
  };
@@ -4456,13 +4503,6 @@ function generateRapiDocUI(specUrl, globalOptions) {
4456
4503
  </html>
4457
4504
  `;
4458
4505
  }
4459
- function typeboxToOpenAPI(schema) {
4460
- if (!schema) {
4461
- return void 0;
4462
- }
4463
- const { $id, $schema, ...rest } = schema;
4464
- return rest;
4465
- }
4466
4506
  function extractPathParams(path2, paramSchema) {
4467
4507
  const params = [];
4468
4508
  const regex = /:([a-zA-Z0-9_]+)/g;
@@ -4470,8 +4510,10 @@ function extractPathParams(path2, paramSchema) {
4470
4510
  while ((match = regex.exec(path2)) !== null) {
4471
4511
  const name = match[1];
4472
4512
  let schema = { type: "string" };
4473
- if (paramSchema && paramSchema.type === "object" && paramSchema.properties && paramSchema.properties[name]) {
4474
- schema = typeboxToOpenAPI(paramSchema.properties[name]) || {
4513
+ if (paramSchema && paramSchema.shape && paramSchema.shape[name]) {
4514
+ schema = safeToJSONSchema(
4515
+ paramSchema.shape[name]
4516
+ ) || {
4475
4517
  type: "string"
4476
4518
  };
4477
4519
  }