@genapi/presets 3.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.
- package/LICENSE.md +21 -0
- package/dist/axios/index.d.ts +9 -0
- package/dist/axios/index.mjs +7 -0
- package/dist/axios/js/config/index.d.ts +5 -0
- package/dist/axios/js/config/index.mjs +18 -0
- package/dist/axios/js/index.d.ts +6 -0
- package/dist/axios/js/index.mjs +25 -0
- package/dist/axios/js/parser/index.d.ts +12 -0
- package/dist/axios/js/parser/index.mjs +77 -0
- package/dist/axios/ts/config/index.d.ts +5 -0
- package/dist/axios/ts/config/index.mjs +22 -0
- package/dist/axios/ts/index.d.ts +6 -0
- package/dist/axios/ts/index.mjs +25 -0
- package/dist/axios/ts/parser/index.d.ts +12 -0
- package/dist/axios/ts/parser/index.mjs +76 -0
- package/dist/fetch/index.d.ts +9 -0
- package/dist/fetch/index.mjs +7 -0
- package/dist/fetch/js/config/index.d.ts +5 -0
- package/dist/fetch/js/config/index.mjs +13 -0
- package/dist/fetch/js/index.d.ts +6 -0
- package/dist/fetch/js/index.mjs +25 -0
- package/dist/fetch/js/parser/index.d.ts +12 -0
- package/dist/fetch/js/parser/index.mjs +83 -0
- package/dist/fetch/ts/config/index.d.ts +5 -0
- package/dist/fetch/ts/config/index.mjs +11 -0
- package/dist/fetch/ts/index.d.ts +6 -0
- package/dist/fetch/ts/index.mjs +25 -0
- package/dist/fetch/ts/parser/index.d.ts +12 -0
- package/dist/fetch/ts/parser/index.mjs +79 -0
- package/dist/got/index.d.ts +9 -0
- package/dist/got/index.mjs +7 -0
- package/dist/got/js/config/index.d.ts +5 -0
- package/dist/got/js/config/index.mjs +18 -0
- package/dist/got/js/index.d.ts +6 -0
- package/dist/got/js/index.mjs +25 -0
- package/dist/got/js/parser/index.d.ts +12 -0
- package/dist/got/js/parser/index.mjs +82 -0
- package/dist/got/ts/config/index.d.ts +5 -0
- package/dist/got/ts/config/index.mjs +22 -0
- package/dist/got/ts/index.d.ts +6 -0
- package/dist/got/ts/index.mjs +25 -0
- package/dist/got/ts/parser/index.d.ts +12 -0
- package/dist/got/ts/parser/index.mjs +82 -0
- package/dist/index--xiziIvG.d.ts +8 -0
- package/dist/index-BByL5W2V.d.ts +18 -0
- package/dist/index-BIRCFgYi.d.ts +18 -0
- package/dist/index-BJrp0lx3.d.ts +18 -0
- package/dist/index-BWvY4w5U.d.ts +8 -0
- package/dist/index-BnNpBsOT.d.ts +18 -0
- package/dist/index-CApSnKVf.d.ts +8 -0
- package/dist/index-CQJFGBWr.d.ts +8 -0
- package/dist/index-DBbTnSBj.d.ts +18 -0
- package/dist/index-DEC0eIj4.d.ts +18 -0
- package/dist/index-DZ7D8MkM.d.ts +18 -0
- package/dist/index-IqUGJ_D5.d.ts +8 -0
- package/dist/index-VKTbyIEw.d.ts +18 -0
- package/dist/index-pIxMzJ5o.d.ts +18 -0
- package/dist/index-vrb8mA6n.d.ts +18 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.mjs +21 -0
- package/dist/ky/index.d.ts +9 -0
- package/dist/ky/index.mjs +7 -0
- package/dist/ky/js/config/index.d.ts +5 -0
- package/dist/ky/js/config/index.mjs +18 -0
- package/dist/ky/js/index.d.ts +6 -0
- package/dist/ky/js/index.mjs +25 -0
- package/dist/ky/js/parser/index.d.ts +12 -0
- package/dist/ky/js/parser/index.mjs +79 -0
- package/dist/ky/ts/config/index.d.ts +5 -0
- package/dist/ky/ts/config/index.mjs +22 -0
- package/dist/ky/ts/index.d.ts +6 -0
- package/dist/ky/ts/index.mjs +25 -0
- package/dist/ky/ts/parser/index.d.ts +12 -0
- package/dist/ky/ts/parser/index.mjs +78 -0
- package/dist/ofetch/index.d.ts +9 -0
- package/dist/ofetch/index.mjs +7 -0
- package/dist/ofetch/js/config/index.d.ts +5 -0
- package/dist/ofetch/js/config/index.mjs +18 -0
- package/dist/ofetch/js/index.d.ts +6 -0
- package/dist/ofetch/js/index.mjs +25 -0
- package/dist/ofetch/js/parser/index.d.ts +12 -0
- package/dist/ofetch/js/parser/index.mjs +78 -0
- package/dist/ofetch/ts/config/index.d.ts +5 -0
- package/dist/ofetch/ts/config/index.mjs +22 -0
- package/dist/ofetch/ts/index.d.ts +6 -0
- package/dist/ofetch/ts/index.mjs +25 -0
- package/dist/ofetch/ts/parser/index.d.ts +12 -0
- package/dist/ofetch/ts/parser/index.mjs +76 -0
- package/package.json +71 -0
- package/src/axios/index.ts +2 -0
- package/src/axios/js/config/index.ts +19 -0
- package/src/axios/js/index.ts +19 -0
- package/src/axios/js/parser/index.ts +96 -0
- package/src/axios/ts/config/index.ts +24 -0
- package/src/axios/ts/index.ts +20 -0
- package/src/axios/ts/parser/index.ts +95 -0
- package/src/fetch/index.ts +2 -0
- package/src/fetch/js/config/index.ts +12 -0
- package/src/fetch/js/index.ts +19 -0
- package/src/fetch/js/parser/index.ts +107 -0
- package/src/fetch/ts/config/index.ts +11 -0
- package/src/fetch/ts/index.ts +20 -0
- package/src/fetch/ts/parser/index.ts +98 -0
- package/src/got/index.ts +2 -0
- package/src/got/js/config/index.ts +19 -0
- package/src/got/js/index.ts +20 -0
- package/src/got/js/parser/index.ts +101 -0
- package/src/got/ts/config/index.ts +24 -0
- package/src/got/ts/index.ts +20 -0
- package/src/got/ts/parser/index.ts +102 -0
- package/src/index.ts +21 -0
- package/src/ky/index.ts +2 -0
- package/src/ky/js/config/index.ts +19 -0
- package/src/ky/js/index.ts +20 -0
- package/src/ky/js/parser/index.ts +97 -0
- package/src/ky/ts/config/index.ts +24 -0
- package/src/ky/ts/index.ts +20 -0
- package/src/ky/ts/parser/index.ts +97 -0
- package/src/ofetch/index.ts +2 -0
- package/src/ofetch/js/config/index.ts +19 -0
- package/src/ofetch/js/index.ts +20 -0
- package/src/ofetch/js/parser/index.ts +97 -0
- package/src/ofetch/ts/config/index.ts +24 -0
- package/src/ofetch/ts/index.ts +20 -0
- package/src/ofetch/ts/parser/index.ts +96 -0
- package/tsup.config.ts +18 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { ApiPipeline, StatementFunction, StatementInterface } from '@genapi/shared'
|
|
2
|
+
import type { Paths } from 'openapi-specification-types'
|
|
3
|
+
import {
|
|
4
|
+
literalFieldsToString,
|
|
5
|
+
parseHeaderCommits,
|
|
6
|
+
parseMethodMetadata,
|
|
7
|
+
parseMethodParameters,
|
|
8
|
+
parseOpenapiSpecification,
|
|
9
|
+
transformBaseURL,
|
|
10
|
+
transformDefinitions,
|
|
11
|
+
transformParameters,
|
|
12
|
+
transformQueryParams,
|
|
13
|
+
transformUrlSyntax,
|
|
14
|
+
traversePaths,
|
|
15
|
+
} from '@genapi/parser'
|
|
16
|
+
|
|
17
|
+
export interface PathsTransformOptions {
|
|
18
|
+
configRead: ApiPipeline.ConfigRead
|
|
19
|
+
interfaces: StatementInterface[]
|
|
20
|
+
functions: StatementFunction[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function parser(configRead: ApiPipeline.ConfigRead) {
|
|
24
|
+
const source = parseOpenapiSpecification(configRead.source)
|
|
25
|
+
|
|
26
|
+
const comments = parseHeaderCommits(source)
|
|
27
|
+
|
|
28
|
+
const interfaces: StatementInterface[] = []
|
|
29
|
+
const functions: StatementFunction[] = []
|
|
30
|
+
|
|
31
|
+
transformBaseURL(source, {
|
|
32
|
+
configRead,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
transformDefinitions(source.definitions, {
|
|
36
|
+
interfaces,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
transformPaths(source.paths, {
|
|
40
|
+
configRead,
|
|
41
|
+
functions,
|
|
42
|
+
interfaces,
|
|
43
|
+
})
|
|
44
|
+
configRead.graphs.comments = comments
|
|
45
|
+
configRead.graphs.functions = functions
|
|
46
|
+
configRead.graphs.interfaces = interfaces
|
|
47
|
+
|
|
48
|
+
return configRead
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function transformPaths(paths: Paths, { configRead, functions, interfaces }: PathsTransformOptions) {
|
|
52
|
+
traversePaths(paths, (config) => {
|
|
53
|
+
/**
|
|
54
|
+
* function params/function options/function use interfaces
|
|
55
|
+
*/
|
|
56
|
+
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config, {
|
|
57
|
+
formData: 'body',
|
|
58
|
+
query: 'params',
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
let { name, description, url, responseType, body } = parseMethodMetadata(config)
|
|
62
|
+
|
|
63
|
+
interfaces.push(...attachInters)
|
|
64
|
+
parameters.push({
|
|
65
|
+
name: 'options',
|
|
66
|
+
type: 'import(\'ofetch\').FetchOptions',
|
|
67
|
+
required: false,
|
|
68
|
+
})
|
|
69
|
+
options.push(['...', 'options'])
|
|
70
|
+
options.unshift(['method', `"${config.method}"`])
|
|
71
|
+
if (configRead.config.baseURL)
|
|
72
|
+
options.unshift('baseURL')
|
|
73
|
+
|
|
74
|
+
transformParameters(parameters, {
|
|
75
|
+
syntax: 'ecmascript',
|
|
76
|
+
configRead,
|
|
77
|
+
description,
|
|
78
|
+
interfaces,
|
|
79
|
+
responseType,
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
url = transformQueryParams('query', { body, options, url })
|
|
83
|
+
url = transformUrlSyntax(url, { baseURL: configRead.config.baseURL })
|
|
84
|
+
|
|
85
|
+
functions.push({
|
|
86
|
+
export: true,
|
|
87
|
+
async: true,
|
|
88
|
+
name,
|
|
89
|
+
description,
|
|
90
|
+
parameters,
|
|
91
|
+
body: [
|
|
92
|
+
...body,
|
|
93
|
+
`return ofetch(${url}, {${literalFieldsToString(options)}})`,
|
|
94
|
+
],
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ApiPipeline } from '@genapi/shared'
|
|
2
|
+
import { config as _config } from '@genapi/pipeline'
|
|
3
|
+
|
|
4
|
+
export function config(userConfig: ApiPipeline.Config): ApiPipeline.ConfigRead {
|
|
5
|
+
userConfig.import = userConfig.import || {}
|
|
6
|
+
userConfig.import.http = userConfig.import.http || 'ofetch'
|
|
7
|
+
|
|
8
|
+
const configRead = _config(userConfig)
|
|
9
|
+
|
|
10
|
+
configRead.graphs.imports.push({
|
|
11
|
+
name: 'ofetch',
|
|
12
|
+
names: userConfig.import.http === 'ofetch' ? ['FetchOptions'] : undefined,
|
|
13
|
+
value: userConfig.import.http,
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
if (userConfig.import.http !== 'ofetch') {
|
|
17
|
+
configRead.graphs.imports.push({
|
|
18
|
+
names: ['FetchOptions'],
|
|
19
|
+
value: 'ofetch',
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return configRead
|
|
24
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ApiPipeline } from '@genapi/shared'
|
|
2
|
+
import pipeline, { compiler, dest, generate, original } from '@genapi/pipeline'
|
|
3
|
+
|
|
4
|
+
import { config } from './config'
|
|
5
|
+
import { parser } from './parser'
|
|
6
|
+
|
|
7
|
+
function openapiPipeline(userConfig: ApiPipeline.Config) {
|
|
8
|
+
const process = pipeline(
|
|
9
|
+
userConfig => config(userConfig),
|
|
10
|
+
configRead => original(configRead),
|
|
11
|
+
configRead => parser(configRead),
|
|
12
|
+
configRead => compiler(configRead),
|
|
13
|
+
configRead => generate(configRead),
|
|
14
|
+
configRead => dest(configRead),
|
|
15
|
+
)
|
|
16
|
+
return process(userConfig)
|
|
17
|
+
}
|
|
18
|
+
export { compiler, config, dest, generate, original, parser }
|
|
19
|
+
|
|
20
|
+
export default openapiPipeline
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { ApiPipeline, StatementFunction, StatementInterface } from '@genapi/shared'
|
|
2
|
+
import type { Paths } from 'openapi-specification-types'
|
|
3
|
+
import {
|
|
4
|
+
literalFieldsToString,
|
|
5
|
+
parseHeaderCommits,
|
|
6
|
+
parseMethodMetadata,
|
|
7
|
+
parseMethodParameters,
|
|
8
|
+
parseOpenapiSpecification,
|
|
9
|
+
transformBaseURL,
|
|
10
|
+
transformDefinitions,
|
|
11
|
+
transformParameters,
|
|
12
|
+
transformQueryParams,
|
|
13
|
+
transformUrlSyntax,
|
|
14
|
+
traversePaths,
|
|
15
|
+
} from '@genapi/parser'
|
|
16
|
+
|
|
17
|
+
export interface PathsTransformOptions {
|
|
18
|
+
configRead: ApiPipeline.ConfigRead
|
|
19
|
+
interfaces: StatementInterface[]
|
|
20
|
+
functions: StatementFunction[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function parser(configRead: ApiPipeline.ConfigRead) {
|
|
24
|
+
const source = parseOpenapiSpecification(configRead.source)
|
|
25
|
+
|
|
26
|
+
const comments = parseHeaderCommits(source)
|
|
27
|
+
|
|
28
|
+
const interfaces: StatementInterface[] = []
|
|
29
|
+
const functions: StatementFunction[] = []
|
|
30
|
+
|
|
31
|
+
transformBaseURL(source, {
|
|
32
|
+
configRead,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
transformDefinitions(source.definitions, {
|
|
36
|
+
interfaces,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
transformPaths(source.paths, {
|
|
40
|
+
configRead,
|
|
41
|
+
functions,
|
|
42
|
+
interfaces,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
configRead.graphs.comments = comments
|
|
46
|
+
configRead.graphs.functions = functions
|
|
47
|
+
configRead.graphs.interfaces = interfaces
|
|
48
|
+
|
|
49
|
+
return configRead
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function transformPaths(paths: Paths, { configRead, functions, interfaces }: PathsTransformOptions) {
|
|
53
|
+
traversePaths(paths, (config) => {
|
|
54
|
+
/**
|
|
55
|
+
* function params/function options/function use interfaces
|
|
56
|
+
*/
|
|
57
|
+
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config, {
|
|
58
|
+
formData: 'body',
|
|
59
|
+
query: 'params',
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
let { name, description, url, responseType, body } = parseMethodMetadata(config)
|
|
63
|
+
|
|
64
|
+
interfaces.push(...attachInters)
|
|
65
|
+
parameters.push({
|
|
66
|
+
name: 'options',
|
|
67
|
+
type: 'FetchOptions',
|
|
68
|
+
required: false,
|
|
69
|
+
})
|
|
70
|
+
options.push(['...', 'options'])
|
|
71
|
+
options.unshift(['method', `"${config.method}"`])
|
|
72
|
+
if (configRead.config.baseURL)
|
|
73
|
+
options.unshift('baseURL')
|
|
74
|
+
|
|
75
|
+
const { spaceResponseType } = transformParameters(parameters, {
|
|
76
|
+
syntax: 'typescript',
|
|
77
|
+
configRead,
|
|
78
|
+
description,
|
|
79
|
+
interfaces,
|
|
80
|
+
responseType,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
url = transformQueryParams('query', { body, options, url })
|
|
84
|
+
url = transformUrlSyntax(url)
|
|
85
|
+
|
|
86
|
+
functions.push({
|
|
87
|
+
export: true,
|
|
88
|
+
name,
|
|
89
|
+
description,
|
|
90
|
+
parameters,
|
|
91
|
+
body: [
|
|
92
|
+
`return ofetch<${spaceResponseType}>(${url}, {${literalFieldsToString(options)}})`,
|
|
93
|
+
],
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { esbuildPluginFilePathExtensions } from 'esbuild-plugin-file-path-extensions'
|
|
2
|
+
import { defineConfig } from 'tsup'
|
|
3
|
+
import { dependencies, name } from './package.json'
|
|
4
|
+
|
|
5
|
+
export default defineConfig((_options) => {
|
|
6
|
+
return {
|
|
7
|
+
esbuildPlugins: [esbuildPluginFilePathExtensions()],
|
|
8
|
+
outExtension: () => ({ js: '.mjs' }),
|
|
9
|
+
entry: ['src/**/*.ts'],
|
|
10
|
+
format: ['esm'],
|
|
11
|
+
clean: true,
|
|
12
|
+
dts: true,
|
|
13
|
+
name,
|
|
14
|
+
bundle: true,
|
|
15
|
+
// minify: !options.watch,
|
|
16
|
+
external: Object.keys(dependencies || {}),
|
|
17
|
+
}
|
|
18
|
+
})
|