This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type helpers for queries/mutations (#27)
- Loading branch information
Showing
9 changed files
with
287 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@next-fetch/react-query": patch | ||
"@next-fetch/swr": patch | ||
--- | ||
|
||
Add type helpers to get input/output of hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { test, describe } from "vitest"; | ||
import type { | ||
inputOf as rqInputOf, | ||
outputOf as rqOutputOf, | ||
} from "@next-fetch/react-query"; | ||
import type { | ||
useListPeopleWith as ReactQueryUseListPeopleWith, | ||
usePerson as ReactQueryUsePerson, | ||
useAllPeople as ReactQueryUseAllPeople, | ||
} from "../../pages/api/rq/people.rq"; | ||
import type { | ||
inputOf as swrInputOf, | ||
outputOf as swrOutputOf, | ||
} from "@next-fetch/swr"; | ||
import type { | ||
useListPeopleWith as SWRUseListPeopleWith, | ||
usePerson as SWRUsePerson, | ||
useAllPeople as SWRUseAllPeople, | ||
} from "../../pages/api/people.swr"; | ||
|
||
type IsAny<T> = 0 extends 1 & T ? true : false; | ||
type SameType<T, U> = true extends IsAny<T> | IsAny<U> | ||
? false | ||
: [T] extends [U] | ||
? [U] extends [T] | ||
? true | ||
: false | ||
: false; | ||
function expectSameType<T, U>(_v: SameType<T, U>) {} | ||
|
||
describe("helpers", () => { | ||
test("expectSameType", () => { | ||
expectSameType<string, boolean>(false); | ||
expectSameType<string, string>(true); | ||
expectSameType<unknown, string>(false); | ||
|
||
// Check that `any` fails the conditional as well | ||
expectSameType<any, string>(false); | ||
expectSameType<string, any>(false); | ||
|
||
// @ts-expect-error this should fail | ||
expectSameType<string, string>(false); | ||
}); | ||
}); | ||
|
||
describe("react query", () => { | ||
test("mutation", () => { | ||
type Input = rqInputOf<typeof ReactQueryUseListPeopleWith>; | ||
type Output = rqOutputOf<typeof ReactQueryUseListPeopleWith>; | ||
expectSameType<Input, { name: string }>(true); | ||
expectSameType<Output, string[]>(true); | ||
}); | ||
|
||
test("query with no args", () => { | ||
type Input = rqInputOf<typeof ReactQueryUseAllPeople>; | ||
type Output = rqOutputOf<typeof ReactQueryUseAllPeople>; | ||
expectSameType<Input, void>(true); | ||
expectSameType<Output, string>(true); | ||
}); | ||
|
||
test("query with args", () => { | ||
type Input = rqInputOf<typeof ReactQueryUsePerson>; | ||
type Output = rqOutputOf<typeof ReactQueryUsePerson>; | ||
expectSameType<Input, { name: string }>(true); | ||
expectSameType<Output, string>(true); | ||
}); | ||
}); | ||
|
||
describe("swr", () => { | ||
test("mutation", () => { | ||
type Input = swrInputOf<typeof SWRUseListPeopleWith>; | ||
type Output = swrOutputOf<typeof SWRUseListPeopleWith>; | ||
expectSameType<Input, { name: string }>(true); | ||
expectSameType<Output, string[]>(true); | ||
}); | ||
|
||
test("query with no args", () => { | ||
type Input = swrInputOf<typeof SWRUseAllPeople>; | ||
type Output = swrOutputOf<typeof SWRUseAllPeople>; | ||
expectSameType<Input, void>(true); | ||
expectSameType<Output, string>(true); | ||
}); | ||
|
||
test("query with args", () => { | ||
type Input = swrInputOf<typeof SWRUsePerson>; | ||
type Output = swrOutputOf<typeof SWRUsePerson>; | ||
expectSameType<Input, { name: string }>(true); | ||
expectSameType<Output, string>(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig.json", | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"incremental": false | ||
}, | ||
"include": ["./tests/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from "vitest/config"; | ||
export default defineConfig({ | ||
test: { | ||
exclude: ["**/node_modules/**", "**/dist/**", "**/e2e/**"], | ||
}, | ||
}); |
Oops, something went wrong.
5b90c45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-swr-endpoints-example-app – ./packages/example-app
next-swr-endpoints-example-app-vercel-labs.vercel.app
next-swr-endpoints-example-app-git-main-vercel-labs.vercel.app
next-swr-endpoints-example-app.vercel.app
5b90c45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-fetch – ./packages/docs
next-fetch-pi.vercel.app
next-fetch-git-main-vercel-labs.vercel.app
next-fetch-vercel-labs.vercel.app