@kubb/plugin-fetch 5.14.0-canary.20261109T134631 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,39 +1,21 @@
1
- import {
2
- createClientCore,
3
- getResponseContentType,
4
- resolveResponseType,
5
- type ResponseType,
6
- type ResponseTypeMap,
7
- type Transport,
8
- type TransportResult,
9
- } from './core.ts'
10
- import { isJsonMediaType } from './serializers.ts'
1
+ import { createClientCore, createLazyClient, toRequestInit } from './core.ts'
2
+ import { defaultConfig } from './config.ts'
11
3
  import * as core from './core.ts'
12
4
 
13
5
  export * from './core.ts'
14
6
 
15
- /** The request and response objects of this transport, carried on every result and error. */
7
+ /**
8
+ * The native request and response that every result and error carries.
9
+ */
16
10
  export type NativeRequest = Request
17
11
  export type NativeResponse = Response
18
12
 
19
13
  /**
20
- * Extra `fetch` init the transport spreads onto every `Request`, an escape hatch for fields the runtime does not set itself such as `cache`, `redirect`, and Next.js's `next`.
14
+ * Extra `fetch` init for a call or client, such as `cache`, `redirect`, or Next.js's `next`.
21
15
  */
22
- export type FetchOptions = RequestInit & { next?: Record<string, unknown> }
16
+ export type NativeOptions = RequestInit & { next?: Record<string, unknown> }
23
17
 
24
- export type RequestConfig<TBody = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = core.RequestConfig<TBody, TRequest, TResponse> & {
25
- options?: FetchOptions
26
- }
27
- export type Options<TData extends core.DataShape, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = core.Options<
28
- TData,
29
- ThrowOnError,
30
- TRequest,
31
- TResponse
32
- > & { options?: FetchOptions }
33
- export type ClientConfig<TRequest = NativeRequest, TResponse = NativeResponse> = core.ClientConfig<TRequest, TResponse> & { options?: FetchOptions }
34
- export type ResolvedRequest = core.ResolvedRequest & { options?: FetchOptions }
35
18
  export type ResponseError<TError = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = core.ResponseError<TError, TRequest, TResponse>
36
- /** The core's `ResponseError` class, typed with this transport's request and response. `instanceof` and `ResponseError.is` both match what the client throws. */
37
19
  export const ResponseError = core.ResponseError as {
38
20
  new <TError = unknown, TRequest = NativeRequest, TResponse = NativeResponse>(
39
21
  config: ConstructorParameters<typeof core.ResponseError<TError, TRequest, TResponse>>[0],
@@ -43,69 +25,68 @@ export const ResponseError = core.ResponseError as {
43
25
  prototype: core.ResponseError<unknown, NativeRequest, NativeResponse>
44
26
  }
45
27
 
46
- async function parseResponse(response: Response, responseType?: ResponseType | ResponseTypeMap): Promise<unknown> {
47
- if (response.status === 204 || response.status === 205 || response.status === 304 || !response.body) {
48
- return undefined
49
- }
28
+ export type RequestConfig<TBody = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = core.RequestConfig<TBody, TRequest, TResponse> & {
29
+ options?: NativeOptions
30
+ }
31
+ export type Options<TData extends core.DataShape, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = core.Options<
32
+ TData,
33
+ ThrowOnError,
34
+ TRequest,
35
+ TResponse
36
+ > & { options?: NativeOptions }
37
+ export type ClientConfig<TRequest = NativeRequest, TResponse = NativeResponse> = core.ClientConfig<TRequest, TResponse> & { options?: NativeOptions }
38
+ export type ResolvedRequest = core.ResolvedRequest & { options?: NativeOptions }
39
+ export type RequestResult<
40
+ TResponses,
41
+ ThrowOnError extends boolean = true,
42
+ TRequest = NativeRequest,
43
+ TResponse = NativeResponse,
44
+ THeaders = unknown,
45
+ > = core.RequestResult<TResponses, ThrowOnError, TRequest, TResponse, THeaders>
46
+ export type UnwrappedResult<
47
+ TResponses,
48
+ ThrowOnError extends boolean = true,
49
+ TRequest = NativeRequest,
50
+ TResponse = NativeResponse,
51
+ THeaders = unknown,
52
+ > = core.UnwrappedResult<TResponses, ThrowOnError, TRequest, TResponse, THeaders>
53
+ export type ResponseErrorConfig<TError = unknown> = core.ResponseError<TError, NativeRequest, NativeResponse>
54
+ export type TransportResult<TRequest = NativeRequest, TResponse = NativeResponse> = core.TransportResult<TRequest, TResponse>
55
+ export type DecodedResponse<TData = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = core.DecodedResponse<TData, TRequest, TResponse>
56
+ export type Transport<TRequest = NativeRequest, TResponse = NativeResponse> = core.Transport<TRequest, TResponse>
57
+ export type Interceptors<TRequest = NativeRequest, TResponse = NativeResponse> = core.Interceptors<TRequest, TResponse>
58
+ export type ClientInstance<TRequest = NativeRequest, TResponse = NativeResponse> = core.ClientInstance<TRequest, TResponse>
59
+ export type StreamResult<
60
+ TResponses,
61
+ ThrowOnError extends boolean = true,
62
+ TEvent = core.ServerSentEvent<core.SuccessOf<TResponses>>,
63
+ TRequest = NativeRequest,
64
+ TResponse = NativeResponse,
65
+ THeaders = unknown,
66
+ > = core.StreamResult<TResponses, ThrowOnError, TEvent, TRequest, TResponse, THeaders>
67
+ export type ResponseErrorOf<TResponses, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown> = core.ResponseErrorOf<
68
+ TResponses,
69
+ TRequest,
70
+ TResponse,
71
+ THeaders
72
+ >
73
+ export const isResponseError = core.isResponseError as <TResponses = unknown, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown>(
74
+ error: unknown,
75
+ ) => error is ResponseErrorOf<TResponses, TRequest, TResponse, THeaders>
50
76
 
51
- const contentType = response.headers.get('Content-Type')
52
- // A server may answer a streamed operation with JSON, or with an error body: both are decoded by their content type. Any other streamed success body stays a stream.
53
- const resolved = resolveResponseType(responseType, contentType)
54
- const detected = resolved === 'stream' && (!response.ok || isJsonMediaType(contentType)) ? resolveResponseType(undefined, contentType) : resolved
55
- switch (detected) {
56
- case 'text':
57
- case 'document':
58
- return response.text()
59
- case 'blob':
60
- return response.blob()
61
- case 'arraybuffer':
62
- return response.arrayBuffer()
63
- case 'stream':
64
- return response.body ?? undefined
65
- case 'json': {
66
- // An empty body with a JSON content-type would make response.json() throw, so treat it as no data.
67
- const body = await response.text()
68
- return body ? JSON.parse(body) : undefined
69
- }
70
- }
77
+ export const defaultTransport = async (request: core.ResolvedRequest): Promise<TransportResult> => {
78
+ const nativeRequest = new Request(request.url, toRequestInit(request))
79
+ // `Request` drops the non-standard `next` field, so Next.js reads it from the init.
80
+ const nextInit: NativeOptions | undefined = request.options?.next ? { next: request.options.next } : undefined
81
+ const response = await globalThis.fetch(nativeRequest, nextInit)
71
82
 
72
- const text = await response.text()
73
- if (!text) return undefined
74
- try {
75
- return JSON.parse(text)
76
- } catch {
77
- return text
78
- }
83
+ return { webResponse: response, request: nativeRequest, response }
79
84
  }
80
85
 
81
86
  /**
82
- * The default transport that sends the resolved request through `globalThis.fetch` and returns the parsed body with the native request and response.
87
+ * The client every generated operation uses unless a call passes its own. It is created on first use.
83
88
  */
84
- export const defaultTransport: Transport = async (request: ResolvedRequest): Promise<TransportResult> => {
85
- const init: RequestInit = {
86
- ...request.options,
87
- method: request.method,
88
- headers: request.headers,
89
- body: request.body,
90
- signal: request.signal,
91
- }
92
- if (request.credentials) init.credentials = request.credentials
93
-
94
- const nativeRequest = new Request(request.url, init)
95
- const response = await globalThis.fetch(nativeRequest)
96
- const data = await parseResponse(response, request.responseType)
97
-
98
- return {
99
- data,
100
- status: response.status,
101
- statusText: response.statusText,
102
- headers: response.headers,
103
- contentType: getResponseContentType(response.headers),
104
- request: nativeRequest,
105
- response,
106
- }
107
- }
108
-
109
- export const client = createClientCore({ defaultTransport })
89
+ export const client: ClientInstance = createLazyClient((config) => createClientCore({ defaultTransport, ...config }))
90
+ client.setConfig(defaultConfig)
110
91
 
111
- export const createClient = (config?: Parameters<typeof client.createClient>[0]) => client.createClient(config)
92
+ export const createClient: ClientInstance['createClient'] = client.createClient
@@ -1,39 +0,0 @@
1
- import { n as __exportAll, r as __reExport, t as core_d_exports } from "./core-D6FAGTsL.js";
2
- declare namespace client_d_exports {
3
- export { ClientConfig, FetchOptions, NativeRequest, NativeResponse, Options, RequestConfig, ResolvedRequest, ResponseError, client, createClient, defaultTransport };
4
- }
5
- /** The request and response objects of this transport, carried on every result and error. */
6
- type NativeRequest = Request;
7
- type NativeResponse = Response;
8
- /**
9
- * Extra `fetch` init the transport spreads onto every `Request`, an escape hatch for fields the runtime does not set itself such as `cache`, `redirect`, and Next.js's `next`.
10
- */
11
- type FetchOptions = RequestInit & {
12
- next?: Record<string, unknown>;
13
- };
14
- type RequestConfig<TBody = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = core_d_exports.RequestConfig<TBody, TRequest, TResponse> & {
15
- options?: FetchOptions;
16
- };
17
- type Options<TData extends core_d_exports.DataShape, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = core_d_exports.Options<TData, ThrowOnError, TRequest, TResponse> & {
18
- options?: FetchOptions;
19
- };
20
- type ClientConfig<TRequest = NativeRequest, TResponse = NativeResponse> = core_d_exports.ClientConfig<TRequest, TResponse> & {
21
- options?: FetchOptions;
22
- };
23
- type ResolvedRequest = core_d_exports.ResolvedRequest & {
24
- options?: FetchOptions;
25
- };
26
- type ResponseError<TError = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = core_d_exports.ResponseError<TError, TRequest, TResponse>;
27
- /** The core's `ResponseError` class, typed with this transport's request and response. `instanceof` and `ResponseError.is` both match what the client throws. */
28
- declare const ResponseError: {
29
- new <TError = unknown, TRequest = NativeRequest, TResponse = NativeResponse>(config: ConstructorParameters<typeof core_d_exports.ResponseError<TError, TRequest, TResponse>>[0]): core_d_exports.ResponseError<TError, TRequest, TResponse>;
30
- is: typeof core_d_exports.ResponseError.is;
31
- prototype: core_d_exports.ResponseError<unknown, NativeRequest, NativeResponse>;
32
- };
33
- /**
34
- * The default transport that sends the resolved request through `globalThis.fetch` and returns the parsed body with the native request and response.
35
- */
36
- declare const defaultTransport: core_d_exports.Transport;
37
- declare const client: core_d_exports.ClientInstance<unknown, unknown>;
38
- declare const createClient: (config?: Parameters<typeof client.createClient>[0]) => core_d_exports.ClientInstance<unknown, unknown>;
39
- export { Options as a, ResponseError as c, createClient as d, defaultTransport as f, NativeResponse as i, client as l, FetchOptions as n, RequestConfig as o, NativeRequest as r, ResolvedRequest as s, ClientConfig as t, client_d_exports as u };
@@ -1,2 +0,0 @@
1
- import * as import__kubb_plugin_client_templates_core_ts from "@kubb/plugin-client/templates/core.ts";
2
- export { __exportAll as n, __reExport as r, core_d_exports as t };
@@ -1,2 +0,0 @@
1
- import "../core-D6FAGTsL.js";
2
- export * from "@kubb/plugin-client/templates/core.ts";
@@ -1,6 +0,0 @@
1
- import { a as Options, c as ResponseError, d as createClient, f as defaultTransport, i as NativeResponse, l as client, n as FetchOptions, o as RequestConfig, r as NativeRequest, s as ResolvedRequest, t as ClientConfig, u as client_d_exports } from "../client-Dn80AjdR.js";
2
- export * from "@kubb/plugin-client/templates/core.ts";
3
- declare namespace fetch_d_exports {
4
- export { ClientConfig, FetchOptions, NativeRequest, NativeResponse, Options, RequestConfig, ResolvedRequest, ResponseError, client, createClient, defaultTransport };
5
- }
6
- export { ClientConfig, FetchOptions, NativeRequest, NativeResponse, Options, RequestConfig, ResolvedRequest, ResponseError, client, createClient, defaultTransport };
@@ -1 +0,0 @@
1
- export * from "@kubb/plugin-client/templates/serializers.ts";
@@ -1 +0,0 @@
1
- export * from "@kubb/plugin-client/templates/standardSchema.ts";
@@ -1,10 +0,0 @@
1
- import { createClientGenerator } from '@internals/client'
2
- import { sseTemplatePath } from '../templates.ts'
3
- import type { PluginFetch } from '../types.ts'
4
-
5
- /**
6
- * Built-in operation generator for `@kubb/plugin-fetch`. Emits one async function per OpenAPI
7
- * operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
8
- * that forwards a single `options` object to the bundled `client` and returns the `RequestResult`.
9
- */
10
- export const clientGenerator = createClientGenerator<PluginFetch>('fetch', { sse: sseTemplatePath })
package/src/templates.ts DELETED
@@ -1,16 +0,0 @@
1
- import { createRequire } from 'node:module'
2
- import { fileURLToPath } from 'node:url'
3
-
4
- const require = createRequire(import.meta.url)
5
-
6
- /** Absolute path to the fetch client template, copied into `.kubb/client.ts`. */
7
- export const fetchClientTemplatePath = fileURLToPath(new URL('../templates/client.ts', import.meta.url))
8
-
9
- /**
10
- * Absolute paths to the runtime templates `@kubb/plugin-client` ships for every client plugin, copied into `.kubb/core.ts`,
11
- * `.kubb/serializers.ts`, `.kubb/standardSchema.ts`, and `.kubb/sse.ts`.
12
- */
13
- export const coreTemplatePath = require.resolve('@kubb/plugin-client/templates/core.ts')
14
- export const serializersTemplatePath = require.resolve('@kubb/plugin-client/templates/serializers.ts')
15
- export const standardSchemaTemplatePath = require.resolve('@kubb/plugin-client/templates/standardSchema.ts')
16
- export const sseTemplatePath = require.resolve('@kubb/plugin-client/templates/sse.ts')
package/templates/core.ts DELETED
@@ -1,2 +0,0 @@
1
- // Resolves `./core.ts` for the client template next to it. The generator copies the file from `@kubb/plugin-client` into `.kubb/core.ts`.
2
- export * from '@kubb/plugin-client/templates/core.ts'
@@ -1,2 +0,0 @@
1
- // The client template was `templates/fetch.ts` before it became `templates/client.ts`; this keeps the old subpath working.
2
- export * from './client.ts'
@@ -1,2 +0,0 @@
1
- // Resolves `./serializers.ts` for the client template next to it. The generator copies the file from `@kubb/plugin-client` into `.kubb/serializers.ts`.
2
- export * from '@kubb/plugin-client/templates/serializers.ts'
@@ -1,2 +0,0 @@
1
- // Resolves `./standardSchema.ts` for the client template next to it. The generator copies the file from `@kubb/plugin-client` into `.kubb/standardSchema.ts`.
2
- export * from '@kubb/plugin-client/templates/standardSchema.ts'