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..c8d4293e 100644 --- a/src/code-gen-process.js +++ b/src/code-gen-process.js @@ -204,32 +204,59 @@ class CodeGenProcess { } getRenderTemplateData = () => { + const { schemaParserFabric } = this; + const { schemaFormatters } = schemaParserFabric; return { utils: { Ts: this.config.Ts, formatDescription: - this.schemaParserFabric.schemaFormatters.formatDescription, + schemaParserFabric.schemaFormatters.formatDescription.bind( + 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: + schemaParserFabric.getInlineParseContent.bind(schemaParserFabric), + getParseContent: + schemaParserFabric.getParseContent.bind(schemaParserFabric), + getComponentByRef: this.schemaComponentsMap.get.bind( + this.schemaComponentsMap, + ), + parseSchema: schemaParserFabric.parseSchema.bind(schemaParserFabric), + checkAndAddNull: schemaParserFabric.schemaUtils.safeAddNullToType.bind( + schemaParserFabric.schemaUtils, + ), safeAddNullToType: - this.schemaParserFabric.schemaUtils.safeAddNullToType, + schemaParserFabric.schemaUtils.safeAddNullToType.bind( + schemaParserFabric.schemaUtils, + ), isNeedToAddNull: - this.schemaParserFabric.schemaUtils.isNullMissingInType, - inlineExtraFormatters: this.schemaParserFabric.schemaFormatters.inline, - formatters: this.schemaParserFabric.schemaFormatters.base, - formatModelName: this.typeNameFormatter.format, + schemaParserFabric.schemaUtils.isNullMissingInType.bind( + schemaParserFabric.schemaUtils, + ), + inlineExtraFormatters: Object.keys(schemaFormatters.inline).reduce( + (prev, each) => { + return (prev[each] = + schemaFormatters.inline[each].bind(schemaFormatters)); + }, + {}, + ), + formatters: Object.keys(schemaFormatters.base).reduce((prev, each) => { + return (prev[each] = + schemaFormatters.base[each].bind(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, };