Skip to content

Commit

Permalink
feat: override return error and context
Browse files Browse the repository at this point in the history
  • Loading branch information
7nohe committed Oct 31, 2023
1 parent de598ad commit ea0db30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
32 changes: 24 additions & 8 deletions src/createUseMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createUseMutation = (

const responseDataType = ts.factory.createTypeParameterDeclaration(
undefined,
"ResponseData",
"TData",
undefined,
awaitedResponseDataType
);
Expand Down Expand Up @@ -61,7 +61,21 @@ export const createUseMutation = (
undefined,
ts.factory.createArrowFunction(
undefined,
ts.factory.createNodeArray([responseDataType]),
ts.factory.createNodeArray([
responseDataType,
ts.factory.createTypeParameterDeclaration(
undefined,
"TError",
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
),
ts.factory.createTypeParameterDeclaration(
undefined,
"TContext",
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
),
]),
[
ts.factory.createParameterDeclaration(
undefined,
Expand Down Expand Up @@ -152,7 +166,7 @@ export const createUseMutation = (
]),
]
),
// Omit<UseMutationResult<ResponseData, unknown, { requestBody: NewPet; }, unknown>, 'data'> & { data: ResponseData };
// Omit<UseMutationResult<Awaited<ReturnType<typeof myClass.myMethod>>, TError, params, TContext>, 'data'> & { data: TData };
ts.factory.createIntersectionTypeNode([
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Omit"),
Expand All @@ -161,12 +175,14 @@ export const createUseMutation = (
ts.factory.createIdentifier("UseMutationResult"),
[
awaitedResponseDataType,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("TError"),
undefined
),
methodParameters,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("TContext"),
undefined
),
]
),
Expand All @@ -181,7 +197,7 @@ export const createUseMutation = (
ts.factory.createIdentifier("data"),
undefined,
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("ResponseData"),
ts.factory.createIdentifier("TData"),
undefined
)
),
Expand Down
19 changes: 14 additions & 5 deletions src/createUseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const createUseQuery = (

const responseDataType = ts.factory.createTypeParameterDeclaration(
undefined,
"ResponseData",
"TData",
undefined,
awaitedResponseDataType
);
Expand Down Expand Up @@ -118,6 +118,14 @@ export const createUseQuery = (
)
),
responseDataType,
ts.factory.createTypeParameterDeclaration(
undefined,
"TError",
undefined,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
)
),
]),
[
...requestParam,
Expand Down Expand Up @@ -236,7 +244,7 @@ export const createUseQuery = (
]),
]
),
// Omit<UseQueryResult<ResponseData, unknown>, 'data'> & { data: ResponseData };
// Omit<UseQueryResult<Awaited<ReturnType<typeof myClass.myMethod>>, TError>, 'data'> & { data: TData };
ts.factory.createIntersectionTypeNode([
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Omit"),
Expand All @@ -245,8 +253,9 @@ export const createUseQuery = (
ts.factory.createIdentifier("UseQueryResult"),
[
awaitedResponseDataType,
ts.factory.createKeywordTypeNode(
ts.SyntaxKind.UnknownKeyword
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("TError"),
undefined
),
]
),
Expand All @@ -261,7 +270,7 @@ export const createUseQuery = (
ts.factory.createIdentifier("data"),
undefined,
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("ResponseData"),
ts.factory.createIdentifier("TData"),
undefined
)
),
Expand Down

0 comments on commit ea0db30

Please sign in to comment.