openapi-fetch
Advanced tools
Comparing version 0.1.4 to 0.2.0
# openapi-fetch | ||
## 0.2.0 | ||
### Minor Changes | ||
- 97c8757: Add custom fetch option (#51). Thanks, [@hd-o](https://github.com/hd-o)! | ||
## 0.1.4 | ||
@@ -4,0 +10,0 @@ |
@@ -5,2 +5,4 @@ /** options for each client instance */ | ||
baseUrl?: string; | ||
/** custom fetch (defaults to globalThis.fetch) */ | ||
fetch?: typeof fetch; | ||
} | ||
@@ -76,3 +78,3 @@ export interface BaseParams { | ||
}; | ||
export default function createClient<Paths extends {}>(options?: ClientOptions): { | ||
export default function createClient<Paths extends {}>(clientOptions?: ClientOptions): { | ||
/** Call a GET endpoint */ | ||
@@ -79,0 +81,0 @@ 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>>; |
@@ -5,6 +5,7 @@ // settings | ||
}; | ||
export default function createClient(options) { | ||
export default function createClient(clientOptions = {}) { | ||
const { fetch = globalThis.fetch, ...options } = clientOptions; | ||
const defaultHeaders = new Headers({ | ||
...DEFAULT_HEADERS, | ||
...(options?.headers ?? {}), | ||
...(options.headers ?? {}), | ||
}); | ||
@@ -14,3 +15,3 @@ async function coreFetch(url, fetchOptions) { | ||
// URL | ||
let finalURL = `${options?.baseUrl ?? ''}${url}`; | ||
let finalURL = `${options.baseUrl ?? ''}${url}`; | ||
if (params.path) { | ||
@@ -17,0 +18,0 @@ for (const [k, v] of Object.entries(params.path)) |
@@ -5,2 +5,4 @@ /** options for each client instance */ | ||
baseUrl?: string; | ||
/** custom fetch (defaults to globalThis.fetch) */ | ||
fetch?: typeof fetch; | ||
} | ||
@@ -76,3 +78,3 @@ export interface BaseParams { | ||
}; | ||
export default function createClient<Paths extends {}>(options?: ClientOptions): { | ||
export default function createClient<Paths extends {}>(clientOptions?: ClientOptions): { | ||
/** Call a GET endpoint */ | ||
@@ -79,0 +81,0 @@ 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>>; |
@@ -1,1 +0,1 @@ | ||
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 p=new Headers(P),l=new Headers(u);for(let[a,o]of l.entries())o==null?p.delete(a):p.set(a,o);let n=await fetch(y,{redirect:"follow",...i,...O,headers:p,body:typeof d=="string"?d:JSON.stringify(d)}),h=n.status===204||n.headers.get("Content-Length")==="0"?{}:await n.json();return n.ok?{data:h,response:n}:{error:h,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}; | ||
var F={"Content-Type":"application/json"};function m(P={}){let{fetch:c=globalThis.fetch,...i}=P,u=new Headers({...F,...i.headers??{}});async function s(e,t){let{headers:O,body:p,params:r={},querySerializer:l=a=>new URLSearchParams(a).toString(),...x}=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+=`?${l(r.query)}`);let d=new Headers(u),f=new Headers(O);for(let[a,o]of f.entries())o==null?d.delete(a):d.set(a,o);let n=await c(y,{redirect:"follow",...i,...x,headers:d,body:typeof p=="string"?p:JSON.stringify(p)}),h=n.status===204||n.headers.get("Content-Length")==="0"?{}:await n.json();return n.ok?{data:h,response:n}:{error:h,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{m as default}; |
{ | ||
"name": "openapi-fetch", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -47,3 +47,3 @@ "name": "Drew Powers", | ||
"del-cli": "^5.0.0", | ||
"esbuild": "^0.17.18", | ||
"esbuild": "^0.17.19", | ||
"nanostores": "^0.8.1", | ||
@@ -53,5 +53,5 @@ "openapi-typescript": "^6.2.4", | ||
"typescript": "^5.0.4", | ||
"vitest": "^0.31.0", | ||
"vitest": "^0.31.1", | ||
"vitest-fetch-mock": "^0.2.2" | ||
} | ||
} |
@@ -209,2 +209,3 @@ # 🎾 openapi-fetch | ||
| `baseUrl` | `string` | Prefix all fetch URLs with this option (e.g. `"https://myapi.dev/v1/"`). | | ||
| `fetch` | `fetch` | Fetch function used for requests (defaults to `globalThis.fetch`) | | ||
| (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal` …) ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)) | | ||
@@ -211,0 +212,0 @@ |
@@ -183,2 +183,16 @@ import { atom, computed } from 'nanostores'; | ||
}); | ||
it('accepts a custom fetch function', async () => { | ||
const data = { works: true }; | ||
const client = createClient<paths>({ | ||
fetch: async () => | ||
Promise.resolve({ | ||
headers: new Headers(), | ||
json: async () => data, | ||
status: 200, | ||
ok: true, | ||
} as Response), | ||
}); | ||
expect((await client.get('/self', {})).data).toBe(data); | ||
}); | ||
}); | ||
@@ -185,0 +199,0 @@ |
@@ -10,2 +10,4 @@ // settings | ||
baseUrl?: string; | ||
/** custom fetch (defaults to globalThis.fetch) */ | ||
fetch?: typeof fetch; | ||
} | ||
@@ -55,6 +57,8 @@ export interface BaseParams { | ||
export default function createClient<Paths extends {}>(options?: ClientOptions) { | ||
export default function createClient<Paths extends {}>(clientOptions: ClientOptions = {}) { | ||
const { fetch = globalThis.fetch, ...options } = clientOptions; | ||
const defaultHeaders = new Headers({ | ||
...DEFAULT_HEADERS, | ||
...(options?.headers ?? {}), | ||
...(options.headers ?? {}), | ||
}); | ||
@@ -66,3 +70,3 @@ | ||
// URL | ||
let finalURL = `${options?.baseUrl ?? ''}${url as string}`; | ||
let finalURL = `${options.baseUrl ?? ''}${url as string}`; | ||
if ((params as any).path) { | ||
@@ -69,0 +73,0 @@ for (const [k, v] of Object.entries((params as any).path)) finalURL = finalURL.replace(`{${k}}`, encodeURIComponent(String(v))); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63035
854
231
1