Skip to content

Commit

Permalink
(fix) support abort signal param for POST requests (#1688)
Browse files Browse the repository at this point in the history
* fix/refactoring #1447

extract is body verb

* fix/#1447

has signal added or condition verb equals post

* fix: formatting and removed unused imports

---------

Co-authored-by: andrej kastritsa <wengergruppe@gmail.ru>
  • Loading branch information
gsaandy and andrej kastritsa authored Nov 3, 2024
1 parent 1852bae commit c1851f1
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 26 deletions.
2 changes: 0 additions & 2 deletions packages/angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ const generateImplementation = (
const isFormUrlEncoded = override?.formUrlEncoded !== false;
const isExactOptionalPropertyTypes =
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);
const bodyForm = generateFormDataAndUrlEncodedFunction({
formData,
formUrlEncoded,
Expand Down Expand Up @@ -165,7 +164,6 @@ const generateImplementation = (
isFormData,
isFormUrlEncoded,
hasSignal: false,
isBodyVerb,
isExactOptionalPropertyTypes,
});

Expand Down
2 changes: 0 additions & 2 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const generateAxiosImplementation = (
isFormData,
isFormUrlEncoded,
});
const isBodyVerb = VERBS_WITH_BODY.includes(verb);

if (mutator) {
const mutatorConfig = generateMutatorConfig({
Expand All @@ -106,7 +105,6 @@ const generateAxiosImplementation = (
verb,
isFormData,
isFormUrlEncoded,
isBodyVerb,
hasSignal: false,
isExactOptionalPropertyTypes,
});
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/generators/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { VERBS_WITH_BODY } from '../constants';
import {
GeneratorMutator,
GeneratorSchema,
Expand All @@ -8,7 +7,7 @@ import {
ParamsSerializerOptions,
Verbs,
} from '../types';
import { isObject, stringify } from '../utils';
import { getIsBodyVerb, isObject, stringify } from '../utils';

export const generateBodyOptions = (
body: GetterBody,
Expand Down Expand Up @@ -156,8 +155,7 @@ export const generateOptions = ({
paramsSerializer?: GeneratorMutator;
paramsSerializerOptions?: ParamsSerializerOptions;
}) => {
const isBodyVerb = VERBS_WITH_BODY.includes(verb);
const bodyOptions = isBodyVerb
const bodyOptions = getIsBodyVerb(verb)
? generateBodyOptions(body, isFormData, isFormUrlEncoded)
: '';

Expand Down Expand Up @@ -190,7 +188,7 @@ export const generateOptions = ({
}

return `\n \`${route}\`,${
isBodyVerb ? bodyOptions || 'undefined,' : ''
getIsBodyVerb(verb) ? bodyOptions || 'undefined,' : ''
}${axiosOptions === 'options' ? axiosOptions : options}\n `;
};

Expand Down Expand Up @@ -249,7 +247,6 @@ export const generateMutatorConfig = ({
verb,
isFormData,
isFormUrlEncoded,
isBodyVerb,
hasSignal,
isExactOptionalPropertyTypes,
isVue,
Expand All @@ -262,12 +259,11 @@ export const generateMutatorConfig = ({
verb: Verbs;
isFormData: boolean;
isFormUrlEncoded: boolean;
isBodyVerb: boolean;
hasSignal: boolean;
isExactOptionalPropertyTypes: boolean;
isVue?: boolean;
}) => {
const bodyOptions = isBodyVerb
const bodyOptions = getIsBodyVerb(verb)
? generateBodyMutatorConfig(body, isFormData, isFormUrlEncoded)
: '';

Expand All @@ -286,7 +282,7 @@ export const generateMutatorConfig = ({
: '';

return `{url: \`${route}\`, method: '${verb.toUpperCase()}'${headerOptions}${bodyOptions}${queryParamsOptions}${
!isBodyVerb && hasSignal
hasSignal
? `, ${
isExactOptionalPropertyTypes
? '...(signal ? { signal }: {})'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './sort';
export * from './string';
export * from './tsconfig';
export * from './validator';
export * from './is-body-verb';
4 changes: 4 additions & 0 deletions packages/core/src/utils/is-body-verb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Verbs } from '../types';
import { VERBS_WITH_BODY } from '../constants';

export const getIsBodyVerb = (verb: Verbs) => VERBS_WITH_BODY.includes(verb);
17 changes: 7 additions & 10 deletions packages/query/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
GeneratorDependency,
GeneratorOptions,
OutputHttpClient,
VERBS_WITH_BODY,
generateFormDataAndUrlEncodedFunction,
generateMutatorConfig,
generateMutatorRequestOptions,
Expand All @@ -18,12 +17,11 @@ import {
import { generateRequestFunction as generateFetchRequestFunction } from '@orval/fetch';

import {
isVue,
getHasSignal,
makeRouteSafe,
vueUnRefParams,
vueWrapTypeWithMaybeRef,
} from './utils';
import exp from 'constants';

export const AXIOS_DEPENDENCIES: GeneratorDependency[] = [
{
Expand Down Expand Up @@ -86,11 +84,13 @@ export const generateAxiosRequestFunction = (
const isRequestOptions = override.requestOptions !== false;
const isFormData = override.formData !== false;
const isFormUrlEncoded = override.formUrlEncoded !== false;
const hasSignal = !!override.query.signal;
const hasSignal = getHasSignal({
overrideQuerySignal: override.query.signal,
verb,
});

const isExactOptionalPropertyTypes =
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);

const bodyForm = generateFormDataAndUrlEncodedFunction({
formData,
Expand All @@ -110,7 +110,6 @@ export const generateAxiosRequestFunction = (
verb,
isFormData,
isFormUrlEncoded,
isBodyVerb,
hasSignal,
isExactOptionalPropertyTypes,
isVue,
Expand Down Expand Up @@ -144,9 +143,7 @@ export const generateAxiosRequestFunction = (
isRequestOptions && mutator.hasSecondArg
? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<ReturnType<typeof ${mutator.name}>>,`
: ''
}${
!isBodyVerb && hasSignal ? 'signal?: AbortSignal\n' : ''
}) => {${bodyForm}
}${hasSignal ? 'signal?: AbortSignal\n' : ''}) => {${bodyForm}
return ${operationName}(
${mutatorConfig},
${requestOptions});
Expand All @@ -159,7 +156,7 @@ export const generateAxiosRequestFunction = (
isRequestOptions && mutator.hasSecondArg
? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<typeof ${mutator.name}>,`
: ''
}${!isBodyVerb && hasSignal ? 'signal?: AbortSignal\n' : ''}) => {
}${hasSignal ? 'signal?: AbortSignal\n' : ''}) => {
${isVue ? vueUnRefParams(props) : ''}
${bodyForm}
return ${mutator.name}<${response.definition.success || 'unknown'}>(
Expand Down
6 changes: 5 additions & 1 deletion packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
getQueryOptions,
} from './client';
import {
getHasSignal,
isVue,
normalizeQueryOptions,
vueUnRefParams,
Expand Down Expand Up @@ -1225,7 +1226,10 @@ const generateQueryHook = async (
outputClient,
httpClient,
isExactOptionalPropertyTypes,
hasSignal: !!query.signal,
hasSignal: getHasSignal({
overrideQuerySignal: override.query.signal,
verb,
}),
queryOptionsMutator,
queryKeyMutator,
route,
Expand Down
10 changes: 10 additions & 0 deletions packages/query/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
GetterProps,
GetterPropType,
TEMPLATE_TAG_REGEX,
getIsBodyVerb,
Verbs,
} from '@orval/core';
import chalk from 'chalk';

Expand Down Expand Up @@ -130,3 +132,11 @@ export const makeRouteSafe = (route: string): string =>

export const isVue = (client: OutputClient | OutputClientFunc) =>
OutputClient.VUE_QUERY === client;

export const getHasSignal = ({
overrideQuerySignal = false,
verb,
}: {
verb: Verbs;
overrideQuerySignal?: boolean;
}) => overrideQuerySignal && (!getIsBodyVerb(verb) || verb === Verbs.POST);
2 changes: 0 additions & 2 deletions packages/swr/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const generateAxiosRequestFunction = (
const isFormUrlEncoded = override?.formUrlEncoded !== false;
const isExactOptionalPropertyTypes =
!!context.output.tsconfig?.compilerOptions?.exactOptionalPropertyTypes;
const isBodyVerb = VERBS_WITH_BODY.includes(verb);
const isSyntheticDefaultImportsAllowed = isSyntheticDefaultImportsAllow(
context.output.tsconfig,
);
Expand All @@ -93,7 +92,6 @@ const generateAxiosRequestFunction = (
isFormData,
isFormUrlEncoded,
hasSignal: false,
isBodyVerb,
isExactOptionalPropertyTypes,
});

Expand Down

0 comments on commit c1851f1

Please sign in to comment.