@elysiajs/openapi 1.4.14 → 2.0.0-exp.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.
@@ -1,6 +1,12 @@
1
- import { type AnyElysia, type TSchema, type InputSchema } from 'elysia';
1
+ import type { AnyElysia } from 'elysia/base';
2
+ import type { InputSchema } from 'elysia/types';
2
3
  import type { OpenAPIV3 } from 'openapi-types';
3
- import { TAnySchema, type TProperties } from '@sinclair/typebox';
4
+ type TSchema = OpenAPIV3.SchemaObject & {
5
+ $ref?: string;
6
+ $schema?: string;
7
+ const?: unknown;
8
+ [key: string]: any;
9
+ };
4
10
  import type { AdditionalReferences, ElysiaOpenAPIConfig, MapJsonSchema } from './types';
5
11
  export declare const capitalize: (word: string) => string;
6
12
  /**
@@ -16,7 +22,7 @@ export declare const unwrapSchema: (schema: InputSchema["body"], mapJsonSchema?:
16
22
  *
17
23
  * Otherwise, return the schema as is
18
24
  */
19
- export declare const enumToOpenApi: <T extends TAnySchema | OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined>(_schema: T) => T;
25
+ export declare const enumToOpenApi: <T extends TSchema | OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined>(_schema: T) => T;
20
26
  /**
21
27
  * Converts Elysia routes to OpenAPI 3.0.3 paths schema
22
28
  * @param routes Array of Elysia route objects
@@ -28,6 +34,7 @@ export declare function toOpenAPISchema(app: AnyElysia, exclude?: ElysiaOpenAPIC
28
34
  };
29
35
  paths: OpenAPIV3.PathsObject<{}, {}>;
30
36
  };
31
- export declare const withHeaders: (schema: TSchema, headers: TProperties) => TSchema & {
32
- headers: TProperties;
37
+ export declare const withHeaders: <const T, const H>(schema: T, headers: H) => T & {
38
+ headers: H;
33
39
  };
40
+ export {};
@@ -30,15 +30,8 @@ __export(openapi_exports, {
30
30
  });
31
31
  module.exports = __toCommonJS(openapi_exports);
32
32
  var import_elysia = require("elysia");
33
-
34
- // node_modules/@sinclair/typebox/build/esm/type/symbols/symbols.mjs
35
- var Kind = /* @__PURE__ */ Symbol.for("TypeBox.Kind");
36
-
37
- // src/openapi.ts
38
33
  var capitalize = (word) => word.charAt(0).toUpperCase() + word.slice(1);
39
- var toRef = (name) => import_elysia.t.Ref(
40
- name.startsWith("#/") ? name : `#/components/schemas/${name}`
41
- );
34
+ var toRef = (name) => import_elysia.t.Ref(name.startsWith("#/") ? name : `#/components/schemas/${name}`);
42
35
  var toOperationId = (method, paths) => {
43
36
  let operationId = method.toLowerCase();
44
37
  if (!paths || paths === "/") return operationId + "Index";
@@ -59,12 +52,8 @@ var getPossiblePath = (path) => {
59
52
  }
60
53
  return paths;
61
54
  };
62
- var isValidSchema = (schema) => schema && typeof schema === "object" && (Kind in schema && schema[Kind] !== "Unknown" || schema.type || schema.properties || schema.items);
63
- var getLoosePath = (path) => {
64
- if (path.charCodeAt(path.length - 1) === 47)
65
- return path.slice(0, path.length - 1);
66
- return path + "/";
67
- };
55
+ var isValidSchema = (schema) => schema && typeof schema === "object" && (schema.type || schema.properties || schema.items || schema.anyOf || schema.allOf || schema.oneOf || schema.$ref || schema.enum || schema.const !== void 0);
56
+ var getLoosePath = (path) => path.charCodeAt(path.length - 1) === 47 ? path.slice(0, path.length - 1) : path + "/";
68
57
  var warnings = {
69
58
  zod4: `import openapi from '@elysiajs/openapi'
70
59
  import * as z from 'zod'
@@ -160,12 +149,10 @@ var mergeObjectSchemas = (schemas) => {
160
149
  };
161
150
  var isTSchema = (value) => {
162
151
  if (!value || typeof value !== "object") return false;
163
- if (Kind in value) return true;
152
+ if (value["~standard"]) return false;
164
153
  const keys = Object.keys(value);
165
- if (keys.length > 0 && keys.every((k) => !isNaN(Number(k)))) {
166
- return false;
167
- }
168
- return false;
154
+ if (keys.length > 0 && keys.every((k) => !isNaN(Number(k)))) return false;
155
+ return isValidSchema(value);
169
156
  };
170
157
  var normalizeSchemaReference = (schema) => {
171
158
  if (!schema) return void 0;
@@ -243,8 +230,8 @@ var mergeResponseSchema = (_existing, _incoming, vendors) => {
243
230
  };
244
231
  var mergeStandaloneValidators = (hooks, vendors) => {
245
232
  const merged = { ...hooks };
246
- if (!hooks.standaloneValidator?.length) return merged;
247
- for (const validator of hooks.standaloneValidator) {
233
+ if (!hooks.schemas?.length) return merged;
234
+ for (const validator of hooks.schemas) {
248
235
  if (validator.body)
249
236
  merged.body = mergeSchemaProperty(
250
237
  merged.body,
@@ -297,16 +284,15 @@ var mergeStandaloneValidators = (hooks, vendors) => {
297
284
  if ("type" in response || "$ref" in response) {
298
285
  if (typeof response === "string")
299
286
  merged.response = normalizeSchemaReference(response);
300
- } else {
287
+ } else
301
288
  for (const [status, schema] of Object.entries(response))
302
289
  if (typeof schema === "string")
303
290
  response[status] = normalizeSchemaReference(schema);
304
- }
305
291
  }
306
292
  return merged;
307
293
  };
308
294
  var flattenRoutes = (routes, vendors) => routes.map((route) => {
309
- if (!route.hooks?.standaloneValidator?.length) return route;
295
+ if (!route.hooks?.schemas?.length) return route;
310
296
  return {
311
297
  ...route,
312
298
  hooks: mergeStandaloneValidators(route.hooks, vendors)
@@ -322,19 +308,19 @@ var unwrapReference = (schema, definitions) => {
322
308
  var unwrapSchema = (schema, mapJsonSchema, io = "input") => {
323
309
  if (!schema) return;
324
310
  if (typeof schema === "string") schema = toRef(schema);
325
- if (Kind in schema) return enumToOpenApi(schema);
326
- if (!schema?.["~standard"] && // @ts-ignore
327
- (schema.$schema || schema.type || schema.properties || schema.items))
328
- return schema;
311
+ if (!schema?.["~standard"] && isValidSchema(schema))
312
+ return enumToOpenApi(schema);
329
313
  if (!schema?.["~standard"]) return;
330
314
  const vendor = schema["~standard"].vendor;
331
315
  try {
332
316
  if (mapJsonSchema?.[vendor] && typeof mapJsonSchema[vendor] === "function")
333
317
  return enumToOpenApi(mapJsonSchema[vendor](schema));
334
318
  if (schema["~standard"]?.jsonSchema?.[io])
335
- return enumToOpenApi(schema["~standard"].jsonSchema[io]({
336
- target: "draft-2020-12"
337
- }));
319
+ return enumToOpenApi(
320
+ schema["~standard"].jsonSchema[io]({
321
+ target: "draft-2020-12"
322
+ })
323
+ );
338
324
  switch (vendor) {
339
325
  case "zod":
340
326
  if (warned.zod4 || warned.zod3) break;
@@ -390,9 +376,9 @@ var unwrapSchema = (schema, mapJsonSchema, io = "input") => {
390
376
  };
391
377
  var enumToOpenApi = (_schema) => {
392
378
  if (!_schema || typeof _schema !== "object") return _schema;
393
- if (Kind in _schema) {
379
+ {
394
380
  const schema2 = _schema;
395
- if (schema2[Kind] === "Union" && schema2.anyOf && Array.isArray(schema2.anyOf) && schema2.anyOf.length > 0 && schema2.anyOf.every(
381
+ if (schema2.anyOf && Array.isArray(schema2.anyOf) && schema2.anyOf.length > 0 && schema2.anyOf.every(
396
382
  (item) => item && typeof item === "object" && item.const !== void 0
397
383
  ))
398
384
  return {
@@ -426,7 +412,7 @@ function toOpenAPISchema(app, exclude, references, vendors) {
426
412
  excludeMethods = excludeMethods.map((method) => method.toLowerCase());
427
413
  const excludePaths = Array.isArray(exclude?.paths) ? exclude.paths : typeof exclude?.paths !== "undefined" ? [exclude.paths] : [];
428
414
  const paths = /* @__PURE__ */ Object.create(null);
429
- const definitions = app.getGlobalDefinitions?.().type;
415
+ const definitions = app.models;
430
416
  if (references) {
431
417
  if (!Array.isArray(references)) references = [references];
432
418
  for (let i = 0; i < references.length; i++) {
@@ -434,7 +420,7 @@ function toOpenAPISchema(app, exclude, references, vendors) {
434
420
  if (typeof reference === "function") references[i] = reference();
435
421
  }
436
422
  }
437
- const routes = flattenRoutes(app.getGlobalRoutes(), vendors);
423
+ const routes = flattenRoutes(app.routes, vendors);
438
424
  for (const route of routes) {
439
425
  if (route.hooks?.detail?.hide) continue;
440
426
  const method = route.method.toLowerCase();
@@ -476,7 +462,7 @@ function toOpenAPISchema(app, exclude, references, vendors) {
476
462
  }
477
463
  }
478
464
  }
479
- if (excludeTags && hooks.detail.tags?.some((tag) => excludeTags?.includes(tag)))
465
+ if (excludeTags && hooks.detail?.tags?.some((tag) => excludeTags?.includes(tag)))
480
466
  continue;
481
467
  const operation = {
482
468
  ...hooks.detail
@@ -673,7 +659,7 @@ function toOpenAPISchema(app, exclude, references, vendors) {
673
659
  path = path.replace(/:([^/]+)/g, "{$1}");
674
660
  if (!paths[path]) paths[path] = {};
675
661
  const current = paths[path];
676
- if (method !== "all") {
662
+ if (method !== "all" && method !== "*") {
677
663
  current[method] = {
678
664
  ...operation,
679
665
  operationId
@@ -700,7 +686,11 @@ function toOpenAPISchema(app, exclude, references, vendors) {
700
686
  if (definitions)
701
687
  for (const [name, schema] of Object.entries(definitions)) {
702
688
  const jsonSchema = unwrapSchema(schema, vendors);
703
- if (jsonSchema) schemas[name] = jsonSchema;
689
+ if (jsonSchema)
690
+ schemas[name] = {
691
+ ...jsonSchema,
692
+ $id: `#/components/schemas/${name}`
693
+ };
704
694
  }
705
695
  return {
706
696
  components: {
@@ -24,29 +24,30 @@ __export(scalar_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(scalar_exports);
26
26
  var elysiaCSS = `.light-mode {
27
- --scalar-color-1: #2a2f45;
28
- --scalar-color-2: #757575;
29
- --scalar-color-3: #8e8e8e;
30
- --scalar-color-accent: #f06292;
27
+ --scalar-color-1: oklch(43.5% 0.029 321.78);
28
+ --scalar-color-2: oklch(54.2% 0.034 322.5);
29
+ --scalar-color-3: oklch(71.1% 0.019 323.02);
30
+ --scalar-color-accent: oklch(71.2% 0.194 13.428);
31
31
 
32
- --scalar-background-1: #fff;
33
- --scalar-background-2: #f6f6f6;
34
- --scalar-background-3: #e7e7e7;
32
+ --scalar-background-1: oklch(98.5% 0 0);
33
+ --scalar-background-2: oklch(96% 0.003 325.6);
34
+ --scalar-background-3: oklch(92.2% 0.005 325.62);
35
35
 
36
- --scalar-border-color: rgba(0, 0, 0, 0.1);
36
+ --scalar-border-color: oklch(92.2% 0.005 325.62);
37
37
  }
38
+
38
39
  .dark-mode {
39
- --scalar-color-1: rgba(255, 255, 255, 0.9);
40
- --scalar-color-2: rgba(156, 163, 175, 1);
41
- --scalar-color-3: rgba(255, 255, 255, 0.44);
42
- --scalar-color-accent: #f06292;
40
+ --scalar-color-1: oklch(98.5% 0 0);
41
+ --scalar-color-2: oklch(71.1% 0.019 323.02);
42
+ --scalar-color-3: oklch(86.5% 0.012 325.68);
43
+ --scalar-color-accent: oklch(89.2% 0.058 10.001);
43
44
 
44
- --scalar-background-1: #111728;
45
- --scalar-background-2: #1e293b;
46
- --scalar-background-3: #334155;
47
- --scalar-background-accent: #f062921f;
45
+ --scalar-background-1: oklch(21.2% 0.019 322.12);
46
+ --scalar-background-2: oklch(26.3% 0.024 320.12);
47
+ --scalar-background-3: oklch(36.4% 0.029 323.89);
48
+ --scalar-background-accent: oklch(43.5% 0.029 321.78);
48
49
 
49
- --scalar-border-color: rgba(255, 255, 255, 0.1);
50
+ --scalar-border-color: oklch(36.4% 0.029 323.89);
50
51
  }
51
52
 
52
53
  /* Document Sidebar */
@@ -1,4 +1,4 @@
1
- import type { TSchema } from 'elysia';
1
+ import type { TSchema } from 'typebox';
2
2
  import type { OpenAPIV3 } from 'openapi-types';
3
3
  import type { ApiReferenceConfiguration } from '@scalar/types';
4
4
  import type { SwaggerUIOptions } from './swagger/types';
@@ -102,7 +102,7 @@ export interface ElysiaOpenAPIConfig<Enabled extends boolean = true, Path extend
102
102
  *'
103
103
  * @see https://github.com/scalar/scalar/blob/main/documentation/configuration.md
104
104
  */
105
- scalar?: ApiReferenceConfiguration & {
105
+ scalar?: Partial<ApiReferenceConfiguration> & {
106
106
  /**
107
107
  * Version to use for Scalar cdn bundle
108
108
  *
@@ -54,12 +54,17 @@ export interface OpenAPIGeneratorOptions {
54
54
  }
55
55
  export declare function extractRootObjects(code: string): string[];
56
56
  export declare function declarationToJSONSchema(declaration: string): AdditionalReference;
57
+ /**
58
+ * Collect top-level `type X = ...` aliases and `interface X ...` declarations
59
+ * from a `.d.ts` into a name -> type-string map.
60
+ */
61
+ export declare function extractTypeContext(declaration: string): Record<string, string>;
62
+ export declare function resolveTypeRefs(schema: any, context: Record<string, any>, chain?: Set<string>): any;
63
+ export declare function declarationToReference(declaration: string, instanceName?: string): AdditionalReference | undefined;
57
64
  /**
58
65
  * Auto generate OpenAPI schema from Elysia instance
59
66
  *
60
67
  * It's expected that this command should run in project root
61
- *
62
- * @experimental use at your own risk
63
68
  */
64
69
  export declare const fromTypes: (
65
70
  /**