@kubb/plugin-client 5.0.0-beta.4 → 5.0.0-beta.56
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.
- package/README.md +39 -24
- package/dist/clients/axios.cjs +26 -4
- package/dist/clients/axios.cjs.map +1 -1
- package/dist/clients/axios.d.ts +11 -5
- package/dist/clients/axios.js +26 -4
- package/dist/clients/axios.js.map +1 -1
- package/dist/clients/fetch.cjs +77 -9
- package/dist/clients/fetch.cjs.map +1 -1
- package/dist/clients/fetch.d.ts +10 -3
- package/dist/clients/fetch.js +77 -9
- package/dist/clients/fetch.js.map +1 -1
- package/dist/index.cjs +836 -514
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +206 -105
- package/dist/index.js +833 -515
- package/dist/index.js.map +1 -1
- package/dist/templates/clients/axios.source.cjs +1 -1
- package/dist/templates/clients/axios.source.cjs.map +1 -1
- package/dist/templates/clients/axios.source.d.ts +1 -1
- package/dist/templates/clients/axios.source.js +1 -1
- package/dist/templates/clients/axios.source.js.map +1 -1
- package/dist/templates/clients/fetch.source.cjs +1 -1
- package/dist/templates/clients/fetch.source.cjs.map +1 -1
- package/dist/templates/clients/fetch.source.d.ts +1 -1
- package/dist/templates/clients/fetch.source.js +1 -1
- package/dist/templates/clients/fetch.source.js.map +1 -1
- package/dist/templates/config.source.cjs.map +1 -1
- package/dist/templates/config.source.d.ts +1 -1
- package/dist/templates/config.source.js.map +1 -1
- package/package.json +14 -26
- package/src/clients/axios.ts +41 -7
- package/src/clients/fetch.ts +106 -6
- package/src/components/ClassClient.tsx +47 -24
- package/src/components/Client.tsx +100 -71
- package/src/components/StaticClassClient.tsx +47 -24
- package/src/components/Url.tsx +10 -12
- package/src/components/WrapperClient.tsx +9 -5
- package/src/functionParams.ts +8 -8
- package/src/generators/classClientGenerator.tsx +63 -51
- package/src/generators/clientGenerator.tsx +45 -48
- package/src/generators/groupedClientGenerator.tsx +12 -6
- package/src/generators/operationsGenerator.ts +47 -0
- package/src/generators/staticClassClientGenerator.tsx +57 -45
- package/src/index.ts +2 -1
- package/src/plugin.ts +30 -28
- package/src/resolvers/resolverClient.ts +32 -8
- package/src/types.ts +111 -65
- package/src/utils.ts +142 -63
- package/extension.yaml +0 -776
- package/src/components/Operations.tsx +0 -28
- package/src/generators/operationsGenerator.tsx +0 -28
- package/templates/clients/axios.ts +0 -73
- package/templates/clients/fetch.ts +0 -96
- package/templates/config.ts +0 -43
- /package/dist/{chunk-ByKO4r7w.cjs → chunk-Bx3C2hgW.cjs} +0 -0
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { URLPath } from '@internals/utils'
|
|
2
|
-
import type { ast } from '@kubb/core'
|
|
3
|
-
import { Const, File } from '@kubb/renderer-jsx'
|
|
4
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
5
|
-
|
|
6
|
-
type OperationsProps = {
|
|
7
|
-
name: string
|
|
8
|
-
nodes: Array<ast.OperationNode>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function Operations({ name, nodes }: OperationsProps): KubbReactNode {
|
|
12
|
-
const operationsObject: Record<string, { path: string; method: string }> = {}
|
|
13
|
-
|
|
14
|
-
nodes.forEach((node) => {
|
|
15
|
-
operationsObject[node.operationId] = {
|
|
16
|
-
path: new URLPath(node.path).URL,
|
|
17
|
-
method: node.method.toLowerCase(),
|
|
18
|
-
}
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
return (
|
|
22
|
-
<File.Source name={name} isExportable isIndexable>
|
|
23
|
-
<Const name={name} export>
|
|
24
|
-
{JSON.stringify(operationsObject, undefined, 2)}
|
|
25
|
-
</Const>
|
|
26
|
-
</File.Source>
|
|
27
|
-
)
|
|
28
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { defineGenerator } from '@kubb/core'
|
|
2
|
-
import { File, jsxRenderer } from '@kubb/renderer-jsx'
|
|
3
|
-
import { Operations } from '../components/Operations'
|
|
4
|
-
import type { PluginClient } from '../types'
|
|
5
|
-
|
|
6
|
-
export const operationsGenerator = defineGenerator<PluginClient>({
|
|
7
|
-
name: 'client',
|
|
8
|
-
renderer: jsxRenderer,
|
|
9
|
-
operations(nodes, ctx) {
|
|
10
|
-
const { config, resolver, adapter, root } = ctx
|
|
11
|
-
const { output, group } = ctx.options
|
|
12
|
-
|
|
13
|
-
const name = 'operations'
|
|
14
|
-
const file = resolver.resolveFile({ name, extname: '.ts' }, { root, output, group })
|
|
15
|
-
|
|
16
|
-
return (
|
|
17
|
-
<File
|
|
18
|
-
baseName={file.baseName}
|
|
19
|
-
path={file.path}
|
|
20
|
-
meta={file.meta}
|
|
21
|
-
banner={resolver.resolveBanner(adapter.inputNode, { output, config })}
|
|
22
|
-
footer={resolver.resolveFooter(adapter.inputNode, { output, config })}
|
|
23
|
-
>
|
|
24
|
-
<Operations name={name} nodes={nodes} />
|
|
25
|
-
</File>
|
|
26
|
-
)
|
|
27
|
-
},
|
|
28
|
-
})
|
|
@@ -1,73 +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>, request?: unknown) => 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>(
|
|
64
|
-
config: RequestConfig<TVariables>,
|
|
65
|
-
_request?: unknown,
|
|
66
|
-
): Promise<ResponseConfig<TData>> => {
|
|
67
|
-
return axiosInstance.request<TData, ResponseConfig<TData>>(mergeConfig(getConfig(), config)).catch((e: AxiosError<TError>) => {
|
|
68
|
-
throw e
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
fetch.getConfig = getConfig
|
|
73
|
-
fetch.setConfig = setConfig
|
|
@@ -1,96 +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>, request?: unknown) => Promise<ResponseConfig<TData>>
|
|
56
|
-
|
|
57
|
-
export const fetch = async <TData, _TError = unknown, TVariables = unknown>(
|
|
58
|
-
paramsConfig: RequestConfig<TVariables>,
|
|
59
|
-
_request?: unknown,
|
|
60
|
-
): Promise<ResponseConfig<TData>> => {
|
|
61
|
-
const normalizedParams = new URLSearchParams()
|
|
62
|
-
|
|
63
|
-
const config = mergeConfig(getConfig(), paramsConfig)
|
|
64
|
-
|
|
65
|
-
Object.entries(config.params || {}).forEach(([key, value]) => {
|
|
66
|
-
if (value !== undefined) {
|
|
67
|
-
normalizedParams.append(key, value === null ? 'null' : value.toString())
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
let targetUrl = [config.baseURL, config.url].filter(Boolean).join('')
|
|
72
|
-
|
|
73
|
-
if (config.params) {
|
|
74
|
-
targetUrl += `?${normalizedParams}`
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const response = await globalThis.fetch(targetUrl, {
|
|
78
|
-
credentials: config.credentials || 'same-origin',
|
|
79
|
-
method: config.method?.toUpperCase(),
|
|
80
|
-
body: config.data instanceof FormData ? config.data : JSON.stringify(config.data),
|
|
81
|
-
signal: config.signal,
|
|
82
|
-
headers: config.headers,
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
const data = [204, 205, 304].includes(response.status) || !response.body ? {} : await response.json()
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
data: data as TData,
|
|
89
|
-
status: response.status,
|
|
90
|
-
statusText: response.statusText,
|
|
91
|
-
headers: response.headers as Headers,
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
fetch.getConfig = getConfig
|
|
96
|
-
fetch.setConfig = setConfig
|
package/templates/config.ts
DELETED
|
@@ -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
|
-
}
|
|
File without changes
|
|
File without changes
|