@1szx1/vite-plugin-swagger2ts 1.0.6 → 1.0.8

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@1szx1/vite-plugin-swagger2ts",
3
3
  "description": "fork for https://github.com/1SZX1/vite-plugin-swagger2ts",
4
- "version": "1.0.6",
4
+ "version": "1.0.8",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "index.d.ts",
@@ -18,9 +18,21 @@
18
18
  "src",
19
19
  "index.d.ts"
20
20
  ],
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "require": "./dist/index.js",
25
+ "import": "./dist/index.mjs"
26
+ },
27
+ "./adapters": {
28
+ "types": "./dist/adapters/index.d.ts",
29
+ "require": "./dist/adapters/index.js",
30
+ "import": "./dist/adapters/index.mjs"
31
+ }
32
+ },
21
33
  "scripts": {
22
34
  "dev": "pnpm run build -- --watch --ignore-watch examples",
23
- "build": "tsup src/index.ts --dts --format cjs,esm",
35
+ "build": "tsup src/index.ts src/adapters/index.ts --dts --format cjs,esm",
24
36
  "prepublishOnly": "pnpm run build",
25
37
  "release": "pnpx git-ensure -a && pnpx bumpp --commit --tag --push",
26
38
  "demo:dev": "pnpm -C examples/demo run dev",
@@ -47,6 +59,7 @@
47
59
  "@types/node": "^14.18.12",
48
60
  "@typescript-eslint/eslint-plugin": "^4.33.0",
49
61
  "@typescript-eslint/parser": "^5.14.0",
62
+ "axios": "^1.3.4",
50
63
  "conventional-changelog": "^3.1.25",
51
64
  "cz-conventional-changelog": "^3.3.0",
52
65
  "eslint": "^7.32.0",
@@ -83,4 +96,4 @@
83
96
  "path": "./node_modules/cz-conventional-changelog"
84
97
  }
85
98
  }
86
- }
99
+ }
@@ -0,0 +1,41 @@
1
+ import type { AxiosInstance, AxiosRequestConfig } from 'axios'
2
+ import {Get, mergeUrlParam} from './utils'
3
+
4
+ function axiosAdapter<T>(httpInstance: AxiosInstance) {
5
+ type UrlKey = string & keyof T
6
+ type MethodKey<U extends UrlKey> = string & keyof T[U]
7
+
8
+ type SwaggerInterfaceSingle<U extends UrlKey, M extends MethodKey<U>> = T[U][M]
9
+ type SwaggerField<U extends UrlKey, M extends MethodKey<U>> = keyof SwaggerInterfaceSingle<U, M>
10
+ type SwaggerFieldType<U extends UrlKey, M extends MethodKey<U>, F extends SwaggerField<U, M>> = SwaggerInterfaceSingle<U, M>[F]
11
+
12
+ type Param<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, 'param' & SwaggerField<U, M>>
13
+ type Response<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, 'response' & SwaggerField<U, M>>
14
+
15
+ async function request<U extends UrlKey, M extends MethodKey<U>>(
16
+ url: U, method: M,
17
+ param?: Get<Param<U, M>, M extends 'get' ? 'query' : 'body'>,
18
+ config?: AxiosRequestConfig & {
19
+ data?: Get<Param<U, M>, 'body'>
20
+ params?: Get<Param<U, M>, 'query'>
21
+ path?: Get<Param<U, M>, 'path'>
22
+ },
23
+ ) {
24
+ if (config?.path)
25
+ (url as string) = mergeUrlParam(url, config.path)
26
+
27
+ const response = await httpInstance.request<Response<U, M>>({
28
+ url,
29
+ method,
30
+ params: method === 'get' ? param : config?.params,
31
+ data: method !== 'get' ? param : config?.data,
32
+ ...config,
33
+ })
34
+ return response.data
35
+ }
36
+
37
+ return { request }
38
+ }
39
+
40
+
41
+ export { axiosAdapter }
@@ -0,0 +1 @@
1
+ export * from './axios'
@@ -0,0 +1,5 @@
1
+ export type Get<T, K> = T[K & keyof T]
2
+
3
+ export function mergeUrlParam(url: string, paths: Record<string, any>) {
4
+ return url.replace(/{(.*?)}/g, (_, key) => paths[key])
5
+ }