@voxgig/sdkgen 0.16.0 → 0.18.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 (112) hide show
  1. package/dist/action/feature.js +3 -2
  2. package/dist/action/feature.js.map +1 -1
  3. package/dist/action/target.js +11 -6
  4. package/dist/action/target.js.map +1 -1
  5. package/dist/cmp/Entity.js +2 -1
  6. package/dist/cmp/Entity.js.map +1 -1
  7. package/dist/cmp/Main.js +23 -9
  8. package/dist/cmp/Main.js.map +1 -1
  9. package/dist/sdkgen.d.ts +7 -1
  10. package/dist/sdkgen.js +5 -2
  11. package/dist/sdkgen.js.map +1 -1
  12. package/dist/tsconfig.tsbuildinfo +1 -1
  13. package/dist/utility.js +1 -2
  14. package/dist/utility.js.map +1 -1
  15. package/model/sdkgen.jsonic +8 -5
  16. package/package.json +7 -5
  17. package/project/.sdk/model/feature/limit.jsonic +12 -0
  18. package/project/.sdk/model/feature/log.jsonic +32 -0
  19. package/project/.sdk/model/feature/page.jsonic +9 -0
  20. package/project/.sdk/model/feature/telemetry.jsonic +10 -0
  21. package/project/.sdk/model/target/go.jsonic +7 -0
  22. package/project/.sdk/model/target/js.jsonic +23 -0
  23. package/project/.sdk/model/target/ts.jsonic +25 -0
  24. package/project/.sdk/src/cmp/js/Config_js.ts +49 -0
  25. package/project/.sdk/src/cmp/js/Entity_js.ts +92 -0
  26. package/project/.sdk/src/cmp/js/MainEntity_js.ts +22 -0
  27. package/project/.sdk/src/cmp/js/Main_js.ts +88 -0
  28. package/project/.sdk/src/cmp/js/Package_js.ts +58 -0
  29. package/project/.sdk/src/cmp/js/Quick_js.ts +82 -0
  30. package/project/.sdk/src/cmp/js/ReadmeInstall_js.ts +19 -0
  31. package/project/.sdk/src/cmp/js/ReadmeQuick_js.ts +25 -0
  32. package/project/.sdk/src/cmp/js/TestAcceptEntity_js.ts +13 -0
  33. package/project/.sdk/src/cmp/js/TestAccept_js.ts +18 -0
  34. package/project/.sdk/src/cmp/js/TestEntity_js.ts +13 -0
  35. package/project/.sdk/src/cmp/js/TestMain_js.ts +19 -0
  36. package/project/.sdk/src/cmp/js/Test_js.ts +24 -0
  37. package/project/.sdk/src/cmp/js/fragment/Entity.fragment.js +79 -0
  38. package/project/.sdk/src/cmp/js/fragment/EntityCreateOp.fragment.js +61 -0
  39. package/project/.sdk/src/cmp/js/fragment/EntityListOp.fragment.js +57 -0
  40. package/project/.sdk/src/cmp/js/fragment/EntityLoadOp.fragment.js +61 -0
  41. package/project/.sdk/src/cmp/js/fragment/EntityRemoveOp.fragment.js +61 -0
  42. package/project/.sdk/src/cmp/js/fragment/EntityUpdateOp.fragment.js +61 -0
  43. package/project/.sdk/src/cmp/js/fragment/Main.fragment.js +67 -0
  44. package/project/.sdk/src/cmp/ts/Config_ts.ts +36 -0
  45. package/project/.sdk/src/cmp/ts/Entity_ts.ts +93 -0
  46. package/project/.sdk/src/cmp/ts/MainEntity_ts.ts.off +22 -0
  47. package/project/.sdk/src/cmp/ts/Main_ts.ts +88 -0
  48. package/project/.sdk/src/cmp/ts/Main_ts.ts~ +88 -0
  49. package/project/.sdk/src/cmp/ts/Package_ts.ts +63 -0
  50. package/project/.sdk/src/cmp/ts/Package_ts.ts~ +58 -0
  51. package/project/.sdk/src/cmp/ts/Quick_ts.ts.off +82 -0
  52. package/project/.sdk/src/cmp/ts/ReadmeInstall_ts.ts.off +19 -0
  53. package/project/.sdk/src/cmp/ts/ReadmeQuick_ts.ts.off +25 -0
  54. package/project/.sdk/src/cmp/ts/TestAcceptEntity_ts.ts.off +13 -0
  55. package/project/.sdk/src/cmp/ts/TestAccept_ts.ts.off +18 -0
  56. package/project/.sdk/src/cmp/ts/TestEntity_ts.ts.off +13 -0
  57. package/project/.sdk/src/cmp/ts/TestMain_ts.ts.off +19 -0
  58. package/project/.sdk/src/cmp/ts/Test_ts.ts.off +24 -0
  59. package/project/.sdk/src/cmp/ts/fragment/Config.fragment.ts +15 -0
  60. package/project/.sdk/src/cmp/ts/fragment/Entity.fragment.js +79 -0
  61. package/project/.sdk/src/cmp/ts/fragment/EntityCreateOp.fragment.js +61 -0
  62. package/project/.sdk/src/cmp/ts/fragment/EntityListOp.fragment.js +57 -0
  63. package/project/.sdk/src/cmp/ts/fragment/EntityLoadOp.fragment.js +61 -0
  64. package/project/.sdk/src/cmp/ts/fragment/EntityRemoveOp.fragment.js +61 -0
  65. package/project/.sdk/src/cmp/ts/fragment/EntityUpdateOp.fragment.js +61 -0
  66. package/project/.sdk/src/cmp/ts/fragment/Main.fragment.js +67 -0
  67. package/project/.sdk/tm/go/LICENSE +22 -0
  68. package/project/.sdk/tm/js/LICENSE +22 -0
  69. package/project/.sdk/tm/js/src/feature/log/LogFeature.js +108 -0
  70. package/project/.sdk/tm/js/src/utility/AuthUtility.js +21 -0
  71. package/project/.sdk/tm/js/src/utility/BodyUtility.js +29 -0
  72. package/project/.sdk/tm/js/src/utility/DoneUtility.js +15 -0
  73. package/project/.sdk/tm/js/src/utility/ErrorUtility.js +33 -0
  74. package/project/.sdk/tm/js/src/utility/FindparamUtility.js +31 -0
  75. package/project/.sdk/tm/js/src/utility/FullurlUtility.js +39 -0
  76. package/project/.sdk/tm/js/src/utility/HeadersUtility.js +13 -0
  77. package/project/.sdk/tm/js/src/utility/JoinurlUtility.js +14 -0
  78. package/project/.sdk/tm/js/src/utility/MethodUtility.js +22 -0
  79. package/project/.sdk/tm/js/src/utility/OperatorUtility.js +44 -0
  80. package/project/.sdk/tm/js/src/utility/OptionsUtility.js +54 -0
  81. package/project/.sdk/tm/js/src/utility/ParamsUtility.js +21 -0
  82. package/project/.sdk/tm/js/src/utility/QueryUtility.js +21 -0
  83. package/project/.sdk/tm/js/src/utility/ReqformUtility.js +32 -0
  84. package/project/.sdk/tm/js/src/utility/RequestUtility.js +48 -0
  85. package/project/.sdk/tm/js/src/utility/ResbasicUtility.js +27 -0
  86. package/project/.sdk/tm/js/src/utility/ResbodyUtility.js +15 -0
  87. package/project/.sdk/tm/js/src/utility/ResformUtility.js +34 -0
  88. package/project/.sdk/tm/js/src/utility/ResheadersUtility.js +19 -0
  89. package/project/.sdk/tm/js/src/utility/ResponseUtility.js +37 -0
  90. package/project/.sdk/tm/js/src/utility/ResultUtility.js +28 -0
  91. package/project/.sdk/tm/js/src/utility/SpecUtility.js +35 -0
  92. package/project/.sdk/tm/js/src/utility/StructUtility.js +1203 -0
  93. package/project/.sdk/tm/js/src/utility/Utility.js +74 -0
  94. package/project/.sdk/tm/js/test/runner.js +171 -0
  95. package/project/.sdk/tm/js/test/utility/Custom.test.js +85 -0
  96. package/project/.sdk/tm/js/test/utility/PrimaryUtility.test.js +187 -0
  97. package/project/.sdk/tm/js/test/utility/StructUtility.test.js +367 -0
  98. package/project/.sdk/tm/py/LICENSE +22 -0
  99. package/project/.sdk/tm/ts/LICENSE +22 -0
  100. package/project/.sdk/tm/ts/src/README.md +1 -0
  101. package/project/.sdk/tm/ts/src/feature/README.md +1 -0
  102. package/project/.sdk/tm/ts/src/tsconfig.json +15 -0
  103. package/project/.sdk/tm/ts/src/utility/README.md +3 -0
  104. package/project/.sdk/tm/ts/test/README.md +2 -0
  105. package/project/.sdk/tm/ts/test/README.md~ +2 -0
  106. package/project/.sdk/tm/ts/test/tsconfig.json +13 -0
  107. package/src/action/feature.ts +4 -3
  108. package/src/action/target.ts +12 -6
  109. package/src/cmp/Entity.ts +3 -1
  110. package/src/cmp/Main.ts +30 -10
  111. package/src/sdkgen.ts +15 -5
  112. package/src/utility.ts +1 -2
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) $$const.year$$ $$const.Name$$
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.
22
+
@@ -0,0 +1,108 @@
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
+ PostConstructEntity(ctx) {
56
+ this.#loghook('PostConstructEntity', ctx)
57
+ }
58
+
59
+ SetData(ctx) {
60
+ this.#loghook('SetData', ctx)
61
+ }
62
+
63
+ GetData(ctx) {
64
+ this.#loghook('GetData', ctx)
65
+ }
66
+
67
+ GetMatch(ctx) {
68
+ this.#loghook('GetMatch', ctx)
69
+ }
70
+
71
+
72
+ PreOperation(ctx) {
73
+ this.#loghook('PreOperation', ctx)
74
+ }
75
+
76
+ PreSpec(ctx) {
77
+ this.#loghook('PreSpec', ctx)
78
+ }
79
+
80
+ PreRequest(ctx) {
81
+ this.#loghook('PreRequest', ctx)
82
+ }
83
+
84
+ PreResponse(ctx) {
85
+ this.#loghook('PreResponse', ctx)
86
+ }
87
+
88
+ PreResult(ctx) {
89
+ this.#loghook('PreResult', ctx)
90
+ }
91
+
92
+ PostOperation(ctx) {
93
+ this.#loghook('PostOperation', ctx)
94
+ }
95
+
96
+
97
+ #loghook(hook, ctx, level) {
98
+ level = level || 'info'
99
+ if(this.#logger) {
100
+ this.#logger[level]({hook,op:ctx.op,spec:ctx.spec,ctx})
101
+ }
102
+ }
103
+ }
104
+
105
+
106
+ module.exports = {
107
+ LogFeature
108
+ }
@@ -0,0 +1,21 @@
1
+
2
+ function auth(ctx) {
3
+ const { client, spec } = ctx
4
+ const { headers } = spec
5
+
6
+ let options = client.options()
7
+
8
+ if(null != options.apikey && '' !== options.apikey) {
9
+ headers['authorization'] = 'Bearer '+options.apikey
10
+ }
11
+ else {
12
+ delete headers['authorization']
13
+ }
14
+
15
+ return spec
16
+ }
17
+
18
+
19
+ module.exports = {
20
+ auth
21
+ }
@@ -0,0 +1,29 @@
1
+
2
+
3
+ function body(ctx) {
4
+ const { op, result, utility } = ctx
5
+ const { struct, error } = utility
6
+ const { isfunc, transform } = struct
7
+
8
+ let body = undefined
9
+
10
+ if('req' === op.kind) {
11
+ try {
12
+ const reqform = op.reqform
13
+ body = isfunc(reqform) ? reqform(ctx) : transform(op, op.reqform)
14
+
15
+ if(op.check.nobody && null == body) {
16
+ return error(ctx, new Error('Request body is empty.'))
17
+ }
18
+ }
19
+ catch (err) {
20
+ return error(ctx, err)
21
+ }
22
+ }
23
+
24
+ return body
25
+ }
26
+
27
+ module.exports = {
28
+ body
29
+ }
@@ -0,0 +1,15 @@
1
+
2
+ function done(ctx) {
3
+ const { error } = ctx.utility
4
+
5
+ if(ctx.result.ok) {
6
+ return ctx.result.resdata
7
+ }
8
+
9
+ return error(ctx)
10
+ }
11
+
12
+
13
+ module.exports = {
14
+ done
15
+ }
@@ -0,0 +1,33 @@
1
+
2
+ function error(ctx, err) {
3
+
4
+ ctx = ctx || {}
5
+ const op = ctx.op || {}
6
+ op.name = op.name || 'unknown operation'
7
+
8
+ const result = ctx.result = ctx.result || {}
9
+ result.ok = false
10
+
11
+ const reserr = result.err
12
+
13
+ err = undefined === err ? reserr : err
14
+ err = err || new Error('unknown error')
15
+
16
+ const errmsg = err.message || 'unknown error'
17
+ const msg = 'StatuspageSDK: '+op.name+': '+errmsg
18
+ err.message = msg
19
+ // result.err = {...err}
20
+
21
+ const spec = ctx.spec || {}
22
+
23
+ err.result = ctx.result
24
+ err.spec = spec
25
+
26
+ // TODO: model option to return instead
27
+ throw err
28
+ }
29
+
30
+
31
+ module.exports = {
32
+ error
33
+ }
@@ -0,0 +1,31 @@
1
+
2
+ /* Find value of a match parameter, possibly using an alias.
3
+ *
4
+ * The match parameter may have an alias key. For example, the parameter `foo_id` may be
5
+ * aliased to `id` in the entity data.
6
+ *
7
+ * This function returns `undefined` rather than failing.
8
+ */
9
+ function findparam(ctx, key) {
10
+ let { op, spec } = ctx
11
+ let { match, data, alias } = op
12
+
13
+ let source = ('res' === op.kind ? match : data) || {}
14
+ let val = source[key]
15
+
16
+ if(null == val) {
17
+ alias = alias || {}
18
+ let akey = alias[key]
19
+ val = source[akey]
20
+
21
+ spec = spec || {}
22
+ spec.alias = spec.alias || {}
23
+ spec.alias[akey] = key
24
+ }
25
+
26
+ return val
27
+ }
28
+
29
+ module.exports = {
30
+ findparam
31
+ }
@@ -0,0 +1,39 @@
1
+
2
+
3
+ function fullurl(ctx) {
4
+ const { op, spec, utility: { struct, findparam } } = ctx
5
+ const { escurl, escre, joinurl } = struct
6
+
7
+ let url = joinurl([spec.base, spec.prefix, spec.path, spec.suffix])
8
+
9
+ const params = spec.params
10
+ for(let key of op.params) {
11
+ if(null == params[key]) {
12
+ params[key] = findparam(ctx, key)
13
+ }
14
+ }
15
+
16
+ for(let key in params) {
17
+ const val = params[key]
18
+ if(null != val) {
19
+ url = url.replace(RegExp('{'+escre(key)+'}'), escurl(val))
20
+ }
21
+ }
22
+
23
+ let qsep = '?'
24
+ for(let key in spec.query) {
25
+ if(null == spec.alias[key]) {
26
+ const val = spec.query[key]
27
+ if(null != val) {
28
+ url += qsep + escurl(key) + '=' + escurl(val)
29
+ qsep = '&'
30
+ }
31
+ }
32
+ }
33
+
34
+ return url
35
+ }
36
+
37
+ module.exports = {
38
+ fullurl
39
+ }
@@ -0,0 +1,13 @@
1
+
2
+ function headers(ctx) {
3
+ const out = {}
4
+
5
+ // TODO: should come from options via config
6
+ out['content-type'] = 'application/json'
7
+
8
+ return out
9
+ }
10
+
11
+ module.exports = {
12
+ headers
13
+ }
@@ -0,0 +1,14 @@
1
+
2
+ // TODO: MOVE TO STRUCT
3
+ function joinurl(...s) {
4
+ return s
5
+ .filter(s=>null!=s&&''!==s)
6
+ .map((s,i)=> 0===i ? s.replace(/([^\/])\/+/,'$1/').replace(/\/+$/,'') :
7
+ s.replace(/([^\/])\/+/,'$1/').replace(/^\/+/,'').replace(/\/+$/,''))
8
+ .filter(s=>''!==s)
9
+ .join('/')
10
+ }
11
+
12
+ module.exports = {
13
+ joinurl
14
+ }
@@ -0,0 +1,22 @@
1
+
2
+ function method(ctx) {
3
+ const { op } = ctx
4
+ const opname = op.name
5
+
6
+ let key = opname
7
+
8
+ // TODO: options
9
+ const mmap = {
10
+ create: 'POST',
11
+ update: 'PUT',
12
+ load: 'GET',
13
+ list: 'GET',
14
+ remove: 'DELETE',
15
+ }
16
+
17
+ return mmap[key]
18
+ }
19
+
20
+ module.exports = {
21
+ method
22
+ }
@@ -0,0 +1,44 @@
1
+
2
+
3
+ const OPKIND = {
4
+ create: 'req',
5
+ update: 'req',
6
+ remove: 'req',
7
+ load: 'res',
8
+ list: 'res',
9
+ }
10
+
11
+
12
+ // Ensure standard operation definition.
13
+ function operator(ctx) {
14
+ const { op, utility } = ctx
15
+ const { validate } = utility.struct
16
+
17
+ const opspec = {
18
+
19
+ // Required.
20
+ name: '`$STRING`',
21
+ kind: ['`$ONE`','req','res'],
22
+ path: '`$STRING`',
23
+ entity: '`$STRING`',
24
+ reqform: ['`$ONE`','`$STRING`','`$OBJECT`','`$FUNCTION`'],
25
+ resform: ['`$ONE`','`$STRING`','`$OBJECT`','`$FUNCTION`'],
26
+
27
+ // Optional.
28
+ params: ['`$CHILD`', '`$STRING`'],
29
+ alias: {'`$CHILD`': '`$STRING`' },
30
+ match: {},
31
+ data: ['`$ONE`',{}, []],
32
+ state: {},
33
+ check: {},
34
+ }
35
+
36
+ ctx.op.kind = OPKIND[op.name]
37
+
38
+ ctx.op = validate(ctx.op, opspec)
39
+ }
40
+
41
+
42
+ module.exports = {
43
+ operator
44
+ }
@@ -0,0 +1,54 @@
1
+
2
+ function options(ctx) {
3
+ const { options, utility } = ctx
4
+
5
+ let opts = { ...(options||{}) }
6
+
7
+ const customUtils = opts.utility || {}
8
+ for(let key of Object.keys(customUtils)) {
9
+ utility[key] = customUtils[key]
10
+ }
11
+
12
+ const { isempty, merge, validate } = utility.struct
13
+
14
+ let config = ctx.config || {}
15
+ let cfgopts = config.options || {}
16
+
17
+ // Standard SDK option values.
18
+ const optspec = {
19
+ apikey: '',
20
+ base: 'http://localhost:8000',
21
+ prefix: '',
22
+ suffix: '',
23
+ entity: {
24
+ '`$CHILD`': {
25
+ '`$OPEN`': true,
26
+ active: false,
27
+ alias: {}
28
+ }
29
+ },
30
+ feature: {
31
+ '`$CHILD`': {
32
+ '`$OPEN`': true,
33
+ active: false,
34
+ }
35
+ },
36
+ utility: {},
37
+ system: {},
38
+ }
39
+
40
+ // JavaScript specific option values.
41
+ optspec.system.fetch = optspec.system.fetch || global.fetch
42
+
43
+ opts = merge([{},cfgopts,opts])
44
+
45
+ opts = validate(opts, optspec)
46
+
47
+ return opts
48
+ }
49
+
50
+
51
+
52
+ module.exports = {
53
+ options
54
+ }
@@ -0,0 +1,21 @@
1
+
2
+ function params(ctx) {
3
+ const { op } = ctx
4
+ let { params, match } = op
5
+ params = params || []
6
+ match = match || {}
7
+
8
+ const out = {}
9
+ for(let key of params) {
10
+ let val = match[key]
11
+ if(null!=val) {
12
+ out[key] = val
13
+ }
14
+ }
15
+
16
+ return out
17
+ }
18
+
19
+ module.exports = {
20
+ params
21
+ }
@@ -0,0 +1,21 @@
1
+
2
+ function query(ctx) {
3
+ const { op } = ctx
4
+ let { params, match } = op
5
+ params = params || []
6
+ match = match || {}
7
+
8
+ const out = {}
9
+ for(let key of Object.keys(match)) {
10
+ let val = match[key]
11
+ if(null!=val && !params.includes(key)) {
12
+ out[key] = val
13
+ }
14
+ }
15
+
16
+ return out
17
+ }
18
+
19
+ module.exports = {
20
+ query
21
+ }
@@ -0,0 +1,32 @@
1
+
2
+ /* Convert entity data or match query into a srtucture suitable for use as request data.
3
+ *
4
+ * The operation (op) property `reqform` is used to perform the data preparation.
5
+ */
6
+ function reqform(ctx) {
7
+ const { op, spec, result, utility } = ctx
8
+ const { struct, error } = utility
9
+ const { isfunc, transform } = struct
10
+
11
+ if(spec) {
12
+ spec.step = 'reqform'
13
+ }
14
+
15
+ // if (!result.ok) {
16
+ // return undefined
17
+ // }
18
+
19
+ try {
20
+ const reqform = ctx.op.reqform
21
+ const reqdata = isfunc(reqform) ? reqform(ctx) : transform(ctx.op, reqform)
22
+ return reqdata
23
+ }
24
+ catch (err) {
25
+ return error(ctx, err)
26
+ }
27
+ }
28
+
29
+
30
+ module.exports = {
31
+ reqform
32
+ }
@@ -0,0 +1,48 @@
1
+
2
+
3
+ async function request(ctx) {
4
+ const { spec, utility, client } = ctx
5
+ const { fullurl, error } = utility
6
+
7
+ let response = {}
8
+
9
+ try {
10
+ spec.step = 'prepare'
11
+
12
+ const options = client.options()
13
+ const url = spec.url = fullurl(ctx)
14
+
15
+ const fetch = options.system.fetch
16
+
17
+ const fetchdef = {
18
+ method: spec.method,
19
+ headers: spec.headers,
20
+ }
21
+
22
+ if(null != spec.body) {
23
+ fetchdef.body =
24
+ 'object' === typeof spec.body ? JSON.stringify(spec.body) : spec.body
25
+ }
26
+
27
+ spec.step = 'prerequest'
28
+ response = await fetch(url, fetchdef)
29
+
30
+ if(null == response) {
31
+ response = { err: new Error('response: undefined') }
32
+ }
33
+ }
34
+ catch(err) {
35
+ response = response || {}
36
+ response.err = err
37
+ }
38
+
39
+ spec.step = 'postrequest'
40
+
41
+ ctx.response = response
42
+
43
+ return response
44
+ }
45
+
46
+ module.exports = {
47
+ request
48
+ }
@@ -0,0 +1,27 @@
1
+
2
+ function resbasic(ctx) {
3
+ const { response, result } = ctx
4
+
5
+ if(null != response) {
6
+ result.status = response.status || -1
7
+ result.statusText = response.statusText || 'no-status'
8
+
9
+ // TODO: use spec!
10
+ if(400 <= result.status) {
11
+ const msg = 'request: '+result.status+': '+result.statusText
12
+ if(result.err) {
13
+ const prevmsg = null == result.err.message ? '' : result.err.message
14
+ result.err.message = prevmsg+': '+msg
15
+ }
16
+ else {
17
+ result.err = new Error(msg)
18
+ }
19
+ }
20
+ }
21
+
22
+ return result
23
+ }
24
+
25
+ module.exports = {
26
+ resbasic
27
+ }
@@ -0,0 +1,15 @@
1
+
2
+ async function resbody(ctx) {
3
+ const { response, result } = ctx
4
+
5
+ if(response && response.json) {
6
+ const json = await response.json()
7
+ result.body = json
8
+ }
9
+
10
+ return result
11
+ }
12
+
13
+ module.exports = {
14
+ resbody
15
+ }
@@ -0,0 +1,34 @@
1
+
2
+ /* Convert data from respnse into a structure suitable for use as entity data.
3
+ *
4
+ * The operation (op) property `resform` is used to perform the data extraction.
5
+ */
6
+ function resform(ctx) {
7
+ const { op, spec, result, utility } = ctx
8
+ const { struct, error } = utility
9
+ const { isfunc, transform } = struct
10
+
11
+ if(spec) {
12
+ spec.step = 'resform'
13
+ }
14
+
15
+ if (!result.ok) {
16
+ return undefined
17
+ }
18
+
19
+ try {
20
+ const resform = ctx.op.resform
21
+ const resdata = isfunc(resform) ? resform(ctx) : transform(ctx.result, resform)
22
+ result.resdata = resdata
23
+
24
+ return resdata
25
+ }
26
+ catch (err) {
27
+ return error(ctx, err)
28
+ }
29
+ }
30
+
31
+
32
+ module.exports = {
33
+ resform
34
+ }
@@ -0,0 +1,19 @@
1
+
2
+ function resheaders(ctx) {
3
+ const { response, result } = ctx
4
+
5
+ if(response && response.headers && response.headers.forEach) {
6
+ const headers = {}
7
+ response.headers.forEach((v,k)=>headers[k]=v)
8
+ result.headers = headers
9
+ }
10
+ else {
11
+ result.headers = {}
12
+ }
13
+
14
+ return result
15
+ }
16
+
17
+ module.exports = {
18
+ resheaders
19
+ }