Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const params = program
.option('--name <value>', 'Custom client class name')
.option('--useOptions', 'Use options instead of arguments')
.option('--useUnionTypes', 'Use union types instead of enums')
.option('--useTuples <value>', 'Whether to convert constant size arrays to tuples.', false)
.option('--exportCore <value>', 'Write core files to disk', true)
.option('--exportServices <value>', 'Write services to disk', true)
.option('--exportModels <value>', 'Write models to disk', true)
Expand All @@ -37,6 +38,7 @@ if (OpenAPI) {
clientName: params.name,
useOptions: params.useOptions,
useUnionTypes: params.useUnionTypes,
useTuples: JSON.parse(params.useTuples) === true,
exportCore: JSON.parse(params.exportCore) === true,
exportServices: JSON.parse(params.exportServices) === true,
exportModels: JSON.parse(params.exportModels) === true,
Expand Down
1 change: 1 addition & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const handlebarsPlugin = () => ({
escapeComment: true,
escapeDescription: true,
camelCase: true,
repeatTimes: true,
},
});
return `export default ${templateSpec};`;
Expand Down
1 change: 1 addition & 0 deletions src/client/interfaces/Model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface Model extends Schema {
enum: Enum[];
enums: Model[];
properties: Model[];
isConstantSize?: boolean;
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type Options = {
clientName?: string;
useOptions?: boolean;
useUnionTypes?: boolean;
useTuples?: boolean;
exportCore?: boolean;
exportServices?: boolean;
exportModels?: boolean;
Expand Down Expand Up @@ -57,6 +58,7 @@ export const generate = async ({
clientName,
useOptions = false,
useUnionTypes = false,
useTuples = false,
exportCore = true,
exportServices = true,
exportModels = true,
Expand All @@ -73,6 +75,7 @@ export const generate = async ({
httpClient,
useUnionTypes,
useOptions,
useTuples,
});

switch (openApiVersion) {
Expand All @@ -87,6 +90,7 @@ export const generate = async ({
httpClient,
useOptions,
useUnionTypes,
useTuples,
exportCore,
exportServices,
exportModels,
Expand All @@ -111,6 +115,7 @@ export const generate = async ({
httpClient,
useOptions,
useUnionTypes,
useTuples,
exportCore,
exportServices,
exportModels,
Expand Down
10 changes: 6 additions & 4 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,28 @@ export const getModel = (
}

if (definition.type === 'array' && definition.items) {
model.export = 'array';
model.isConstantSize = Boolean(
definition.minItems && definition.maxItems && definition.minItems === definition.maxItems
);

if (definition.items.$ref) {
const arrayItems = getType(definition.items.$ref);
model.export = 'array';
model.type = arrayItems.type;
model.base = arrayItems.base;
model.template = arrayItems.template;
model.imports.push(...arrayItems.imports);
model.default = getModelDefault(definition, model);
return model;
} else {
const arrayItems = getModel(openApi, definition.items);
model.export = 'array';
model.type = arrayItems.type;
model.base = arrayItems.base;
model.template = arrayItems.template;
model.link = arrayItems;
model.imports.push(...arrayItems.imports);
model.default = getModelDefault(definition, model);
return model;
}
return model;
}

if (
Expand Down
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const getModelProperties = (
enum: model.enum,
enums: model.enums,
properties: model.properties,
isConstantSize: model.isConstantSize,
...propertyValues,
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/templates/partials/type.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
{{else equals export 'enum'}}
{{>typeEnum}}
{{else equals export 'array'}}

{{~#if @root.useTuples~}}
{{~#if isConstantSize ~}}
{{>typeTuple}}
{{~else~}}
{{>typeArray}}
{{~/if~}}
{{~else~}}
{{>typeArray}}
{{~/if~}}

{{else equals export 'dictionary'}}
{{>typeDictionary}}
{{else equals export 'one-of'}}
Expand Down
21 changes: 21 additions & 0 deletions src/templates/partials/typeTuple.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{~#if link~}}

[
{{~#repeatTimes minItems~}}
{{>type link}}
{{#unless isLast}}, {{/unless}}
{{~/repeatTimes~}}
]
{{>isNullable}}

{{~else~}}

[
{{~#repeatTimes minItems~}}
{{>base}}
{{#unless isLast}}, {{/unless}}
{{~/repeatTimes~}}
]
{{>isNullable}}

{{~/if~}}
1 change: 1 addition & 0 deletions src/utils/registerHandlebarHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('registerHandlebarHelpers', () => {
httpClient: HttpClient.FETCH,
useOptions: false,
useUnionTypes: false,
useTuples: false,
});
const helpers = Object.keys(Handlebars.helpers);
expect(helpers).toContain('ifdef');
Expand Down
13 changes: 13 additions & 0 deletions src/utils/registerHandlebarHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const registerHandlebarHelpers = (root: {
httpClient: HttpClient;
useOptions: boolean;
useUnionTypes: boolean;
useTuples: boolean;
}): void => {
Handlebars.registerHelper('ifdef', function (this: any, ...args): string {
const options = args.pop();
Expand Down Expand Up @@ -104,4 +105,16 @@ export const registerHandlebarHelpers = (root: {
Handlebars.registerHelper('camelCase', function (value: string): string {
return camelCase(value);
});

Handlebars.registerHelper('repeatTimes', function (n, block) {
let accum = '';
for (let i = 0; i < n; ++i) {
const t = Handlebars.createFrame(this);
if (i == n - 1) {
t.isLast = true;
}
accum += block.fn(t);
}
return accum;
});
};
1 change: 1 addition & 0 deletions src/utils/registerHandlebarTemplates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('registerHandlebarTemplates', () => {
httpClient: HttpClient.FETCH,
useOptions: false,
useUnionTypes: false,
useTuples: false,
});
expect(templates.index).toBeDefined();
expect(templates.exports.model).toBeDefined();
Expand Down
3 changes: 3 additions & 0 deletions src/utils/registerHandlebarTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import partialTypeGeneric from '../templates/partials/typeGeneric.hbs';
import partialTypeInterface from '../templates/partials/typeInterface.hbs';
import partialTypeIntersection from '../templates/partials/typeIntersection.hbs';
import partialTypeReference from '../templates/partials/typeReference.hbs';
import partialTypeTuple from '../templates/partials/typeTuple.hbs';
import partialTypeUnion from '../templates/partials/typeUnion.hbs';
import { registerHandlebarHelpers } from './registerHandlebarHelpers';

Expand Down Expand Up @@ -113,6 +114,7 @@ export const registerHandlebarTemplates = (root: {
httpClient: HttpClient;
useOptions: boolean;
useUnionTypes: boolean;
useTuples: boolean;
}): Templates => {
registerHandlebarHelpers(root);

Expand Down Expand Up @@ -162,6 +164,7 @@ export const registerHandlebarTemplates = (root: {
Handlebars.registerPartial('typeGeneric', Handlebars.template(partialTypeGeneric));
Handlebars.registerPartial('typeInterface', Handlebars.template(partialTypeInterface));
Handlebars.registerPartial('typeReference', Handlebars.template(partialTypeReference));
Handlebars.registerPartial('typeTuple', Handlebars.template(partialTypeTuple));
Handlebars.registerPartial('typeUnion', Handlebars.template(partialTypeUnion));
Handlebars.registerPartial('typeIntersection', Handlebars.template(partialTypeIntersection));
Handlebars.registerPartial('base', Handlebars.template(partialBase));
Expand Down
1 change: 1 addition & 0 deletions src/utils/writeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('writeClient', () => {
HttpClient.FETCH,
false,
false,
false,
true,
true,
true,
Expand Down
12 changes: 11 additions & 1 deletion src/utils/writeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const writeClient = async (
httpClient: HttpClient,
useOptions: boolean,
useUnionTypes: boolean,
useTuples: boolean,
exportCore: boolean,
exportServices: boolean,
exportModels: boolean,
Expand Down Expand Up @@ -91,7 +92,15 @@ export const writeClient = async (
if (exportModels) {
await rmdir(outputPathModels);
await mkdir(outputPathModels);
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
await writeClientModels(
client.models,
templates,
outputPathModels,
httpClient,
useUnionTypes,
useTuples,
indent
);
}

if (isDefined(clientName)) {
Expand All @@ -106,6 +115,7 @@ export const writeClient = async (
templates,
outputPath,
useUnionTypes,
useTuples,
exportCore,
exportServices,
exportModels,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientIndex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('writeClientIndex', () => {
},
};

await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', '');
await writeClientIndex(client, templates, '/', true, true, true, true, true, true, 'Service', '');

expect(writeFile).toBeCalledWith(resolve('/', '/index.ts'), 'index');
});
Expand Down
2 changes: 2 additions & 0 deletions src/utils/writeClientIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const writeClientIndex = async (
templates: Templates,
outputPath: string,
useUnionTypes: boolean,
useTuples: boolean,
exportCore: boolean,
exportServices: boolean,
exportModels: boolean,
Expand All @@ -42,6 +43,7 @@ export const writeClientIndex = async (
exportModels,
exportSchemas,
useUnionTypes,
useTuples,
postfixServices,
postfixModels,
clientName,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/writeClientModels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('writeClientModels', () => {
},
};

await writeClientModels(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4);
await writeClientModels(models, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4);

expect(writeFile).toBeCalledWith(resolve('/', '/User.ts'), `model${EOL}`);
});
Expand Down
2 changes: 2 additions & 0 deletions src/utils/writeClientModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const writeClientModels = async (
outputPath: string,
httpClient: HttpClient,
useUnionTypes: boolean,
useTuples: boolean,
indent: Indent
): Promise<void> => {
for (const model of models) {
Expand All @@ -31,6 +32,7 @@ export const writeClientModels = async (
...model,
httpClient,
useUnionTypes,
useTuples,
});
await writeFile(file, i(f(templateResult), indent));
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/writeClientSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const writeClientSchemas = async (
...model,
httpClient,
useUnionTypes,
useTuples: true,
});
await writeFile(file, i(f(templateResult), indent));
}
Expand Down