@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
@@ -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
+ })