@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.
Files changed (126) hide show
  1. package/LICENSE.md +21 -0
  2. package/dist/axios/index.d.ts +9 -0
  3. package/dist/axios/index.mjs +7 -0
  4. package/dist/axios/js/config/index.d.ts +5 -0
  5. package/dist/axios/js/config/index.mjs +18 -0
  6. package/dist/axios/js/index.d.ts +6 -0
  7. package/dist/axios/js/index.mjs +25 -0
  8. package/dist/axios/js/parser/index.d.ts +12 -0
  9. package/dist/axios/js/parser/index.mjs +77 -0
  10. package/dist/axios/ts/config/index.d.ts +5 -0
  11. package/dist/axios/ts/config/index.mjs +22 -0
  12. package/dist/axios/ts/index.d.ts +6 -0
  13. package/dist/axios/ts/index.mjs +25 -0
  14. package/dist/axios/ts/parser/index.d.ts +12 -0
  15. package/dist/axios/ts/parser/index.mjs +76 -0
  16. package/dist/fetch/index.d.ts +9 -0
  17. package/dist/fetch/index.mjs +7 -0
  18. package/dist/fetch/js/config/index.d.ts +5 -0
  19. package/dist/fetch/js/config/index.mjs +13 -0
  20. package/dist/fetch/js/index.d.ts +6 -0
  21. package/dist/fetch/js/index.mjs +25 -0
  22. package/dist/fetch/js/parser/index.d.ts +12 -0
  23. package/dist/fetch/js/parser/index.mjs +83 -0
  24. package/dist/fetch/ts/config/index.d.ts +5 -0
  25. package/dist/fetch/ts/config/index.mjs +11 -0
  26. package/dist/fetch/ts/index.d.ts +6 -0
  27. package/dist/fetch/ts/index.mjs +25 -0
  28. package/dist/fetch/ts/parser/index.d.ts +12 -0
  29. package/dist/fetch/ts/parser/index.mjs +79 -0
  30. package/dist/got/index.d.ts +9 -0
  31. package/dist/got/index.mjs +7 -0
  32. package/dist/got/js/config/index.d.ts +5 -0
  33. package/dist/got/js/config/index.mjs +18 -0
  34. package/dist/got/js/index.d.ts +6 -0
  35. package/dist/got/js/index.mjs +25 -0
  36. package/dist/got/js/parser/index.d.ts +12 -0
  37. package/dist/got/js/parser/index.mjs +82 -0
  38. package/dist/got/ts/config/index.d.ts +5 -0
  39. package/dist/got/ts/config/index.mjs +22 -0
  40. package/dist/got/ts/index.d.ts +6 -0
  41. package/dist/got/ts/index.mjs +25 -0
  42. package/dist/got/ts/parser/index.d.ts +12 -0
  43. package/dist/got/ts/parser/index.mjs +82 -0
  44. package/dist/index--xiziIvG.d.ts +8 -0
  45. package/dist/index-BByL5W2V.d.ts +18 -0
  46. package/dist/index-BIRCFgYi.d.ts +18 -0
  47. package/dist/index-BJrp0lx3.d.ts +18 -0
  48. package/dist/index-BWvY4w5U.d.ts +8 -0
  49. package/dist/index-BnNpBsOT.d.ts +18 -0
  50. package/dist/index-CApSnKVf.d.ts +8 -0
  51. package/dist/index-CQJFGBWr.d.ts +8 -0
  52. package/dist/index-DBbTnSBj.d.ts +18 -0
  53. package/dist/index-DEC0eIj4.d.ts +18 -0
  54. package/dist/index-DZ7D8MkM.d.ts +18 -0
  55. package/dist/index-IqUGJ_D5.d.ts +8 -0
  56. package/dist/index-VKTbyIEw.d.ts +18 -0
  57. package/dist/index-pIxMzJ5o.d.ts +18 -0
  58. package/dist/index-vrb8mA6n.d.ts +18 -0
  59. package/dist/index.d.ts +48 -0
  60. package/dist/index.mjs +21 -0
  61. package/dist/ky/index.d.ts +9 -0
  62. package/dist/ky/index.mjs +7 -0
  63. package/dist/ky/js/config/index.d.ts +5 -0
  64. package/dist/ky/js/config/index.mjs +18 -0
  65. package/dist/ky/js/index.d.ts +6 -0
  66. package/dist/ky/js/index.mjs +25 -0
  67. package/dist/ky/js/parser/index.d.ts +12 -0
  68. package/dist/ky/js/parser/index.mjs +79 -0
  69. package/dist/ky/ts/config/index.d.ts +5 -0
  70. package/dist/ky/ts/config/index.mjs +22 -0
  71. package/dist/ky/ts/index.d.ts +6 -0
  72. package/dist/ky/ts/index.mjs +25 -0
  73. package/dist/ky/ts/parser/index.d.ts +12 -0
  74. package/dist/ky/ts/parser/index.mjs +78 -0
  75. package/dist/ofetch/index.d.ts +9 -0
  76. package/dist/ofetch/index.mjs +7 -0
  77. package/dist/ofetch/js/config/index.d.ts +5 -0
  78. package/dist/ofetch/js/config/index.mjs +18 -0
  79. package/dist/ofetch/js/index.d.ts +6 -0
  80. package/dist/ofetch/js/index.mjs +25 -0
  81. package/dist/ofetch/js/parser/index.d.ts +12 -0
  82. package/dist/ofetch/js/parser/index.mjs +78 -0
  83. package/dist/ofetch/ts/config/index.d.ts +5 -0
  84. package/dist/ofetch/ts/config/index.mjs +22 -0
  85. package/dist/ofetch/ts/index.d.ts +6 -0
  86. package/dist/ofetch/ts/index.mjs +25 -0
  87. package/dist/ofetch/ts/parser/index.d.ts +12 -0
  88. package/dist/ofetch/ts/parser/index.mjs +76 -0
  89. package/package.json +71 -0
  90. package/src/axios/index.ts +2 -0
  91. package/src/axios/js/config/index.ts +19 -0
  92. package/src/axios/js/index.ts +19 -0
  93. package/src/axios/js/parser/index.ts +96 -0
  94. package/src/axios/ts/config/index.ts +24 -0
  95. package/src/axios/ts/index.ts +20 -0
  96. package/src/axios/ts/parser/index.ts +95 -0
  97. package/src/fetch/index.ts +2 -0
  98. package/src/fetch/js/config/index.ts +12 -0
  99. package/src/fetch/js/index.ts +19 -0
  100. package/src/fetch/js/parser/index.ts +107 -0
  101. package/src/fetch/ts/config/index.ts +11 -0
  102. package/src/fetch/ts/index.ts +20 -0
  103. package/src/fetch/ts/parser/index.ts +98 -0
  104. package/src/got/index.ts +2 -0
  105. package/src/got/js/config/index.ts +19 -0
  106. package/src/got/js/index.ts +20 -0
  107. package/src/got/js/parser/index.ts +101 -0
  108. package/src/got/ts/config/index.ts +24 -0
  109. package/src/got/ts/index.ts +20 -0
  110. package/src/got/ts/parser/index.ts +102 -0
  111. package/src/index.ts +21 -0
  112. package/src/ky/index.ts +2 -0
  113. package/src/ky/js/config/index.ts +19 -0
  114. package/src/ky/js/index.ts +20 -0
  115. package/src/ky/js/parser/index.ts +97 -0
  116. package/src/ky/ts/config/index.ts +24 -0
  117. package/src/ky/ts/index.ts +20 -0
  118. package/src/ky/ts/parser/index.ts +97 -0
  119. package/src/ofetch/index.ts +2 -0
  120. package/src/ofetch/js/config/index.ts +19 -0
  121. package/src/ofetch/js/index.ts +20 -0
  122. package/src/ofetch/js/parser/index.ts +97 -0
  123. package/src/ofetch/ts/config/index.ts +24 -0
  124. package/src/ofetch/ts/index.ts +20 -0
  125. package/src/ofetch/ts/parser/index.ts +96 -0
  126. package/tsup.config.ts +18 -0
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@genapi/presets",
3
+ "type": "module",
4
+ "version": "3.0.0",
5
+ "author": "Hairyf <wwu710632@gmail.com>",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/hairyf/genapi#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/hairyf/genapi.git"
11
+ },
12
+ "bugs": "https://github.com/hairyf/genapi/issues",
13
+ "keywords": [
14
+ "genapi",
15
+ "shared"
16
+ ],
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": "./dist/index.mjs",
20
+ "./swag-axios-js": {
21
+ "types": "./dist/axios/js/index.d.ts",
22
+ "import": "./dist/axios/js/index.js"
23
+ },
24
+ "./swag-axios-ts": {
25
+ "types": "./dist/axios/ts/index.d.ts",
26
+ "import": "./dist/axios/ts/index.js"
27
+ },
28
+ "./swag-fetch-js": {
29
+ "types": "./dist/fetch/js/index.d.ts",
30
+ "import": "./dist/fetch/js/index.js"
31
+ },
32
+ "./swag-fetch-ts": {
33
+ "types": "./dist/fetch/ts/index.d.ts",
34
+ "import": "./dist/fetch/ts/index.js"
35
+ },
36
+ "./swag-got-js": {
37
+ "types": "./dist/got/js/index.d.ts",
38
+ "import": "./dist/got/js/index.js"
39
+ },
40
+ "./swag-got-ts": {
41
+ "types": "./dist/got/ts/index.d.ts",
42
+ "import": "./dist/got/ts/index.js"
43
+ },
44
+ "./swag-ky-js": {
45
+ "types": "./dist/ky/js/index.d.ts",
46
+ "import": "./dist/ky/js/index.js"
47
+ },
48
+ "./swag-ky-ts": {
49
+ "types": "./dist/ky/ts/index.d.ts",
50
+ "import": "./dist/ky/ts/index.js"
51
+ },
52
+ "./swag-ofetch-js": {
53
+ "types": "./dist/ofetch/js/index.d.ts",
54
+ "import": "./dist/ofetch/js/index.js"
55
+ },
56
+ "./swag-ofetch-ts": {
57
+ "types": "./dist/ofetch/ts/index.d.ts",
58
+ "import": "./dist/ofetch/ts/index.js"
59
+ }
60
+ },
61
+ "dependencies": {
62
+ "openapi-specification-types": "^0.0.3",
63
+ "@genapi/shared": "3.0.0",
64
+ "@genapi/pipeline": "3.0.0",
65
+ "@genapi/parser": "3.0.0"
66
+ },
67
+ "scripts": {
68
+ "build": "tsup",
69
+ "start": "tsx src/index.ts"
70
+ }
71
+ }
@@ -0,0 +1,2 @@
1
+ export * as js from './js'
2
+ export * as ts from './ts'
@@ -0,0 +1,19 @@
1
+ import type { ApiPipeline } from '@genapi/shared'
2
+ import { replaceMainext } from '@genapi/parser'
3
+ import { config as _config } from '@genapi/pipeline'
4
+
5
+ export function config(userConfig: ApiPipeline.Config): ApiPipeline.ConfigRead {
6
+ userConfig.import = userConfig.import || {}
7
+ userConfig.output = userConfig.output || {}
8
+ userConfig.import.http = userConfig.import.http || 'axios'
9
+ userConfig.output = replaceMainext(userConfig.output) || 'src/api/index.js'
10
+
11
+ const configRead = _config(userConfig)
12
+
13
+ configRead.graphs.imports.push({
14
+ name: 'http',
15
+ value: userConfig.import.http,
16
+ })
17
+
18
+ return configRead
19
+ }
@@ -0,0 +1,19 @@
1
+ import type { ApiPipeline } from '@genapi/shared'
2
+ import pipeline, { compiler, dest, generate, original } from '@genapi/pipeline'
3
+ import { config } from './config'
4
+ import { parser } from './parser'
5
+
6
+ function openapiPipeline(userConfig: ApiPipeline.Config) {
7
+ const process = pipeline(
8
+ userConfig => config(userConfig),
9
+ configRead => original(configRead),
10
+ configRead => parser(configRead),
11
+ configRead => compiler(configRead),
12
+ configRead => generate(configRead),
13
+ configRead => dest(configRead),
14
+ )
15
+ return process(userConfig)
16
+ }
17
+ export { compiler, config, dest, generate, original, parser }
18
+
19
+ 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
+ transformUrlSyntax,
13
+ traversePaths,
14
+ } from '@genapi/parser'
15
+
16
+ export interface PathsTransformOptions {
17
+ configRead: ApiPipeline.ConfigRead
18
+ interfaces: StatementInterface[]
19
+ functions: StatementFunction[]
20
+ }
21
+
22
+ export function parser(configRead: ApiPipeline.ConfigRead) {
23
+ const source = parseOpenapiSpecification(configRead.source)
24
+
25
+ const comments = parseHeaderCommits(source)
26
+
27
+ const interfaces: StatementInterface[] = []
28
+ const functions: StatementFunction[] = []
29
+
30
+ transformBaseURL(source, {
31
+ configRead,
32
+ })
33
+
34
+ transformDefinitions(source.definitions, {
35
+ interfaces,
36
+ })
37
+
38
+ transformPaths(source.paths, {
39
+ configRead,
40
+ functions,
41
+ interfaces,
42
+ })
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
+ body: 'data',
58
+ query: 'params',
59
+ })
60
+ let { name, description, url, responseType } = parseMethodMetadata(config)
61
+
62
+ options.push(['...', 'config'])
63
+ interfaces.push(...attachInters)
64
+ parameters.push({
65
+ type: 'import(\'axios\').AxiosRequestConfig',
66
+ name: 'config',
67
+ required: false,
68
+ })
69
+ options.unshift('url')
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
+ generic: 'import(\'axios\').AxiosResponse<{__type__}>',
81
+ })
82
+
83
+ url = transformUrlSyntax(url)
84
+
85
+ functions.push({
86
+ export: true,
87
+ name,
88
+ description,
89
+ parameters,
90
+ body: [
91
+ `const url = ${url}`,
92
+ `return http.request({ ${literalFieldsToString(options)} })`,
93
+ ],
94
+ })
95
+ })
96
+ }
@@ -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 || 'axios'
7
+
8
+ const configRead = _config(userConfig)
9
+
10
+ configRead.graphs.imports.push({
11
+ name: 'http',
12
+ names: userConfig.import.http === 'axios' ? ['AxiosRequestConfig'] : undefined,
13
+ value: userConfig.import.http,
14
+ })
15
+
16
+ if (userConfig.import.http !== 'axios') {
17
+ configRead.graphs.imports.push({
18
+ names: ['AxiosRequestConfig'],
19
+ value: 'axios',
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,95 @@
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
+ transformUrlSyntax,
13
+ traversePaths,
14
+ } from '@genapi/parser'
15
+
16
+ export interface PathsTransformOptions {
17
+ configRead: ApiPipeline.ConfigRead
18
+ interfaces: StatementInterface[]
19
+ functions: StatementFunction[]
20
+ }
21
+
22
+ export function parser(configRead: ApiPipeline.ConfigRead) {
23
+ const source = parseOpenapiSpecification(configRead.source)
24
+
25
+ const comments = parseHeaderCommits(source)
26
+
27
+ const interfaces: StatementInterface[] = []
28
+ const functions: StatementFunction[] = []
29
+
30
+ transformBaseURL(source, {
31
+ configRead,
32
+ })
33
+
34
+ transformDefinitions(source.definitions, {
35
+ interfaces,
36
+ })
37
+
38
+ transformPaths(source.paths, {
39
+ configRead,
40
+ functions,
41
+ interfaces,
42
+ })
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
+ body: 'data',
58
+ query: 'params',
59
+ })
60
+
61
+ let { name, description, url, responseType } = parseMethodMetadata(config)
62
+
63
+ options.push(['...', 'config'])
64
+ interfaces.push(...attachInters)
65
+ parameters.push({
66
+ name: 'config',
67
+ type: 'AxiosRequestConfig',
68
+ required: false,
69
+ })
70
+ options.unshift('url')
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
+ url = transformUrlSyntax(url)
83
+
84
+ functions.push({
85
+ export: true,
86
+ name,
87
+ description,
88
+ parameters,
89
+ body: [
90
+ `const url = ${url}`,
91
+ `return http.request<${spaceResponseType}>({ ${literalFieldsToString(options)} })`,
92
+ ],
93
+ })
94
+ })
95
+ }
@@ -0,0 +1,2 @@
1
+ export * as js from './js'
2
+ export * as ts from './ts'
@@ -0,0 +1,12 @@
1
+ import type { ApiPipeline } from '@genapi/shared'
2
+ import { replaceMainext } from '@genapi/parser'
3
+ import { config as _config } from '@genapi/pipeline'
4
+
5
+ export function config(userConfig: ApiPipeline.Config): ApiPipeline.ConfigRead {
6
+ userConfig.import = userConfig.import || {}
7
+ userConfig.output = userConfig.output || {}
8
+ userConfig.output = replaceMainext(userConfig.output) || 'src/api/index.js'
9
+ const configRead = _config(userConfig)
10
+
11
+ return configRead
12
+ }
@@ -0,0 +1,19 @@
1
+ import type { ApiPipeline } from '@genapi/shared'
2
+ import pipeline, { compiler, dest, generate, original } from '@genapi/pipeline'
3
+ import { config } from './config'
4
+ import { parser } from './parser'
5
+
6
+ function openapiPipeline(userConfig: ApiPipeline.Config) {
7
+ const process = pipeline(
8
+ userConfig => config(userConfig),
9
+ configRead => original(configRead),
10
+ configRead => parser(configRead),
11
+ configRead => compiler(configRead),
12
+ configRead => generate(configRead),
13
+ configRead => dest(configRead),
14
+ )
15
+ return process(userConfig)
16
+ }
17
+ export { compiler, config, dest, generate, original, parser }
18
+
19
+ export default openapiPipeline
@@ -0,0 +1,107 @@
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
+ transformBodyStringify,
11
+ transformDefinitions,
12
+ transformHeaderOptions,
13
+ transformParameters,
14
+ transformQueryParams,
15
+ transformUrlSyntax,
16
+ traversePaths,
17
+ } from '@genapi/parser'
18
+
19
+ export interface PathsTransformOptions {
20
+ configRead: ApiPipeline.ConfigRead
21
+ interfaces: StatementInterface[]
22
+ functions: StatementFunction[]
23
+ }
24
+
25
+ export function parser(configRead: ApiPipeline.ConfigRead) {
26
+ const source = parseOpenapiSpecification(configRead.source)
27
+
28
+ const comments = parseHeaderCommits(source)
29
+
30
+ const interfaces: StatementInterface[] = []
31
+ const functions: StatementFunction[] = []
32
+
33
+ transformBaseURL(source, {
34
+ configRead,
35
+ })
36
+
37
+ transformDefinitions(source.definitions, {
38
+ interfaces,
39
+ })
40
+
41
+ transformPaths(source.paths, {
42
+ configRead,
43
+ functions,
44
+ interfaces,
45
+ })
46
+
47
+ configRead.graphs.comments = comments
48
+ configRead.graphs.functions = functions
49
+ configRead.graphs.interfaces = interfaces
50
+
51
+ return configRead
52
+ }
53
+
54
+ export function transformPaths(paths: Paths, { configRead, functions, interfaces }: PathsTransformOptions) {
55
+ traversePaths(paths, (config) => {
56
+ /**
57
+ * function params/function options/function use interfaces
58
+ */
59
+ const { parameters, interfaces: attachInters, options } = parseMethodParameters(config, {
60
+ formData: 'body',
61
+ })
62
+
63
+ let { name, description, url, responseType, body } = parseMethodMetadata(config)
64
+
65
+ interfaces.push(...attachInters)
66
+ parameters.push({
67
+ name: 'config',
68
+ type: 'RequestInit',
69
+ required: false,
70
+ })
71
+
72
+ if (config.method.toLowerCase() !== 'get')
73
+ options.unshift(['method', `"${config.method}"`])
74
+
75
+ transformHeaderOptions('body', { options, parameters })
76
+
77
+ options.push(['...', 'config'])
78
+
79
+ transformParameters(parameters, {
80
+ syntax: 'ecmascript',
81
+ configRead,
82
+ description,
83
+ interfaces,
84
+ responseType,
85
+ })
86
+
87
+ transformBodyStringify('body', { options, parameters })
88
+
89
+ url = transformQueryParams('query', { body, options, url })
90
+ url = transformUrlSyntax(url, { baseURL: configRead.config.baseURL })
91
+
92
+ functions.push({
93
+ export: true,
94
+ async: true,
95
+ name,
96
+ description,
97
+ parameters,
98
+ body: [
99
+ ...body,
100
+ `const response = await fetch(${url}, {
101
+ ${literalFieldsToString(options)}
102
+ })`,
103
+ 'return response.json()',
104
+ ],
105
+ })
106
+ })
107
+ }
@@ -0,0 +1,11 @@
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 || 'axios'
7
+
8
+ const configRead = _config(userConfig)
9
+
10
+ return configRead
11
+ }
@@ -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,98 @@
1
+ import type { ApiPipeline, StatementFunction, StatementInterface } from '@genapi/shared'
2
+ import type { Paths } from 'openapi-specification-types'
3
+ import {
4
+ parseHeaderCommits,
5
+ parseMethodMetadata,
6
+ parseMethodParameters,
7
+ parseOpenapiSpecification,
8
+ transformBaseURL,
9
+ transformBodyStringify,
10
+ transformDefinitions,
11
+ transformFetchBody,
12
+ transformHeaderOptions,
13
+ transformParameters,
14
+ transformQueryParams,
15
+ transformUrlSyntax,
16
+ traversePaths,
17
+ } from '@genapi/parser'
18
+
19
+ export interface PathsTransformOptions {
20
+ configRead: ApiPipeline.ConfigRead
21
+ interfaces: StatementInterface[]
22
+ functions: StatementFunction[]
23
+ }
24
+
25
+ export function parser(configRead: ApiPipeline.ConfigRead) {
26
+ const source = parseOpenapiSpecification(configRead.source)
27
+ const comments = parseHeaderCommits(source)
28
+
29
+ const interfaces: StatementInterface[] = []
30
+ const functions: StatementFunction[] = []
31
+
32
+ transformBaseURL(source, {
33
+ configRead,
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
+ let { name, description, url, responseType, body } = parseMethodMetadata(config)
59
+
60
+ interfaces.push(...attachInters)
61
+ parameters.push({
62
+ name: 'config',
63
+ type: 'RequestInit',
64
+ required: false,
65
+ })
66
+
67
+ if (config.method.toLowerCase() !== 'get')
68
+ options.unshift(['method', `"${config.method}"`])
69
+
70
+ transformHeaderOptions('body', { options, parameters })
71
+
72
+ options.push(['...', 'config'])
73
+
74
+ const { spaceResponseType } = transformParameters(parameters, {
75
+ syntax: 'typescript',
76
+ configRead,
77
+ description,
78
+ interfaces,
79
+ responseType,
80
+ })
81
+
82
+ transformBodyStringify('body', { options, parameters })
83
+ url = transformQueryParams('query', { body, options, url })
84
+ url = transformUrlSyntax(url, { baseURL: configRead.config.baseURL })
85
+ const fetch = transformFetchBody(url, options, spaceResponseType)
86
+ functions.push({
87
+ export: true,
88
+ async: true,
89
+ name,
90
+ description,
91
+ parameters,
92
+ body: [
93
+ ...body,
94
+ ...fetch,
95
+ ],
96
+ })
97
+ })
98
+ }
@@ -0,0 +1,2 @@
1
+ export * as js from './js'
2
+ export * as ts from './ts'
@@ -0,0 +1,19 @@
1
+ import type { ApiPipeline } from '@genapi/shared'
2
+ import { replaceMainext } from '@genapi/parser'
3
+ import { config as _config } from '@genapi/pipeline'
4
+
5
+ export function config(userConfig: ApiPipeline.Config): ApiPipeline.ConfigRead {
6
+ userConfig.import = userConfig.import || {}
7
+ userConfig.output = userConfig.output || {}
8
+ userConfig.import.http = userConfig.import.http || 'got'
9
+ userConfig.output = replaceMainext(userConfig.output) || 'src/api/index.js'
10
+
11
+ const configRead = _config(userConfig)
12
+
13
+ configRead.graphs.imports.push({
14
+ name: 'http',
15
+ value: userConfig.import.http,
16
+ })
17
+
18
+ return configRead
19
+ }
@@ -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