Skip to content

Commit

Permalink
chore(schemas): use TypeScript Compiler API to create schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Apr 5, 2024
1 parent 54ec139 commit 3530ae7
Show file tree
Hide file tree
Showing 21 changed files with 889 additions and 5,291 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-mails-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(api): use TypeScript Compiler API to create schemas
1 change: 0 additions & 1 deletion packages/openapi-ts/src/templates/exportSchema.hbs

This file was deleted.

17 changes: 0 additions & 17 deletions packages/openapi-ts/src/templates/partials/schema.hbs

This file was deleted.

22 changes: 0 additions & 22 deletions packages/openapi-ts/src/templates/partials/schemaArray.hbs

This file was deleted.

19 changes: 0 additions & 19 deletions packages/openapi-ts/src/templates/partials/schemaComposition.hbs

This file was deleted.

22 changes: 0 additions & 22 deletions packages/openapi-ts/src/templates/partials/schemaDictionary.hbs

This file was deleted.

18 changes: 0 additions & 18 deletions packages/openapi-ts/src/templates/partials/schemaEnum.hbs

This file was deleted.

62 changes: 0 additions & 62 deletions packages/openapi-ts/src/templates/partials/schemaGeneric.hbs

This file was deleted.

26 changes: 0 additions & 26 deletions packages/openapi-ts/src/templates/partials/schemaInterface.hbs

This file was deleted.

1 change: 0 additions & 1 deletion packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('registerHandlebarTemplates', () => {
}
);
expect(templates.exports.model).toBeDefined();
expect(templates.exports.schema).toBeDefined();
expect(templates.exports.service).toBeDefined();
expect(templates.core.settings).toBeDefined();
expect(templates.core.apiError).toBeDefined();
Expand Down
24 changes: 3 additions & 21 deletions packages/openapi-ts/src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import xhrGetResponseHeader from '../templates/core/xhr/getResponseHeader.hbs';
import xhrRequest from '../templates/core/xhr/request.hbs';
import xhrSendRequest from '../templates/core/xhr/sendRequest.hbs';
import templateExportModel from '../templates/exportModel.hbs';
import templateExportSchema from '../templates/exportSchema.hbs';
import templateExportService from '../templates/exportService.hbs';
import partialBase from '../templates/partials/base.hbs';
import partialExportComposition from '../templates/partials/exportComposition.hbs';
Expand All @@ -62,13 +61,6 @@ import partialOperationParameters from '../templates/partials/operationParameter
import partialOperationResult from '../templates/partials/operationResult.hbs';
import partialOperationTypes from '../templates/partials/operationTypes.hbs';
import partialRequestConfig from '../templates/partials/requestConfig.hbs';
import partialSchema from '../templates/partials/schema.hbs';
import partialSchemaArray from '../templates/partials/schemaArray.hbs';
import partialSchemaComposition from '../templates/partials/schemaComposition.hbs';
import partialSchemaDictionary from '../templates/partials/schemaDictionary.hbs';
import partialSchemaEnum from '../templates/partials/schemaEnum.hbs';
import partialSchemaGeneric from '../templates/partials/schemaGeneric.hbs';
import partialSchemaInterface from '../templates/partials/schemaInterface.hbs';
import partialType from '../templates/partials/type.hbs';
import partialTypeArray from '../templates/partials/typeArray.hbs';
import partialTypeDictionary from '../templates/partials/typeDictionary.hbs';
Expand All @@ -94,6 +86,8 @@ const escapeComment = (value: string) =>
export const escapeDescription = (value: string) =>
value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');

Check warning on line 87 in packages/openapi-ts/src/utils/handlebars.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/handlebars.ts#L87

Added line #L87 was not covered by tests

export const escapeNewline = (value: string) => value.replace(/\n/g, '\\n');

const dataDestructure = (config: Config, operation: Operation) => {
if (config.name) {
if (config.useOptions) {
Expand Down Expand Up @@ -264,10 +258,7 @@ export const registerHandlebarHelpers = (config: Config, client: Client): void =

Handlebars.registerHelper('escapeComment', escapeComment);
Handlebars.registerHelper('escapeDescription', escapeDescription);

Handlebars.registerHelper('escapeNewline', function (value: string) {
return value.replace(/\n/g, '\\n');
});
Handlebars.registerHelper('escapeNewline', escapeNewline);

Handlebars.registerHelper('exactArray', function (this: unknown, model: Model, options: Handlebars.HelperOptions) {
if (model.export === 'array' && model.maxItems && model.minItems && model.maxItems === model.minItems) {
Expand Down Expand Up @@ -368,7 +359,6 @@ export interface Templates {
};
exports: {
model: Handlebars.TemplateDelegate;
schema: Handlebars.TemplateDelegate;
service: Handlebars.TemplateDelegate;
};
}
Expand Down Expand Up @@ -396,7 +386,6 @@ export const registerHandlebarTemplates = (config: Config, client: Client): Temp
},
exports: {
model: Handlebars.template(templateExportModel),
schema: Handlebars.template(templateExportSchema),
service: Handlebars.template(templateExportService),
},
};
Expand All @@ -413,13 +402,6 @@ export const registerHandlebarTemplates = (config: Config, client: Client): Temp
Handlebars.registerPartial('operationResult', Handlebars.template(partialOperationResult));
Handlebars.registerPartial('operationTypes', Handlebars.template(partialOperationTypes));
Handlebars.registerPartial('requestConfig', Handlebars.template(partialRequestConfig));
Handlebars.registerPartial('schema', Handlebars.template(partialSchema));
Handlebars.registerPartial('schemaArray', Handlebars.template(partialSchemaArray));
Handlebars.registerPartial('schemaComposition', Handlebars.template(partialSchemaComposition));
Handlebars.registerPartial('schemaDictionary', Handlebars.template(partialSchemaDictionary));
Handlebars.registerPartial('schemaEnum', Handlebars.template(partialSchemaEnum));
Handlebars.registerPartial('schemaGeneric', Handlebars.template(partialSchemaGeneric));
Handlebars.registerPartial('schemaInterface', Handlebars.template(partialSchemaInterface));
Handlebars.registerPartial('type', Handlebars.template(partialType));
Handlebars.registerPartial('typeArray', Handlebars.template(partialTypeArray));
Handlebars.registerPartial('typeDictionary', Handlebars.template(partialTypeDictionary));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ describe('writeClientSchemas', () => {
write: true,
});

expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/schemas.ts'), expect.stringContaining('schema'));
expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/schemas.ts'), expect.anything());
});
});
Loading

0 comments on commit 3530ae7

Please sign in to comment.