@voxgig/apidef 0.0.2 → 0.0.4
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.js +90 -52
- package/dist/apidef.js.map +1 -1
- package/package.json +4 -4
- package/src/apidef.ts +113 -61
package/dist/apidef.js
CHANGED
|
@@ -32,12 +32,9 @@ const jostraca_1 = require("jostraca");
|
|
|
32
32
|
function ApiDef(opts = {}) {
|
|
33
33
|
const fs = opts.fs || Fs;
|
|
34
34
|
async function watch(spec) {
|
|
35
|
-
console.log('APIDEF START', spec.def);
|
|
36
35
|
await generate(spec);
|
|
37
|
-
console.log('APIDEF START GEN', spec.def);
|
|
38
36
|
const fsw = new chokidar_1.FSWatcher();
|
|
39
37
|
fsw.on('change', (...args) => {
|
|
40
|
-
console.log('APIDEF CHANGE', args);
|
|
41
38
|
generate(spec);
|
|
42
39
|
});
|
|
43
40
|
fsw.add(spec.def);
|
|
@@ -54,7 +51,13 @@ function ApiDef(opts = {}) {
|
|
|
54
51
|
const model = {
|
|
55
52
|
main: { api: { entity: {} } }
|
|
56
53
|
};
|
|
57
|
-
|
|
54
|
+
try {
|
|
55
|
+
transform(bundle.bundle.parsed, model);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
console.log('APIDEF ERROR', err);
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
58
61
|
let vxgsrc = JSON.stringify(model, null, 2);
|
|
59
62
|
vxgsrc = vxgsrc.substring(1, vxgsrc.length - 1);
|
|
60
63
|
fs.writeFileSync(spec.model, vxgsrc);
|
|
@@ -71,65 +74,100 @@ function ApiDef(opts = {}) {
|
|
|
71
74
|
function resolveTranform(spec, opts) {
|
|
72
75
|
return makeOpenAPITransform(spec, opts);
|
|
73
76
|
}
|
|
77
|
+
function extractFields(properties) {
|
|
78
|
+
const fieldMap = (0, jostraca_1.each)(properties)
|
|
79
|
+
.reduce((a, p) => (a[p.key$] =
|
|
80
|
+
{ name: p.key$, kind: (0, jostraca_1.camelify)(p.type) }, a), {});
|
|
81
|
+
return fieldMap;
|
|
82
|
+
}
|
|
83
|
+
function fixName(base, name, prop = 'name') {
|
|
84
|
+
base[prop.toLowerCase()] = name.toLowerCase();
|
|
85
|
+
base[(0, jostraca_1.camelify)(prop)] = (0, jostraca_1.camelify)(name);
|
|
86
|
+
base[prop.toUpperCase()] = name.toUpperCase();
|
|
87
|
+
}
|
|
74
88
|
function makeOpenAPITransform(spec, opts) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
const paramBuilder = (paramMap, paramDef, entityModel, pathdef, op, path, entity, model) => {
|
|
90
|
+
paramMap[paramDef.name] = {
|
|
91
|
+
required: paramDef.required
|
|
92
|
+
};
|
|
93
|
+
fixName(paramMap[paramDef.name], paramDef.name);
|
|
94
|
+
fixName(paramMap[paramDef.name], paramDef.schema.type, 'type');
|
|
95
|
+
};
|
|
96
|
+
const queryBuilder = (queryMap, queryDef, entityModel, pathdef, op, path, entity, model) => {
|
|
97
|
+
queryMap[queryDef.name] = {
|
|
98
|
+
required: queryDef.required
|
|
99
|
+
};
|
|
100
|
+
fixName(queryMap[queryDef.name], queryDef.name);
|
|
101
|
+
fixName(queryMap[queryDef.name], queryDef.schema.type, 'type');
|
|
102
|
+
};
|
|
103
|
+
const opBuilder = {
|
|
104
|
+
any: (entityModel, pathdef, op, path, entity, model) => {
|
|
105
|
+
const em = entityModel.op[op.key$] = {
|
|
106
|
+
path: path.key$,
|
|
107
|
+
method: op.val$,
|
|
108
|
+
param: {},
|
|
109
|
+
query: {},
|
|
110
|
+
};
|
|
111
|
+
fixName(em, op.key$);
|
|
112
|
+
// Params are in the path
|
|
113
|
+
if (0 < path.params.length) {
|
|
114
|
+
let params = (0, jostraca_1.getx)(pathdef[op.val$], 'parameters?in=path') || [];
|
|
115
|
+
if (Array.isArray(params)) {
|
|
116
|
+
params.reduce((a, p) => (paramBuilder(a, p, entityModel, pathdef, op, path, entity, model), a), em.param);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// Queries are after the ?
|
|
120
|
+
let queries = (0, jostraca_1.getx)(pathdef[op.val$], 'parameters?in!=path') || [];
|
|
121
|
+
if (Array.isArray(queries)) {
|
|
122
|
+
queries.reduce((a, p) => (queryBuilder(a, p, entityModel, pathdef, op, path, entity, model), a), em.query);
|
|
123
|
+
}
|
|
124
|
+
return em;
|
|
125
|
+
},
|
|
126
|
+
list: (entityModel, pathdef, op, path, entity, model) => {
|
|
127
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model);
|
|
128
|
+
},
|
|
129
|
+
load: (entityModel, pathdef, op, path, entity, model) => {
|
|
130
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model);
|
|
131
|
+
},
|
|
132
|
+
create: (entityModel, pathdef, op, path, entity, model) => {
|
|
133
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model);
|
|
134
|
+
},
|
|
135
|
+
save: (entityModel, pathdef, op, path, entity, model) => {
|
|
136
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model);
|
|
137
|
+
},
|
|
138
|
+
remove: (entityModel, pathdef, op, path, entity, model) => {
|
|
139
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model);
|
|
140
|
+
},
|
|
141
|
+
};
|
|
81
142
|
return function OpenAPITransform(def, model) {
|
|
82
|
-
|
|
83
|
-
model.main.api.name = spec.meta.name;
|
|
143
|
+
fixName(model.main.api, spec.meta.name);
|
|
84
144
|
(0, jostraca_1.each)(spec.entity, (entity) => {
|
|
85
|
-
// console.log('ENTITY', entity)
|
|
86
145
|
const entityModel = model.main.api.entity[entity.key$] = {
|
|
146
|
+
op: {},
|
|
87
147
|
field: {},
|
|
88
148
|
cmd: {},
|
|
89
149
|
};
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const
|
|
150
|
+
fixName(entityModel, entity.key$);
|
|
151
|
+
// const firstPath: any = Object.keys(entity.path)[0]
|
|
152
|
+
// const firstParts = firstPath.split('/')
|
|
153
|
+
// const entityPathPrefix = firstParts[0]
|
|
93
154
|
(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
155
|
const pathdef = def.paths[path.key$];
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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);
|
|
156
|
+
if (null == pathdef) {
|
|
157
|
+
throw new Error('APIDEF: path not found in OpenAPI: ' + path.key$ +
|
|
158
|
+
' (entity: ' + entity.name + ')');
|
|
116
159
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
};
|
|
160
|
+
path.parts = path.key$.split('/');
|
|
161
|
+
path.params = path.parts
|
|
162
|
+
.filter((p) => p.startsWith('{'))
|
|
163
|
+
.map((p) => p.substring(1, p.length - 1));
|
|
164
|
+
// console.log('ENTITY-PATH', entity, path)
|
|
165
|
+
(0, jostraca_1.each)(path.op, (op) => {
|
|
166
|
+
const opbuild = opBuilder[op.key$];
|
|
167
|
+
if (opbuild) {
|
|
168
|
+
opbuild(entityModel, pathdef, op, path, entity, model);
|
|
131
169
|
}
|
|
132
|
-
}
|
|
170
|
+
});
|
|
133
171
|
});
|
|
134
172
|
});
|
|
135
173
|
};
|
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;;;;;;;;;;;;;;;;;;;;;;;;;AAqOlD,wBAAM;AAnOR,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,CAAC,IAAS;QAC5B,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAA;QAEpB,MAAM,GAAG,GAAG,IAAI,oBAAS,EAAE,CAAA;QAE3B,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAW,EAAE,EAAE;YAClC,QAAQ,CAAC,IAAI,CAAC,CAAA;QAChB,CAAC,CAAC,CAAA;QAEF,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IACnB,CAAC;IAGD,KAAK,UAAU,QAAQ,CAAC,IAAS;QAC/B,MAAM,SAAS,GAAG,eAAe,CAAC,IAA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AG,IAAI,CAAC,IAAI;wBAC/D,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,GAAG,CAAC,CAAA;gBACrC,CAAC;gBAED,IAAI,CAAC,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;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.4",
|
|
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.1",
|
|
39
39
|
"aontu": "^0.21.1",
|
|
40
40
|
"esbuild": "^0.23.1",
|
|
41
41
|
"json-schema-to-ts": "^3.1.0",
|
|
@@ -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.21.1",
|
|
47
47
|
"chokidar": "^3.6.0",
|
|
48
|
-
"jostraca": "^0.
|
|
48
|
+
"jostraca": "^0.3.0"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/src/apidef.ts
CHANGED
|
@@ -21,14 +21,11 @@ function ApiDef(opts: ApiDefOptions = {}) {
|
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
async function watch(spec: any) {
|
|
24
|
-
console.log('APIDEF START', spec.def)
|
|
25
24
|
await generate(spec)
|
|
26
|
-
console.log('APIDEF START GEN', spec.def)
|
|
27
25
|
|
|
28
26
|
const fsw = new FSWatcher()
|
|
29
27
|
|
|
30
28
|
fsw.on('change', (...args: any[]) => {
|
|
31
|
-
console.log('APIDEF CHANGE', args)
|
|
32
29
|
generate(spec)
|
|
33
30
|
})
|
|
34
31
|
|
|
@@ -52,7 +49,13 @@ function ApiDef(opts: ApiDefOptions = {}) {
|
|
|
52
49
|
main: { api: { entity: {} } }
|
|
53
50
|
}
|
|
54
51
|
|
|
55
|
-
|
|
52
|
+
try {
|
|
53
|
+
transform(bundle.bundle.parsed, model)
|
|
54
|
+
}
|
|
55
|
+
catch (err: any) {
|
|
56
|
+
console.log('APIDEF ERROR', err)
|
|
57
|
+
throw err
|
|
58
|
+
}
|
|
56
59
|
|
|
57
60
|
let vxgsrc = JSON.stringify(model, null, 2)
|
|
58
61
|
vxgsrc = vxgsrc.substring(1, vxgsrc.length - 1)
|
|
@@ -82,87 +85,136 @@ function resolveTranform(spec: any, opts: any) {
|
|
|
82
85
|
return makeOpenAPITransform(spec, opts)
|
|
83
86
|
}
|
|
84
87
|
|
|
88
|
+
|
|
89
|
+
function extractFields(properties: any) {
|
|
90
|
+
const fieldMap = each(properties)
|
|
91
|
+
.reduce((a: any, p: any) => (a[p.key$] =
|
|
92
|
+
{ name: p.key$, kind: camelify(p.type) }, a), {})
|
|
93
|
+
return fieldMap
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
function fixName(base: any, name: string, prop = 'name') {
|
|
98
|
+
base[prop.toLowerCase()] = name.toLowerCase()
|
|
99
|
+
base[camelify(prop)] = camelify(name)
|
|
100
|
+
base[prop.toUpperCase()] = name.toUpperCase()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
85
104
|
function makeOpenAPITransform(spec: any, opts: any) {
|
|
86
105
|
|
|
106
|
+
const paramBuilder = (paramMap: any, paramDef: any,
|
|
107
|
+
entityModel: any, pathdef: any,
|
|
108
|
+
op: any, path: any, entity: any, model: any) => {
|
|
109
|
+
paramMap[paramDef.name] = {
|
|
110
|
+
required: paramDef.required
|
|
111
|
+
}
|
|
112
|
+
fixName(paramMap[paramDef.name], paramDef.name)
|
|
113
|
+
fixName(paramMap[paramDef.name], paramDef.schema.type, 'type')
|
|
114
|
+
}
|
|
115
|
+
|
|
87
116
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
117
|
+
const queryBuilder = (queryMap: any, queryDef: any,
|
|
118
|
+
entityModel: any, pathdef: any,
|
|
119
|
+
op: any, path: any, entity: any, model: any) => {
|
|
120
|
+
queryMap[queryDef.name] = {
|
|
121
|
+
required: queryDef.required
|
|
122
|
+
}
|
|
123
|
+
fixName(queryMap[queryDef.name], queryDef.name)
|
|
124
|
+
fixName(queryMap[queryDef.name], queryDef.schema.type, 'type')
|
|
93
125
|
}
|
|
94
126
|
|
|
95
127
|
|
|
96
|
-
|
|
97
|
-
|
|
128
|
+
const opBuilder: any = {
|
|
129
|
+
any: (entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any) => {
|
|
130
|
+
const em = entityModel.op[op.key$] = {
|
|
131
|
+
path: path.key$,
|
|
132
|
+
method: op.val$,
|
|
133
|
+
param: {},
|
|
134
|
+
query: {},
|
|
135
|
+
}
|
|
136
|
+
fixName(em, op.key$)
|
|
137
|
+
|
|
138
|
+
// Params are in the path
|
|
139
|
+
if (0 < path.params.length) {
|
|
140
|
+
let params = getx(pathdef[op.val$], 'parameters?in=path') || []
|
|
141
|
+
if (Array.isArray(params)) {
|
|
142
|
+
params.reduce((a: any, p: any) =>
|
|
143
|
+
(paramBuilder(a, p, entityModel, pathdef, op, path, entity, model), a), em.param)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
98
146
|
|
|
99
|
-
|
|
147
|
+
// Queries are after the ?
|
|
148
|
+
let queries = getx(pathdef[op.val$], 'parameters?in!=path') || []
|
|
149
|
+
if (Array.isArray(queries)) {
|
|
150
|
+
queries.reduce((a: any, p: any) =>
|
|
151
|
+
(queryBuilder(a, p, entityModel, pathdef, op, path, entity, model), a), em.query)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return em
|
|
155
|
+
},
|
|
100
156
|
|
|
101
|
-
each(spec.entity, (entity: any) => {
|
|
102
|
-
// console.log('ENTITY', entity)
|
|
103
157
|
|
|
158
|
+
list: (entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any) => {
|
|
159
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model)
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
load: (entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any) => {
|
|
163
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model)
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
create: (entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any) => {
|
|
167
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model)
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
save: (entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any) => {
|
|
171
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model)
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
remove: (entityModel: any, pathdef: any, op: any, path: any, entity: any, model: any) => {
|
|
175
|
+
return opBuilder.any(entityModel, pathdef, op, path, entity, model)
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return function OpenAPITransform(def: any, model: any) {
|
|
181
|
+
fixName(model.main.api, spec.meta.name)
|
|
182
|
+
|
|
183
|
+
each(spec.entity, (entity: any) => {
|
|
104
184
|
const entityModel: any = model.main.api.entity[entity.key$] = {
|
|
185
|
+
op: {},
|
|
105
186
|
field: {},
|
|
106
187
|
cmd: {},
|
|
107
188
|
}
|
|
108
189
|
|
|
109
|
-
|
|
110
|
-
const firstParts = firstPath.split('/')
|
|
111
|
-
const entityPathPrefix = firstParts[0]
|
|
190
|
+
fixName(entityModel, entity.key$)
|
|
112
191
|
|
|
113
|
-
|
|
114
|
-
|
|
192
|
+
// const firstPath: any = Object.keys(entity.path)[0]
|
|
193
|
+
// const firstParts = firstPath.split('/')
|
|
194
|
+
// const entityPathPrefix = firstParts[0]
|
|
115
195
|
|
|
116
|
-
|
|
196
|
+
each(entity.path, (path: any) => {
|
|
117
197
|
const pathdef = def.paths[path.key$]
|
|
118
198
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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)
|
|
199
|
+
if (null == pathdef) {
|
|
200
|
+
throw new Error('APIDEF: path not found in OpenAPI: ' + path.key$ +
|
|
201
|
+
' (entity: ' + entity.name + ')')
|
|
142
202
|
}
|
|
143
203
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (2 < parts.length && parts[0] === entityPathPrefix) {
|
|
149
|
-
const suffix = parts[parts.length - 1]
|
|
204
|
+
path.parts = path.key$.split('/')
|
|
205
|
+
path.params = path.parts
|
|
206
|
+
.filter((p: string) => p.startsWith('{'))
|
|
207
|
+
.map((p: string) => p.substring(1, p.length - 1))
|
|
150
208
|
|
|
151
|
-
|
|
209
|
+
// console.log('ENTITY-PATH', entity, path)
|
|
152
210
|
|
|
153
|
-
|
|
211
|
+
each(path.op, (op: any) => {
|
|
212
|
+
const opbuild = opBuilder[op.key$]
|
|
154
213
|
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
}
|
|
214
|
+
if (opbuild) {
|
|
215
|
+
opbuild(entityModel, pathdef, op, path, entity, model)
|
|
164
216
|
}
|
|
165
|
-
}
|
|
217
|
+
})
|
|
166
218
|
})
|
|
167
219
|
})
|
|
168
220
|
}
|