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

Guess the used content type when multiple content is defined #1600

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
71 changes: 41 additions & 30 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import type {
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */

/** Options for each client instance */
export interface ClientOptions extends Omit<RequestInit, "headers"> {
export interface ClientOptions<AcceptHeader extends MediaType = MediaType>
extends Omit<RequestInit, "headers"> {
/** set the common root URL for all API requests */
baseUrl?: string;
/** custom fetch (defaults to globalThis.fetch) */
Expand All @@ -24,10 +25,10 @@ export interface ClientOptions extends Omit<RequestInit, "headers"> {
querySerializer?: QuerySerializer<unknown> | QuerySerializerOptions;
/** global bodySerializer */
bodySerializer?: BodySerializer<unknown>;
headers?: HeadersOptions;
headers?: HeadersOptions<AcceptHeader>;
}

export type HeadersOptions =
export type HeadersOptions<AcceptHeader extends MediaType> = (
| HeadersInit
| Record<
string,
Expand All @@ -37,7 +38,8 @@ export type HeadersOptions =
| (string | number | boolean)[]
| null
| undefined
>;
>
) & { Accept?: AcceptHeader };

export type QuerySerializer<T> = (
query: T extends { parameters: any }
Expand Down Expand Up @@ -108,31 +110,38 @@ export type RequestBodyOption<T> =
? { body?: OperationRequestBodyContent<T> }
: { body: OperationRequestBodyContent<T> };

export type FetchOptions<T> = RequestOptions<T> &
export type FetchOptions<T, AcceptHeader extends MediaType> = RequestOptions<
T,
AcceptHeader
> &
Omit<RequestInit, "body" | "headers">;

export type FetchResponse<T, O, Media extends MediaType> =
export type FetchResponse<T, O, AcceptHeader extends MediaType> =
| {
data: ParseAsResponse<
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, Media>,
// FIXME: Here the "AcceptHeader" is MediaType and not the inferred one. Why?
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, AcceptHeader>,
O
>;
error?: never;
response: Response;
}
| {
data?: never;
error: FilterKeys<ErrorResponse<ResponseObjectMap<T>>, Media>;
error: FilterKeys<ErrorResponse<ResponseObjectMap<T>>, AcceptHeader>;
response: Response;
};

export type RequestOptions<T> = ParamsOption<T> &
export type RequestOptions<
T,
AcceptHeader extends MediaType,
> = ParamsOption<T> &
RequestBodyOption<T> & {
querySerializer?: QuerySerializer<T> | QuerySerializerOptions;
bodySerializer?: BodySerializer<T>;
parseAs?: ParseAs;
fetch?: ClientOptions["fetch"];
headers?: HeadersOptions;
headers?: HeadersOptions<AcceptHeader>;
};

export type MergedOptions<T = unknown> = {
Expand Down Expand Up @@ -172,45 +181,47 @@ export interface Middleware {
type PathMethods = Partial<Record<HttpMethod, {}>>;

/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
export type MaybeOptionalInit<P extends PathMethods, M extends keyof P> =
HasRequiredKeys<FetchOptions<FilterKeys<P, M>>> extends never
? [(FetchOptions<FilterKeys<P, M>> | undefined)?]
: [FetchOptions<FilterKeys<P, M>>];
export type MaybeOptionalInit<
P extends PathMethods,
M extends keyof P,
AcceptHeader extends MediaType,
> =
HasRequiredKeys<FetchOptions<FilterKeys<P, M>, AcceptHeader>> extends never
? [(FetchOptions<FilterKeys<P, M>, AcceptHeader> | undefined)?]
: [FetchOptions<FilterKeys<P, M>, AcceptHeader>];

export type ClientMethod<
Paths extends Record<string, PathMethods>,
M extends HttpMethod,
Media extends MediaType,
> = <
AcceptHeader extends MediaType,
P extends PathsWithMethod<Paths, M>,
I extends MaybeOptionalInit<Paths[P], M>,
I extends MaybeOptionalInit<Paths[P], M, AcceptHeader>,
>(
url: P,
...init: I
) => Promise<FetchResponse<Paths[P][M], I[0], Media>>;
) => Promise<FetchResponse<Paths[P][M], I[0], AcceptHeader>>;

export default function createClient<
Paths extends {},
Media extends MediaType = MediaType,
>(
// FIXME: Add a default AcceptHeader inferred from the ClientOptions
export default function createClient<Paths extends {}>(
clientOptions?: ClientOptions,
): {
/** Call a GET endpoint */
GET: ClientMethod<Paths, "get", Media>;
GET: ClientMethod<Paths, "get">;
/** Call a PUT endpoint */
PUT: ClientMethod<Paths, "put", Media>;
PUT: ClientMethod<Paths, "put">;
/** Call a POST endpoint */
POST: ClientMethod<Paths, "post", Media>;
POST: ClientMethod<Paths, "post">;
/** Call a DELETE endpoint */
DELETE: ClientMethod<Paths, "delete", Media>;
DELETE: ClientMethod<Paths, "delete">;
/** Call a OPTIONS endpoint */
OPTIONS: ClientMethod<Paths, "options", Media>;
OPTIONS: ClientMethod<Paths, "options">;
/** Call a HEAD endpoint */
HEAD: ClientMethod<Paths, "head", Media>;
HEAD: ClientMethod<Paths, "head">;
/** Call a PATCH endpoint */
PATCH: ClientMethod<Paths, "patch", Media>;
PATCH: ClientMethod<Paths, "patch">;
/** Call a TRACE endpoint */
TRACE: ClientMethod<Paths, "trace", Media>;
TRACE: ClientMethod<Paths, "trace">;
/** Register middleware */
use(...middleware: Middleware[]): void;
/** Unregister middleware */
Expand Down Expand Up @@ -285,7 +296,7 @@ export declare function createFinalURL<O>(

/** Merge headers a and b, with b taking priority */
export declare function mergeHeaders(
...allHeaders: (HeadersOptions | undefined)[]
...allHeaders: (HeadersOptions<MediaType> | undefined)[]
): Headers;

export {};
7 changes: 5 additions & 2 deletions packages/openapi-fetch/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ describe("client", () => {
});

it("use the selected content", async () => {
const client = createClient<paths, "application/ld+json">();
const client = createClient<paths>();
mockFetchOnce({
status: 200,
headers: { "Content-Type": "application/ld+json" },
Expand All @@ -1061,12 +1061,15 @@ describe("client", () => {
name: null,
}),
});
const { data } = await client.GET("/multiple-response-content", {
const query = client.GET("/multiple-response-content", {
headers: {
Accept: "application/ld+json",
},
});

const { data } = await query;
// ^?

data satisfies
| {
"@id": string;
Expand Down
Loading