@voxgig/sdkgen 0.16.0 → 0.17.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 (53) hide show
  1. package/package.json +3 -1
  2. package/project/generate/model/feature/limit.jsonic +12 -0
  3. package/project/generate/model/feature/log.jsonic +28 -0
  4. package/project/generate/model/feature/page.jsonic +9 -0
  5. package/project/generate/model/feature/telemetry.jsonic +10 -0
  6. package/project/generate/model/target/go.jsonic +7 -0
  7. package/project/generate/model/target/js.jsonic +24 -0
  8. package/project/generate/src/cmp/js/Config_js.ts +34 -0
  9. package/project/generate/src/cmp/js/Entity_js.ts +91 -0
  10. package/project/generate/src/cmp/js/MainEntity_js.ts +22 -0
  11. package/project/generate/src/cmp/js/Main_js.ts +82 -0
  12. package/project/generate/src/cmp/js/Package_js.ts +57 -0
  13. package/project/generate/src/cmp/js/Quick_js.ts +82 -0
  14. package/project/generate/src/cmp/js/ReadmeInstall_js.ts +19 -0
  15. package/project/generate/src/cmp/js/ReadmeQuick_js.ts +25 -0
  16. package/project/generate/src/cmp/js/TestAcceptEntity_js.ts +101 -0
  17. package/project/generate/src/cmp/js/TestAccept_js.ts +60 -0
  18. package/project/generate/src/cmp/js/TestEntity_js.ts +24 -0
  19. package/project/generate/src/cmp/js/TestMain_js.ts +120 -0
  20. package/project/generate/src/cmp/js/Test_js.ts +24 -0
  21. package/project/generate/src/cmp/js/fragment/Entity.fragment.js +71 -0
  22. package/project/generate/src/cmp/js/fragment/EntityCreateOp.fragment.js +66 -0
  23. package/project/generate/src/cmp/js/fragment/EntityListOp.fragment.js +72 -0
  24. package/project/generate/src/cmp/js/fragment/EntityLoadOp.fragment.js +66 -0
  25. package/project/generate/src/cmp/js/fragment/EntityRemoveOp.fragment.js +66 -0
  26. package/project/generate/src/cmp/js/fragment/EntityUpdateOp.fragment.js +66 -0
  27. package/project/generate/src/cmp/js/fragment/Main.fragment.js +44 -0
  28. package/project/generate/tm/go/LICENSE +22 -0
  29. package/project/generate/tm/js/LICENSE +22 -0
  30. package/project/generate/tm/js/src/feature/log/LogFeature.js +104 -0
  31. package/project/generate/tm/js/src/utility/AuthUtility.js +16 -0
  32. package/project/generate/tm/js/src/utility/BodyUtility.js +27 -0
  33. package/project/generate/tm/js/src/utility/EmptyUtility.js +8 -0
  34. package/project/generate/tm/js/src/utility/ErrorUtility.js +15 -0
  35. package/project/generate/tm/js/src/utility/EscreUtility.js +8 -0
  36. package/project/generate/tm/js/src/utility/EscurlUtility.js +8 -0
  37. package/project/generate/tm/js/src/utility/FetchUtility.js +33 -0
  38. package/project/generate/tm/js/src/utility/FullurlUtility.js +41 -0
  39. package/project/generate/tm/js/src/utility/HeadersUtility.js +12 -0
  40. package/project/generate/tm/js/src/utility/InwardUtility.js +23 -0
  41. package/project/generate/tm/js/src/utility/JoinurlUtility.js +12 -0
  42. package/project/generate/tm/js/src/utility/MethodUtility.js +21 -0
  43. package/project/generate/tm/js/src/utility/OperatorUtility.js +36 -0
  44. package/project/generate/tm/js/src/utility/OptionsUtility.js +22 -0
  45. package/project/generate/tm/js/src/utility/OutwardUtility.js +23 -0
  46. package/project/generate/tm/js/src/utility/ParamsUtility.js +20 -0
  47. package/project/generate/tm/js/src/utility/QueryUtility.js +19 -0
  48. package/project/generate/tm/js/src/utility/ResbasicUtility.js +17 -0
  49. package/project/generate/tm/js/src/utility/ResbodyUtility.js +10 -0
  50. package/project/generate/tm/js/src/utility/ResheadersUtility.js +11 -0
  51. package/project/generate/tm/js/src/utility/ResponseUtility.js +36 -0
  52. package/project/generate/tm/js/src/utility/SpecUtility.js +41 -0
  53. package/project/generate/tm/js/src/utility/Utility.js +55 -0
@@ -0,0 +1,104 @@
1
+
2
+
3
+ const Pino = require('pino')
4
+ const PinoPretty = require('pino-pretty')
5
+
6
+
7
+ class LogFeature {
8
+ version = 'FEATURE_VERSION'
9
+
10
+ #client // SDK client instance
11
+ #options // SDK client feature options
12
+ #config // Feature config from model
13
+ #logger // Pino-style interface
14
+
15
+ constructor(client, options, config) {
16
+ // TOOD: make this a default for all features
17
+ options = options || {}
18
+ options.active = false !== options.active
19
+
20
+ this.#client = client
21
+ this.#options = options
22
+ this.#config = config
23
+
24
+ if(options.active) {
25
+ let logger = options.logger
26
+
27
+ if(null ==logger) {
28
+ let pretty = PinoPretty({
29
+ sync: true,
30
+ ignore: 'ctx',
31
+ })
32
+
33
+ let name = options.name || 'FEATURE_Name'
34
+ let level = options.level || 'info'
35
+
36
+ logger = Pino({ name, level }, pretty)
37
+ }
38
+ this.#logger = logger
39
+ }
40
+ }
41
+
42
+ options() {
43
+ return { ...this.#options }
44
+ }
45
+
46
+ client() {
47
+ return this.#client
48
+ }
49
+
50
+
51
+ PostConstruct(ctx) {
52
+ this.#loghook('PostConstruct', ctx)
53
+ }
54
+
55
+ SetData(ctx) {
56
+ this.#loghook('SetData', ctx)
57
+ }
58
+
59
+ GetData(ctx) {
60
+ this.#loghook('GetData', ctx)
61
+ }
62
+
63
+ GetQuery(ctx) {
64
+ this.#loghook('GetQuery', ctx)
65
+ }
66
+
67
+
68
+ PreOperation(ctx) {
69
+ this.#loghook('PreOperation', ctx)
70
+ }
71
+
72
+ ModifyOp(ctx) {
73
+ this.#loghook('ModifyOp', ctx)
74
+ }
75
+
76
+ PreFetch(ctx) {
77
+ this.#loghook('PreFetch', ctx)
78
+ }
79
+
80
+ PostFetch(ctx) {
81
+ this.#loghook('PostFetch', ctx)
82
+ }
83
+
84
+ ModifyResult(ctx) {
85
+ this.#loghook('ModifyResult', ctx)
86
+ }
87
+
88
+ PostOperation(ctx) {
89
+ this.#loghook('PostOperation', ctx)
90
+ }
91
+
92
+
93
+ #loghook(hook, ctx, level) {
94
+ level = level || 'info'
95
+ if(this.#logger) {
96
+ this.#logger[level]({hook,op:ctx.op,spec:ctx.spec,ctx})
97
+ }
98
+ }
99
+ }
100
+
101
+
102
+ module.exports = {
103
+ LogFeature
104
+ }
@@ -0,0 +1,16 @@
1
+
2
+ function auth(ctx, spec) {
3
+ const { client } = ctx
4
+ const { headers } = spec
5
+
6
+ let options = client.options()
7
+
8
+ headers['authorization'] = 'Bearer '+options.apikey
9
+
10
+ return spec
11
+ }
12
+
13
+
14
+ module.exports = {
15
+ auth
16
+ }
@@ -0,0 +1,27 @@
1
+
2
+ const { error } = require('./ErrorUtility')
3
+
4
+
5
+ function body(ctx) {
6
+ const { op } = ctx
7
+
8
+ let body = undefined
9
+
10
+ if('req' === op.kind) {
11
+ try {
12
+ body = ctx.op.outward(ctx)
13
+ }
14
+ catch (err) {
15
+ // TDOD: need error codes and err msg text
16
+ ctx.result.ok = false
17
+ ctx.result.err = err
18
+ return error(ctx)
19
+ }
20
+ }
21
+
22
+ return body
23
+ }
24
+
25
+ module.exports = {
26
+ body
27
+ }
@@ -0,0 +1,8 @@
1
+
2
+ function empty(s) {
3
+ return null == s || '' === s
4
+ }
5
+
6
+ module.exports = {
7
+ empty
8
+ }
@@ -0,0 +1,15 @@
1
+
2
+ function error(ctx) {
3
+
4
+ const err = new Error('Error: '+ctx.op.name+': '+(ctx.result.err?.message||'unknown'))
5
+ err.result = ctx.result
6
+ err.spec = ctx.spec
7
+
8
+ // TODO: model option to return instead
9
+ throw err
10
+ }
11
+
12
+
13
+ module.exports = {
14
+ error
15
+ }
@@ -0,0 +1,8 @@
1
+
2
+ function escre(s) {
3
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
4
+ }
5
+
6
+ module.exports = {
7
+ escre
8
+ }
@@ -0,0 +1,8 @@
1
+
2
+ function escurl(s) {
3
+ return encodeURIComponent(s)
4
+ }
5
+
6
+ module.exports = {
7
+ escurl
8
+ }
@@ -0,0 +1,33 @@
1
+
2
+ const { fullurl } = require('./FullurlUtility')
3
+
4
+ // Make HTTP request.
5
+ async function fetch(ctx) {
6
+ const {op, spec} = ctx
7
+ let response = {}
8
+
9
+ try {
10
+ const url = spec.url = fullurl(ctx)
11
+
12
+ const fetchReq = {
13
+ method: spec.method,
14
+ headers: spec.headers,
15
+ }
16
+
17
+ if(null != spec.body) {
18
+ fetchReq.body =
19
+ 'object' === typeof spec.body ? JSON.stringify(spec.body) : spec.body
20
+ }
21
+
22
+ response = global.fetch(url, fetchReq)
23
+ }
24
+ catch(err) {
25
+ response.err = err
26
+ }
27
+
28
+ return response
29
+ }
30
+
31
+ module.exports = {
32
+ fetch
33
+ }
@@ -0,0 +1,41 @@
1
+
2
+ const { joinurl } = require('./JoinurlUtility')
3
+ const { escurl } = require('./EscurlUtility')
4
+ const { escre } = require('./EscreUtility')
5
+
6
+
7
+ function fullurl(ctx) {
8
+ const { op, spec } = ctx
9
+ const { query, data } = op
10
+
11
+ let url = joinurl(spec.base, spec.prefix, spec.path, spec.suffix)
12
+
13
+ const params = spec.params
14
+ for(let key of op.params) {
15
+ if(null == params[key]) {
16
+ params[key] = 'res' === op.kind ? query[key] : data[key]
17
+ }
18
+ }
19
+
20
+ for(let key in params) {
21
+ const val = spec.params[key]
22
+ if(null != val) {
23
+ url = url.replace(RegExp('{'+escre(key)+'}'), escurl(val))
24
+ }
25
+ }
26
+
27
+ let qsep = '?'
28
+ for(let key in spec.query) {
29
+ const val = spec.query[key]
30
+ if(null != val) {
31
+ url += qsep + escurl(key) + '=' + escurl(val)
32
+ qsep = '&'
33
+ }
34
+ }
35
+
36
+ return url
37
+ }
38
+
39
+ module.exports = {
40
+ fullurl
41
+ }
@@ -0,0 +1,12 @@
1
+
2
+ function headers(ctx) {
3
+ const out = {}
4
+
5
+ out['content-type'] = 'application/json'
6
+
7
+ return out
8
+ }
9
+
10
+ module.exports = {
11
+ headers
12
+ }
@@ -0,0 +1,23 @@
1
+
2
+ const { error } = require('./ErrorUtility')
3
+
4
+ function inward(ctx) {
5
+ if (!ctx.result.ok) {
6
+ return undefined
7
+ }
8
+
9
+ try {
10
+ return ctx.op.inward(ctx)
11
+ }
12
+ catch (err) {
13
+ // TDOD: need error codes and err msg text
14
+ ctx.result.ok = false
15
+ ctx.result.err = err
16
+ return error(ctx)
17
+ }
18
+ }
19
+
20
+
21
+ module.exports = {
22
+ inward
23
+ }
@@ -0,0 +1,12 @@
1
+
2
+ function joinurl(...s) {
3
+ return s
4
+ .filter(s=>null!=s&&''!==s)
5
+ .map(s=>s.replace(/^\/+/,'').replace(/\/+$/,''))
6
+ .filter(s=>null!=s&&''!==s)
7
+ .join('/')
8
+ }
9
+
10
+ module.exports = {
11
+ joinurl
12
+ }
@@ -0,0 +1,21 @@
1
+
2
+ function method(ctx) {
3
+ const { op } = ctx
4
+ const opname = op.name
5
+
6
+ let key = opname
7
+
8
+ const mmap = {
9
+ create: 'POST',
10
+ save: 'PUT',
11
+ load: 'GET',
12
+ list: 'GET',
13
+ remove: 'DELETE',
14
+ }
15
+
16
+ return mmap[key]
17
+ }
18
+
19
+ module.exports = {
20
+ method
21
+ }
@@ -0,0 +1,36 @@
1
+
2
+
3
+ // NOTE: duplicated in @voxgig/apidef - dedup to @voxgig/util?
4
+ const OPKIND = {
5
+ create: 'req',
6
+ update: 'req',
7
+ remove: 'req',
8
+ load: 'res',
9
+ list: 'res',
10
+ }
11
+
12
+
13
+ // Ensure standard operation definition.
14
+ function operator(ctx) {
15
+ const { op } = ctx
16
+
17
+ let out = {
18
+ name: op.name,
19
+ kind: OPKIND[op.name],
20
+ entity: op.entity,
21
+ path: op.path,
22
+ params: op.params || [],
23
+ query: {...op.query} || {},
24
+ data: {...op.data} || {},
25
+ state: op.state,
26
+ inward: op.inward,
27
+ outward: op.outward,
28
+ }
29
+
30
+ return out
31
+ }
32
+
33
+
34
+ module.exports = {
35
+ operator
36
+ }
@@ -0,0 +1,22 @@
1
+
2
+ const { empty } = require('./EmptyUtility')
3
+
4
+ function options(ctx) {
5
+ let config = ctx.config || {}
6
+ let copts = config.options || {}
7
+
8
+ let options = { ...(ctx.options||{}) }
9
+
10
+ options.base = empty(options.base) ?
11
+ empty(copts.base) ? 'http://localhost:8000' :
12
+ copts.base : options.base
13
+
14
+ options.prefix = empty(options.prefix) ? '' : options.prefix
15
+ options.suffix = empty(options.suffix) ? '' : options.suffix
16
+
17
+ return options
18
+ }
19
+
20
+ module.exports = {
21
+ options
22
+ }
@@ -0,0 +1,23 @@
1
+
2
+ const { error } = require('./ErrorUtility')
3
+
4
+ function outward(ctx) {
5
+ if (!ctx.result.ok) {
6
+ return undefined
7
+ }
8
+
9
+ try {
10
+ return ctx.op.outward(ctx)
11
+ }
12
+ catch (err) {
13
+ // TDOD: need error codes and err msg text
14
+ ctx.result.ok = false
15
+ ctx.result.err = err
16
+ return error(ctx)
17
+ }
18
+ }
19
+
20
+
21
+ module.exports = {
22
+ outward
23
+ }
@@ -0,0 +1,20 @@
1
+
2
+ function params(ctx) {
3
+ const { op } = ctx
4
+
5
+ const { params, query } = op
6
+
7
+ const out = {}
8
+ for(let key of params) {
9
+ let val = query[key]
10
+ if(null!=val) {
11
+ out[key] = val
12
+ }
13
+ }
14
+
15
+ return out
16
+ }
17
+
18
+ module.exports = {
19
+ params
20
+ }
@@ -0,0 +1,19 @@
1
+
2
+ function query(ctx) {
3
+ const { op } = ctx
4
+ const { params, query } = op
5
+
6
+ const out = {}
7
+ for(let key of Object.keys(query)) {
8
+ let val = query[key]
9
+ if(null!=val && !params.includes(key)) {
10
+ out[key] = val
11
+ }
12
+ }
13
+
14
+ return out
15
+ }
16
+
17
+ module.exports = {
18
+ query
19
+ }
@@ -0,0 +1,17 @@
1
+
2
+ function resbasic(response, fetchResponse) {
3
+ response.status = fetchResponse.status
4
+ response.statusText = fetchResponse.statusText || 'no-status'
5
+
6
+ // TODO: use spec!
7
+ if(400 <= response.status) {
8
+ response.ok = false
9
+ response.err = new Error(response.status+': '+response.statusText)
10
+ }
11
+
12
+ return response
13
+ }
14
+
15
+ module.exports = {
16
+ resbasic
17
+ }
@@ -0,0 +1,10 @@
1
+
2
+ async function resbody(response, fetchResponse) {
3
+ const json = await fetchResponse.json()
4
+ response.body = json
5
+ return response
6
+ }
7
+
8
+ module.exports = {
9
+ resbody
10
+ }
@@ -0,0 +1,11 @@
1
+
2
+ function resheaders(response, fetchResponse) {
3
+ out = {}
4
+ fetchResponse.headers.forEach((v,k)=>out[k]=v)
5
+ response.headers = out
6
+ return response
7
+ }
8
+
9
+ module.exports = {
10
+ resheaders
11
+ }
@@ -0,0 +1,36 @@
1
+
2
+ const { resheaders } = require('./ResheadersUtility')
3
+ const { resbasic } = require('./ResbasicUtility')
4
+ const { resbody } = require('./ResbodyUtility')
5
+
6
+ async function response(ctx) {
7
+ let { response } = ctx
8
+
9
+ let result = {
10
+ ok: false,
11
+ status: -1,
12
+ statusText: '',
13
+ headers: {},
14
+ body: undefined,
15
+ err: response.err,
16
+ }
17
+
18
+ try {
19
+ result = resbasic(result, response)
20
+ result = resheaders(result, response)
21
+ result = await resbody(result, response)
22
+
23
+ if(null == result.err) {
24
+ result.ok = true
25
+ }
26
+ }
27
+ catch(err) {
28
+ result.err = err
29
+ }
30
+
31
+ return result
32
+ }
33
+
34
+ module.exports = {
35
+ response
36
+ }
@@ -0,0 +1,41 @@
1
+
2
+ const { method } = require('./MethodUtility')
3
+ const { params } = require('./ParamsUtility')
4
+ const { query } = require('./QueryUtility')
5
+ const { headers } = require('./HeadersUtility')
6
+ const { body } = require('./BodyUtility')
7
+ const { auth } = require('./AuthUtility')
8
+
9
+ // Create request specificaton.
10
+ function spec(ctx) {
11
+ const {client, op} = ctx
12
+
13
+ let options = client.options()
14
+
15
+ const reqMethod = method(ctx)
16
+ const reqParams = params(ctx)
17
+ const reqQuery = query(ctx)
18
+ const reqHeaders = headers(ctx)
19
+ const reqBody = body(ctx)
20
+
21
+ let spec = {
22
+ base: options.base, // string, URL endpoint base prefix,
23
+ prefix: options.prefix,
24
+ path: op.path,
25
+ suffix: options.suffix,
26
+ method: reqMethod,
27
+ params: reqParams,
28
+ query: reqQuery,
29
+ headers: reqHeaders,
30
+ body: reqBody,
31
+ }
32
+
33
+ spec = auth(ctx, spec)
34
+
35
+ return spec
36
+ }
37
+
38
+
39
+ module.exports = {
40
+ spec
41
+ }
@@ -0,0 +1,55 @@
1
+
2
+ const { auth } = require('./AuthUtility')
3
+ const { body } = require('./BodyUtility')
4
+ const { empty } = require('./EmptyUtility')
5
+ const { error } = require('./ErrorUtility')
6
+ const { escre } = require('./EscreUtility')
7
+ const { escurl } = require('./EscurlUtility')
8
+ const { fetch } = require('./FetchUtility')
9
+ const { fullurl } = require('./FullurlUtility')
10
+ const { headers } = require('./HeadersUtility')
11
+ const { inward } = require('./InwardUtility')
12
+ const { joinurl } = require('./JoinurlUtility')
13
+ const { method } = require('./MethodUtility')
14
+ const { operator } = require('./OperatorUtility')
15
+ const { options } = require('./OptionsUtility')
16
+ const { outward } = require('./outwardUtility')
17
+ const { params } = require('./ParamsUtility')
18
+ const { query } = require('./QueryUtility')
19
+ const { resbasic } = require('./ResbasicUtility')
20
+ const { resbody } = require('./ResbodyUtility')
21
+ const { resheaders } = require('./ResheadersUtility')
22
+ const { response } = require('./ResponseUtility')
23
+ const { spec } = require('./SpecUtility')
24
+
25
+
26
+ const Utility = {
27
+ auth,
28
+ body,
29
+ empty,
30
+ error,
31
+ escre,
32
+ escurl,
33
+ fetch,
34
+ fullurl,
35
+ headers,
36
+ inward,
37
+ joinurl,
38
+ method,
39
+ operator,
40
+ options,
41
+ outward,
42
+ params,
43
+ query,
44
+ resbasic,
45
+ resbody,
46
+ resheaders,
47
+ response,
48
+ spec,
49
+ }
50
+
51
+
52
+ module.exports = {
53
+ Utility
54
+ }
55
+