Skip to content

Commit e3781fd

Browse files
committed
Update openapi-react-query library naming, tags and dependencies
1 parent 92dd0ef commit e3781fd

17 files changed

+130
-102
lines changed

packages/openapi-tanstack-query/package.json renamed to packages/openapi-react-query/package.json

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "openapi-tanstack-query",
3-
"description": "Fast, type-safe fetch client for your OpenAPI schema. Only 5 kb (min). Works with React, Vue, Svelte, or vanilla JS.",
2+
"name": "openapi-react-query",
3+
"description": "Fast, type-safe @tanstack/react-query client to work with your OpenAPI schema.",
44
"version": "0.0.1",
55
"author": {
6-
"name": "Drew Powers",
7-
"email": "[email protected]"
6+
"name": "Martin Paucot",
7+
"email": "[email protected]"
88
},
99
"license": "MIT",
1010
"type": "module",
@@ -28,7 +28,7 @@
2828
"repository": {
2929
"type": "git",
3030
"url": "https://github.com/openapi-ts/openapi-typescript",
31-
"directory": "packages/openapi-fetch"
31+
"directory": "packages/openapi-react-query"
3232
},
3333
"bugs": {
3434
"url": "https://github.com/openapi-ts/openapi-typescript/issues"
@@ -43,8 +43,8 @@
4343
"typescript",
4444
"fetch",
4545
"react",
46-
"vue",
47-
"svelte"
46+
"react-query",
47+
"tanstack"
4848
],
4949
"scripts": {
5050
"build": "pnpm run build:clean && pnpm run build:js && pnpm run build:js-min && pnpm run build:cjs",
@@ -62,16 +62,12 @@
6262
"version": "pnpm run prepare && pnpm run build"
6363
},
6464
"dependencies": {
65-
"@tanstack/react-query": "^5.45.1",
66-
"nanoid": "^5.0.7",
67-
"openapi-fetch": "workspace:^",
6865
"openapi-typescript-helpers": "workspace:^"
6966
},
7067
"devDependencies": {
7168
"@testing-library/react": "^16.0.0",
7269
"@types/react": "18.3.1",
7370
"@vitejs/plugin-react": "^4.3.1",
74-
"axios": "^1.7.2",
7571
"del-cli": "^5.1.0",
7672
"esbuild": "^0.20.2",
7773
"execa": "^8.0.1",
@@ -80,8 +76,11 @@
8076
"openapi-typescript-codegen": "^0.25.0",
8177
"openapi-typescript-fetch": "^2.0.0",
8278
"react": "^18.3.1",
83-
"superagent": "^9.0.2",
8479
"typescript": "^5.4.5",
8580
"vitest": "^1.6.0"
81+
},
82+
"peerDependencies": {
83+
"@tanstack/react-query": "^5.45.1",
84+
"openapi-fetch": "workspace:^"
8685
}
8786
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import type {
2+
UseMutationResult,
3+
UseQueryOptions,
4+
UseQueryResult,
5+
} from "@tanstack/react-query";
6+
import type { MaybeOptionalInit, Client, FetchResponse } from "openapi-fetch";
7+
import type {
8+
ErrorResponse,
9+
FilterKeys,
10+
HasRequiredKeys,
11+
HttpMethod,
12+
MediaType,
13+
OperationRequestBodyContent,
14+
PathsWithMethod,
15+
ResponseObjectMap,
16+
SuccessResponse,
17+
} from "openapi-typescript-helpers";
18+
19+
type Prettify<T> = {
20+
[K in keyof T]: T[K];
21+
} & {};
22+
23+
export type UseQueryMethod<
24+
Paths extends Record<string, Record<HttpMethod, {}>>,
25+
Media extends MediaType,
26+
> = <
27+
Method extends HttpMethod,
28+
Path extends PathsWithMethod<Paths, Method>,
29+
Init extends MaybeOptionalInit<Paths[Path], Method>,
30+
Response extends FetchResponse<Paths[Path][Method], Init, Media>,
31+
Options extends Omit<
32+
UseQueryOptions<
33+
NonNullable<Response["data"]>,
34+
NonNullable<Response["error"]>
35+
>,
36+
"queryKey" | "queryFn"
37+
>,
38+
>(
39+
method: Method,
40+
url: Path,
41+
...init: HasRequiredKeys<Init> extends never
42+
? ?[(Init & { [key: string]: unknown })?, Options?] // note: the arbitrary [key: string]: addition MUST happen here after all the inference happens (otherwise TS can’t infer if it’s required or not)
43+
: [Init & { [key: string]: unknown }, Options?]
44+
) => UseQueryResult<
45+
NonNullable<Response["data"]>,
46+
NonNullable<Response["error"]>
47+
>;
48+
49+
export type UseMutationMethod<
50+
Paths extends Record<string, Record<HttpMethod, {}>>,
51+
Media extends MediaType,
52+
> = <
53+
Method extends HttpMethod,
54+
Path extends PathsWithMethod<Paths, Method>,
55+
Init extends MaybeOptionalInit<Paths[Path], Method>,
56+
Response extends FetchResponse<Paths[Path][Method], Init, Media>,
57+
>(
58+
method: Method,
59+
url: Path,
60+
test: void,
61+
) => UseMutationResult<
62+
NonNullable<Response["data"]>,
63+
NonNullable<Response["error"]>,
64+
Init & { [key: string]: unknown }
65+
>;
66+
67+
export interface OpenapiQueryClient<
68+
Paths extends {},
69+
Media extends MediaType = MediaType,
70+
> {
71+
useQuery: UseQueryMethod<Paths, Media>;
72+
useMutation: UseMutationMethod<Paths, Media>;
73+
}
74+
75+
export default function createClient<
76+
Paths extends {},
77+
Media extends MediaType = MediaType,
78+
>(
79+
client: Client<Paths, Media>,
80+
): {
81+
useQuery: UseQueryMethod<Paths, Media>;
82+
useMutation: UseMutationMethod<Paths, Media>;
83+
};

packages/openapi-tanstack-query/src/index.js renamed to packages/openapi-react-query/src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export default function createClient(client) {
1818
},
1919
...options,
2020
}),
21-
useMutation: (method, url, init, options) =>
21+
useMutation: (method, url, options) =>
2222
useMutation({
23-
mutationKey: [method, url, init],
24-
mutationFn: async () => {
23+
mutationKey: [method, url],
24+
mutationFn: async (init) => {
2525
const { data, error } = await client[method.toUpperCase()](url, init);
2626
if (error) {
2727
throw error;

packages/openapi-tanstack-query/test/index.test.tsx renamed to packages/openapi-react-query/test/index.test.tsx

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { afterAll, beforeAll, describe, expect, it } from "vitest";
2-
import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.js";
2+
import {
3+
server,
4+
baseUrl,
5+
useMockRequestHandler,
6+
} from "./fixtures/mock-server.js";
37
import type { paths } from "./fixtures/api.js";
48
import createClient from "../src/index.js";
59
import createFetchClient from "openapi-fetch";
@@ -10,7 +14,9 @@ import React, { type ReactNode } from "react";
1014
beforeAll(() => {
1115
server.listen({
1216
onUnhandledRequest: (request) => {
13-
throw new Error(`No request handler found for ${request.method} ${request.url}`);
17+
throw new Error(
18+
`No request handler found for ${request.method} ${request.url}`,
19+
);
1420
},
1521
});
1622
});
@@ -46,7 +52,7 @@ describe("client", () => {
4652
body: { message: "OK" },
4753
});
4854

49-
const { result } = renderHook(() => client.useQuery("get", "/self"), {
55+
const { result } = renderHook(() => client.useQuery("get", "/self", {}), {
5056
wrapper,
5157
});
5258

@@ -64,17 +70,26 @@ describe("client", () => {
6470
useMockRequestHandler({
6571
baseUrl,
6672
method: "get",
67-
path: "/self",
73+
path: "/tag/*",
6874
status: 200,
6975
body: { message: "OK" },
7076
});
7177

72-
const { result } = renderHook(() => client.useMutation("get", "/self"), {
73-
wrapper,
78+
const { result } = renderHook(
79+
() => client.useMutation("get", "/tag/{name}"),
80+
{
81+
wrapper,
82+
},
83+
);
84+
85+
result.current.mutate({
86+
params: {
87+
path: {
88+
name: "test",
89+
},
90+
},
7491
});
7592

76-
result.current.mutate({});
77-
7893
await waitFor(() => expect(result.current.isSuccess).toBe(true));
7994

8095
expect(result.current.data).toEqual({ message: "OK" });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["src"]
4+
}

packages/openapi-tanstack-query/tsconfig.json renamed to packages/openapi-react-query/tsconfig.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"lib": ["ESNext", "DOM"],
5+
"module": "ES2022",
6+
"moduleResolution": "Node",
7+
"outDir": "dist",
58
"skipLibCheck": true,
69
"sourceRoot": ".",
7-
"outDir": "dist",
8-
"types": ["vitest/globals"],
9-
"jsx": "react"
10+
"jsx": "react",
11+
"types": ["vitest/globals"]
1012
},
1113
"include": ["scripts", "src", "test", "*.ts"],
1214
"exclude": ["node_modules"]

packages/openapi-tanstack-query/src/index.d.ts

-51
This file was deleted.

packages/openapi-tanstack-query/tsconfig.build.json

-8
This file was deleted.

packages/openapi-tanstack-query/tsconfig.examples.json

Whitespace-only changes.

pnpm-lock.yaml

+1-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)