From dc787a17a9257b33452d5484b85785ee5d002ebc Mon Sep 17 00:00:00 2001 From: zWing <371657110@qq.com> Date: Tue, 25 Apr 2023 09:43:05 +0000 Subject: [PATCH] fix: fix renderTmpl.utils context --- index.d.ts | 8 ++++++++ src/code-gen-process.js | 26 ++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index fccb5403..a6698b01 100644 --- a/index.d.ts +++ b/index.d.ts @@ -718,6 +718,14 @@ export interface GenerateApiConfiguration { typeName?: string, formattersMap?: Record string>, ) => ModelType; + safeAddNullToType: ( + schema: { type: string; nullable?: boolean; "x-nullable"?: boolean }, + type: unknown, + ) => string; + isNeedToAddNull: ( + schema: { type: string; nullable?: boolean; "x-nullable"?: boolean }, + type: unknown, + ) => boolean; formatters: Record< MAIN_SCHEMA_TYPES, (content: string | object | string[] | object[]) => string diff --git a/src/code-gen-process.js b/src/code-gen-process.js index 712fe256..c2d96afe 100644 --- a/src/code-gen-process.js +++ b/src/code-gen-process.js @@ -208,28 +208,30 @@ class CodeGenProcess { utils: { Ts: this.config.Ts, formatDescription: - this.schemaParserFabric.schemaFormatters.formatDescription, + this.schemaParserFabric.schemaFormatters.formatDescription.bind( + this.schemaParserFabric.schemaFormatters, + ), internalCase: internalCase, classNameCase: pascalCase, pascalCase: pascalCase, - getInlineParseContent: this.schemaParserFabric.getInlineParseContent, - getParseContent: this.schemaParserFabric.getParseContent, - getComponentByRef: this.schemaComponentsMap.get, - parseSchema: this.schemaParserFabric.parseSchema, - checkAndAddNull: this.schemaParserFabric.schemaUtils.safeAddNullToType, + getInlineParseContent: this.schemaParserFabric.getInlineParseContent.bind(this.schemaParserFabric), + getParseContent: this.schemaParserFabric.getParseContent.bind(this.schemaParserFabric), + getComponentByRef: this.schemaComponentsMap.get.bind(this.schemaComponentsMap), + parseSchema: this.schemaParserFabric.parseSchema.bind(this.schemaParserFabric), + checkAndAddNull: this.schemaParserFabric.schemaUtils.safeAddNullToType.bind(this.schemaParserFabric.schemaUtils), safeAddNullToType: - this.schemaParserFabric.schemaUtils.safeAddNullToType, + this.schemaParserFabric.schemaUtils.safeAddNullToType.bind(this.schemaParserFabric.schemaUtils), isNeedToAddNull: - this.schemaParserFabric.schemaUtils.isNullMissingInType, - inlineExtraFormatters: this.schemaParserFabric.schemaFormatters.inline, - formatters: this.schemaParserFabric.schemaFormatters.base, - formatModelName: this.typeNameFormatter.format, + this.schemaParserFabric.schemaUtils.isNullMissingInType.bind(this.schemaParserFabric.schemaUtils), + inlineExtraFormatters: this.schemaParserFabric.schemaFormatters.inline.bind(this.schemaParserFabric.schemaFormatters), + formatters: this.schemaParserFabric.schemaFormatters.base.bind(this.schemaParserFabric.schemaFormatters), + formatModelName: this.typeNameFormatter.format.bind(this.typeNameFormatter), fmtToJSDocLine: function fmtToJSDocLine(line, { eol = true }) { return ` * ${line}${eol ? "\n" : ""}`; }, NameResolver: NameResolver, _, - require: this.templatesWorker.requireFnFromTemplate, + require: this.templatesWorker.requireFnFromTemplate.bind(this.templatesWorker), }, config: this.config, };