-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathpatch-nest-swagger.ts
50 lines (41 loc) · 1.7 KB
/
patch-nest-swagger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
/**
* This file was copied from:
* https://github.com/kbkk/abitia/blob/master/packages/zod-dto/src/OpenApi/patchNestjsSwagger.ts
*/
import {generateSchema} from '@anatine/zod-openapi';
import type {SchemaObject} from 'openapi3-ts/oas31';
interface Type<T = any> extends Function {
new (...args: any[]): T;
}
type SchemaObjectFactoryModule =
typeof import('@nestjs/swagger/dist/services/schema-object-factory');
export const patchNestjsSwagger = (
schemaObjectFactoryModule: SchemaObjectFactoryModule | undefined = undefined,
openApiVersion: '3.0' | '3.1' = '3.0'
): void => {
const { SchemaObjectFactory } = (schemaObjectFactoryModule ??
require('@nestjs/swagger/dist/services/schema-object-factory')) as SchemaObjectFactoryModule;
const orgExploreModelSchema =
SchemaObjectFactory.prototype.exploreModelSchema;
SchemaObjectFactory.prototype.exploreModelSchema = function (
type: Type<unknown> | Function | any,
schemas: any | Record<string, SchemaObject>,
schemaRefsStack: string[] = []
// type: Type<unknown> | Function | any,
// schemas: Record<string, SchemaObject>,
// schemaRefsStack: string[] = []
) {
// @ts-expect-error Reported as private, but since we are patching, we will be able to reach it
if (this.isLazyTypeFunc(type)) {
// eslint-disable-next-line @typescript-eslint/ban-types
type = (type as Function)();
}
if (!type.zodSchema) {
return orgExploreModelSchema.call(this, type, schemas, schemaRefsStack);
}
schemas[type.name] = generateSchema(type.zodSchema, false, openApiVersion);
return type.name;
};
};