Skip to content

Commit

Permalink
fix(generators): clear return types on each generation
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Jul 14, 2022
1 parent e09abfb commit a663a17
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 45 deletions.
23 changes: 16 additions & 7 deletions src/core/generators/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ANGULAR_DEPENDENCIES: GeneratorDependency[] = [
},
];

const returnTypesToWrite: string[] = [];
let returnTypesToWrite: string[] = [];

export const getAngularDependencies = () => ANGULAR_DEPENDENCIES;

Expand Down Expand Up @@ -101,8 +101,12 @@ export class ${title} {
private http: HttpClient,
) {}`;

export const generateAngularFooter = () =>
`};\n\n${returnTypesToWrite.join('\n')}`;
export const generateAngularFooter = () => {
const footer = `};\n\n${returnTypesToWrite.join('\n')}`;
// quick fix to clear global state
returnTypesToWrite = [];
return footer;
};

const generateImplementation = (
{
Expand Down Expand Up @@ -165,10 +169,15 @@ const generateImplementation = (
)
: '';

return ` ${operationName}<TData = ${dataType}>(\n ${toObjectString(
props,
'implementation',
)}\n ${
const propsImplementation =
mutator.bodyTypeName && body.definition
? toObjectString(props, 'implementation').replace(
new RegExp(`(\\w*):\\s?${body.definition}`),
`$1: ${mutator.bodyTypeName}<${body.definition}>`,
)
: toObjectString(props, 'implementation');

return ` ${operationName}<TData = ${dataType}>(\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasThirdArg
? `options?: ThirdParameter<typeof ${mutator.name}>`
: ''
Expand Down
18 changes: 13 additions & 5 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AXIOS_DEPENDENCIES: GeneratorDependency[] = [
},
];

const returnTypesToWrite: Map<string, (title?: string) => string> = new Map();
let returnTypesToWrite: Map<string, (title?: string) => string> = new Map();

export const getAxiosDependencies = (hasGlobalMutator: boolean) => [
...(!hasGlobalMutator ? AXIOS_DEPENDENCIES : []),
Expand Down Expand Up @@ -109,10 +109,15 @@ const generateAxiosImplementation = (
}>>>`,
);

return `const ${operationName} = (\n ${toObjectString(
props,
'implementation',
)}\n ${
const propsImplementation =
mutator.bodyTypeName && body.definition
? toObjectString(props, 'implementation').replace(
new RegExp(`(\\w*):\\s?${body.definition}`),
`$1: ${mutator.bodyTypeName}<${body.definition}>`,
)
: toObjectString(props, 'implementation');

return `const ${operationName} = (\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasSecondArg
? `options?: SecondParameter<typeof ${mutator.name}>,`
: ''
Expand Down Expand Up @@ -212,6 +217,9 @@ export const generateAxiosFooter: ClientFooterBuilder = ({
returnTypes += returnTypesArr.join('\n');
}

// quick fix to clear global state
returnTypesToWrite = new Map();

return noFunction ? returnTypes : functionFooter + returnTypes;
};

Expand Down
47 changes: 36 additions & 11 deletions src/core/generators/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ export const generateImports = ({
.join('\n');
};

export const generateMutatorImports = (
mutators: GeneratorMutator[],
oneMore?: boolean,
) => {
export const generateMutatorImports = ({
mutators,
implementation,
oneMore,
}: {
mutators: GeneratorMutator[];
implementation?: string;
oneMore?: boolean;
}) => {
const imports = uniqWith(
mutators,
(a, b) => a.name === b.name && a.default === b.default,
Expand All @@ -67,13 +72,33 @@ export const generateMutatorImports = (

let dep = `import ${importDefault} from '${path}'`;

if (mutator.hasErrorType || mutator.bodyTypeName) {
dep += '\n';
dep += `import type { ${
mutator.hasErrorType ? `${mutator.errorTypeName}` : ''
}${mutator.hasErrorType && mutator.bodyTypeName ? ', ' : ''}${
mutator.bodyTypeName ? `${mutator.bodyTypeName}` : ''
} } from '${path}'`;
if (implementation && (mutator.hasErrorType || mutator.bodyTypeName)) {
let errorImportName = '';
if (
mutator.hasErrorType &&
implementation.includes(mutator.errorTypeName)
) {
errorImportName = mutator.default
? `ErrorType as ${mutator.errorTypeName}`
: mutator.errorTypeName;
}

let bodyImportName = '';
if (
mutator.bodyTypeName &&
implementation.includes(mutator.bodyTypeName)
) {
bodyImportName = mutator.default
? `BodyType as ${mutator.bodyTypeName}`
: mutator.bodyTypeName;
}

if (bodyImportName || errorImportName) {
dep += '\n';
dep += `import type { ${errorImportName}${
errorImportName && bodyImportName ? ', ' : ''
}${bodyImportName} } from '${path}'`;
}
}

return dep;
Expand Down
13 changes: 9 additions & 4 deletions src/core/generators/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,22 @@ const generateSwrRequestFunction = (
isExactOptionalPropertyTypes,
});

const propsImplementation =
mutator?.bodyTypeName && body.definition
? toObjectString(props, 'implementation').replace(
new RegExp(`(\\w*):\\s?${body.definition}`),
`$1: ${mutator.bodyTypeName}<${body.definition}>`,
)
: toObjectString(props, 'implementation');

const requestOptions = isRequestOptions
? generateMutatorRequestOptions(
override?.requestOptions,
mutator.hasSecondArg,
)
: '';

return `export const ${operationName} = (\n ${toObjectString(
props,
'implementation',
)}\n ${
return `export const ${operationName} = (\n ${propsImplementation}\n ${
isRequestOptions && mutator.hasSecondArg
? `options?: SecondParameter<typeof ${mutator.name}>`
: ''
Expand Down
6 changes: 3 additions & 3 deletions src/core/writers/singleMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ export const writeSingleMode = async ({
}

if (mutators) {
data += generateMutatorImports(mutators);
data += generateMutatorImports({ mutators, implementation });
}

if (formData) {
data += generateMutatorImports(formData);
data += generateMutatorImports({ mutators: formData });
}

if (formUrlEncoded) {
data += generateMutatorImports(formUrlEncoded);
data += generateMutatorImports({ mutators: formUrlEncoded });
}

if (!output.schemas) {
Expand Down
11 changes: 8 additions & 3 deletions src/core/writers/splitMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,20 @@ export const writeSplitMode = async ({
}

if (mutators) {
implementationData += generateMutatorImports(mutators);
implementationData += generateMutatorImports({
mutators,
implementation,
});
}

if (formData) {
implementationData += generateMutatorImports(formData);
implementationData += generateMutatorImports({ mutators: formData });
}

if (formUrlEncoded) {
implementationData += generateMutatorImports(formUrlEncoded);
implementationData += generateMutatorImports({
mutators: formUrlEncoded,
});
}

implementationData += `\n${implementation}`;
Expand Down
16 changes: 13 additions & 3 deletions src/core/writers/splitTagsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,24 @@ export const writeSplitTagsMode = async ({
}

if (mutators) {
implementationData += generateMutatorImports(mutators, true);
implementationData += generateMutatorImports({
mutators,
implementation,
oneMore: true,
});
}

if (formData) {
implementationData += generateMutatorImports(formData, true);
implementationData += generateMutatorImports({
mutators: formData,
oneMore: true,
});
}
if (formUrlEncoded) {
implementationData += generateMutatorImports(formUrlEncoded);
implementationData += generateMutatorImports({
mutators: formUrlEncoded,
oneMore: true,
});
}

implementationData += `\n${implementation}`;
Expand Down
6 changes: 3 additions & 3 deletions src/core/writers/tagsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ export const writeTagsMode = async ({
}

if (mutators) {
data += generateMutatorImports(mutators);
data += generateMutatorImports({ mutators, implementation });
}

if (formData) {
data += generateMutatorImports(formData);
data += generateMutatorImports({ mutators: formData });
}

if (formUrlEncoded) {
data += generateMutatorImports(formUrlEncoded);
data += generateMutatorImports({ mutators: formUrlEncoded });
}

data += '\n\n';
Expand Down
17 changes: 17 additions & 0 deletions tests/configs/angular.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ export default defineConfig({
},
},
},
customClient: {
output: {
target: '../generated/angular/custom-client/endpoints.ts',
schemas: '../generated/angular/custom-client/model',
client: 'angular',
mock: true,
override: {
mutator: '../mutators/custom-client-angular.ts',
},
},
input: {
target: '../specifications/petstore.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
});
8 changes: 4 additions & 4 deletions tests/configs/axios.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default defineConfig({
},
mutator: {
output: {
target: '../generated/axios/petstore/endpoints.ts',
schemas: '../generated/axios/petstore/model',
target: '../generated/axios/mutator/endpoints.ts',
schemas: '../generated/axios/mutator/model',
mock: true,
client: 'axios',
override: {
Expand All @@ -35,8 +35,8 @@ export default defineConfig({
},
multiArguments: {
output: {
target: '../generated/axios/petstore/endpoints.ts',
schemas: '../generated/axios/petstore/model',
target: '../generated/axios/multi-arguments/endpoints.ts',
schemas: '../generated/axios/multi-arguments/model',
mock: true,
client: 'axios',
override: {
Expand Down
4 changes: 2 additions & 2 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default defineConfig({
endpointParameters: {
input: '../specifications/parameters.yaml',
output: {
target: '../generated/default/endpointParameters/endpoints.ts',
schemas: '../generated/default/endpointParameters/model',
target: '../generated/default/endpoint-parameters/endpoints.ts',
schemas: '../generated/default/endpoint-parameters/model',
mock: true,
},
},
Expand Down
21 changes: 21 additions & 0 deletions tests/configs/svelte-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ export default defineConfig({
schemas: '../generated/svelte-query/petstore/model',
client: 'svelte-query',
mock: true,
override: {
query: {
useQuery: true,
useInfinite: true,
useInfiniteQueryParam: 'limit',
},
},
},
input: {
target: '../specifications/petstore.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
mutator: {
output: {
target: '../generated/svelte-query/mutator/endpoints.ts',
schemas: '../generated/svelte-query/mutator/model',
client: 'svelte-query',
mock: true,
override: {
mutator: {
path: '../mutators/custom-instance.ts',
Expand Down
40 changes: 40 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,44 @@ export default defineConfig({
},
},
},
mutator: {
output: {
target: '../generated/swr/mutator/endpoints.ts',
schemas: '../generated/swr/mutator/model',
client: 'swr',
mock: true,
override: {
mutator: {
path: '../mutators/custom-instance.ts',
name: 'customInstance',
},
},
},
input: {
target: '../specifications/petstore.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
customClient: {
output: {
target: '../generated/swr/mutator/endpoints.ts',
schemas: '../generated/swr/mutator/model',
client: 'swr',
mock: true,
override: {
mutator: {
path: '../mutators/custom-client.ts',
name: 'customClient',
},
},
},
input: {
target: '../specifications/petstore.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
});
Loading

1 comment on commit a663a17

@vercel
Copy link

@vercel vercel bot commented on a663a17 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.