Skip to content

Commit

Permalink
chore(parser): use single namespace for service types
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Apr 11, 2024
1 parent 6b49588 commit 74d1e2f
Show file tree
Hide file tree
Showing 38 changed files with 4,599 additions and 4,755 deletions.
2 changes: 0 additions & 2 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const initConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
debug = false,
dryRun = false,
enums = false,
experimental = false,
exportCore = true,
exportModels = true,
exportServices = true,
Expand Down Expand Up @@ -152,7 +151,6 @@ const initConfig = async (userConfig: UserConfig, dependencies: Dependencies) =>
debug,
dryRun,
enums,
experimental,
exportCore,
exportModels,
exportServices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('getOperationName', () => {
debug: false,
dryRun: true,
enums: false,
experimental: false,
exportCore: false,
exportModels: false,
exportServices: false,
Expand All @@ -30,7 +29,6 @@ describe('getOperationName', () => {
debug: false,
dryRun: true,
enums: false,
experimental: false,
exportCore: false,
exportModels: false,
exportServices: false,
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi-ts/src/openApi/common/parser/getEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const getEnums = (definition: WithEnumExtension, values?: ReadonlyArray<s
}

const descriptions = (definition['x-enum-descriptions'] ?? []).filter(value => typeof value === 'string');
const names = (definition['x-enum-varnames'] ?? definition['x-enumNames'] ?? []).filter(value => typeof value === 'string');
const names = (definition['x-enum-varnames'] ?? definition['x-enumNames'] ?? []).filter(
value => typeof value === 'string'
);

return values
.filter(unique)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('getServices', () => {
debug: false,
dryRun: true,
enums: false,
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('getServices', () => {
debug: false,
dryRun: true,
enums: false,
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{#if @root.$config.useOptions~}}
{{#if parameters}}data: {{{nameOperationDataType @root 'req' this}}}{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}}{{/if}}
{{#if parameters}}data: {{{nameOperationDataType 'req' this}}}{{#ifOperationDataOptional parameters}} = {}{{/ifOperationDataOptional}}{{/if}}
{{~else}}
{{#if parameters}}

{{#each parameters}}
{{{name}}}{{{modelIsRequired this}}}: {{{nameOperationDataType @root 'req' ../this name}}}{{#hasDefault this}} = {{{getDefaultPrintable this}}}{{/hasDefault}},
{{{name}}}{{{modelIsRequired this}}}: {{{nameOperationDataType 'req' ../this name}}}{{#hasDefault this}} = {{{getDefaultPrintable this}}}{{/hasDefault}},
{{/each}}
{{/if}}
{{/if}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{~#if @root.$config.useOptions~}}
{{~#equals @root.$config.serviceResponse 'response'}}ApiResult<{{/equals~}}
{{~/if~}}
{{{nameOperationDataType @root 'res' this}}}
{{{nameOperationDataType 'res' this}}}
{{~#if @root.$config.useOptions~}}
{{~#equals @root.$config.serviceResponse 'response'}}>{{/equals~}}
{{~/if~}}
5 changes: 0 additions & 5 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export interface UserConfig {
* @default false
*/
enums?: 'javascript' | 'typescript' | false;
/**
* Generate an experimental build?
* @default false
*/
experimental?: boolean;
/**
* Generate core client classes?
* @default true
Expand Down
2 changes: 0 additions & 2 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('registerHandlebarHelpers', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down Expand Up @@ -49,7 +48,6 @@ describe('registerHandlebarTemplates', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
76 changes: 29 additions & 47 deletions packages/openapi-ts/src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,9 @@ const dataDestructure = (operation: Operation) => {
} else {
if (config.useOptions) {
if (operation.parameters.length) {
// TODO: extract query parameters from query key

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L70 was not covered by tests
return `const {
${config.experimental ? 'query,' : ''}
${operation.parameters
.map(parameter => {
if (config.experimental) {
if (parameter.in !== 'query') {
return parameter.name;
}
} else {
return parameter.name;
}
})
.filter(Boolean)
.join(',\n')}
${operation.parameters.map(parameter => parameter.name).join(',\n')}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L72 was not covered by tests
} = data;`;
}
}
Expand All @@ -89,24 +78,22 @@ const dataDestructure = (operation: Operation) => {
};

const dataParameters = (parameters: OperationParameter[]) => {
const config = getConfig();

if (config.experimental) {
let output = parameters
.filter(parameter => getDefaultPrintable(parameter) !== undefined)
.map(parameter => {
const key = parameter.prop;
const value = parameter.name;
if (key === value || escapeName(key) === key) {
return `${key}: ${getDefaultPrintable(parameter)}`;
}
return `'${key}': ${getDefaultPrintable(parameter)}`;
});
if (parameters.every(parameter => parameter.in === 'query')) {
output = [...output, '...query'];
}
return output.join(', ');
}
// if (config.experimental) {
// let output = parameters
// .filter(parameter => getDefaultPrintable(parameter) !== undefined)
// .map(parameter => {
// const key = parameter.prop;
// const value = parameter.name;
// if (key === value || escapeName(key) === key) {
// return `${key}: ${getDefaultPrintable(parameter)}`;
// }
// return `'${key}': ${getDefaultPrintable(parameter)}`;
// });
// if (parameters.every(parameter => parameter.in === 'query')) {
// output = [...output, '...query'];
// }
// return output.join(', ');
// }

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

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/handlebars.ts#L81-L96

Added lines #L81 - L96 were not covered by tests

const output = parameters.map(parameter => {
const key = parameter.prop;
Expand All @@ -122,26 +109,22 @@ const dataParameters = (parameters: OperationParameter[]) => {
return output.join(', ');
};

export const serviceExportedNamespace = (service: Service) => {
const exported = `$OpenApiTs${camelCase(service.name, { pascalCase: true })}`;
return exported;
};

export const operationKey = (operation: Service['operations'][number]) => {
const key = camelCase(operation.name, { pascalCase: true });
return key;
};
export const serviceExportedNamespace = () => '$OpenApiTs';

export const nameOperationDataType = (
service: Service,
namespace: 'req' | 'res',

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L115 was not covered by tests
operation: Service['operations'][number],
name?: string | object
) => {
const exported = serviceExportedNamespace(service);
const key = operationKey(operation);
const path = `${exported}['${namespace}']['${key}']`;
return name && typeof name === 'string' ? `${path}['${name}']` : path;
const exported = serviceExportedNamespace();
if (namespace === 'req') {
const path = `${exported}['${operation.path}']['${operation.method.toLocaleLowerCase()}']['${namespace}']`;
return name && typeof name === 'string' ? `${path}['${name}']` : path;
}
if (namespace === 'res') {
const path = `${exported}['${operation.path}']['${operation.method.toLocaleLowerCase()}']['${namespace}']`;
return name && typeof name === 'string' ? `${path}['${name}']` : path;
}

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

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/handlebars.ts#L119-L127

Added lines #L119 - L127 were not covered by tests
};

export const registerHandlebarHelpers = (): void => {
Expand Down Expand Up @@ -191,12 +174,11 @@ export const registerHandlebarHelpers = (): void => {
Handlebars.registerHelper(
'nameOperationDataType',
function (
service: Service,
namespace: 'req' | 'res',

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L177 was not covered by tests
operation: Service['operations'][number],
name: string | undefined
) {
return nameOperationDataType(service, namespace, operation, name);
return nameOperationDataType(namespace, operation, name);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L181 was not covered by tests
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('writeClientClass', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('writeClient', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
3 changes: 0 additions & 3 deletions packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('writeClientCore', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down Expand Up @@ -71,7 +70,6 @@ describe('writeClientCore', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down Expand Up @@ -113,7 +111,6 @@ describe('writeClientCore', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('writeClientIndex', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('writeClientModels', () => {
debug: false,
dryRun: false,
enums: 'javascript',
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('writeClientServices', () => {
debug: false,
dryRun: false,
enums: false,
experimental: false,
exportCore: true,
exportModels: true,
exportServices: true,
Expand Down
Loading

0 comments on commit 74d1e2f

Please sign in to comment.