@voxgig/apidef 0.0.2
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 +21 -0
- package/README.md +2 -0
- package/dist/apidef.d.ts +18 -0
- package/dist/apidef.js +137 -0
- package/dist/apidef.js.map +1 -0
- package/package.json +50 -0
- package/src/apidef.ts +179 -0
- package/src/tsconfig.json +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Voxgig Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/apidef.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type ApiDefOptions = {
|
|
2
|
+
fs?: any;
|
|
3
|
+
};
|
|
4
|
+
declare function ApiDef(opts?: ApiDefOptions): {
|
|
5
|
+
watch: (spec: any) => Promise<void>;
|
|
6
|
+
generate: (spec: any) => Promise<{
|
|
7
|
+
ok: boolean;
|
|
8
|
+
model: {
|
|
9
|
+
main: {
|
|
10
|
+
api: {
|
|
11
|
+
entity: {};
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
export type { ApiDefOptions, };
|
|
18
|
+
export { ApiDef, };
|
package/dist/apidef.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2024 Richard Rodger, MIT License */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.ApiDef = ApiDef;
|
|
28
|
+
const Fs = __importStar(require("node:fs"));
|
|
29
|
+
const openapi_core_1 = require("@redocly/openapi-core");
|
|
30
|
+
const chokidar_1 = require("chokidar");
|
|
31
|
+
const jostraca_1 = require("jostraca");
|
|
32
|
+
function ApiDef(opts = {}) {
|
|
33
|
+
const fs = opts.fs || Fs;
|
|
34
|
+
async function watch(spec) {
|
|
35
|
+
console.log('APIDEF START', spec.def);
|
|
36
|
+
await generate(spec);
|
|
37
|
+
console.log('APIDEF START GEN', spec.def);
|
|
38
|
+
const fsw = new chokidar_1.FSWatcher();
|
|
39
|
+
fsw.on('change', (...args) => {
|
|
40
|
+
console.log('APIDEF CHANGE', args);
|
|
41
|
+
generate(spec);
|
|
42
|
+
});
|
|
43
|
+
fsw.add(spec.def);
|
|
44
|
+
}
|
|
45
|
+
async function generate(spec) {
|
|
46
|
+
const transform = resolveTranform(spec, opts);
|
|
47
|
+
const source = fs.readFileSync(spec.def, 'utf8');
|
|
48
|
+
const config = await (0, openapi_core_1.createConfig)({});
|
|
49
|
+
const bundle = await (0, openapi_core_1.bundleFromString)({
|
|
50
|
+
source,
|
|
51
|
+
config,
|
|
52
|
+
dereference: true,
|
|
53
|
+
});
|
|
54
|
+
const model = {
|
|
55
|
+
main: { api: { entity: {} } }
|
|
56
|
+
};
|
|
57
|
+
transform(bundle.bundle.parsed, model);
|
|
58
|
+
let vxgsrc = JSON.stringify(model, null, 2);
|
|
59
|
+
vxgsrc = vxgsrc.substring(1, vxgsrc.length - 1);
|
|
60
|
+
fs.writeFileSync(spec.model, vxgsrc);
|
|
61
|
+
return {
|
|
62
|
+
ok: true,
|
|
63
|
+
model,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
watch,
|
|
68
|
+
generate,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function resolveTranform(spec, opts) {
|
|
72
|
+
return makeOpenAPITransform(spec, opts);
|
|
73
|
+
}
|
|
74
|
+
function makeOpenAPITransform(spec, opts) {
|
|
75
|
+
function extractFields(properties) {
|
|
76
|
+
const fieldMap = (0, jostraca_1.each)(properties)
|
|
77
|
+
.reduce((a, p) => (a[p.key$] =
|
|
78
|
+
{ name: p.key$, kind: (0, jostraca_1.camelify)(p.type) }, a), {});
|
|
79
|
+
return fieldMap;
|
|
80
|
+
}
|
|
81
|
+
return function OpenAPITransform(def, model) {
|
|
82
|
+
// console.log('DEF', def)
|
|
83
|
+
model.main.api.name = spec.meta.name;
|
|
84
|
+
(0, jostraca_1.each)(spec.entity, (entity) => {
|
|
85
|
+
// console.log('ENTITY', entity)
|
|
86
|
+
const entityModel = model.main.api.entity[entity.key$] = {
|
|
87
|
+
field: {},
|
|
88
|
+
cmd: {},
|
|
89
|
+
};
|
|
90
|
+
const firstPath = Object.keys(entity.path)[0];
|
|
91
|
+
const firstParts = firstPath.split('/');
|
|
92
|
+
const entityPathPrefix = firstParts[0];
|
|
93
|
+
(0, jostraca_1.each)(entity.path, (path) => {
|
|
94
|
+
// console.log('PATH', entity.key$, entityPathPrefix, path.key$)
|
|
95
|
+
// console.dir(def.paths[path.key$], { depth: null })
|
|
96
|
+
const pathdef = def.paths[path.key$];
|
|
97
|
+
const parts = path.key$.split('/');
|
|
98
|
+
// TODO: use method prop in model!!!
|
|
99
|
+
// Entity Fields
|
|
100
|
+
if (pathdef.get) {
|
|
101
|
+
// GET foo/{id} -> single item
|
|
102
|
+
let properties = (0, jostraca_1.getx)(pathdef.get, 'parameters=1 ^1 responses 200 content ' +
|
|
103
|
+
'application/json schema properties');
|
|
104
|
+
// GET foo -> item list
|
|
105
|
+
if (null == properties) {
|
|
106
|
+
properties = (0, jostraca_1.getx)(pathdef.get, 'parameters=null ^1 responses 200 content ' +
|
|
107
|
+
'application/json schema items properties');
|
|
108
|
+
}
|
|
109
|
+
// console.log('properties', properties)
|
|
110
|
+
// TODO: refactor to util function
|
|
111
|
+
// const field = each(properties)
|
|
112
|
+
// .reduce((a: any, p: any) => (a[p.key$] =
|
|
113
|
+
// { kind: camelify(p.type) }, a), {})
|
|
114
|
+
const field = extractFields(properties);
|
|
115
|
+
Object.assign(entityModel.field, field);
|
|
116
|
+
}
|
|
117
|
+
// Entity Commands
|
|
118
|
+
else if (pathdef.post) {
|
|
119
|
+
// console.log('CMD', parts, pathdef.post)
|
|
120
|
+
if (2 < parts.length && parts[0] === entityPathPrefix) {
|
|
121
|
+
const suffix = parts[parts.length - 1];
|
|
122
|
+
let param = (0, jostraca_1.getx)(pathdef.post, 'parameters?in=path') || [];
|
|
123
|
+
let query = (0, jostraca_1.getx)(pathdef.post, 'parameters?in!=path') || [];
|
|
124
|
+
let response = (0, jostraca_1.getx)(pathdef.post, 'responses 200 content ' +
|
|
125
|
+
'application/json schema properties');
|
|
126
|
+
entityModel.cmd[suffix] = {
|
|
127
|
+
query,
|
|
128
|
+
param: param.reduce((a, p) => (a[p.name] = { name: p.name, kind: (0, jostraca_1.camelify)(p.schema.type) }, a), {}),
|
|
129
|
+
response: { field: extractFields(response) }
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=apidef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apidef.js","sourceRoot":"","sources":["../src/apidef.ts"],"names":[],"mappings":";AAAA,oDAAoD;;;;;;;;;;;;;;;;;;;;;;;;;AAiLlD,wBAAM;AA/KR,4CAA6B;AAE7B,wDAAsE;AAEtE,uCAAoC;AAEpC,uCAA+C;AAU/C,SAAS,MAAM,CAAC,OAAsB,EAAE;IACtC,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,IAAI,EAAE,CAAA;IAGxB,KAAK,UAAU,KAAK,C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AK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;wBAEtC,IAAI,KAAK,GAAG,IAAA,eAAI,EAAC,OAAO,CAAC,IAAI,EAAE,oBAAoB,CAAC,IAAI,EAAE,CAAA;wBAE1D,IAAI,KAAK,GAAG,IAAA,eAAI,EAAC,OAAO,CAAC,IAAI,EAAE,qBAAqB,CAAC,IAAI,EAAE,CAAA;wBAE3D,IAAI,QAAQ,GAAG,IAAA,eAAI,EAAC,OAAO,CAAC,IAAI,EAAE,wBAAwB;4BACxD,oCAAoC,CAAC,CAAA;wBAEvC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG;4BACxB,KAAK;4BACL,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAM,EAAE,CAAM,EAAE,EAAE,CACrC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAA,mBAAQ,EAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC;4BACvE,QAAQ,EAAE,EAAE,KAAK,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE;yBAC7C,CAAA;oBACH,CAAC;gBACH,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@voxgig/apidef",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"main": "dist/apidef.js",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"types": "dist/apidef.d.ts",
|
|
7
|
+
"description": "Voxgig SDK Generator.",
|
|
8
|
+
"homepage": "https://github.com/voxgig/voxgig-apidef",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"voxgig-apidef",
|
|
11
|
+
"voxgig-apidef"
|
|
12
|
+
],
|
|
13
|
+
"author": "Richard Rodger (http://richardrodger.com)",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git://github.com/voxgig/apidef.git"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --enable-source-maps --test dist-test",
|
|
20
|
+
"test-some": "node --enable-source-maps --test-name-pattern=\"$npm_config_pattern\" --test dist-test",
|
|
21
|
+
"watch": "tsc --build src test -w",
|
|
22
|
+
"build": "tsc --build src test",
|
|
23
|
+
"clean": "rm -rf node_modules yarn.lock package-lock.json",
|
|
24
|
+
"reset": "npm run clean && npm i && npm run build && npm test",
|
|
25
|
+
"repo-tag": "REPO_VERSION=`node -e \"console.log(require('./package').version)\"` && echo TAG: v$REPO_VERSION && git commit -a -m v$REPO_VERSION && git push && git tag v$REPO_VERSION && git push --tags;",
|
|
26
|
+
"repo-publish": "npm run clean && npm i && npm run repo-publish-quick",
|
|
27
|
+
"repo-publish-quick": "npm run build && npm run test && npm run repo-tag && npm publish --registry https://registry.npmjs.org --access=public"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"dist",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@hapi/code": "^9.0.3",
|
|
37
|
+
"@types/js-yaml": "^4.0.9",
|
|
38
|
+
"@types/node": "22.5.0",
|
|
39
|
+
"aontu": "^0.21.1",
|
|
40
|
+
"esbuild": "^0.23.1",
|
|
41
|
+
"json-schema-to-ts": "^3.1.0",
|
|
42
|
+
"memfs": "^4.11.1",
|
|
43
|
+
"typescript": "^5.5.4"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@redocly/openapi-core": "^1.20.1",
|
|
47
|
+
"chokidar": "^3.6.0",
|
|
48
|
+
"jostraca": "^0.2.0"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/apidef.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/* Copyright (c) 2024 Richard Rodger, MIT License */
|
|
2
|
+
|
|
3
|
+
import * as Fs from 'node:fs'
|
|
4
|
+
|
|
5
|
+
import { bundleFromString, createConfig } from '@redocly/openapi-core'
|
|
6
|
+
|
|
7
|
+
import { FSWatcher } from 'chokidar'
|
|
8
|
+
|
|
9
|
+
import { getx, each, camelify } from 'jostraca'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
type ApiDefOptions = {
|
|
13
|
+
fs?: any
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
function ApiDef(opts: ApiDefOptions = {}) {
|
|
20
|
+
const fs = opts.fs || Fs
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async function watch(spec: any) {
|
|
24
|
+
console.log('APIDEF START', spec.def)
|
|
25
|
+
await generate(spec)
|
|
26
|
+
console.log('APIDEF START GEN', spec.def)
|
|
27
|
+
|
|
28
|
+
const fsw = new FSWatcher()
|
|
29
|
+
|
|
30
|
+
fsw.on('change', (...args: any[]) => {
|
|
31
|
+
console.log('APIDEF CHANGE', args)
|
|
32
|
+
generate(spec)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
fsw.add(spec.def)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
async function generate(spec: any) {
|
|
40
|
+
const transform = resolveTranform(spec, opts)
|
|
41
|
+
|
|
42
|
+
const source = fs.readFileSync(spec.def, 'utf8')
|
|
43
|
+
|
|
44
|
+
const config = await createConfig({})
|
|
45
|
+
const bundle = await bundleFromString({
|
|
46
|
+
source,
|
|
47
|
+
config,
|
|
48
|
+
dereference: true,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const model = {
|
|
52
|
+
main: { api: { entity: {} } }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
transform(bundle.bundle.parsed, model)
|
|
56
|
+
|
|
57
|
+
let vxgsrc = JSON.stringify(model, null, 2)
|
|
58
|
+
vxgsrc = vxgsrc.substring(1, vxgsrc.length - 1)
|
|
59
|
+
|
|
60
|
+
fs.writeFileSync(
|
|
61
|
+
spec.model,
|
|
62
|
+
vxgsrc
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
ok: true,
|
|
67
|
+
model,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
watch,
|
|
74
|
+
generate,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
function resolveTranform(spec: any, opts: any) {
|
|
82
|
+
return makeOpenAPITransform(spec, opts)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function makeOpenAPITransform(spec: any, opts: any) {
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
function extractFields(properties: any) {
|
|
89
|
+
const fieldMap = each(properties)
|
|
90
|
+
.reduce((a: any, p: any) => (a[p.key$] =
|
|
91
|
+
{ name: p.key$, kind: camelify(p.type) }, a), {})
|
|
92
|
+
return fieldMap
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
return function OpenAPITransform(def: any, model: any) {
|
|
97
|
+
// console.log('DEF', def)
|
|
98
|
+
|
|
99
|
+
model.main.api.name = spec.meta.name
|
|
100
|
+
|
|
101
|
+
each(spec.entity, (entity: any) => {
|
|
102
|
+
// console.log('ENTITY', entity)
|
|
103
|
+
|
|
104
|
+
const entityModel: any = model.main.api.entity[entity.key$] = {
|
|
105
|
+
field: {},
|
|
106
|
+
cmd: {},
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const firstPath: any = Object.keys(entity.path)[0]
|
|
110
|
+
const firstParts = firstPath.split('/')
|
|
111
|
+
const entityPathPrefix = firstParts[0]
|
|
112
|
+
|
|
113
|
+
each(entity.path, (path: any) => {
|
|
114
|
+
// console.log('PATH', entity.key$, entityPathPrefix, path.key$)
|
|
115
|
+
|
|
116
|
+
// console.dir(def.paths[path.key$], { depth: null })
|
|
117
|
+
const pathdef = def.paths[path.key$]
|
|
118
|
+
|
|
119
|
+
const parts = path.key$.split('/')
|
|
120
|
+
|
|
121
|
+
// TODO: use method prop in model!!!
|
|
122
|
+
|
|
123
|
+
// Entity Fields
|
|
124
|
+
if (pathdef.get) {
|
|
125
|
+
// GET foo/{id} -> single item
|
|
126
|
+
let properties = getx(pathdef.get, 'parameters=1 ^1 responses 200 content ' +
|
|
127
|
+
'application/json schema properties')
|
|
128
|
+
|
|
129
|
+
// GET foo -> item list
|
|
130
|
+
if (null == properties) {
|
|
131
|
+
properties = getx(pathdef.get, 'parameters=null ^1 responses 200 content ' +
|
|
132
|
+
'application/json schema items properties')
|
|
133
|
+
}
|
|
134
|
+
// console.log('properties', properties)
|
|
135
|
+
|
|
136
|
+
// TODO: refactor to util function
|
|
137
|
+
// const field = each(properties)
|
|
138
|
+
// .reduce((a: any, p: any) => (a[p.key$] =
|
|
139
|
+
// { kind: camelify(p.type) }, a), {})
|
|
140
|
+
const field = extractFields(properties)
|
|
141
|
+
Object.assign(entityModel.field, field)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Entity Commands
|
|
145
|
+
else if (pathdef.post) {
|
|
146
|
+
// console.log('CMD', parts, pathdef.post)
|
|
147
|
+
|
|
148
|
+
if (2 < parts.length && parts[0] === entityPathPrefix) {
|
|
149
|
+
const suffix = parts[parts.length - 1]
|
|
150
|
+
|
|
151
|
+
let param = getx(pathdef.post, 'parameters?in=path') || []
|
|
152
|
+
|
|
153
|
+
let query = getx(pathdef.post, 'parameters?in!=path') || []
|
|
154
|
+
|
|
155
|
+
let response = getx(pathdef.post, 'responses 200 content ' +
|
|
156
|
+
'application/json schema properties')
|
|
157
|
+
|
|
158
|
+
entityModel.cmd[suffix] = {
|
|
159
|
+
query,
|
|
160
|
+
param: param.reduce((a: any, p: any) =>
|
|
161
|
+
(a[p.name] = { name: p.name, kind: camelify(p.schema.type) }, a), {}),
|
|
162
|
+
response: { field: extractFields(response) }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
export type {
|
|
173
|
+
ApiDefOptions,
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
export {
|
|
178
|
+
ApiDef,
|
|
179
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
"module": "nodenext",
|
|
5
|
+
"noEmitOnError": true,
|
|
6
|
+
"outDir":"../dist",
|
|
7
|
+
"rootDir":".",
|
|
8
|
+
"resolveJsonModule": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"target": "es2021",
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"declarationDir": "../dist"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|