Skip to content

Commit

Permalink
fix: fix renderTmpl.utils context
Browse files Browse the repository at this point in the history
  • Loading branch information
zWingz committed Jun 28, 2024
1 parent 537ebdd commit 1f16a5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,14 @@ export interface GenerateApiConfiguration {
typeName?: string,
formattersMap?: Record<MAIN_SCHEMA_TYPES, (content: ModelType) => 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
Expand Down
52 changes: 40 additions & 12 deletions src/code-gen-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,32 +204,60 @@ 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,
};
Expand Down

0 comments on commit 1f16a5f

Please sign in to comment.