@kubb/plugin-client 5.0.0-alpha.9 → 5.0.0-beta.10

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.
Files changed (62) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +27 -7
  3. package/dist/clients/axios.cjs +10 -3
  4. package/dist/clients/axios.cjs.map +1 -1
  5. package/dist/clients/axios.d.ts +5 -4
  6. package/dist/clients/axios.js +9 -2
  7. package/dist/clients/axios.js.map +1 -1
  8. package/dist/clients/fetch.cjs +5 -2
  9. package/dist/clients/fetch.cjs.map +1 -1
  10. package/dist/clients/fetch.d.ts +3 -2
  11. package/dist/clients/fetch.js +5 -2
  12. package/dist/clients/fetch.js.map +1 -1
  13. package/dist/index.cjs +1818 -97
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.ts +330 -4
  16. package/dist/index.js +1804 -95
  17. package/dist/index.js.map +1 -1
  18. package/dist/templates/clients/axios.source.cjs +1 -1
  19. package/dist/templates/clients/axios.source.js +1 -1
  20. package/dist/templates/clients/fetch.source.cjs +1 -1
  21. package/dist/templates/clients/fetch.source.js +1 -1
  22. package/extension.yaml +776 -0
  23. package/package.json +62 -85
  24. package/src/clients/axios.ts +19 -4
  25. package/src/clients/fetch.ts +10 -2
  26. package/src/components/ClassClient.tsx +46 -145
  27. package/src/components/Client.tsx +109 -139
  28. package/src/components/Operations.tsx +10 -10
  29. package/src/components/StaticClassClient.tsx +46 -142
  30. package/src/components/Url.tsx +38 -50
  31. package/src/components/WrapperClient.tsx +11 -7
  32. package/src/functionParams.ts +118 -0
  33. package/src/generators/classClientGenerator.tsx +143 -172
  34. package/src/generators/clientGenerator.tsx +83 -81
  35. package/src/generators/groupedClientGenerator.tsx +50 -52
  36. package/src/generators/operationsGenerator.tsx +11 -18
  37. package/src/generators/staticClassClientGenerator.tsx +172 -184
  38. package/src/index.ts +9 -2
  39. package/src/plugin.ts +115 -145
  40. package/src/resolvers/resolverClient.ts +38 -0
  41. package/src/types.ts +128 -44
  42. package/src/utils.ts +156 -0
  43. package/dist/StaticClassClient-By-aMAe4.cjs +0 -677
  44. package/dist/StaticClassClient-By-aMAe4.cjs.map +0 -1
  45. package/dist/StaticClassClient-CCn9g9eF.js +0 -636
  46. package/dist/StaticClassClient-CCn9g9eF.js.map +0 -1
  47. package/dist/components.cjs +0 -7
  48. package/dist/components.d.ts +0 -216
  49. package/dist/components.js +0 -2
  50. package/dist/generators-BYUJaeZP.js +0 -723
  51. package/dist/generators-BYUJaeZP.js.map +0 -1
  52. package/dist/generators-DTxD9FDY.cjs +0 -753
  53. package/dist/generators-DTxD9FDY.cjs.map +0 -1
  54. package/dist/generators.cjs +0 -7
  55. package/dist/generators.d.ts +0 -517
  56. package/dist/generators.js +0 -2
  57. package/dist/types-DBQdg-BV.d.ts +0 -169
  58. package/src/components/index.ts +0 -5
  59. package/src/generators/index.ts +0 -5
  60. package/templates/clients/axios.ts +0 -70
  61. package/templates/clients/fetch.ts +0 -93
  62. package/templates/config.ts +0 -43
@@ -1,169 +0,0 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { Group, Output, PluginFactoryOptions, ResolveNameParams } from "@kubb/core";
3
- import { Exclude, Include, Override, ResolvePathOptions } from "@kubb/plugin-oas";
4
- import { Generator as Generator$1 } from "@kubb/plugin-oas/generators";
5
- import { Oas, contentType } from "@kubb/oas";
6
-
7
- //#region src/types.d.ts
8
- /**
9
- * Use either a preset `client` type OR a custom `importPath`, not both.
10
- * `importPath` will override the default `client` preset when both are provided.
11
- * These options are mutually exclusive. `bundle` and `importPath` are also
12
- * mutually exclusive since `bundle` only has effect when `importPath` is not set.
13
- */
14
- type ClientImportPath = {
15
- /**
16
- * Which client should be used to do the HTTP calls.
17
- * - 'axios' uses axios client for HTTP requests.
18
- * - 'fetch' uses native fetch API for HTTP requests.
19
- * @default 'axios'
20
- */
21
- client?: 'axios' | 'fetch';
22
- importPath?: never;
23
- } | {
24
- client?: never;
25
- /**
26
- * Client import path for API calls.
27
- * Used as `import client from '${importPath}'`.
28
- * Accepts relative and absolute paths; path changes are not performed.
29
- */
30
- importPath: string;
31
- /**
32
- * `bundle` has no effect when `importPath` is set.
33
- * Use either `bundle` (with `client`) or `importPath`, not both.
34
- */
35
- bundle?: never;
36
- };
37
- type Options = {
38
- /**
39
- * Specify the export location for the files and define the behavior of the output
40
- * @default { path: 'clients', barrelType: 'named' }
41
- */
42
- output?: Output<Oas>;
43
- /**
44
- * Define which contentType should be used.
45
- * By default, the first JSON valid mediaType is used
46
- */
47
- contentType?: contentType;
48
- /**
49
- * Group the clients based on the provided name.
50
- */
51
- group?: Group;
52
- /**
53
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
54
- */
55
- exclude?: Array<Exclude>;
56
- /**
57
- * Array containing include parameters to include tags/operations/methods/paths.
58
- */
59
- include?: Array<Include>;
60
- /**
61
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
62
- */
63
- override?: Array<Override<ResolvedOptions>>;
64
- /**
65
- * Create `operations.ts` file with all operations grouped by methods.
66
- * @default false
67
- */
68
- operations?: boolean;
69
- /**
70
- * Export urls that are used by operation x.
71
- * - 'export' makes them part of your barrel file.
72
- * - false does not make them exportable.
73
- * @default false
74
- * @example getGetPetByIdUrl
75
- */
76
- urlType?: 'export' | false;
77
- /**
78
- * Allows you to set a custom base url for all generated calls.
79
- */
80
- baseURL?: string;
81
- /**
82
- * ReturnType that is used when calling the client.
83
- * - 'data' returns ResponseConfig[data].
84
- * - 'full' returns ResponseConfig.
85
- * @default 'data'
86
- */
87
- dataReturnType?: 'data' | 'full';
88
- /**
89
- * How to style your params, by default no casing is applied
90
- * - 'camelcase' uses camelCase for pathParams, queryParams and headerParams names
91
- * @note response types (data/body) are not affected by this option
92
- */
93
- paramsCasing?: 'camelcase';
94
- /**
95
- * How to pass your params.
96
- * - 'object' returns the params and pathParams as an object.
97
- * - 'inline' returns the params as comma separated params.
98
- * @default 'inline'
99
- */
100
- paramsType?: 'object' | 'inline';
101
- /**
102
- * How to pass your pathParams.
103
- * - 'object' returns the pathParams as an object.
104
- * - 'inline' returns the pathParams as comma separated params.
105
- * @default 'inline'
106
- */
107
- pathParamsType?: 'object' | 'inline';
108
- /**
109
- * Which parser can be used before returning the data.
110
- * - 'client' returns the data as-is from the client.
111
- * - 'zod' uses @kubb/plugin-zod to parse the data.
112
- * @default 'client'
113
- */
114
- parser?: 'client' | 'zod';
115
- /**
116
- * How to generate the client code.
117
- * - 'function' generates standalone functions for each operation.
118
- * - 'class' generates a class with methods for each operation.
119
- * - 'staticClass' generates a class with static methods for each operation.
120
- * @default 'function'
121
- */
122
- clientType?: 'function' | 'class' | 'staticClass';
123
- /**
124
- * Bundle the selected client into the generated `.kubb` directory.
125
- * When disabled the generated clients will import the shared runtime from `@kubb/plugin-client/clients/*`.
126
- * @default false
127
- * In version 5 of Kubb this is by default true
128
- */
129
- bundle?: boolean;
130
- /**
131
- * Generate a wrapper class that composes all tag-based client classes into a single entry point.
132
- */
133
- wrapper?: {
134
- /**
135
- * Name of the wrapper class.
136
- */
137
- className: string;
138
- };
139
- transformers?: {
140
- /**
141
- * Customize the names based on the type that is provided by the plugin.
142
- */
143
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
144
- };
145
- /**
146
- * Define some generators next to the client generators
147
- */
148
- generators?: Array<Generator$1<PluginClient>>;
149
- } & ClientImportPath;
150
- type ResolvedOptions = {
151
- output: Output<Oas>;
152
- group?: Options['group'];
153
- baseURL: string | undefined;
154
- client: Options['client'];
155
- clientType: NonNullable<Options['clientType']>;
156
- bundle: NonNullable<Options['bundle']>;
157
- parser: NonNullable<Options['parser']>;
158
- urlType: NonNullable<Options['urlType']>;
159
- importPath: Options['importPath'];
160
- dataReturnType: NonNullable<Options['dataReturnType']>;
161
- pathParamsType: NonNullable<Options['pathParamsType']>;
162
- paramsType: NonNullable<Options['paramsType']>;
163
- paramsCasing: Options['paramsCasing'];
164
- wrapper: Options['wrapper'];
165
- };
166
- type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>;
167
- //#endregion
168
- export { Options as n, PluginClient as r, ClientImportPath as t };
169
- //# sourceMappingURL=types-DBQdg-BV.d.ts.map
@@ -1,5 +0,0 @@
1
- export { ClassClient } from './ClassClient.tsx'
2
- export { Client } from './Client.tsx'
3
- export { Operations } from './Operations.tsx'
4
- export { StaticClassClient } from './StaticClassClient.tsx'
5
- export { Url } from './Url.tsx'
@@ -1,5 +0,0 @@
1
- export { classClientGenerator } from './classClientGenerator.tsx'
2
- export { clientGenerator } from './clientGenerator.tsx'
3
- export { groupedClientGenerator } from './groupedClientGenerator.tsx'
4
- export { operationsGenerator } from './operationsGenerator.tsx'
5
- export { staticClassClientGenerator } from './staticClassClientGenerator.tsx'
@@ -1,70 +0,0 @@
1
- import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
2
- import axios from 'axios'
3
-
4
- declare const AXIOS_BASE: string
5
- declare const AXIOS_HEADERS: string
6
-
7
- /**
8
- * Subset of AxiosRequestConfig
9
- */
10
- export type RequestConfig<TData = unknown> = {
11
- baseURL?: string
12
- url?: string
13
- method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'
14
- params?: unknown
15
- data?: TData | FormData
16
- responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
17
- signal?: AbortSignal
18
- validateStatus?: (status: number) => boolean
19
- headers?: AxiosRequestConfig['headers']
20
- }
21
-
22
- /**
23
- * Subset of AxiosResponse
24
- */
25
- export type ResponseConfig<TData = unknown> = {
26
- data: TData
27
- status: number
28
- statusText: string
29
- headers: AxiosResponse['headers']
30
- }
31
-
32
- export type ResponseErrorConfig<TError = unknown> = AxiosError<TError>
33
-
34
- export type Client = <TData, _TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>) => Promise<ResponseConfig<TData>>
35
-
36
- let _config: Partial<RequestConfig> = {
37
- baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,
38
- headers: typeof AXIOS_HEADERS !== 'undefined' ? JSON.parse(AXIOS_HEADERS) : undefined,
39
- }
40
-
41
- export const getConfig = () => _config
42
-
43
- export const setConfig = (config: RequestConfig) => {
44
- _config = config
45
- return getConfig()
46
- }
47
-
48
- export const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T>>): Partial<T> => {
49
- return configs.reduce<Partial<T>>((merged, config) => {
50
- return {
51
- ...merged,
52
- ...config,
53
- headers: {
54
- ...merged.headers,
55
- ...config.headers,
56
- },
57
- }
58
- }, {})
59
- }
60
-
61
- export const axiosInstance = axios.create(getConfig())
62
-
63
- export const fetch = async <TData, TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>): Promise<ResponseConfig<TData>> => {
64
- return axiosInstance.request<TData, ResponseConfig<TData>>(mergeConfig(getConfig(), config)).catch((e: AxiosError<TError>) => {
65
- throw e
66
- })
67
- }
68
-
69
- fetch.getConfig = getConfig
70
- fetch.setConfig = setConfig
@@ -1,93 +0,0 @@
1
- /**
2
- * RequestCredentials
3
- */
4
- export type RequestCredentials = 'omit' | 'same-origin' | 'include'
5
-
6
- /**
7
- * Subset of FetchRequestConfig
8
- */
9
- export type RequestConfig<TData = unknown> = {
10
- baseURL?: string
11
- url?: string
12
- method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'
13
- params?: unknown
14
- data?: TData | FormData
15
- responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
16
- signal?: AbortSignal
17
- headers?: [string, string][] | Record<string, string>
18
- credentials?: RequestCredentials
19
- }
20
-
21
- /**
22
- * Subset of FetchResponse
23
- */
24
- export type ResponseConfig<TData = unknown> = {
25
- data: TData
26
- status: number
27
- statusText: string
28
- headers: Headers
29
- }
30
-
31
- let _config: Partial<RequestConfig> = {}
32
-
33
- export const getConfig = () => _config
34
-
35
- export const setConfig = (config: Partial<RequestConfig>) => {
36
- _config = config
37
- return getConfig()
38
- }
39
-
40
- export const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T>>): Partial<T> => {
41
- return configs.reduce<Partial<T>>((merged, config) => {
42
- return {
43
- ...merged,
44
- ...config,
45
- headers: {
46
- ...(Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers),
47
- ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),
48
- },
49
- }
50
- }, {})
51
- }
52
-
53
- export type ResponseErrorConfig<TError = unknown> = TError
54
-
55
- export type Client = <TData, _TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>) => Promise<ResponseConfig<TData>>
56
-
57
- export const fetch = async <TData, _TError = unknown, TVariables = unknown>(paramsConfig: RequestConfig<TVariables>): Promise<ResponseConfig<TData>> => {
58
- const normalizedParams = new URLSearchParams()
59
-
60
- const config = mergeConfig(getConfig(), paramsConfig)
61
-
62
- Object.entries(config.params || {}).forEach(([key, value]) => {
63
- if (value !== undefined) {
64
- normalizedParams.append(key, value === null ? 'null' : value.toString())
65
- }
66
- })
67
-
68
- let targetUrl = [config.baseURL, config.url].filter(Boolean).join('')
69
-
70
- if (config.params) {
71
- targetUrl += `?${normalizedParams}`
72
- }
73
-
74
- const response = await globalThis.fetch(targetUrl, {
75
- credentials: config.credentials || 'same-origin',
76
- method: config.method?.toUpperCase(),
77
- body: config.data instanceof FormData ? config.data : JSON.stringify(config.data),
78
- signal: config.signal,
79
- headers: config.headers,
80
- })
81
-
82
- const data = [204, 205, 304].includes(response.status) || !response.body ? {} : await response.json()
83
-
84
- return {
85
- data: data as TData,
86
- status: response.status,
87
- statusText: response.statusText,
88
- headers: response.headers as Headers,
89
- }
90
- }
91
-
92
- fetch.getConfig = getConfig
93
- fetch.setConfig = setConfig
@@ -1,43 +0,0 @@
1
- export function buildFormData<T = unknown>(data: T): FormData {
2
- const formData = new FormData()
3
-
4
- function appendData(key: string, value: any) {
5
- if (value instanceof Blob) {
6
- formData.append(key, value)
7
- return
8
- }
9
- if (value instanceof Date) {
10
- formData.append(key, value.toISOString())
11
- return
12
- }
13
- if (typeof value === 'number' || typeof value === 'boolean') {
14
- formData.append(key, String(value))
15
- return
16
- }
17
- if (typeof value === 'string') {
18
- formData.append(key, value)
19
- return
20
- }
21
- if (typeof value === 'object') {
22
- formData.append(key, new Blob([JSON.stringify(value)], { type: 'application/json' }))
23
- return
24
- }
25
- }
26
-
27
- if (data) {
28
- Object.entries(data).forEach(([key, value]) => {
29
- if (value === undefined || value === null) return
30
-
31
- if (Array.isArray(value)) {
32
- for (const valueItem of value) {
33
- if (valueItem === undefined || valueItem === null) continue
34
- appendData(key, valueItem)
35
- }
36
- } else {
37
- appendData(key, value)
38
- }
39
- })
40
- }
41
-
42
- return formData
43
- }