Skip to content

Commit

Permalink
chore(compiler): export models
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Apr 7, 2024
1 parent ed7a51b commit 9f9c505
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 302 deletions.
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isType, ots } from './utils';
* @param value - the unknown value.
* @returns ts.Expression
*/
export const toExpression = (value: unknown): ts.Expression | undefined => {
export const toExpression = (value: unknown, unescape = false): ts.Expression | undefined => {
if (Array.isArray(value)) {
return createArrayType(value);
} else if (typeof value === 'object' && value !== null) {
Expand All @@ -17,7 +17,7 @@ export const toExpression = (value: unknown): ts.Expression | undefined => {
} else if (typeof value === 'boolean') {
return ots.boolean(value);
} else if (typeof value === 'string') {
return ots.string(value);
return ots.string(value, unescape);
} else if (value === null) {
return ts.factory.createNull();
}
Expand Down
16 changes: 9 additions & 7 deletions packages/openapi-ts/src/compiler/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ts from 'typescript';

import { unescapeName } from '../utils/escape';

export const CONFIG = {
newLine: ts.NewLineKind.LineFeed,
scriptKind: ts.ScriptKind.TS,
Expand Down Expand Up @@ -56,15 +58,15 @@ export const ots = {
}
},
// Create a string literal. This handles strings that start with '`' or "'".
string: (value: string) => {
string: (value: string, unescape = false) => {
if (!unescape) {
value = unescapeName(value);
}
const text = encodeURIComponent(value);
if (value.startsWith('`')) {
return ts.factory.createIdentifier(encodeURIComponent(value));
} else {
return ts.factory.createStringLiteral(
encodeURIComponent(value),
value.includes("'") ? false : CONFIG.useSingleQuotes
);
return ts.factory.createIdentifier(text);
}
return ts.factory.createStringLiteral(text, value.includes("'") ? false : CONFIG.useSingleQuotes);
},
};

Expand Down
13 changes: 0 additions & 13 deletions packages/openapi-ts/src/templates/exportModel.hbs

This file was deleted.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

11 changes: 0 additions & 11 deletions packages/openapi-ts/src/templates/partials/exportType.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 @@ -84,7 +84,6 @@ describe('registerHandlebarTemplates', () => {
version: '',
}
);
expect(templates.exports.model).toBeDefined();
expect(templates.exports.service).toBeDefined();
expect(templates.core.settings).toBeDefined();
expect(templates.core.apiError).toBeDefined();
Expand Down
11 changes: 0 additions & 11 deletions packages/openapi-ts/src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ import xhrGetResponseBody from '../templates/core/xhr/getResponseBody.hbs';
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 templateExportService from '../templates/exportService.hbs';
import partialExportComposition from '../templates/partials/exportComposition.hbs';
import partialExportEnum from '../templates/partials/exportEnum.hbs';
import partialExportInterface from '../templates/partials/exportInterface.hbs';
import partialExportType from '../templates/partials/exportType.hbs';
import partialIsNullable from '../templates/partials/isNullable.hbs';
import partialIsReadOnly from '../templates/partials/isReadOnly.hbs';
import partialOperationParameters from '../templates/partials/operationParameters.hbs';
Expand Down Expand Up @@ -292,7 +287,6 @@ export interface Templates {
types: Handlebars.TemplateDelegate;
};
exports: {
model: Handlebars.TemplateDelegate;
service: Handlebars.TemplateDelegate;
};
}
Expand All @@ -319,16 +313,11 @@ export const registerHandlebarTemplates = (config: Config, client: Client): Temp
types: Handlebars.template(templateCoreTypes),
},
exports: {
model: Handlebars.template(templateExportModel),
service: Handlebars.template(templateExportService),
},
};

// Partials for the generations of the models, services, etc.
Handlebars.registerPartial('exportComposition', Handlebars.template(partialExportComposition));
Handlebars.registerPartial('exportEnum', Handlebars.template(partialExportEnum));
Handlebars.registerPartial('exportInterface', Handlebars.template(partialExportInterface));
Handlebars.registerPartial('exportType', Handlebars.template(partialExportType));
Handlebars.registerPartial('isNullable', Handlebars.template(partialIsNullable));
Handlebars.registerPartial('isReadOnly', Handlebars.template(partialIsReadOnly));
Handlebars.registerPartial('operationParameters', Handlebars.template(partialOperationParameters));
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 @@ -16,7 +16,6 @@ export const mockTemplates: Templates = {
types: vi.fn().mockReturnValue('types'),
},
exports: {
model: vi.fn().mockReturnValue('model'),
service: vi.fn().mockReturnValue('service'),
},
};
Loading

0 comments on commit 9f9c505

Please sign in to comment.