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 5b7f485
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 310 deletions.
24 changes: 17 additions & 7 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@ 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) {
}

Check warning on line 13 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L13

Added line #L13 was not covered by tests

if (typeof value === 'object' && value !== null) {
return createObjectType(value);
} else if (typeof value === 'number') {
}

if (typeof value === 'number') {

Check warning on line 19 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L18-L19

Added lines #L18 - L19 were not covered by tests
return ots.number(value);
} else if (typeof value === 'boolean') {
}

if (typeof value === 'boolean') {

Check warning on line 23 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L21-L23

Added lines #L21 - L23 were not covered by tests
return ots.boolean(value);
} else if (typeof value === 'string') {
return ots.string(value);
} else if (value === null) {
}

if (typeof value === 'string') {
return ots.string(value, unescape);
}

if (value === null) {

Check warning on line 31 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L25-L31

Added lines #L25 - L31 were not covered by tests
return ts.factory.createNull();
}
};
Expand Down
19 changes: 10 additions & 9 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 @@ -51,20 +53,19 @@ export const ots = {
ts.SyntaxKind.MinusToken,
ts.factory.createNumericLiteral(Math.abs(value))
);
} else {
return ts.factory.createNumericLiteral(value);
}
return ts.factory.createNumericLiteral(value);

Check warning on line 57 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L57

Added line #L57 was not covered by tests
},
// Create a string literal. This handles strings that start with '`' or "'".
string: (value: string) => {
string: (value: string, unescape = false) => {
if (unescape) {
value = unescapeName(value);
}

Check warning on line 63 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L62-L63

Added lines #L62 - L63 were not covered by tests
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);

Check warning on line 66 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L66

Added line #L66 was not covered by tests
}
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'),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ describe('writeClientModels', () => {
write: true,
});

expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/models.ts'), 'model');
expect(writeFileSync).toHaveBeenCalledWith(path.resolve('/', '/models.ts'), expect.anything());
});
});
Loading

0 comments on commit 5b7f485

Please sign in to comment.