Skip to content
Open
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
41 changes: 26 additions & 15 deletions packages/openapi-hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type Paths = {
[key: string]: {
[key: string]: any;
};
}
// A lightweight description of the `paths` object shape emitted by
// `openapi-typescript`. We intentionally do not require an index signature so
// that the generated interface can be used directly.
type PathsRecord = Record<string, Record<string, any>>;


type HTTPMethod =
| 'get'
Expand All @@ -14,11 +14,15 @@ type HTTPMethod =
| 'patch'
| 'trace';

export type PathMethods<paths extends Paths, TPath extends keyof paths> = {
[TMethod in HTTPMethod]: paths[TPath][TMethod] extends undefined
? never
: TMethod;
}[HTTPMethod];
export type PathMethods<paths, TPath extends keyof paths> = paths[TPath] extends Record<string, any>
? {
[TMethod in HTTPMethod]: TMethod extends keyof paths[TPath]
? paths[TPath][TMethod] extends undefined
? never
: TMethod
: never;
}[HTTPMethod]
: never;

type AnyRequestBody = {
content: Record<string, any>;
Expand Down Expand Up @@ -134,7 +138,10 @@ const convertBody = (
}
};

const decodeResponse = async (response, responseContentType) => {
const decodeResponse = async (
response: Response,
responseContentType: string | null
) => {
switch (responseContentType) {
// eslint-disable-next-line unicorn/no-null
case null:
Expand Down Expand Up @@ -162,7 +169,7 @@ const decodeResponse = async (response, responseContentType) => {
}
};

export const createFetch = <paths extends Paths>(options?: OpenApiHookOptions) => {
export const createFetch = <paths>(options?: OpenApiHookOptions) => {
const { baseUrl = window.location.toString(), headers: defaultHeaders, onError } = options ?? {};

/**
Expand Down Expand Up @@ -254,9 +261,13 @@ export const createFetch = <paths extends Paths>(options?: OpenApiHookOptions) =
TOptions extends TRoute['parameters'] & ApiRequestBody<TRoute['requestBody']> & {
fetchOptions?: RequestInit;
},
TRoute extends AnyRoute = paths[TPath][TMethod] extends AnyRoute
? paths[TPath][TMethod]
: never,
TRoute extends AnyRoute = paths[TPath] extends Record<string, any>
? TMethod extends keyof paths[TPath]
? paths[TPath][TMethod] extends AnyRoute
? paths[TPath][TMethod]
: never
: never
: never,
>(
path: TPath,
method: TMethod,
Expand Down