Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-fetch

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-fetch - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

6

CHANGELOG.md
# openapi-fetch
## 0.1.1
### Patch Changes
- 5d1fb7d: Fix bad HTTP method lookup causing polymorphsim
## 0.1.0

@@ -4,0 +10,0 @@

58

dist/index.d.ts

@@ -11,2 +11,12 @@ /** options for each client instance */

}
export type PathItemObject = {
[M in HttpMethod]: OperationObject;
} & {
parameters?: any;
};
export interface OperationObject {
parameters: any;
requestBody: any;
responses: any;
}
export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace';

@@ -16,3 +26,3 @@ export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207;

/** Get a union of paths which have method */
export type PathsWith<Paths extends {}, PathnameMethod extends HttpMethod> = {
export type PathsWith<Paths extends Record<string, PathItemObject>, PathnameMethod extends HttpMethod> = {
[Pathname in keyof Paths]: Paths[Pathname] extends {

@@ -28,25 +38,27 @@ [K in PathnameMethod]: any;

export type JSONLike = `${string}json${string}`;
export type Params<OperationObj> = OperationObj extends {
export type Params<O> = O extends {
parameters: any;
} ? {
params: OperationObj['parameters'];
params: NonNullable<O['parameters']>;
} : BaseParams;
export type RequestBodyObj<OperationObj> = FilterKeys<OperationObj, 'requestBody'>;
export type RequestBodyContent<OperationObj> = undefined extends RequestBodyObj<OperationObj> ? FilterKeys<NonNullable<RequestBodyObj<OperationObj>>, 'content'> | undefined : FilterKeys<RequestBodyObj<OperationObj>, 'content'>;
export type RequestBodyJSON<OperationObj> = FilterKeys<RequestBodyContent<OperationObj>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<OperationObj>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<OperationObj>, JSONLike>;
export type RequestBody<OperationObj> = undefined extends RequestBodyJSON<OperationObj> ? {
body?: RequestBodyJSON<OperationObj>;
export type RequestBodyObj<O> = O extends {
requestBody: any;
} ? O['requestBody'] : never;
export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, 'content'> | undefined : FilterKeys<RequestBodyObj<O>, 'content'>;
export type RequestBodyJSON<O> = FilterKeys<RequestBodyContent<O>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<O>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<O>, JSONLike>;
export type RequestBody<O> = undefined extends RequestBodyJSON<O> ? {
body?: RequestBodyJSON<O>;
} : {
body: RequestBodyJSON<OperationObj>;
body: RequestBodyJSON<O>;
};
export type QuerySerializer<OperationObj> = {
querySerializer?: (query: OperationObj extends {
export type QuerySerializer<O> = {
querySerializer?: (query: O extends {
parameters: {
query: any;
};
} ? OperationObj['parameters']['query'] : Record<string, unknown>) => string;
} ? O['parameters']['query'] : Record<string, unknown>) => string;
};
export type FetchOptions<OperationObj> = Params<OperationObj> & RequestBody<OperationObj> & Omit<RequestInit, 'body'> & QuerySerializer<OperationObj>;
export type Success<OperationObj> = FilterKeys<FilterKeys<OperationObj, OkStatus>, 'content'>;
export type Error<OperationObj> = FilterKeys<FilterKeys<OperationObj, ErrorStatus>, 'content'>;
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'> & QuerySerializer<T>;
export type Success<O> = FilterKeys<FilterKeys<O, OkStatus>, 'content'>;
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, 'content'>;
export type FetchResponse<T> = {

@@ -67,18 +79,18 @@ data: T extends {

/** Call a GET endpoint */
get<Pathname extends PathsWith<Paths, "get">>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], "get">>): Promise<FetchResponse<Paths[Pathname][keyof Paths[Pathname]]>>;
get<P extends PathsWith<Paths, "get">>(url: P, init: FetchOptions<FilterKeys<Paths[P], "get">>): Promise<FetchResponse<"get" extends infer T ? T extends "get" ? T extends keyof Paths[P] ? Paths[P][T] : unknown : never : never>>;
/** Call a PUT endpoint */
put<Pathname_1 extends PathsWith<Paths, "put">>(url: Pathname_1, init: FetchOptions<FilterKeys<Paths[Pathname_1], "put">>): Promise<FetchResponse<Paths[Pathname_1][keyof Paths[Pathname_1]]>>;
put<P_1 extends PathsWith<Paths, "put">>(url: P_1, init: FetchOptions<FilterKeys<Paths[P_1], "put">>): Promise<FetchResponse<"put" extends infer T_1 ? T_1 extends "put" ? T_1 extends keyof Paths[P_1] ? Paths[P_1][T_1] : unknown : never : never>>;
/** Call a POST endpoint */
post<Pathname_2 extends PathsWith<Paths, "post">>(url: Pathname_2, init: FetchOptions<FilterKeys<Paths[Pathname_2], "post">>): Promise<FetchResponse<Paths[Pathname_2][keyof Paths[Pathname_2]]>>;
post<P_2 extends PathsWith<Paths, "post">>(url: P_2, init: FetchOptions<FilterKeys<Paths[P_2], "post">>): Promise<FetchResponse<"put" extends infer T_2 ? T_2 extends "put" ? T_2 extends keyof Paths[P_2] ? Paths[P_2][T_2] : unknown : never : never>>;
/** Call a DELETE endpoint */
del<Pathname_3 extends PathsWith<Paths, "delete">>(url: Pathname_3, init: FetchOptions<FilterKeys<Paths[Pathname_3], "delete">>): Promise<FetchResponse<Paths[Pathname_3][keyof Paths[Pathname_3]]>>;
del<P_3 extends PathsWith<Paths, "delete">>(url: P_3, init: FetchOptions<FilterKeys<Paths[P_3], "delete">>): Promise<FetchResponse<"delete" extends infer T_3 ? T_3 extends "delete" ? T_3 extends keyof Paths[P_3] ? Paths[P_3][T_3] : unknown : never : never>>;
/** Call a OPTIONS endpoint */
options<Pathname_4 extends PathsWith<Paths, "options">>(url: Pathname_4, init: FetchOptions<FilterKeys<Paths[Pathname_4], "options">>): Promise<FetchResponse<Paths[Pathname_4][keyof Paths[Pathname_4]]>>;
options<P_4 extends PathsWith<Paths, "options">>(url: P_4, init: FetchOptions<FilterKeys<Paths[P_4], "options">>): Promise<FetchResponse<"delete" extends infer T_4 ? T_4 extends "delete" ? T_4 extends keyof Paths[P_4] ? Paths[P_4][T_4] : unknown : never : never>>;
/** Call a HEAD endpoint */
head<Pathname_5 extends PathsWith<Paths, "head">>(url: Pathname_5, init: FetchOptions<FilterKeys<Paths[Pathname_5], "head">>): Promise<FetchResponse<Paths[Pathname_5][keyof Paths[Pathname_5]]>>;
head<P_5 extends PathsWith<Paths, "head">>(url: P_5, init: FetchOptions<FilterKeys<Paths[P_5], "head">>): Promise<FetchResponse<"head" extends infer T_5 ? T_5 extends "head" ? T_5 extends keyof Paths[P_5] ? Paths[P_5][T_5] : unknown : never : never>>;
/** Call a PATCH endpoint */
patch<Pathname_6 extends PathsWith<Paths, "patch">>(url: Pathname_6, init: FetchOptions<FilterKeys<Paths[Pathname_6], "patch">>): Promise<FetchResponse<Paths[Pathname_6][keyof Paths[Pathname_6]]>>;
patch<P_6 extends PathsWith<Paths, "patch">>(url: P_6, init: FetchOptions<FilterKeys<Paths[P_6], "patch">>): Promise<FetchResponse<"patch" extends infer T_6 ? T_6 extends "patch" ? T_6 extends keyof Paths[P_6] ? Paths[P_6][T_6] : unknown : never : never>>;
/** Call a TRACE endpoint */
trace<Pathname_7 extends PathsWith<Paths, "trace">>(url: Pathname_7, init: FetchOptions<FilterKeys<Paths[Pathname_7], "trace">>): Promise<FetchResponse<Paths[Pathname_7][keyof Paths[Pathname_7]]>>;
trace<P_7 extends PathsWith<Paths, "trace">>(url: P_7, init: FetchOptions<FilterKeys<Paths[P_7], "trace">>): Promise<FetchResponse<"trace" extends infer T_7 ? T_7 extends "trace" ? T_7 extends keyof Paths[P_7] ? Paths[P_7][T_7] : unknown : never : never>>;
};
export {};

@@ -11,2 +11,12 @@ /** options for each client instance */

}
export type PathItemObject = {
[M in HttpMethod]: OperationObject;
} & {
parameters?: any;
};
export interface OperationObject {
parameters: any;
requestBody: any;
responses: any;
}
export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace';

@@ -16,3 +26,3 @@ export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207;

/** Get a union of paths which have method */
export type PathsWith<Paths extends {}, PathnameMethod extends HttpMethod> = {
export type PathsWith<Paths extends Record<string, PathItemObject>, PathnameMethod extends HttpMethod> = {
[Pathname in keyof Paths]: Paths[Pathname] extends {

@@ -28,25 +38,27 @@ [K in PathnameMethod]: any;

export type JSONLike = `${string}json${string}`;
export type Params<OperationObj> = OperationObj extends {
export type Params<O> = O extends {
parameters: any;
} ? {
params: OperationObj['parameters'];
params: NonNullable<O['parameters']>;
} : BaseParams;
export type RequestBodyObj<OperationObj> = FilterKeys<OperationObj, 'requestBody'>;
export type RequestBodyContent<OperationObj> = undefined extends RequestBodyObj<OperationObj> ? FilterKeys<NonNullable<RequestBodyObj<OperationObj>>, 'content'> | undefined : FilterKeys<RequestBodyObj<OperationObj>, 'content'>;
export type RequestBodyJSON<OperationObj> = FilterKeys<RequestBodyContent<OperationObj>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<OperationObj>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<OperationObj>, JSONLike>;
export type RequestBody<OperationObj> = undefined extends RequestBodyJSON<OperationObj> ? {
body?: RequestBodyJSON<OperationObj>;
export type RequestBodyObj<O> = O extends {
requestBody: any;
} ? O['requestBody'] : never;
export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, 'content'> | undefined : FilterKeys<RequestBodyObj<O>, 'content'>;
export type RequestBodyJSON<O> = FilterKeys<RequestBodyContent<O>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<O>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<O>, JSONLike>;
export type RequestBody<O> = undefined extends RequestBodyJSON<O> ? {
body?: RequestBodyJSON<O>;
} : {
body: RequestBodyJSON<OperationObj>;
body: RequestBodyJSON<O>;
};
export type QuerySerializer<OperationObj> = {
querySerializer?: (query: OperationObj extends {
export type QuerySerializer<O> = {
querySerializer?: (query: O extends {
parameters: {
query: any;
};
} ? OperationObj['parameters']['query'] : Record<string, unknown>) => string;
} ? O['parameters']['query'] : Record<string, unknown>) => string;
};
export type FetchOptions<OperationObj> = Params<OperationObj> & RequestBody<OperationObj> & Omit<RequestInit, 'body'> & QuerySerializer<OperationObj>;
export type Success<OperationObj> = FilterKeys<FilterKeys<OperationObj, OkStatus>, 'content'>;
export type Error<OperationObj> = FilterKeys<FilterKeys<OperationObj, ErrorStatus>, 'content'>;
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'> & QuerySerializer<T>;
export type Success<O> = FilterKeys<FilterKeys<O, OkStatus>, 'content'>;
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, 'content'>;
export type FetchResponse<T> = {

@@ -67,18 +79,18 @@ data: T extends {

/** Call a GET endpoint */
get<Pathname extends PathsWith<Paths, "get">>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], "get">>): Promise<FetchResponse<Paths[Pathname][keyof Paths[Pathname]]>>;
get<P extends PathsWith<Paths, "get">>(url: P, init: FetchOptions<FilterKeys<Paths[P], "get">>): Promise<FetchResponse<"get" extends infer T ? T extends "get" ? T extends keyof Paths[P] ? Paths[P][T] : unknown : never : never>>;
/** Call a PUT endpoint */
put<Pathname_1 extends PathsWith<Paths, "put">>(url: Pathname_1, init: FetchOptions<FilterKeys<Paths[Pathname_1], "put">>): Promise<FetchResponse<Paths[Pathname_1][keyof Paths[Pathname_1]]>>;
put<P_1 extends PathsWith<Paths, "put">>(url: P_1, init: FetchOptions<FilterKeys<Paths[P_1], "put">>): Promise<FetchResponse<"put" extends infer T_1 ? T_1 extends "put" ? T_1 extends keyof Paths[P_1] ? Paths[P_1][T_1] : unknown : never : never>>;
/** Call a POST endpoint */
post<Pathname_2 extends PathsWith<Paths, "post">>(url: Pathname_2, init: FetchOptions<FilterKeys<Paths[Pathname_2], "post">>): Promise<FetchResponse<Paths[Pathname_2][keyof Paths[Pathname_2]]>>;
post<P_2 extends PathsWith<Paths, "post">>(url: P_2, init: FetchOptions<FilterKeys<Paths[P_2], "post">>): Promise<FetchResponse<"put" extends infer T_2 ? T_2 extends "put" ? T_2 extends keyof Paths[P_2] ? Paths[P_2][T_2] : unknown : never : never>>;
/** Call a DELETE endpoint */
del<Pathname_3 extends PathsWith<Paths, "delete">>(url: Pathname_3, init: FetchOptions<FilterKeys<Paths[Pathname_3], "delete">>): Promise<FetchResponse<Paths[Pathname_3][keyof Paths[Pathname_3]]>>;
del<P_3 extends PathsWith<Paths, "delete">>(url: P_3, init: FetchOptions<FilterKeys<Paths[P_3], "delete">>): Promise<FetchResponse<"delete" extends infer T_3 ? T_3 extends "delete" ? T_3 extends keyof Paths[P_3] ? Paths[P_3][T_3] : unknown : never : never>>;
/** Call a OPTIONS endpoint */
options<Pathname_4 extends PathsWith<Paths, "options">>(url: Pathname_4, init: FetchOptions<FilterKeys<Paths[Pathname_4], "options">>): Promise<FetchResponse<Paths[Pathname_4][keyof Paths[Pathname_4]]>>;
options<P_4 extends PathsWith<Paths, "options">>(url: P_4, init: FetchOptions<FilterKeys<Paths[P_4], "options">>): Promise<FetchResponse<"delete" extends infer T_4 ? T_4 extends "delete" ? T_4 extends keyof Paths[P_4] ? Paths[P_4][T_4] : unknown : never : never>>;
/** Call a HEAD endpoint */
head<Pathname_5 extends PathsWith<Paths, "head">>(url: Pathname_5, init: FetchOptions<FilterKeys<Paths[Pathname_5], "head">>): Promise<FetchResponse<Paths[Pathname_5][keyof Paths[Pathname_5]]>>;
head<P_5 extends PathsWith<Paths, "head">>(url: P_5, init: FetchOptions<FilterKeys<Paths[P_5], "head">>): Promise<FetchResponse<"head" extends infer T_5 ? T_5 extends "head" ? T_5 extends keyof Paths[P_5] ? Paths[P_5][T_5] : unknown : never : never>>;
/** Call a PATCH endpoint */
patch<Pathname_6 extends PathsWith<Paths, "patch">>(url: Pathname_6, init: FetchOptions<FilterKeys<Paths[Pathname_6], "patch">>): Promise<FetchResponse<Paths[Pathname_6][keyof Paths[Pathname_6]]>>;
patch<P_6 extends PathsWith<Paths, "patch">>(url: P_6, init: FetchOptions<FilterKeys<Paths[P_6], "patch">>): Promise<FetchResponse<"patch" extends infer T_6 ? T_6 extends "patch" ? T_6 extends keyof Paths[P_6] ? Paths[P_6][T_6] : unknown : never : never>>;
/** Call a TRACE endpoint */
trace<Pathname_7 extends PathsWith<Paths, "trace">>(url: Pathname_7, init: FetchOptions<FilterKeys<Paths[Pathname_7], "trace">>): Promise<FetchResponse<Paths[Pathname_7][keyof Paths[Pathname_7]]>>;
trace<P_7 extends PathsWith<Paths, "trace">>(url: P_7, init: FetchOptions<FilterKeys<Paths[P_7], "trace">>): Promise<FetchResponse<"trace" extends infer T_7 ? T_7 extends "trace" ? T_7 extends keyof Paths[P_7] ? Paths[P_7][T_7] : unknown : never : never>>;
};
export {};

@@ -1,1 +0,1 @@

var l={"Content-Type":"application/json"};function b(i){let O=new Headers({...l,...i?.headers??{}});async function a(e,t){let{headers:P,body:p,params:r={},querySerializer:u=s=>new URLSearchParams(s).toString(),...c}=t||{},h=`${i?.baseUrl??""}${e}`;if(r.path)for(let[s,o]of Object.entries(r.path))h=h.replace(`{${s}}`,encodeURIComponent(String(o)));r.query&&Object.keys(r.query).length&&(h+=`?${u(r.query)}`);let y=new Headers(O),m=new Headers(P);for(let[s,o]of m.entries())o==null?y.delete(s):y.set(s,o);let n=await fetch(h,{redirect:"follow",...i,...c,headers:y,body:typeof p=="string"?p:JSON.stringify(p)}),d=n.status===204||n.headers.get("Content-Length")==="0"?{}:await n.json();return n.ok?{data:d,response:n}:{error:d,response:n}}return{async get(e,t){return a(e,{...t,method:"GET"})},async put(e,t){return a(e,{...t,method:"PUT"})},async post(e,t){return a(e,{...t,method:"POST"})},async del(e,t){return a(e,{...t,method:"DELETE"})},async options(e,t){return a(e,{...t,method:"OPTIONS"})},async head(e,t){return a(e,{...t,method:"HEAD"})},async patch(e,t){return a(e,{...t,method:"PATCH"})},async trace(e,t){return a(e,{...t,method:"TRACE"})}}}export{b as default};
var x={"Content-Type":"application/json"};function F(i){let P=new Headers({...x,...i?.headers??{}});async function s(e,t){let{headers:u,body:d,params:r={},querySerializer:c=a=>new URLSearchParams(a).toString(),...O}=t||{},y=`${i?.baseUrl??""}${e}`;if(r.path)for(let[a,o]of Object.entries(r.path))y=y.replace(`{${a}}`,encodeURIComponent(String(o)));r.query&&Object.keys(r.query).length&&(y+=`?${c(r.query)}`);let h=new Headers(P),l=new Headers(u);for(let[a,o]of l.entries())o==null?h.delete(a):h.set(a,o);let n=await fetch(y,{redirect:"follow",...i,...O,headers:h,body:typeof d=="string"?d:JSON.stringify(d)}),p=n.status===204||n.headers.get("Content-Length")==="0"?{}:await n.json();return n.ok?{data:p,response:n}:{error:p,response:n}}return{async get(e,t){return s(e,{...t,method:"GET"})},async put(e,t){return s(e,{...t,method:"PUT"})},async post(e,t){return s(e,{...t,method:"POST"})},async del(e,t){return s(e,{...t,method:"DELETE"})},async options(e,t){return s(e,{...t,method:"OPTIONS"})},async head(e,t){return s(e,{...t,method:"HEAD"})},async patch(e,t){return s(e,{...t,method:"PATCH"})},async trace(e,t){return s(e,{...t,method:"TRACE"})}}}export{F as default};
{
"name": "openapi-fetch",
"version": "0.1.0",
"version": "0.1.1",
"author": {

@@ -52,6 +52,5 @@ "name": "Drew Powers",

"typescript": "^5.0.4",
"vite": "^4.3.3",
"vitest": "^0.30.1",
"vitest": "^0.31.0",
"vitest-fetch-mock": "^0.2.2"
}
}

@@ -19,3 +19,3 @@ import { atom, computed } from 'nanostores';

describe('createClient', () => {
describe('client', () => {
it('generates all proper functions', () => {

@@ -117,3 +117,5 @@ const client = createClient<paths>();

// (no error)
await client.put('/post', { body: { title: 'Foo', body: 'Bar', publish_date: new Date('2023-04-01T12:00:00Z').getTime() } });
await client.put('/post', {
body: { title: 'Foo', body: 'Bar', publish_date: new Date('2023-04-01T12:00:00Z').getTime() },
});
});

@@ -172,3 +174,3 @@

fetchMocker.mockResponseOnce(JSON.stringify({ email: 'user@user.com' }));
await client.get('/self', { headers: { 'Cache-Control': 'no-cache' } });
await client.get('/self', { params: {}, headers: { 'Cache-Control': 'no-cache' } });

@@ -238,2 +240,13 @@ // assert default headers were passed

// note: this was a previous bug in the type inference
it('handles array-type responses', async () => {
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ status: 200, body: '[]' }));
const { data } = await client.get('/posts', { params: {} });
if (!data) throw new Error('data empty');
// assert array type (and only array type) was inferred
expect(data.length).toBe(0);
});
it('escapes URLs properly', async () => {

@@ -291,2 +304,3 @@ const client = createClient<paths>();

const { data, error, response } = await client.put('/post', {
params: {},
body: {

@@ -315,2 +329,3 @@ title: 'New Post',

const { data, error, response } = await client.put('/comment', {
params: {},
body: {

@@ -317,0 +332,0 @@ message: 'My reply',

@@ -16,2 +16,9 @@ // settings

// const
export type PathItemObject = { [M in HttpMethod]: OperationObject } & { parameters?: any };
export interface OperationObject {
parameters: any;
requestBody: any; // note: "any" will get overridden in inference
responses: any;
}
export type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace';

@@ -23,3 +30,3 @@ export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207;

/** Get a union of paths which have method */
export type PathsWith<Paths extends {}, PathnameMethod extends HttpMethod> = {
export type PathsWith<Paths extends Record<string, PathItemObject>, PathnameMethod extends HttpMethod> = {
[Pathname in keyof Paths]: Paths[Pathname] extends { [K in PathnameMethod]: any } ? Pathname : never;

@@ -33,13 +40,11 @@ }[keyof Paths];

// fetch types
export type Params<OperationObj> = OperationObj extends { parameters: any } ? { params: OperationObj['parameters'] } : BaseParams;
export type RequestBodyObj<OperationObj> = FilterKeys<OperationObj, 'requestBody'>;
export type RequestBodyContent<OperationObj> = undefined extends RequestBodyObj<OperationObj> ? FilterKeys<NonNullable<RequestBodyObj<OperationObj>>, 'content'> | undefined : FilterKeys<RequestBodyObj<OperationObj>, 'content'>;
export type RequestBodyJSON<OperationObj> = FilterKeys<RequestBodyContent<OperationObj>, JSONLike> extends never
? FilterKeys<NonNullable<RequestBodyContent<OperationObj>>, JSONLike> | undefined
: FilterKeys<RequestBodyContent<OperationObj>, JSONLike>;
export type RequestBody<OperationObj> = undefined extends RequestBodyJSON<OperationObj> ? { body?: RequestBodyJSON<OperationObj> } : { body: RequestBodyJSON<OperationObj> };
export type QuerySerializer<OperationObj> = { querySerializer?: (query: OperationObj extends { parameters: { query: any } } ? OperationObj['parameters']['query'] : Record<string, unknown>) => string };
export type FetchOptions<OperationObj> = Params<OperationObj> & RequestBody<OperationObj> & Omit<RequestInit, 'body'> & QuerySerializer<OperationObj>;
export type Success<OperationObj> = FilterKeys<FilterKeys<OperationObj, OkStatus>, 'content'>;
export type Error<OperationObj> = FilterKeys<FilterKeys<OperationObj, ErrorStatus>, 'content'>;
export type Params<O> = O extends { parameters: any } ? { params: NonNullable<O['parameters']> } : BaseParams;
export type RequestBodyObj<O> = O extends { requestBody: any } ? O['requestBody'] : never;
export type RequestBodyContent<O> = undefined extends RequestBodyObj<O> ? FilterKeys<NonNullable<RequestBodyObj<O>>, 'content'> | undefined : FilterKeys<RequestBodyObj<O>, 'content'>;
export type RequestBodyJSON<O> = FilterKeys<RequestBodyContent<O>, JSONLike> extends never ? FilterKeys<NonNullable<RequestBodyContent<O>>, JSONLike> | undefined : FilterKeys<RequestBodyContent<O>, JSONLike>;
export type RequestBody<O> = undefined extends RequestBodyJSON<O> ? { body?: RequestBodyJSON<O> } : { body: RequestBodyJSON<O> };
export type QuerySerializer<O> = { querySerializer?: (query: O extends { parameters: { query: any } } ? O['parameters']['query'] : Record<string, unknown>) => string };
export type FetchOptions<T> = Params<T> & RequestBody<T> & Omit<RequestInit, 'body'> & QuerySerializer<T>;
export type Success<O> = FilterKeys<FilterKeys<O, OkStatus>, 'content'>;
export type Error<O> = FilterKeys<FilterKeys<O, ErrorStatus>, 'content'>;
export type FetchResponse<T> =

@@ -55,4 +60,4 @@ | { data: T extends { responses: any } ? NonNullable<FilterKeys<Success<T['responses']>, JSONLike>> : unknown; error?: never; response: Response }

async function coreFetch<Pathname extends keyof Paths, PathnameMethod extends keyof Paths[Pathname]>(url: Pathname, fetchOptions: FetchOptions<Paths[Pathname][PathnameMethod]>): Promise<FetchResponse<Paths[Pathname][PathnameMethod]>> {
let { headers, body: requestBody, params = {}, querySerializer = (q: QuerySerializer<Paths[Pathname][PathnameMethod]>) => new URLSearchParams(q as any).toString(), ...init } = fetchOptions || {};
async function coreFetch<P extends keyof Paths, M extends HttpMethod>(url: P, fetchOptions: FetchOptions<M extends keyof Paths[P] ? Paths[P][M] : never>): Promise<FetchResponse<M extends keyof Paths[P] ? Paths[P][M] : unknown>> {
let { headers, body: requestBody, params = {}, querySerializer = (q: QuerySerializer<M extends keyof Paths[P] ? Paths[P][M] : never>) => new URLSearchParams(q as any).toString(), ...init } = fetchOptions || {};

@@ -92,34 +97,34 @@ // URL

/** Call a GET endpoint */
async get<Pathname extends PathsWith<Paths, 'get'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'get'>>) {
return coreFetch(url, { ...init, method: 'GET' } as any);
async get<P extends PathsWith<Paths, 'get'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'get'>>) {
return coreFetch<P, 'get'>(url, { ...init, method: 'GET' } as any);
},
/** Call a PUT endpoint */
async put<Pathname extends PathsWith<Paths, 'put'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'put'>>) {
return coreFetch(url, { ...init, method: 'PUT' } as any);
async put<P extends PathsWith<Paths, 'put'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'put'>>) {
return coreFetch<P, 'put'>(url, { ...init, method: 'PUT' } as any);
},
/** Call a POST endpoint */
async post<Pathname extends PathsWith<Paths, 'post'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'post'>>) {
return coreFetch(url, { ...init, method: 'POST' } as any);
async post<P extends PathsWith<Paths, 'post'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'post'>>) {
return coreFetch<P, 'put'>(url, { ...init, method: 'POST' } as any);
},
/** Call a DELETE endpoint */
async del<Pathname extends PathsWith<Paths, 'delete'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'delete'>>) {
return coreFetch(url, { ...init, method: 'DELETE' } as any);
async del<P extends PathsWith<Paths, 'delete'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'delete'>>) {
return coreFetch<P, 'delete'>(url, { ...init, method: 'DELETE' } as any);
},
/** Call a OPTIONS endpoint */
async options<Pathname extends PathsWith<Paths, 'options'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'options'>>) {
return coreFetch(url, { ...init, method: 'OPTIONS' } as any);
async options<P extends PathsWith<Paths, 'options'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'options'>>) {
return coreFetch<P, 'delete'>(url, { ...init, method: 'OPTIONS' } as any);
},
/** Call a HEAD endpoint */
async head<Pathname extends PathsWith<Paths, 'head'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'head'>>) {
return coreFetch(url, { ...init, method: 'HEAD' } as any);
async head<P extends PathsWith<Paths, 'head'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'head'>>) {
return coreFetch<P, 'head'>(url, { ...init, method: 'HEAD' } as any);
},
/** Call a PATCH endpoint */
async patch<Pathname extends PathsWith<Paths, 'patch'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'patch'>>) {
return coreFetch(url, { ...init, method: 'PATCH' } as any);
async patch<P extends PathsWith<Paths, 'patch'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'patch'>>) {
return coreFetch<P, 'patch'>(url, { ...init, method: 'PATCH' } as any);
},
/** Call a TRACE endpoint */
async trace<Pathname extends PathsWith<Paths, 'trace'>>(url: Pathname, init: FetchOptions<FilterKeys<Paths[Pathname], 'trace'>>) {
return coreFetch(url, { ...init, method: 'TRACE' } as any);
async trace<P extends PathsWith<Paths, 'trace'>>(url: P, init: FetchOptions<FilterKeys<Paths[P], 'trace'>>) {
return coreFetch<P, 'trace'>(url, { ...init, method: 'TRACE' } as any);
},
};
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc