openapi-typescript-fetch
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -57,3 +57,3 @@ declare module 'openapi-typescript-fetch/fetcher' { | ||
} : never; | ||
type _OpReturnType<T> = 200 extends keyof T ? T[200] : 201 extends keyof T ? T[201] : 'default' extends keyof T ? T['default'] : unknown; | ||
type _OpReturnType<T> = 200 extends keyof T ? T[200] : 201 extends keyof T ? T[201] : 204 extends keyof T ? T[204] : 'default' extends keyof T ? T['default'] : unknown; | ||
export type OpReturnType<OP> = _OpReturnType<OpResponseTypes<OP>>; | ||
@@ -64,7 +64,7 @@ type _OpDefaultReturnType<T> = 'default' extends keyof T ? T['default'] : unknown; | ||
type _OpErrorType<T> = { | ||
[S in Exclude<keyof T, 200 | 201>]: { | ||
[S in Exclude<keyof T, 200 | 201 | 204>]: { | ||
status: S extends 'default' ? typeof never : S; | ||
data: T[S]; | ||
}; | ||
}[Exclude<keyof T, 200 | 201>]; | ||
}[Exclude<keyof T, 200 | 201 | 204>]; | ||
type Coalesce<T, D> = [T] extends [never] ? D : T; | ||
@@ -79,3 +79,5 @@ export type OpErrorType<OP> = Coalesce<_OpErrorType<OpResponseTypes<OP>>, { | ||
export type Fetch = (url: string, init: CustomRequestInit) => Promise<ApiResponse>; | ||
export type _TypedFetch<OP> = (arg: OpArgType<OP>, init?: RequestInit) => Promise<ApiResponse<OpReturnType<OP>>>; | ||
export type _TypedFetch<OP> = (arg: OpArgType<OP>, init?: RequestInit & { | ||
baseUrl?: string; | ||
}) => Promise<ApiResponse<OpReturnType<OP>>>; | ||
export type TypedFetch<OP> = _TypedFetch<OP> & { | ||
@@ -99,3 +101,3 @@ Error: new (error: ApiError) => ApiError & { | ||
export type FetchConfig = { | ||
baseUrl?: string; | ||
baseUrl?: string | (() => string); | ||
init?: RequestInit; | ||
@@ -102,0 +104,0 @@ use?: Middleware[]; |
@@ -194,3 +194,3 @@ // src/types.ts | ||
(payload, init) => fetchUrl({ | ||
baseUrl: baseUrl || "", | ||
baseUrl: init?.baseUrl ?? (typeof baseUrl === "function" ? baseUrl() : baseUrl), | ||
path, | ||
@@ -197,0 +197,0 @@ method, |
{ | ||
"name": "openapi-typescript-fetch", | ||
"description": "A typed fetch client for openapi-typescript", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">= 12.0.0", |
@@ -39,4 +39,2 @@ [![version(scoped)](https://img.shields.io/npm/v/openapi-typescript-fetch.svg)](https://www.npmjs.com/package/openapi-typescript-fetch) | ||
```ts | ||
import 'whatwg-fetch' | ||
import { Fetcher } from 'openapi-typescript-fetch' | ||
@@ -129,26 +127,2 @@ | ||
### Server Side Usage | ||
This library can be used server side with [node-fetch](https://www.npmjs.com/package/node-fetch) | ||
Node CommonJS setup | ||
```ts | ||
// install node-fetch v2 | ||
npm install node-fetch@2 | ||
npm install @types/node-fetch@2 | ||
// fetch-polyfill.ts | ||
import fetch, { Headers, Request, Response } from 'node-fetch' | ||
if (!globalThis.fetch) { | ||
globalThis.fetch = fetch as any | ||
globalThis.Headers = Headers as any | ||
globalThis.Request = Request as any | ||
globalThis.Response = Response as any | ||
} | ||
// index.ts | ||
import './fetch-polyfill' | ||
``` | ||
### Utility Types | ||
@@ -195,2 +169,21 @@ | ||
### Changing the baseUrl at runtime | ||
The baseUrl can be configured with a function that returns the url at runtime | ||
```ts | ||
fetcher.configure({ | ||
baseUrl: () => getBaseUrl(...) | ||
}) | ||
``` | ||
It can also be overriden per method invocation | ||
```ts | ||
await findPetsByStatus( | ||
{ status: ['available', 'pending'] }, | ||
{ baseUrl: "https://staging.petstore.swagger.io/v2" } | ||
) | ||
``` | ||
Happy fetching! 👍 |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25766
596
2
186