Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MethodResponse for openapi-react-query #1945

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType =
useMutation: UseMutationMethod<Paths, Media>;
}

export type MethodResponse<
CreatedClient extends OpenapiQueryClient<any, any>,
Method extends HttpMethod,
Path extends CreatedClient extends OpenapiQueryClient<infer Paths, infer _Media>
? PathsWithMethod<Paths, Method>
: never,
Options = object,
> = CreatedClient extends OpenapiQueryClient<infer Paths extends { [key: string]: any }, infer Media extends MediaType>
? NonNullable<FetchResponse<Paths[Path][Method], Options, Media>["data"]>
: never;

// TODO: Add the ability to bring queryClient as argument
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(
client: FetchClient<Paths, Media>,
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.js";
import type { paths } from "./fixtures/api.js";
import createClient from "../src/index.js";
import createClient, { type MethodResponse } from "../src/index.js";
import createFetchClient from "openapi-fetch";
import { fireEvent, render, renderHook, screen, waitFor, act } from "@testing-library/react";
import { QueryClient, QueryClientProvider, useQueries } from "@tanstack/react-query";
Expand Down Expand Up @@ -212,6 +212,7 @@ describe("client", () => {

const { data, error } = result.current;

expectTypeOf(data).toEqualTypeOf<MethodResponse<typeof client, "get", "/string-array"> | undefined>();
expectTypeOf(data).toEqualTypeOf<string[] | undefined>();
expectTypeOf(error).toEqualTypeOf<{ code: number; message: string } | null>();
});
Expand Down