Skip to content

Commit

Permalink
fix(clients): handling typescript option exactOptionalPropertyTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Jul 2, 2022
1 parent 39cd404 commit 460d3bb
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/core/generators/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const generateImplementation = (
formData,
formUrlEncoded,
}: GeneratorVerbOptions,
{ route }: GeneratorOptions,
{ route, context }: GeneratorOptions,
) => {
const isRequestOptions = override?.requestOptions !== false;
const isFormData = override?.formData !== false;
Expand All @@ -140,6 +140,9 @@ const generateImplementation = (
);

if (mutator) {
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;

const mutatorConfig = generateMutatorConfig({
route,
body,
Expand All @@ -150,6 +153,7 @@ const generateImplementation = (
isFormUrlEncoded,
hasSignal: false,
isBodyVerb,
isExactOptionalPropertyTypes,
});

const requestOptions = isRequestOptions
Expand Down
4 changes: 4 additions & 0 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ const generateAxiosImplementation = (
const isBodyVerb = VERBS_WITH_BODY.includes(verb);

if (mutator) {
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;

const mutatorConfig = generateMutatorConfig({
route,
body,
Expand All @@ -82,6 +85,7 @@ const generateAxiosImplementation = (
isFormUrlEncoded,
isBodyVerb,
hasSignal: true,
isExactOptionalPropertyTypes,
});

const requestOptions = isRequestOptions
Expand Down
24 changes: 16 additions & 8 deletions src/core/generators/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@ export const generateBodyMutatorConfig = (

export const generateQueryParamsAxiosConfig = (
response: GetterResponse,
queryParams?: GeneratorSchema,
queryParams?: GetterQueryParam,
) => {
if (!queryParams && !response.isBlob) {
return '';
}

let value = ',';
let value = '';

if (queryParams) {
value += '\n params,';
value += ',\n params';
}

if (response.isBlob) {
value += `\n responseType: 'blob',`;
value += `,\n responseType: 'blob'`;
}

return value;
Expand All @@ -166,6 +166,7 @@ export const generateMutatorConfig = ({
isFormUrlEncoded,
isBodyVerb,
hasSignal,
isExactOptionalPropertyTypes,
}: {
route: string;
body: GetterBody;
Expand All @@ -176,23 +177,30 @@ export const generateMutatorConfig = ({
isFormUrlEncoded: boolean;
isBodyVerb: boolean;
hasSignal: boolean;
isExactOptionalPropertyTypes: boolean;
}) => {
const bodyOptions = isBodyVerb
? generateBodyMutatorConfig(body, isFormData, isFormUrlEncoded)
: '';

const queryParamsOptions = generateQueryParamsAxiosConfig(
response,
queryParams?.schema,
queryParams,
);

const headerOptions = body.contentType
? `,\n headers: {'Content-Type': '${body.contentType}'}`
: '';

return `{url: \`${route}\`, method: '${verb}'${
!isBodyVerb && hasSignal ? ', signal' : ''
}${headerOptions}${bodyOptions}${queryParamsOptions}\n }`;
return `{url: \`${route}\`, method: '${verb}'${headerOptions}${bodyOptions}${queryParamsOptions}${
!isBodyVerb && hasSignal
? `, ${
isExactOptionalPropertyTypes
? '...(signal ? { signal }: {})'
: 'signal'
}`
: ''
}\n }`;
};

export const generateMutatorRequestOptions = (
Expand Down
99 changes: 77 additions & 22 deletions src/core/generators/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ const generateQueryRequestFunction = (
const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(
context.tsconfig,
);
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);

const bodyForm = generateFormDataAndUrlEncodedFunction({
Expand All @@ -168,6 +170,7 @@ const generateQueryRequestFunction = (
isFormUrlEncoded,
isBodyVerb,
hasSignal: true,
isExactOptionalPropertyTypes,
});

const propsImplementation =
Expand Down Expand Up @@ -364,6 +367,58 @@ const generateQueryReturnType = ({
}
};

const getQueryOptions = ({
isRequestOptions,
mutator,
isExactOptionalPropertyTypes,
}: {
isRequestOptions: boolean;
mutator?: GeneratorMutator;
isExactOptionalPropertyTypes: boolean;
}) => {
if (!isRequestOptions) {
return '';
}

if (!mutator) {
return `{ ${
isExactOptionalPropertyTypes ? '...(signal ? { signal } : {})' : 'signal'
}, ...axiosOptions }`;
}

if (mutator.hasSecondArg) {
return 'requestOptions, signal';
}

return 'signal';
};

const getHookOptions = ({
isRequestOptions,
mutator,
}: {
isRequestOptions: boolean;
mutator?: GeneratorMutator;
}) => {
if (!isRequestOptions) {
return '';
}

let value = 'const {query: queryOptions';

if (!mutator) {
value += ', axios: axiosOptions';
}

if (mutator?.hasSecondArg) {
value += ', request: requestOptions';
}

value += '} = options ?? {}';

return value;
};

const generateQueryImplementation = ({
queryOption: { name, queryParam, options, type },
operationName,
Expand All @@ -376,6 +431,7 @@ const generateQueryImplementation = ({
isRequestOptions,
response,
outputClient,
isExactOptionalPropertyTypes,
}: {
queryOption: {
name: string;
Expand All @@ -393,6 +449,7 @@ const generateQueryImplementation = ({
response: GetterResponse;
mutator?: GeneratorMutator;
outputClient: OutputClient | OutputClientFunc;
isExactOptionalPropertyTypes: boolean;
}) => {
const httpFunctionProps = queryParam
? props
Expand Down Expand Up @@ -423,6 +480,17 @@ const generateQueryImplementation = ({
? `ReturnType<typeof use${pascal(operationName)}Hook>`
: `typeof ${operationName}`;

const queryOptions = getQueryOptions({
isRequestOptions,
isExactOptionalPropertyTypes,
mutator,
});

const hookOptions = getHookOptions({
isRequestOptions,
mutator,
});

return `
export type ${pascal(
name,
Expand All @@ -441,17 +509,7 @@ export const ${camel(
},
)}\n ): ${returnType} & { queryKey: QueryKey } => {
${
isRequestOptions
? `const {query: queryOptions${
!mutator
? `, axios: axiosOptions`
: mutator.hasSecondArg
? ', request: requestOptions'
: ''
}} = options ?? {}`
: ''
}
${hookOptions}
const queryKey = queryOptions?.queryKey ?? ${queryKeyFnName}(${properties});
Expand All @@ -469,15 +527,9 @@ export const ${camel(
queryParam && props.some(({ type }) => type === 'queryParam')
? `{ signal, pageParam }`
: '{ signal }'
}) => ${operationName}(${httpFunctionProps}${httpFunctionProps ? ', ' : ''}${
isRequestOptions
? !mutator
? `{ signal, ...axiosOptions }`
: mutator.hasSecondArg
? 'requestOptions, signal'
: 'signal'
: ''
});
}) => ${operationName}(${httpFunctionProps}${
httpFunctionProps ? ', ' : ''
}${queryOptions});
const query = ${camel(`use-${type}`)}<Awaited<ReturnType<${
mutator?.isHook
Expand All @@ -488,7 +540,7 @@ export const ${camel(
options,
type,
})}) as ${returnType} & { queryKey: QueryKey };
query.queryKey = queryKey;
return query;
Expand All @@ -508,12 +560,14 @@ const generateQueryHook = (
response,
operationId,
}: GeneratorVerbOptions,
{ route, override: { operations = {} } }: GeneratorOptions,
{ route, override: { operations = {} }, context }: GeneratorOptions,
outputClient: OutputClient | OutputClientFunc,
) => {
const query = override?.query;
const isRequestOptions = override?.requestOptions !== false;
const operationQueryOptions = operations[operationId]?.query;
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;

if (
verb === Verbs.GET ||
Expand Down Expand Up @@ -570,6 +624,7 @@ const generateQueryHook = (
isRequestOptions,
response,
outputClient,
isExactOptionalPropertyTypes,
}),
'',
)}
Expand Down
4 changes: 4 additions & 0 deletions src/core/generators/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ const generateSwrRequestFunction = (
});

if (mutator) {
const isExactOptionalPropertyTypes =
!!context.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;

const mutatorConfig = generateMutatorConfig({
route,
body,
Expand All @@ -98,6 +101,7 @@ const generateSwrRequestFunction = (
isFormUrlEncoded,
hasSignal: false,
isBodyVerb,
isExactOptionalPropertyTypes,
});

const requestOptions = isRequestOptions
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export interface Tsconfig {
compilerOptions?: {
esModuleInterop?: boolean;
allowSyntheticDefaultImports?: boolean;
exactOptionalPropertyTypes?: boolean;
paths?: Record<string, string[]>;
};
}
Expand Down

1 comment on commit 460d3bb

@vercel
Copy link

@vercel vercel bot commented on 460d3bb Jul 2, 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.