@voxgig/sdkgen 0.36.0 → 0.37.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 (135) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/cmp/Main.js +0 -12
  3. package/dist/cmp/Main.js.map +1 -1
  4. package/package.json +1 -1
  5. package/project/.sdk/src/cmp/go/Config_go.ts +6 -2
  6. package/project/.sdk/src/cmp/js/EntityBase_js.ts +34 -0
  7. package/project/.sdk/src/cmp/js/Main_js.ts +10 -0
  8. package/project/.sdk/src/cmp/js/ReadmeExplanation_js.ts +33 -0
  9. package/project/.sdk/src/cmp/js/ReadmeHowto_js.ts +123 -0
  10. package/project/.sdk/src/cmp/js/ReadmeModel_js.ts +152 -0
  11. package/project/.sdk/src/cmp/js/ReadmeTopHowto_js.ts +25 -0
  12. package/project/.sdk/src/cmp/js/ReadmeTopQuick_js.ts +65 -0
  13. package/project/.sdk/src/cmp/js/ReadmeTopTest_js.ts +36 -0
  14. package/project/.sdk/src/cmp/js/TestDirect_js.ts +53 -5
  15. package/project/.sdk/src/cmp/js/TestEntity_js.ts +114 -20
  16. package/project/.sdk/src/cmp/js/fragment/Entity.fragment.js +7 -139
  17. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +149 -0
  18. package/project/.sdk/src/cmp/js/fragment/EntityCreateOp.fragment.js +6 -10
  19. package/project/.sdk/src/cmp/js/fragment/EntityListOp.fragment.js +6 -10
  20. package/project/.sdk/src/cmp/js/fragment/EntityLoadOp.fragment.js +7 -11
  21. package/project/.sdk/src/cmp/js/fragment/EntityRemoveOp.fragment.js +7 -11
  22. package/project/.sdk/src/cmp/js/fragment/EntityUpdateOp.fragment.js +7 -11
  23. package/project/.sdk/src/cmp/js/fragment/Main.fragment.js +2 -0
  24. package/project/.sdk/src/cmp/js/fragment/SdkError.fragment.js +0 -2
  25. package/project/.sdk/src/cmp/lua/Config_lua.ts +6 -2
  26. package/project/.sdk/src/cmp/lua/TestEntity_lua.ts +3 -1
  27. package/project/.sdk/src/cmp/php/Config_php.ts +6 -2
  28. package/project/.sdk/src/cmp/php/TestDirect_php.ts +2 -2
  29. package/project/.sdk/src/cmp/php/TestEntity_php.ts +10 -15
  30. package/project/.sdk/src/cmp/py/Config_py.ts +6 -2
  31. package/project/.sdk/src/cmp/py/TestEntity_py.ts +3 -1
  32. package/project/.sdk/src/cmp/rb/Config_rb.ts +6 -2
  33. package/project/.sdk/src/cmp/ts/Main_ts.ts +7 -0
  34. package/project/.sdk/tm/go/feature/log_feature.go +1 -1
  35. package/project/.sdk/tm/go/test/runner_test.go +16 -2
  36. package/project/.sdk/tm/js/src/Context.js +142 -0
  37. package/project/.sdk/tm/js/src/Control.js +16 -0
  38. package/project/.sdk/tm/js/src/Operation.js +19 -0
  39. package/project/.sdk/tm/js/src/Point.js +24 -0
  40. package/project/.sdk/tm/js/src/README.md +1 -0
  41. package/project/.sdk/tm/js/src/Response.js +19 -0
  42. package/project/.sdk/tm/js/src/Result.js +21 -0
  43. package/project/.sdk/tm/js/src/Spec.js +26 -0
  44. package/project/.sdk/tm/js/src/feature/README.md +1 -0
  45. package/project/.sdk/tm/js/src/feature/base/BaseFeature.js +45 -0
  46. package/project/.sdk/tm/js/src/feature/log/LogFeature.js +46 -47
  47. package/project/.sdk/tm/js/src/feature/test/TestFeature.js +207 -0
  48. package/project/.sdk/tm/js/src/types.js +22 -0
  49. package/project/.sdk/tm/js/src/utility/CleanUtility.js +31 -0
  50. package/project/.sdk/tm/js/src/utility/DoneUtility.js +11 -4
  51. package/project/.sdk/tm/js/src/utility/FeatureAddUtility.js +42 -0
  52. package/project/.sdk/tm/js/src/utility/FeatureHookUtility.js +25 -0
  53. package/project/.sdk/tm/js/src/utility/FeatureInitUtility.js +11 -0
  54. package/project/.sdk/tm/js/src/utility/FetcherUtility.js +28 -0
  55. package/project/.sdk/tm/js/src/utility/MakeContextUtility.js +11 -0
  56. package/project/.sdk/tm/js/src/utility/MakeErrorUtility.js +55 -0
  57. package/project/.sdk/tm/js/src/utility/MakeFetchDefUtility.js +44 -0
  58. package/project/.sdk/tm/js/src/utility/MakeOptionsUtility.js +93 -0
  59. package/project/.sdk/tm/js/src/utility/MakePointUtility.js +77 -0
  60. package/project/.sdk/tm/js/src/utility/MakeRequestUtility.js +63 -0
  61. package/project/.sdk/tm/js/src/utility/MakeResponseUtility.js +55 -0
  62. package/project/.sdk/tm/js/src/utility/MakeResultUtility.js +54 -0
  63. package/project/.sdk/tm/js/src/utility/MakeSpecUtility.js +58 -0
  64. package/project/.sdk/tm/js/src/utility/MakeUrlUtility.js +40 -0
  65. package/project/.sdk/tm/js/src/utility/ParamUtility.js +61 -0
  66. package/project/.sdk/tm/js/src/utility/PrepareAuthUtility.js +41 -0
  67. package/project/.sdk/tm/js/src/utility/PrepareBodyUtility.js +25 -0
  68. package/project/.sdk/tm/js/src/utility/PrepareHeadersUtility.js +18 -0
  69. package/project/.sdk/tm/js/src/utility/{MethodUtility.js → PrepareMethodUtility.js} +7 -7
  70. package/project/.sdk/tm/js/src/utility/PrepareParamsUtility.js +25 -0
  71. package/project/.sdk/tm/js/src/utility/PreparePathUtility.js +13 -0
  72. package/project/.sdk/tm/js/src/utility/PrepareQueryUtility.js +26 -0
  73. package/project/.sdk/tm/js/src/utility/README.md +1 -0
  74. package/project/.sdk/tm/js/src/utility/ResultBasicUtility.js +34 -0
  75. package/project/.sdk/tm/js/src/utility/ResultBodyUtility.js +18 -0
  76. package/project/.sdk/tm/js/src/utility/ResultHeadersUtility.js +22 -0
  77. package/project/.sdk/tm/js/src/utility/StructUtility.js +2219 -1078
  78. package/project/.sdk/tm/js/src/utility/TransformRequestUtility.js +28 -0
  79. package/project/.sdk/tm/js/src/utility/TransformResponseUtility.js +31 -0
  80. package/project/.sdk/tm/js/src/utility/Utility.js +61 -61
  81. package/project/.sdk/tm/js/test/README.md +1 -0
  82. package/project/.sdk/tm/js/test/exists.test.js +16 -0
  83. package/project/.sdk/tm/js/test/runner.js +323 -107
  84. package/project/.sdk/tm/js/test/utility/Custom.test.js +41 -63
  85. package/project/.sdk/tm/js/test/utility/PrimaryUtility.test.js +390 -116
  86. package/project/.sdk/tm/js/test/utility/StructUtility.test.js +728 -175
  87. package/project/.sdk/tm/js/test/utility/index.js +9 -0
  88. package/project/.sdk/tm/js/test/utility.js +72 -0
  89. package/project/.sdk/tm/lua/test/primary_utility_test.lua +1213 -0
  90. package/project/.sdk/tm/lua/test/runner.lua +2 -2
  91. package/project/.sdk/tm/lua/test/struct_runner.lua +602 -0
  92. package/project/.sdk/tm/lua/test/struct_utility_test.lua +959 -0
  93. package/project/.sdk/tm/lua/utility/struct/struct.lua +10 -0
  94. package/project/.sdk/tm/php/feature/TestFeature.php +59 -96
  95. package/project/.sdk/tm/php/test/PrimaryUtilityTest.php +1309 -0
  96. package/project/.sdk/tm/php/test/Runner.php +24 -1
  97. package/project/.sdk/tm/php/test/StructRunner.php +275 -0
  98. package/project/.sdk/tm/php/test/StructUtilityTest.php +1336 -0
  99. package/project/.sdk/tm/php/utility/Fetcher.php +6 -2
  100. package/project/.sdk/tm/php/utility/MakeOptions.php +5 -1
  101. package/project/.sdk/tm/php/utility/MakeResult.php +3 -0
  102. package/project/.sdk/tm/php/utility/Param.php +9 -7
  103. package/project/.sdk/tm/php/utility/struct/Struct.php +312 -208
  104. package/project/.sdk/tm/py/test/runner.py +13 -0
  105. package/project/.sdk/tm/py/test/struct_runner.py +411 -0
  106. package/project/.sdk/tm/py/test/test_primary_utility.py +1101 -0
  107. package/project/.sdk/tm/py/test/test_struct_utility.py +751 -0
  108. package/project/.sdk/tm/rb/test/primary_utility_test.rb +1083 -0
  109. package/project/.sdk/tm/rb/test/runner.rb +5 -0
  110. package/project/.sdk/tm/rb/test/struct_runner.rb +309 -0
  111. package/project/.sdk/tm/rb/test/struct_utility_test.rb +670 -0
  112. package/src/cmp/Main.ts +1 -16
  113. package/project/.sdk/src/cmp/js/Quick_js.ts +0 -78
  114. package/project/.sdk/src/cmp/js/TestAcceptEntity_js.ts +0 -13
  115. package/project/.sdk/src/cmp/js/TestAccept_js.ts +0 -18
  116. package/project/.sdk/tm/js/src/utility/AuthUtility.js +0 -21
  117. package/project/.sdk/tm/js/src/utility/BodyUtility.js +0 -29
  118. package/project/.sdk/tm/js/src/utility/ErrorUtility.js +0 -33
  119. package/project/.sdk/tm/js/src/utility/FindparamUtility.js +0 -31
  120. package/project/.sdk/tm/js/src/utility/FullurlUtility.js +0 -39
  121. package/project/.sdk/tm/js/src/utility/HeadersUtility.js +0 -13
  122. package/project/.sdk/tm/js/src/utility/JoinurlUtility.js +0 -14
  123. package/project/.sdk/tm/js/src/utility/OperatorUtility.js +0 -44
  124. package/project/.sdk/tm/js/src/utility/OptionsUtility.js +0 -54
  125. package/project/.sdk/tm/js/src/utility/ParamsUtility.js +0 -21
  126. package/project/.sdk/tm/js/src/utility/QueryUtility.js +0 -21
  127. package/project/.sdk/tm/js/src/utility/ReqformUtility.js +0 -32
  128. package/project/.sdk/tm/js/src/utility/RequestUtility.js +0 -48
  129. package/project/.sdk/tm/js/src/utility/ResbasicUtility.js +0 -27
  130. package/project/.sdk/tm/js/src/utility/ResbodyUtility.js +0 -15
  131. package/project/.sdk/tm/js/src/utility/ResformUtility.js +0 -34
  132. package/project/.sdk/tm/js/src/utility/ResheadersUtility.js +0 -19
  133. package/project/.sdk/tm/js/src/utility/ResponseUtility.js +0 -37
  134. package/project/.sdk/tm/js/src/utility/ResultUtility.js +0 -28
  135. package/project/.sdk/tm/js/src/utility/SpecUtility.js +0 -35
@@ -1,78 +0,0 @@
1
-
2
- import { names, getx, each, cmp, File, Content } from '@voxgig/sdkgen'
3
-
4
-
5
- const Quick = cmp(function Quick(props: any) {
6
- const { target } = props
7
- const { model, meta: { spec } } = props.ctx$
8
-
9
- // get quick entity from target config
10
-
11
- let ent: any
12
- let entmap = getx(spec.config.guideModel, 'guide entity?test:quick:active=true')
13
-
14
- if (entmap) {
15
- ent = Object.values(entmap)[0]
16
- ent.name = Object.keys(entmap)[0]
17
- }
18
-
19
- ent = ent || { name: 'Entity' }
20
- names(ent, ent.name)
21
-
22
- // TODO: selected features should be active by default!
23
-
24
- const featureOptions = each(model.main.kit.feature)
25
- .filter((f: any) => f.active)
26
- .reduce((a: any, f: any) => a + `\n ${f.name}: { active: true },`, '')
27
-
28
-
29
- File({ name: 'quick.' + target.name }, () => {
30
-
31
- Content(`
32
- require('dotenv').config({ path: ['../../.env.local']})
33
-
34
- const { ${model.const.Name}SDK } = require('../')
35
-
36
- run()
37
-
38
- async function run() {
39
- const client = new ${model.const.Name}SDK({
40
- apikey: process.env.${model.NAME}_APIKEY,
41
- ${featureOptions}
42
- })
43
-
44
- `)
45
-
46
- if (ent.test?.quick.create) {
47
- Content(`
48
- out = await client.${ent.Name}().create(${JSON.stringify(ent.test?.quick.create)})
49
- console.log('${ent.Name}.load', out)
50
- `)
51
- }
52
-
53
- if (ent.test?.quick.load) {
54
- Content(`
55
- out = await client.${ent.Name}().load(${JSON.stringify(ent.test?.quick.load)})
56
- console.log('${ent.Name}.load', out)
57
- `)
58
- }
59
-
60
- if (ent.test?.quick.list) {
61
- Content(`
62
- out = await client.${ent.Name}().list(${JSON.stringify(ent.test?.quick.list)})
63
- console.log('${ent.Name}.list', out)
64
- `)
65
- }
66
-
67
- Content(`
68
- }
69
- `)
70
-
71
- })
72
- })
73
-
74
-
75
- export {
76
- Quick
77
- }
78
-
@@ -1,13 +0,0 @@
1
-
2
- import { cmp, camelify, Content } from '@voxgig/sdkgen'
3
-
4
-
5
- const TestAcceptEntity = cmp(function TestEntity(props: any) {
6
- const { entity } = props
7
-
8
- })
9
-
10
-
11
- export {
12
- TestAcceptEntity
13
- }
@@ -1,18 +0,0 @@
1
-
2
- import { cmp, each, File, Content } from '@voxgig/sdkgen'
3
-
4
-
5
- import { TestAcceptEntity } from './TestAcceptEntity_js'
6
-
7
-
8
- const TestAccept = cmp(function TestMain(props: any) {
9
- const { target } = props
10
- const { model } = props.ctx$
11
-
12
- })
13
-
14
-
15
- export {
16
- TestAccept
17
- }
18
-
@@ -1,21 +0,0 @@
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
- }
@@ -1,29 +0,0 @@
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
- }
@@ -1,33 +0,0 @@
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
- }
@@ -1,31 +0,0 @@
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
- }
@@ -1,39 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,14 +0,0 @@
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
- }
@@ -1,44 +0,0 @@
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
- }
@@ -1,54 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
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
- }
@@ -1,32 +0,0 @@
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
- }
@@ -1,48 +0,0 @@
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
- }
@@ -1,27 +0,0 @@
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
- }
@@ -1,15 +0,0 @@
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
- }
@@ -1,34 +0,0 @@
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
- }
@@ -1,19 +0,0 @@
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
- }
@@ -1,37 +0,0 @@
1
-
2
- async function response(ctx) {
3
- let { response, spec, utility } = ctx
4
-
5
- const { resheaders, resbasic, resbody, resform } = utility
6
-
7
- spec.step = 'response'
8
-
9
- let result = {
10
- ok: false,
11
- status: -1,
12
- statusText: '',
13
- headers: {},
14
- body: undefined,
15
- err: response.err,
16
- }
17
-
18
- ctx.result = result
19
-
20
- try {
21
- resbasic(ctx)
22
- resheaders(ctx)
23
- await resbody(ctx)
24
- resform(ctx)
25
-
26
- if(null == result.err) {
27
- result.ok = true
28
- }
29
- }
30
- catch(err) {
31
- result.err = err
32
- }
33
- }
34
-
35
- module.exports = {
36
- response
37
- }
@@ -1,28 +0,0 @@
1
-
2
- async function result(ctx) {
3
- let { op, response, spec, utility } = ctx
4
-
5
- const { resheaders, resbasic, resbody, resform } = utility
6
-
7
- spec.step = 'result'
8
-
9
- resform(ctx)
10
-
11
- if('list' == op.name) {
12
- let resdata = ctx.result.resdata
13
- ctx.resdata = []
14
-
15
- if(null != resdata && 0 < resdata.length) {
16
- for(let entry of resdata) {
17
- const entity = entity.make()
18
- entity.data(entry)
19
- ctx.resdata.push(entity)
20
- }
21
- }
22
- }
23
- }
24
-
25
-
26
- module.exports = {
27
- result
28
- }