Skip to content

Commit

Permalink
Merge pull request #271 from hey-api/feat/json-schemas
Browse files Browse the repository at this point in the history
chore(schemas): use TypeScript Compiler API to create schemas
  • Loading branch information
mrlubos authored Apr 5, 2024
2 parents ba19d0f + f1bc611 commit 935bc6c
Show file tree
Hide file tree
Showing 17 changed files with 344 additions and 238 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
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { type PathOrFileDescriptor, writeFileSync } from 'node:fs';
import ts from 'typescript';

import * as module from './module';
import { toString } from './utils';
import { tsNodeToString } from './utils';

export class TypeScriptFile extends Array<ts.Node> {
public write(file: PathOrFileDescriptor) {
const items = this.map(i => toString(i));
const items = this.map(i => tsNodeToString(i));
writeFileSync(file, items.join('\n'));
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const blankSourceFile = ts.createSourceFile('', '', CONFIG.scriptTarget, undefin
* @param node - the node to print.
* @returns string
*/
export function toString(node: ts.Node): string {
const r = printer.printNode(ts.EmitHint.Unspecified, node, blankSourceFile);
return decodeURIComponent(r);
export function tsNodeToString(node: ts.Node): string {
const result = printer.printNode(ts.EmitHint.Unspecified, node, blankSourceFile);
return decodeURIComponent(result);
}

// ots for openapi-ts is helpers to reduce repetition of basic ts factory functions.
Expand Down
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
34 changes: 8 additions & 26 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 @@ -91,6 +83,11 @@ const escapeComment = (value: string) =>
.replace(/\/\*/g, '*')
.replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`);

export const escapeDescription = (value: string) =>
value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');

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 @@ -163,7 +160,7 @@ const dataParameters = (config: Config, parameters: OperationParameter[]) => {
return output.join(', ');
};

const modelIsRequired = (config: Config, model: Model) => {
export const modelIsRequired = (config: Config, model: Model) => {
if (config.useOptions) {
return model.isRequired ? '' : '?';
}
Expand Down Expand Up @@ -260,14 +257,8 @@ export const registerHandlebarHelpers = (config: Config, client: Client): void =
);

Handlebars.registerHelper('escapeComment', escapeComment);

Handlebars.registerHelper('escapeDescription', function (value: string) {
return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
});

Handlebars.registerHelper('escapeNewline', function (value: string) {
return value.replace(/\n/g, '\\n');
});
Handlebars.registerHelper('escapeDescription', escapeDescription);
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
1 change: 0 additions & 1 deletion packages/openapi-ts/src/utils/write/__tests__/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const mockTemplates: Templates = {
},
exports: {
model: vi.fn().mockReturnValue('model'),
schema: vi.fn().mockReturnValue('schema'),
service: vi.fn().mockReturnValue('service'),
},
};
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'), 'schema');
expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/schemas.ts'), expect.anything());
});
});
Loading

0 comments on commit 935bc6c

Please sign in to comment.