@voxgig/apidef 0.0.5 → 0.0.7
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/dist/apidef.d.ts +1 -0
- package/dist/apidef.js +48 -8
- package/dist/apidef.js.map +1 -1
- package/package.json +4 -4
- package/src/apidef.ts +72 -9
package/dist/apidef.d.ts
CHANGED
package/dist/apidef.js
CHANGED
|
@@ -23,9 +23,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
__setModuleDefault(result, mod);
|
|
24
24
|
return result;
|
|
25
25
|
};
|
|
26
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
26
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
30
|
exports.ApiDef = ApiDef;
|
|
28
31
|
const Fs = __importStar(require("node:fs"));
|
|
32
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
29
33
|
const openapi_core_1 = require("@redocly/openapi-core");
|
|
30
34
|
const chokidar_1 = require("chokidar");
|
|
31
35
|
const jostraca_1 = require("jostraca");
|
|
@@ -42,6 +46,7 @@ function ApiDef(opts = {}) {
|
|
|
42
46
|
async function generate(spec) {
|
|
43
47
|
const transform = resolveTranform(spec, opts);
|
|
44
48
|
const source = fs.readFileSync(spec.def, 'utf8');
|
|
49
|
+
const modelBasePath = node_path_1.default.dirname(spec.model);
|
|
45
50
|
const config = await (0, openapi_core_1.createConfig)({});
|
|
46
51
|
const bundle = await (0, openapi_core_1.bundleFromString)({
|
|
47
52
|
source,
|
|
@@ -49,18 +54,31 @@ function ApiDef(opts = {}) {
|
|
|
49
54
|
dereference: true,
|
|
50
55
|
});
|
|
51
56
|
const model = {
|
|
52
|
-
main: {
|
|
57
|
+
main: {
|
|
58
|
+
api: {
|
|
59
|
+
entity: {}
|
|
60
|
+
},
|
|
61
|
+
def: {},
|
|
62
|
+
},
|
|
53
63
|
};
|
|
54
64
|
try {
|
|
55
|
-
|
|
65
|
+
const def = bundle.bundle.parsed;
|
|
66
|
+
// console.dir(def, { depth: null })
|
|
67
|
+
transform(def, model);
|
|
56
68
|
}
|
|
57
69
|
catch (err) {
|
|
58
70
|
console.log('APIDEF ERROR', err);
|
|
59
71
|
throw err;
|
|
60
72
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
const modelapi = { main: { api: model.main.api } };
|
|
74
|
+
let modelSrc = JSON.stringify(modelapi, null, 2);
|
|
75
|
+
modelSrc = modelSrc.substring(1, modelSrc.length - 1);
|
|
76
|
+
fs.writeFileSync(spec.model, modelSrc);
|
|
77
|
+
const defFilePath = node_path_1.default.join(modelBasePath, 'def.jsonic');
|
|
78
|
+
const modelDef = { main: { def: model.main.def } };
|
|
79
|
+
let modelDefSrc = JSON.stringify(modelDef, null, 2);
|
|
80
|
+
modelDefSrc = modelDefSrc.substring(1, modelDefSrc.length - 1);
|
|
81
|
+
fs.writeFileSync(defFilePath, modelDefSrc);
|
|
64
82
|
return {
|
|
65
83
|
ok: true,
|
|
66
84
|
model,
|
|
@@ -139,8 +157,30 @@ function makeOpenAPITransform(spec, opts) {
|
|
|
139
157
|
return opBuilder.any(entityModel, pathdef, op, path, entity, model);
|
|
140
158
|
},
|
|
141
159
|
};
|
|
160
|
+
function fieldbuild(entityModel, pathdef, op, path, entity, model) {
|
|
161
|
+
// console.log(pathdef)
|
|
162
|
+
let fieldSets = (0, jostraca_1.getx)(pathdef.get, 'responses 200 content application/json schema allOf');
|
|
163
|
+
// console.log(fieldSets)
|
|
164
|
+
// return;
|
|
165
|
+
if (fieldSets) {
|
|
166
|
+
// console.log('=====', entityModel.NAME)
|
|
167
|
+
// console.log(fieldSets)
|
|
168
|
+
(0, jostraca_1.each)(fieldSets, (fieldSet) => {
|
|
169
|
+
(0, jostraca_1.each)(fieldSet.properties, (property) => {
|
|
170
|
+
// console.log(property)
|
|
171
|
+
const field = (entityModel.field[property.key$] = entityModel.field[property.key$] || {});
|
|
172
|
+
field.name = property.key$;
|
|
173
|
+
fixName(field, field.name);
|
|
174
|
+
field.type = property.type;
|
|
175
|
+
fixName(field, field.type, 'type');
|
|
176
|
+
field.short = property.description;
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
142
181
|
return function OpenAPITransform(def, model) {
|
|
143
182
|
fixName(model.main.api, spec.meta.name);
|
|
183
|
+
model.main.def.desc = def.info.description;
|
|
144
184
|
(0, jostraca_1.each)(spec.entity, (entity) => {
|
|
145
185
|
const entityModel = model.main.api.entity[entity.key$] = {
|
|
146
186
|
op: {},
|
|
@@ -148,9 +188,6 @@ function makeOpenAPITransform(spec, opts) {
|
|
|
148
188
|
cmd: {},
|
|
149
189
|
};
|
|
150
190
|
fixName(entityModel, entity.key$);
|
|
151
|
-
// const firstPath: any = Object.keys(entity.path)[0]
|
|
152
|
-
// const firstParts = firstPath.split('/')
|
|
153
|
-
// const entityPathPrefix = firstParts[0]
|
|
154
191
|
(0, jostraca_1.each)(entity.path, (path) => {
|
|
155
192
|
const pathdef = def.paths[path.key$];
|
|
156
193
|
if (null == pathdef) {
|
|
@@ -167,6 +204,9 @@ function makeOpenAPITransform(spec, opts) {
|
|
|
167
204
|
if (opbuild) {
|
|
168
205
|
opbuild(entityModel, pathdef, op, path, entity, model);
|
|
169
206
|
}
|
|
207
|
+
if ('load' === op.key$) {
|
|
208
|
+
fieldbuild(entityModel, pathdef, op, path, entity, model);
|
|
209
|
+
}
|
|
170
210
|
});
|
|
171
211
|
});
|
|
172
212
|
});
|
package/dist/apidef.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apidef.js","sourceRoot":"","sources":["../src/apidef.ts"],"names":[],"mappings":";AAAA,oDAAoD
|
|
1
|
+
{"version":3,"file":"apidef.js","sourceRoot":"","sources":["../src/apidef.ts"],"names":[],"mappings":";AAAA,oDAAoD;;;;;;;;;;;;;;;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,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;gBACjC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK;qBACrB,MAAM,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;qBACxC,GAAG,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;gBAEnD,2CAA2C;gBAE3C,IAAA,eAAI,EAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAAO,EAAE,EAAE;oBACxB,MAAM,OAAO,GAAG,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;oBAElC,IAAI,OAAO,EAAE,CAAC;wBACZ,OAAO,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;oBACxD,CAAC;oBAED,IAAI,MAAM,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;wBACvB,UAAU,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;oBAC3D,CAAC;gBACH,CAAC,CAAC,CAAA;YACJ,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voxgig/apidef",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"main": "dist/apidef.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"types": "dist/apidef.d.ts",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@hapi/code": "^9.0.3",
|
|
37
37
|
"@types/js-yaml": "^4.0.9",
|
|
38
|
-
"@types/node": "22.5.
|
|
38
|
+
"@types/node": "22.5.4",
|
|
39
39
|
"aontu": "^0.22.0",
|
|
40
40
|
"esbuild": "^0.23.1",
|
|
41
41
|
"json-schema-to-ts": "^3.1.1",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"typescript": "^5.5.4"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@redocly/openapi-core": "^1.
|
|
46
|
+
"@redocly/openapi-core": "^1.23.1",
|
|
47
47
|
"chokidar": "^3.6.0",
|
|
48
|
-
"jostraca": "^0.
|
|
48
|
+
"jostraca": "^0.4.0"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/src/apidef.ts
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import * as Fs from 'node:fs'
|
|
4
4
|
|
|
5
|
+
import Path from 'node:path'
|
|
6
|
+
|
|
7
|
+
|
|
5
8
|
import { bundleFromString, createConfig } from '@redocly/openapi-core'
|
|
6
9
|
|
|
7
10
|
import { FSWatcher } from 'chokidar'
|
|
@@ -38,6 +41,8 @@ function ApiDef(opts: ApiDefOptions = {}) {
|
|
|
38
41
|
|
|
39
42
|
const source = fs.readFileSync(spec.def, 'utf8')
|
|
40
43
|
|
|
44
|
+
const modelBasePath = Path.dirname(spec.model)
|
|
45
|
+
|
|
41
46
|
const config = await createConfig({})
|
|
42
47
|
const bundle = await bundleFromString({
|
|
43
48
|
source,
|
|
@@ -46,25 +51,46 @@ function ApiDef(opts: ApiDefOptions = {}) {
|
|
|
46
51
|
})
|
|
47
52
|
|
|
48
53
|
const model = {
|
|
49
|
-
main: {
|
|
54
|
+
main: {
|
|
55
|
+
api: {
|
|
56
|
+
entity: {}
|
|
57
|
+
},
|
|
58
|
+
def: {},
|
|
59
|
+
},
|
|
50
60
|
}
|
|
51
61
|
|
|
52
62
|
try {
|
|
53
|
-
|
|
63
|
+
const def = bundle.bundle.parsed
|
|
64
|
+
// console.dir(def, { depth: null })
|
|
65
|
+
transform(def, model)
|
|
54
66
|
}
|
|
55
67
|
catch (err: any) {
|
|
56
68
|
console.log('APIDEF ERROR', err)
|
|
57
69
|
throw err
|
|
58
70
|
}
|
|
59
71
|
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
const modelapi = { main: { api: model.main.api } }
|
|
73
|
+
let modelSrc = JSON.stringify(modelapi, null, 2)
|
|
74
|
+
modelSrc = modelSrc.substring(1, modelSrc.length - 1)
|
|
62
75
|
|
|
63
76
|
fs.writeFileSync(
|
|
64
77
|
spec.model,
|
|
65
|
-
|
|
78
|
+
modelSrc
|
|
66
79
|
)
|
|
67
80
|
|
|
81
|
+
|
|
82
|
+
const defFilePath = Path.join(modelBasePath, 'def.jsonic')
|
|
83
|
+
|
|
84
|
+
const modelDef = { main: { def: model.main.def } }
|
|
85
|
+
let modelDefSrc = JSON.stringify(modelDef, null, 2)
|
|
86
|
+
modelDefSrc = modelDefSrc.substring(1, modelDefSrc.length - 1)
|
|
87
|
+
|
|
88
|
+
fs.writeFileSync(
|
|
89
|
+
defFilePath,
|
|
90
|
+
modelDefSrc
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
68
94
|
return {
|
|
69
95
|
ok: true,
|
|
70
96
|
model,
|
|
@@ -177,9 +203,46 @@ function makeOpenAPITransform(spec: any, opts: any) {
|
|
|
177
203
|
|
|
178
204
|
}
|
|
179
205
|
|
|
206
|
+
|
|
207
|
+
function fieldbuild(
|
|
208
|
+
entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any
|
|
209
|
+
) {
|
|
210
|
+
// console.log(pathdef)
|
|
211
|
+
let fieldSets = getx(pathdef.get, 'responses 200 content application/json schema allOf')
|
|
212
|
+
// console.log(fieldSets)
|
|
213
|
+
// return;
|
|
214
|
+
|
|
215
|
+
if (fieldSets) {
|
|
216
|
+
// console.log('=====', entityModel.NAME)
|
|
217
|
+
// console.log(fieldSets)
|
|
218
|
+
|
|
219
|
+
each(fieldSets, (fieldSet: any) => {
|
|
220
|
+
each(fieldSet.properties, (property: any) => {
|
|
221
|
+
// console.log(property)
|
|
222
|
+
|
|
223
|
+
const field =
|
|
224
|
+
(entityModel.field[property.key$] = entityModel.field[property.key$] || {})
|
|
225
|
+
|
|
226
|
+
field.name = property.key$
|
|
227
|
+
fixName(field, field.name)
|
|
228
|
+
|
|
229
|
+
field.type = property.type
|
|
230
|
+
fixName(field, field.type, 'type')
|
|
231
|
+
|
|
232
|
+
field.short = property.description
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
|
|
180
239
|
return function OpenAPITransform(def: any, model: any) {
|
|
181
240
|
fixName(model.main.api, spec.meta.name)
|
|
182
241
|
|
|
242
|
+
|
|
243
|
+
model.main.def.desc = def.info.description
|
|
244
|
+
|
|
245
|
+
|
|
183
246
|
each(spec.entity, (entity: any) => {
|
|
184
247
|
const entityModel: any = model.main.api.entity[entity.key$] = {
|
|
185
248
|
op: {},
|
|
@@ -189,10 +252,6 @@ function makeOpenAPITransform(spec: any, opts: any) {
|
|
|
189
252
|
|
|
190
253
|
fixName(entityModel, entity.key$)
|
|
191
254
|
|
|
192
|
-
// const firstPath: any = Object.keys(entity.path)[0]
|
|
193
|
-
// const firstParts = firstPath.split('/')
|
|
194
|
-
// const entityPathPrefix = firstParts[0]
|
|
195
|
-
|
|
196
255
|
each(entity.path, (path: any) => {
|
|
197
256
|
const pathdef = def.paths[path.key$]
|
|
198
257
|
|
|
@@ -214,6 +273,10 @@ function makeOpenAPITransform(spec: any, opts: any) {
|
|
|
214
273
|
if (opbuild) {
|
|
215
274
|
opbuild(entityModel, pathdef, op, path, entity, model)
|
|
216
275
|
}
|
|
276
|
+
|
|
277
|
+
if ('load' === op.key$) {
|
|
278
|
+
fieldbuild(entityModel, pathdef, op, path, entity, model)
|
|
279
|
+
}
|
|
217
280
|
})
|
|
218
281
|
})
|
|
219
282
|
})
|