@kubb/plugin-client 3.0.0-alpha.1 → 3.0.0-alpha.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.
- package/client.ts +1 -1
- package/dist/chunk-3NQF7BGA.cjs +61 -0
- package/dist/chunk-3NQF7BGA.cjs.map +1 -0
- package/dist/chunk-AZDWYBLW.cjs +1 -0
- package/dist/chunk-AZDWYBLW.cjs.map +1 -0
- package/dist/chunk-PO22ZUSH.cjs +128 -0
- package/dist/chunk-PO22ZUSH.cjs.map +1 -0
- package/dist/chunk-QBFKND4S.js +128 -0
- package/dist/chunk-QBFKND4S.js.map +1 -0
- package/dist/chunk-QLVGX3VO.js +61 -0
- package/dist/chunk-QLVGX3VO.js.map +1 -0
- package/dist/chunk-YJOVLRSC.js +1 -0
- package/dist/chunk-YJOVLRSC.js.map +1 -0
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.js.map +1 -1
- package/dist/components.cjs +4 -3
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +3 -5
- package/dist/components.d.ts +3 -5
- package/dist/components.js +2 -1
- package/dist/generators.cjs +8 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +8 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +27 -80
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +27 -80
- package/dist/index.js.map +1 -1
- package/dist/{types-xK4X9e5d.d.cts → types-DETYKDFZ.d.cts} +19 -108
- package/dist/{types-xK4X9e5d.d.ts → types-DETYKDFZ.d.ts} +19 -108
- package/package.json +21 -13
- package/src/components/Client.tsx +77 -198
- package/src/components/Operations.tsx +9 -75
- package/src/components/__snapshots__/Client/showPetById.ts +3 -6
- package/src/components/__snapshots__/Operations/showPetById.ts +5 -5
- package/src/generators/axiosGenerator.tsx +63 -0
- package/src/generators/index.ts +1 -0
- package/src/plugin.ts +21 -38
- package/src/types.ts +9 -3
- package/dist/chunk-W57BRY5O.js +0 -201
- package/dist/chunk-W57BRY5O.js.map +0 -1
- package/dist/chunk-W7F5CMU6.cjs +0 -201
- package/dist/chunk-W7F5CMU6.cjs.map +0 -1
- package/src/OperationGenerator.tsx +0 -65
|
@@ -1,123 +1,29 @@
|
|
|
1
1
|
import { PluginFactoryOptions, ResolveNameParams } from '@kubb/core';
|
|
2
2
|
import * as KubbFile from '@kubb/fs/types';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { KubbNode, Params } from '@kubb/react';
|
|
7
|
-
import { ComponentType, ComponentProps } from 'react';
|
|
3
|
+
import { Operation, HttpMethod } from '@kubb/oas';
|
|
4
|
+
import { OperationSchemas, ResolvePathOptions, Exclude, Include, Override } from '@kubb/plugin-oas';
|
|
5
|
+
import { KubbNode } from '@kubb/react/types';
|
|
8
6
|
|
|
9
|
-
type
|
|
7
|
+
type Props = {
|
|
10
8
|
/**
|
|
11
9
|
* Name of the function
|
|
12
10
|
*/
|
|
13
11
|
name: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
params: Params;
|
|
18
|
-
/**
|
|
19
|
-
* Generics that needs to be added for TypeScript
|
|
20
|
-
*/
|
|
21
|
-
generics?: string;
|
|
22
|
-
/**
|
|
23
|
-
* ReturnType(see async for adding Promise type)
|
|
24
|
-
*/
|
|
25
|
-
returnType?: string;
|
|
26
|
-
/**
|
|
27
|
-
* Options for JSdocs
|
|
28
|
-
*/
|
|
29
|
-
JSDoc?: {
|
|
30
|
-
comments: string[];
|
|
31
|
-
};
|
|
32
|
-
client: {
|
|
33
|
-
baseURL: string | undefined;
|
|
34
|
-
generics: string | string[];
|
|
35
|
-
method: HttpMethod;
|
|
36
|
-
path: URLPath;
|
|
37
|
-
dataReturnType: PluginClient['options']['dataReturnType'];
|
|
38
|
-
withQueryParams: boolean;
|
|
39
|
-
withData: boolean;
|
|
40
|
-
withHeaders: boolean;
|
|
41
|
-
contentType: string;
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
declare function Template$1({ name, generics, returnType, params, JSDoc, client }: TemplateProps$1): KubbNode;
|
|
45
|
-
type RootTemplateProps$1 = {
|
|
46
|
-
children?: React.ReactNode;
|
|
47
|
-
};
|
|
48
|
-
declare function RootTemplate$1({ children }: RootTemplateProps$1): JSX.Element;
|
|
49
|
-
declare const defaultTemplates$1: {
|
|
50
|
-
readonly default: typeof Template$1;
|
|
51
|
-
readonly root: typeof RootTemplate$1;
|
|
52
|
-
};
|
|
53
|
-
type Templates$2 = Partial<typeof defaultTemplates$1>;
|
|
54
|
-
type ClientProps = {
|
|
55
|
-
baseURL: string | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* This will make it possible to override the default behaviour.
|
|
58
|
-
*/
|
|
59
|
-
Template?: ComponentType<ComponentProps<typeof Template$1>>;
|
|
60
|
-
};
|
|
61
|
-
declare function Client({ baseURL, Template }: ClientProps): KubbNode;
|
|
62
|
-
declare namespace Client {
|
|
63
|
-
var File: ({ baseURL, ...props }: FileProps$1) => KubbNode;
|
|
64
|
-
var templates: {
|
|
65
|
-
readonly default: typeof Template$1;
|
|
66
|
-
readonly root: typeof RootTemplate$1;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
type FileProps$1 = {
|
|
70
|
-
baseURL: string | undefined;
|
|
71
|
-
/**
|
|
72
|
-
* This will make it possible to override the default behaviour.
|
|
73
|
-
*/
|
|
74
|
-
templates?: Templates$2;
|
|
12
|
+
options: PluginClient['resolvedOptions'];
|
|
13
|
+
typedSchemas: OperationSchemas;
|
|
14
|
+
operation: Operation;
|
|
75
15
|
};
|
|
16
|
+
declare function Client({ name, options, typedSchemas, operation }: Props): KubbNode;
|
|
76
17
|
|
|
77
|
-
type
|
|
78
|
-
/**
|
|
79
|
-
* Name of the function
|
|
80
|
-
*/
|
|
18
|
+
type OperationsProps = {
|
|
81
19
|
name: string;
|
|
82
|
-
operations: Operation
|
|
83
|
-
baseURL: string | undefined;
|
|
84
|
-
};
|
|
85
|
-
declare function Template({ name, operations }: TemplateProps): KubbNode;
|
|
86
|
-
type RootTemplateProps = {
|
|
87
|
-
children?: React.ReactNode;
|
|
88
|
-
};
|
|
89
|
-
declare function RootTemplate({ children }: RootTemplateProps): JSX.Element;
|
|
90
|
-
declare const defaultTemplates: {
|
|
91
|
-
readonly default: typeof Template;
|
|
92
|
-
readonly root: typeof RootTemplate;
|
|
93
|
-
};
|
|
94
|
-
type Templates$1 = Partial<typeof defaultTemplates>;
|
|
95
|
-
type Props = {
|
|
96
|
-
baseURL: string | undefined;
|
|
97
|
-
/**
|
|
98
|
-
* This will make it possible to override the default behaviour.
|
|
99
|
-
*/
|
|
100
|
-
Template?: ComponentType<ComponentProps<typeof Template>>;
|
|
101
|
-
};
|
|
102
|
-
declare function Operations({ baseURL, Template }: Props): KubbNode;
|
|
103
|
-
declare namespace Operations {
|
|
104
|
-
var File: ({ baseURL, ...props }: FileProps) => KubbNode;
|
|
105
|
-
var templates: {
|
|
106
|
-
readonly default: typeof Template;
|
|
107
|
-
readonly root: typeof RootTemplate;
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
type FileProps = {
|
|
111
|
-
baseURL: string | undefined;
|
|
112
|
-
/**
|
|
113
|
-
* This will make it possible to override the default behaviour.
|
|
114
|
-
*/
|
|
115
|
-
templates?: Templates$1;
|
|
20
|
+
operations: Array<Operation>;
|
|
116
21
|
};
|
|
22
|
+
declare function Operations({ name, operations }: OperationsProps): JSX.Element;
|
|
117
23
|
|
|
118
24
|
type Templates = {
|
|
119
|
-
operations?: typeof Operations
|
|
120
|
-
client?: typeof Client
|
|
25
|
+
operations?: typeof Operations | false;
|
|
26
|
+
client?: typeof Client | false;
|
|
121
27
|
};
|
|
122
28
|
type Options = {
|
|
123
29
|
output?: {
|
|
@@ -136,7 +42,7 @@ type Options = {
|
|
|
136
42
|
extName?: KubbFile.Extname;
|
|
137
43
|
/**
|
|
138
44
|
* Define what needs to exported, here you can also disable the export of barrel files
|
|
139
|
-
* @default `'
|
|
45
|
+
* @default `'barrelNamed'`
|
|
140
46
|
*/
|
|
141
47
|
exportType?: 'barrel' | 'barrelNamed' | false;
|
|
142
48
|
};
|
|
@@ -183,6 +89,11 @@ type Options = {
|
|
|
183
89
|
* @default '@kubb/plugin-client/client'
|
|
184
90
|
*/
|
|
185
91
|
importPath?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Define which HttpMethods can be used for queries
|
|
94
|
+
* @default ['get', 'post', 'put', 'delete']
|
|
95
|
+
*/
|
|
96
|
+
methods?: Array<HttpMethod>;
|
|
186
97
|
};
|
|
187
98
|
/**
|
|
188
99
|
* ReturnType that needs to be used when calling client().
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-client",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.10",
|
|
4
4
|
"description": "Generator plugin-client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -26,6 +26,11 @@
|
|
|
26
26
|
"require": "./dist/index.cjs",
|
|
27
27
|
"default": "./dist/index.cjs"
|
|
28
28
|
},
|
|
29
|
+
"./generators": {
|
|
30
|
+
"import": "./dist/generators.js",
|
|
31
|
+
"require": "./dist/generators.cjs",
|
|
32
|
+
"default": "./dist/generators.cjs"
|
|
33
|
+
},
|
|
29
34
|
"./components": {
|
|
30
35
|
"import": "./dist/components.js",
|
|
31
36
|
"require": "./dist/components.cjs",
|
|
@@ -51,6 +56,9 @@
|
|
|
51
56
|
"*": {
|
|
52
57
|
"components": [
|
|
53
58
|
"./dist/components.d.ts"
|
|
59
|
+
],
|
|
60
|
+
"generators": [
|
|
61
|
+
"./dist/generators.d.ts"
|
|
54
62
|
]
|
|
55
63
|
}
|
|
56
64
|
},
|
|
@@ -64,27 +72,27 @@
|
|
|
64
72
|
"!/**/__tests__/**"
|
|
65
73
|
],
|
|
66
74
|
"dependencies": {
|
|
67
|
-
"@kubb/core": "3.0.0-alpha.
|
|
68
|
-
"@kubb/fs": "3.0.0-alpha.
|
|
69
|
-
"@kubb/oas": "3.0.0-alpha.
|
|
70
|
-
"@kubb/parser-ts": "3.0.0-alpha.
|
|
71
|
-
"@kubb/plugin-oas": "3.0.0-alpha.
|
|
72
|
-
"@kubb/plugin-ts": "3.0.0-alpha.
|
|
73
|
-
"@kubb/react": "3.0.0-alpha.
|
|
75
|
+
"@kubb/core": "3.0.0-alpha.10",
|
|
76
|
+
"@kubb/fs": "3.0.0-alpha.10",
|
|
77
|
+
"@kubb/oas": "3.0.0-alpha.10",
|
|
78
|
+
"@kubb/parser-ts": "3.0.0-alpha.10",
|
|
79
|
+
"@kubb/plugin-oas": "3.0.0-alpha.10",
|
|
80
|
+
"@kubb/plugin-ts": "3.0.0-alpha.10",
|
|
81
|
+
"@kubb/react": "3.0.0-alpha.10"
|
|
74
82
|
},
|
|
75
83
|
"devDependencies": {
|
|
76
|
-
"@types/react": "^18.3.
|
|
84
|
+
"@types/react": "^18.3.5",
|
|
77
85
|
"axios": "^1.7.5",
|
|
78
86
|
"react": "^18.3.1",
|
|
79
87
|
"tsup": "^8.2.4",
|
|
80
88
|
"typescript": "^5.5.4",
|
|
81
|
-
"@kubb/config-biome": "3.0.0-alpha.
|
|
82
|
-
"@kubb/config-ts": "3.0.0-alpha.
|
|
83
|
-
"@kubb/config-tsup": "3.0.0-alpha.
|
|
89
|
+
"@kubb/config-biome": "3.0.0-alpha.10",
|
|
90
|
+
"@kubb/config-ts": "3.0.0-alpha.10",
|
|
91
|
+
"@kubb/config-tsup": "3.0.0-alpha.10"
|
|
84
92
|
},
|
|
85
93
|
"peerDependencies": {
|
|
86
94
|
"axios": "^1.7.2",
|
|
87
|
-
"@kubb/react": "3.0.0-alpha.
|
|
95
|
+
"@kubb/react": "3.0.0-alpha.10"
|
|
88
96
|
},
|
|
89
97
|
"peerDependenciesMeta": {
|
|
90
98
|
"axios": {
|
|
@@ -1,83 +1,85 @@
|
|
|
1
1
|
import { URLPath } from '@kubb/core/utils'
|
|
2
|
-
import { Parser, File, Function, useApp } from '@kubb/react'
|
|
3
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
4
|
-
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
-
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
6
2
|
|
|
7
|
-
import { isOptional } from '@kubb/oas'
|
|
8
|
-
import type {
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import type {
|
|
3
|
+
import { type Operation, isOptional } from '@kubb/oas'
|
|
4
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
5
|
+
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
6
|
+
import { File, Function, createFunctionParams } from '@kubb/react'
|
|
7
|
+
import type { KubbNode, Params } from '@kubb/react/types'
|
|
8
|
+
import type { PluginClient } from '../types.ts'
|
|
12
9
|
|
|
13
|
-
type
|
|
10
|
+
type Props = {
|
|
14
11
|
/**
|
|
15
12
|
* Name of the function
|
|
16
13
|
*/
|
|
17
14
|
name: string
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
params: Params
|
|
22
|
-
/**
|
|
23
|
-
* Generics that needs to be added for TypeScript
|
|
24
|
-
*/
|
|
25
|
-
generics?: string
|
|
26
|
-
/**
|
|
27
|
-
* ReturnType(see async for adding Promise type)
|
|
28
|
-
*/
|
|
29
|
-
returnType?: string
|
|
30
|
-
/**
|
|
31
|
-
* Options for JSdocs
|
|
32
|
-
*/
|
|
33
|
-
JSDoc?: {
|
|
34
|
-
comments: string[]
|
|
35
|
-
}
|
|
36
|
-
client: {
|
|
37
|
-
baseURL: string | undefined
|
|
38
|
-
generics: string | string[]
|
|
39
|
-
method: HttpMethod
|
|
40
|
-
path: URLPath
|
|
41
|
-
dataReturnType: PluginClient['options']['dataReturnType']
|
|
42
|
-
withQueryParams: boolean
|
|
43
|
-
withData: boolean
|
|
44
|
-
withHeaders: boolean
|
|
45
|
-
contentType: string
|
|
46
|
-
}
|
|
15
|
+
options: PluginClient['resolvedOptions']
|
|
16
|
+
typedSchemas: OperationSchemas
|
|
17
|
+
operation: Operation
|
|
47
18
|
}
|
|
48
19
|
|
|
49
|
-
function
|
|
50
|
-
const
|
|
20
|
+
export function Client({ name, options, typedSchemas, operation }: Props): KubbNode {
|
|
21
|
+
const contentType = operation.getContentType()
|
|
22
|
+
const baseURL = options.client.importPath === '@kubb/plugin-client/client' ? options.baseURL : undefined
|
|
23
|
+
const path = new URLPath(operation.path)
|
|
24
|
+
const isFormData = contentType === 'multipart/form-data'
|
|
51
25
|
const headers = [
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
]
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
26
|
+
contentType !== 'application/json' ? `'Content-Type': '${contentType}'` : undefined,
|
|
27
|
+
typedSchemas.headerParams?.name ? '...headers' : undefined,
|
|
28
|
+
].filter(Boolean)
|
|
29
|
+
|
|
30
|
+
const params = createFunctionParams({
|
|
31
|
+
pathParams: {
|
|
32
|
+
mode: options.pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
33
|
+
children: getPathParams(typedSchemas.pathParams, { typed: true }),
|
|
34
|
+
},
|
|
35
|
+
data: typedSchemas.request?.name
|
|
36
|
+
? {
|
|
37
|
+
type: typedSchemas.request?.name,
|
|
38
|
+
optional: isOptional(typedSchemas.request?.schema),
|
|
39
|
+
}
|
|
40
|
+
: undefined,
|
|
41
|
+
params: typedSchemas.queryParams?.name
|
|
42
|
+
? {
|
|
43
|
+
type: typedSchemas.queryParams?.name,
|
|
44
|
+
optional: isOptional(typedSchemas.queryParams?.schema),
|
|
45
|
+
}
|
|
46
|
+
: undefined,
|
|
47
|
+
headers: typedSchemas.headerParams?.name
|
|
48
|
+
? {
|
|
49
|
+
type: typedSchemas.headerParams?.name,
|
|
50
|
+
optional: isOptional(typedSchemas.headerParams?.schema),
|
|
51
|
+
}
|
|
52
|
+
: undefined,
|
|
53
|
+
options: {
|
|
54
|
+
type: 'Partial<Parameters<typeof client>[0]>',
|
|
55
|
+
default: '{}',
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const clientParams = createFunctionParams({
|
|
58
60
|
data: {
|
|
59
61
|
mode: 'object',
|
|
60
62
|
children: {
|
|
61
63
|
method: {
|
|
62
64
|
type: 'string',
|
|
63
|
-
value: JSON.stringify(
|
|
65
|
+
value: JSON.stringify(operation.method),
|
|
64
66
|
},
|
|
65
67
|
url: {
|
|
66
68
|
type: 'string',
|
|
67
|
-
value:
|
|
69
|
+
value: path.template,
|
|
68
70
|
},
|
|
69
|
-
baseURL:
|
|
71
|
+
baseURL: baseURL
|
|
70
72
|
? {
|
|
71
73
|
type: 'string',
|
|
72
|
-
value: JSON.stringify(
|
|
74
|
+
value: JSON.stringify(baseURL),
|
|
73
75
|
}
|
|
74
76
|
: undefined,
|
|
75
|
-
params:
|
|
77
|
+
params: typedSchemas.queryParams?.name
|
|
76
78
|
? {
|
|
77
79
|
type: 'any',
|
|
78
80
|
}
|
|
79
81
|
: undefined,
|
|
80
|
-
data:
|
|
82
|
+
data: typedSchemas.request?.name
|
|
81
83
|
? {
|
|
82
84
|
type: 'any',
|
|
83
85
|
value: isFormData ? 'formData' : undefined,
|
|
@@ -86,7 +88,7 @@ function Template({ name, generics, returnType, params, JSDoc, client }: Templat
|
|
|
86
88
|
headers: headers.length
|
|
87
89
|
? {
|
|
88
90
|
type: 'any',
|
|
89
|
-
value: headers.length ? `{ ${headers}, ...options.headers }` : undefined,
|
|
91
|
+
value: headers.length ? `{ ${headers.join(', ')}, ...options.headers }` : undefined,
|
|
90
92
|
}
|
|
91
93
|
: undefined,
|
|
92
94
|
options: {
|
|
@@ -95,7 +97,7 @@ function Template({ name, generics, returnType, params, JSDoc, client }: Templat
|
|
|
95
97
|
},
|
|
96
98
|
},
|
|
97
99
|
},
|
|
98
|
-
}
|
|
100
|
+
})
|
|
99
101
|
|
|
100
102
|
const formData = isFormData
|
|
101
103
|
? `
|
|
@@ -112,149 +114,26 @@ function Template({ name, generics, returnType, params, JSDoc, client }: Templat
|
|
|
112
114
|
: undefined
|
|
113
115
|
|
|
114
116
|
return (
|
|
115
|
-
<
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
extName,
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
} = useApp<PluginClient>()
|
|
136
|
-
|
|
137
|
-
const { getSchemas, getFile } = useOperationManager()
|
|
138
|
-
const operation = useOperation()
|
|
139
|
-
|
|
140
|
-
const file = getFile(operation)
|
|
141
|
-
const fileType = getFile(operation, { pluginKey: [pluginTsName] })
|
|
142
|
-
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
143
|
-
|
|
144
|
-
return (
|
|
145
|
-
<Parser language="typescript">
|
|
146
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
147
|
-
<File.Import name={'client'} path={importPath} />
|
|
148
|
-
<File.Import name={['ResponseConfig']} path={importPath} isTypeOnly />
|
|
149
|
-
<File.Import
|
|
150
|
-
extName={extName}
|
|
151
|
-
name={[schemas.request?.name, schemas.response.name, schemas.pathParams?.name, schemas.queryParams?.name, schemas.headerParams?.name].filter(Boolean)}
|
|
152
|
-
root={file.path}
|
|
153
|
-
path={fileType.path}
|
|
154
|
-
isTypeOnly
|
|
117
|
+
<File.Source name={name} isExportable isIndexable>
|
|
118
|
+
<Function
|
|
119
|
+
name={name}
|
|
120
|
+
async
|
|
121
|
+
export
|
|
122
|
+
returnType={
|
|
123
|
+
options.dataReturnType === 'data' ? `ResponseConfig<${typedSchemas.response.name}>["data"]` : `ResponseConfig<${typedSchemas.response.name}>`
|
|
124
|
+
}
|
|
125
|
+
params={params}
|
|
126
|
+
JSDoc={{
|
|
127
|
+
comments: getComments(operation),
|
|
128
|
+
}}
|
|
129
|
+
>
|
|
130
|
+
{formData || ''}
|
|
131
|
+
<Function.Call
|
|
132
|
+
name="res"
|
|
133
|
+
to={<Function name="client" async generics={[typedSchemas.response.name, typedSchemas.request?.name].filter(Boolean)} params={clientParams} />}
|
|
155
134
|
/>
|
|
156
|
-
<
|
|
157
|
-
</
|
|
158
|
-
</
|
|
135
|
+
<Function.Return>{options.dataReturnType === 'data' ? 'res.data' : 'res'}</Function.Return>
|
|
136
|
+
</Function>
|
|
137
|
+
</File.Source>
|
|
159
138
|
)
|
|
160
139
|
}
|
|
161
|
-
|
|
162
|
-
const defaultTemplates = { default: Template, root: RootTemplate } as const
|
|
163
|
-
|
|
164
|
-
type Templates = Partial<typeof defaultTemplates>
|
|
165
|
-
|
|
166
|
-
type ClientProps = {
|
|
167
|
-
baseURL: string | undefined
|
|
168
|
-
/**
|
|
169
|
-
* This will make it possible to override the default behaviour.
|
|
170
|
-
*/
|
|
171
|
-
Template?: ComponentType<ComponentProps<typeof Template>>
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function Client({ baseURL, Template = defaultTemplates.default }: ClientProps): KubbNode {
|
|
175
|
-
const {
|
|
176
|
-
plugin: {
|
|
177
|
-
options: { client, dataReturnType, pathParamsType },
|
|
178
|
-
},
|
|
179
|
-
} = useApp<PluginClient>()
|
|
180
|
-
|
|
181
|
-
const { getSchemas, getName } = useOperationManager()
|
|
182
|
-
const operation = useOperation()
|
|
183
|
-
|
|
184
|
-
const contentType = operation.getContentType()
|
|
185
|
-
const name = getName(operation, { type: 'function' })
|
|
186
|
-
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
187
|
-
|
|
188
|
-
return (
|
|
189
|
-
<Template
|
|
190
|
-
name={name}
|
|
191
|
-
params={{
|
|
192
|
-
pathParams: {
|
|
193
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
194
|
-
children: getPathParams(schemas.pathParams, { typed: true }),
|
|
195
|
-
},
|
|
196
|
-
data: schemas.request?.name
|
|
197
|
-
? {
|
|
198
|
-
type: schemas.request?.name,
|
|
199
|
-
optional: isOptional(schemas.request?.schema),
|
|
200
|
-
}
|
|
201
|
-
: undefined,
|
|
202
|
-
params: schemas.queryParams?.name
|
|
203
|
-
? {
|
|
204
|
-
type: schemas.queryParams?.name,
|
|
205
|
-
optional: isOptional(schemas.queryParams?.schema),
|
|
206
|
-
}
|
|
207
|
-
: undefined,
|
|
208
|
-
headers: schemas.headerParams?.name
|
|
209
|
-
? {
|
|
210
|
-
type: schemas.headerParams?.name,
|
|
211
|
-
optional: isOptional(schemas.headerParams?.schema),
|
|
212
|
-
}
|
|
213
|
-
: undefined,
|
|
214
|
-
options: {
|
|
215
|
-
type: 'Partial<Parameters<typeof client>[0]>',
|
|
216
|
-
default: '{}',
|
|
217
|
-
},
|
|
218
|
-
}}
|
|
219
|
-
returnType={dataReturnType === 'data' ? `ResponseConfig<${schemas.response.name}>["data"]` : `ResponseConfig<${schemas.response.name}>`}
|
|
220
|
-
JSDoc={{
|
|
221
|
-
comments: getComments(operation),
|
|
222
|
-
}}
|
|
223
|
-
client={{
|
|
224
|
-
// only set baseURL from serverIndex(swagger) when no custom client(default) is used
|
|
225
|
-
baseURL: client.importPath === '@kubb/plugin-client/client' ? baseURL : undefined,
|
|
226
|
-
generics: [schemas.response.name, schemas.request?.name].filter(Boolean),
|
|
227
|
-
dataReturnType,
|
|
228
|
-
withQueryParams: !!schemas.queryParams?.name,
|
|
229
|
-
withData: !!schemas.request?.name,
|
|
230
|
-
withHeaders: !!schemas.headerParams?.name,
|
|
231
|
-
method: operation.method,
|
|
232
|
-
path: new URLPath(operation.path),
|
|
233
|
-
contentType,
|
|
234
|
-
}}
|
|
235
|
-
/>
|
|
236
|
-
)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
type FileProps = {
|
|
240
|
-
baseURL: string | undefined
|
|
241
|
-
/**
|
|
242
|
-
* This will make it possible to override the default behaviour.
|
|
243
|
-
*/
|
|
244
|
-
templates?: Templates
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
Client.File = function ({ baseURL, ...props }: FileProps): KubbNode {
|
|
248
|
-
const templates = { ...defaultTemplates, ...props.templates }
|
|
249
|
-
|
|
250
|
-
const Template = templates.default
|
|
251
|
-
const RootTemplate = templates.root
|
|
252
|
-
|
|
253
|
-
return (
|
|
254
|
-
<RootTemplate>
|
|
255
|
-
<Client baseURL={baseURL} Template={Template} />
|
|
256
|
-
</RootTemplate>
|
|
257
|
-
)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
Client.templates = defaultTemplates
|
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
import { URLPath } from '@kubb/core/utils'
|
|
2
|
-
import {
|
|
3
|
-
import { Const, File, Parser, useApp } from '@kubb/react'
|
|
2
|
+
import { Const, File } from '@kubb/react'
|
|
4
3
|
|
|
5
4
|
import type { HttpMethod, Operation } from '@kubb/oas'
|
|
6
|
-
import type { KubbNode } from '@kubb/react'
|
|
7
|
-
import type { ComponentProps, ComponentType } from 'react'
|
|
8
|
-
import type { FileMeta, PluginClient } from '../types.ts'
|
|
9
5
|
|
|
10
|
-
type
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
6
|
+
type OperationsProps = {
|
|
14
7
|
name: string
|
|
15
|
-
operations: Operation
|
|
16
|
-
baseURL: string | undefined
|
|
8
|
+
operations: Array<Operation>
|
|
17
9
|
}
|
|
18
10
|
|
|
19
|
-
function
|
|
11
|
+
export function Operations({ name, operations }: OperationsProps) {
|
|
20
12
|
const operationsObject: Record<string, { path: string; method: HttpMethod }> = {}
|
|
21
13
|
|
|
22
14
|
operations.forEach((operation) => {
|
|
@@ -25,70 +17,12 @@ function Template({ name, operations }: TemplateProps): KubbNode {
|
|
|
25
17
|
method: operation.method,
|
|
26
18
|
}
|
|
27
19
|
})
|
|
28
|
-
return (
|
|
29
|
-
<Const name={name} export asConst>
|
|
30
|
-
{JSON.stringify(operationsObject, undefined, 2)}
|
|
31
|
-
</Const>
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
type RootTemplateProps = {
|
|
36
|
-
children?: React.ReactNode
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function RootTemplate({ children }: RootTemplateProps) {
|
|
40
|
-
const {
|
|
41
|
-
pluginManager,
|
|
42
|
-
plugin: { key: pluginKey },
|
|
43
|
-
} = useApp<PluginClient>()
|
|
44
|
-
const file = pluginManager.getFile({ name: 'operations', extName: '.ts', pluginKey })
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
<Parser language="typescript">
|
|
48
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta} exportable={false}>
|
|
49
|
-
<File.Source>{children}</File.Source>
|
|
50
|
-
</File>
|
|
51
|
-
</Parser>
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const defaultTemplates = { default: Template, root: RootTemplate } as const
|
|
56
|
-
|
|
57
|
-
type Templates = Partial<typeof defaultTemplates>
|
|
58
|
-
|
|
59
|
-
type Props = {
|
|
60
|
-
baseURL: string | undefined
|
|
61
|
-
/**
|
|
62
|
-
* This will make it possible to override the default behaviour.
|
|
63
|
-
*/
|
|
64
|
-
Template?: ComponentType<ComponentProps<typeof Template>>
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function Operations({ baseURL, Template = defaultTemplates.default }: Props): KubbNode {
|
|
68
|
-
const operations = useOperations()
|
|
69
|
-
|
|
70
|
-
return <Template baseURL={baseURL} name="operations" operations={operations} />
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
type FileProps = {
|
|
74
|
-
baseURL: string | undefined
|
|
75
|
-
/**
|
|
76
|
-
* This will make it possible to override the default behaviour.
|
|
77
|
-
*/
|
|
78
|
-
templates?: Templates
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
Operations.File = function ({ baseURL, ...props }: FileProps): KubbNode {
|
|
82
|
-
const templates = { ...defaultTemplates, ...props.templates }
|
|
83
|
-
|
|
84
|
-
const Template = templates.default
|
|
85
|
-
const RootTemplate = templates.root
|
|
86
20
|
|
|
87
21
|
return (
|
|
88
|
-
<
|
|
89
|
-
<
|
|
90
|
-
|
|
22
|
+
<File.Source name={name} isExportable isIndexable>
|
|
23
|
+
<Const name={name} export asConst>
|
|
24
|
+
{JSON.stringify(operationsObject, undefined, 2)}
|
|
25
|
+
</Const>
|
|
26
|
+
</File.Source>
|
|
91
27
|
)
|
|
92
28
|
}
|
|
93
|
-
|
|
94
|
-
Operations.templates = defaultTemplates
|
|
@@ -2,10 +2,7 @@
|
|
|
2
2
|
* @summary Info for a specific pet
|
|
3
3
|
* @link /pets/:pet_id
|
|
4
4
|
*/
|
|
5
|
-
export async function showPetById(
|
|
6
|
-
{
|
|
7
|
-
options: Partial<Parameters<typeof client>[0]> = {},
|
|
8
|
-
): Promise<ResponseConfig<ShowPetByIdQueryResponse>['data']> {
|
|
9
|
-
const res = await client<ShowPetByIdQueryResponse>({ method: 'get', url: `/pets/${petId}`, ...options })
|
|
5
|
+
export async function showPetById({ petId, testId }: { petId: ShowPetByIdPathParams["pet_id"]; testId: ShowPetByIdPathParams["testId"] }, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<ShowPetByIdQueryResponse>["data"]> {
|
|
6
|
+
const res = await client<ShowPetByIdQueryResponse>({ method : "get", url : `/pets/${petId}`, ... options })
|
|
10
7
|
return res.data
|
|
11
|
-
}
|
|
8
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const operations = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} as const
|
|
2
|
+
"showPetById": {
|
|
3
|
+
"path": "/pets/:pet_id",
|
|
4
|
+
"method": "get"
|
|
5
|
+
}
|
|
6
|
+
} as const
|