Skip to content

Commit

Permalink
fix(footer): warn about params change if footer function call fail
Browse files Browse the repository at this point in the history
Use it to avoid a breaking change
  • Loading branch information
CPatchane committed Mar 9, 2022
1 parent d27df09 commit e9f073a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
13 changes: 5 additions & 8 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ClientFooterBuilder,
GeneratorClient,
GeneratorDependency,
GeneratorOptions,
Expand Down Expand Up @@ -174,20 +175,16 @@ export const generateAxiosHeader = ({
}
${!noFunction ? `export const ${title} = () => {\n` : ''}`;

export const generateAxiosFooter = ({
operationNames = [],
noFunction,
export const generateAxiosFooter: ClientFooterBuilder = ({
operationNames,
title,
}: {
operationNames?: string[];
noFunction?: boolean;
title: string;
noFunction,
}) => {
const functionFooter = `return {${operationNames.join(',')}}};\n`;
const returnTypesArr = operationNames
.map((n) => {
return returnTypesToWrite.has(n)
? returnTypesToWrite.get(n)?.(noFunction ? undefined : title)
? returnTypesToWrite.get(n)?.(noFunction || !title ? undefined : title)
: '';
})
.filter(Boolean);
Expand Down
20 changes: 17 additions & 3 deletions src/core/generators/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export const GENERATOR_CLIENT: GeneratorClients = {
isRequestOptions: boolean;
}) => generateAxiosHeader({ ...options, noFunction: true }),
dependencies: getAxiosDependencies,
footer: ({operationNames, title }) =>
generateAxiosFooter({ operationNames, title, noFunction: true }),
footer: (options) => generateAxiosFooter({ ...options, noFunction: true }),
title: generateAxiosTitle,
},
angular: {
Expand Down Expand Up @@ -191,8 +190,23 @@ export const generateClientFooter = ({
}): GeneratorClientExtra => {
const titles = generateClientTitle(outputClient, title, customTitleFunc);
const { footer } = getGeneratorClient(outputClient);
let implementation: string;
try {
if (isFunction(outputClient)) {
implementation = (footer as (operationNames: any) => string)(operationNames);
// being here means that the previous call worked
console.warn(
'[WARN] Passing an array of strings for operations names to the footer function is deprecated and will be removed in a future major release. Please pass them in an object instead: { operationNames: string[] }.',
);
} else {
implementation = footer({ operationNames, title: titles.implementation });
}
} catch (e) {
implementation = footer({ operationNames, title: titles.implementation });
}

return {
implementation: footer({ operationNames, title: titles.implementation }),
implementation,
implementationMSW: `]\n`,
};
};
Expand Down
12 changes: 7 additions & 5 deletions src/types/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ export type ClientHeaderBuilder = (params: {
provideIn: boolean | 'root' | 'any';
}) => string;

export type ClientFooterBuilder = (options: {
operationNames?: string[];
noFunction?: boolean;
title: string;
}) => string;
export type ClientFooterBuilder = (
params: {
noFunction?: boolean | undefined;
operationNames: string[];
title?: string;
},
) => string;

export type ClientTitleBuilder = (title: string) => string;

Expand Down

0 comments on commit e9f073a

Please sign in to comment.