Skip to content

Commit

Permalink
fix(client): query add return type (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax authored Nov 25, 2021
1 parent 9440969 commit 326ed10
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core/generators/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const generateClient = (
verbsOptions,
async (acc, verbOption) => {
const { client: generatorClient } = getGeneratorClient(outputClient);
const client = generatorClient(verbOption, options);
const client = generatorClient(verbOption, options, outputClient);
const msw = await generateMock(verbOption, options);

return {
Expand Down
8 changes: 6 additions & 2 deletions src/core/generators/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export const addDependency = ({
specsName: Record<string, string>;
hasSchemaDir: boolean;
}) => {
const toAdds = exports.filter((e) => implementation.includes(e.name));
const toAdds = exports.filter((e) =>
implementation.includes(e.alias || e.name),
);

if (!toAdds.length) {
return undefined;
Expand All @@ -111,7 +113,9 @@ export const addDependency = ({
const defaultDep = deps.find((e) => e.default);

const depsString = uniq(
deps.filter((e) => !e.default).map(({ name }) => name),
deps
.filter((e) => !e.default)
.map(({ name, alias }) => (alias ? `${name} as ${alias}` : name)),
).join(',\n ');

return `import ${!values ? 'type ' : ''}${
Expand Down
33 changes: 30 additions & 3 deletions src/core/generators/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ts-ignore
const esprima = require('esprima');
import omitBy from 'lodash.omitby';
import { Verbs } from '../../types';
import { OutputClient, OutputClientFunc, Verbs } from '../../types';
import {
GeneratorDependency,
GeneratorMutator,
Expand Down Expand Up @@ -50,6 +50,9 @@ const SVELTE_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'UseMutationOptions' },
{ name: 'QueryFunction' },
{ name: 'MutationFunction' },
{ name: 'UseQueryStoreResult' },
{ name: 'UseInfiniteQueryStoreResult' },
{ name: 'QueryKey' },
],
dependency: '@sveltestack/svelte-query',
},
Expand All @@ -69,6 +72,9 @@ const REACT_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'UseMutationOptions' },
{ name: 'QueryFunction' },
{ name: 'MutationFunction' },
{ name: 'UseQueryResult' },
{ name: 'UseInfiniteQueryResult' },
{ name: 'QueryKey' },
],
dependency: 'react-query',
},
Expand All @@ -92,6 +98,9 @@ const VUE_QUERY_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'UseMutationOptions' },
{ name: 'QueryFunction' },
{ name: 'MutationFunction' },
{ name: 'UseQueryResult' },
{ name: 'UseInfiniteQueryResult' },
{ name: 'QueryKey' },
],
dependency: 'vue-query/types',
},
Expand Down Expand Up @@ -308,6 +317,7 @@ const generateQueryImplementation = ({
mutator,
isRequestOptions,
response,
outputClient,
}: {
queryOption: {
name: string;
Expand All @@ -324,6 +334,7 @@ const generateQueryImplementation = ({
props: GetterProps;
response: GetterResponse;
mutator?: GeneratorMutator;
outputClient: OutputClient | OutputClientFunc;
}) => {
const isMutatorHook =
mutator?.name.startsWith('use') && !mutator.mutatorFn.length;
Expand All @@ -337,6 +348,15 @@ const generateQueryImplementation = ({

const isMutatorHasSecondArg = !!mutator && mutator.mutatorFn.length > 1;

const returnType =
outputClient !== OutputClient.SVELTE_QUERY
? ` Use${pascal(type)}Result<TData, TError>`
: `Use${pascal(type)}StoreResult<AsyncReturnType<${
isMutatorHook
? `ReturnType<typeof use${pascal(operationName)}Hook>`
: `typeof ${operationName}`
}>, TError, TData, QueryKey>`;

return `
export const ${camel(`use-${name}`)} = <TData = AsyncReturnType<${
isMutatorHook
Expand All @@ -351,7 +371,7 @@ export const ${camel(`use-${name}`)} = <TData = AsyncReturnType<${
isRequestOptions,
isMutatorHasSecondArg,
type,
})}\n ) => {
})}\n ): ${returnType} & { queryKey: QueryKey } => {
${
isRequestOptions
Expand Down Expand Up @@ -422,6 +442,7 @@ const generateQueryHook = (
operationId,
}: GeneratorVerbOptions,
{ route, override: { operations = {} } }: GeneratorOptions,
outputClient: OutputClient | OutputClientFunc,
) => {
const query = override?.query;
const isRequestOptions = override?.requestOptions !== false;
Expand Down Expand Up @@ -481,6 +502,7 @@ const generateQueryHook = (
mutator,
isRequestOptions,
response,
outputClient,
}),
'',
)}
Expand Down Expand Up @@ -586,13 +608,18 @@ export const generateQueryFooter = () => '';
export const generateQuery = (
verbOptions: GeneratorVerbOptions,
options: GeneratorOptions,
outputClient: OutputClient | OutputClientFunc,
) => {
const imports = generateVerbImports(verbOptions);
const functionImplementation = generateQueryRequestFunction(
verbOptions,
options,
);
const hookImplementation = generateQueryHook(verbOptions, options);
const hookImplementation = generateQueryHook(
verbOptions,
options,
outputClient,
);

return {
implementation: `${functionImplementation}\n\n${hookImplementation}`,
Expand Down
2 changes: 2 additions & 0 deletions src/types/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NormalizedOperationOptions,
NormalizedOverrideOutput,
OutputClient,
OutputClientFunc,
Verbs,
} from './index';

Expand Down Expand Up @@ -125,6 +126,7 @@ export type GeneratorMutator = {
export type ClientBuilder = (
verbOptions: GeneratorVerbOptions,
options: GeneratorOptions,
outputClient: OutputClient | OutputClientFunc,
) => GeneratorClient;

export type ClientHeaderBuilder = (params: {
Expand Down

1 comment on commit 326ed10

@vercel
Copy link

@vercel vercel bot commented on 326ed10 Nov 25, 2021

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.