gingersnap 0.22.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 (251) hide show
  1. package/_virtual/_tslib.cjs +102 -0
  2. package/_virtual/_tslib.mjs +93 -0
  3. package/data/AtomicValue.d.ts +15 -0
  4. package/data/bus.cjs +179 -0
  5. package/data/bus.d.ts +72 -0
  6. package/data/bus.mjs +177 -0
  7. package/data/decoders/index.d.ts +4 -0
  8. package/data/decoders/json.cjs +12 -0
  9. package/data/decoders/json.d.ts +7 -0
  10. package/data/decoders/json.mjs +10 -0
  11. package/data/decoders/msgpack.cjs +14 -0
  12. package/data/decoders/msgpack.d.ts +7 -0
  13. package/data/decoders/msgpack.mjs +12 -0
  14. package/data/decoders/protobuf.d.ts +13 -0
  15. package/data/decoders/string.cjs +12 -0
  16. package/data/decoders/string.d.ts +7 -0
  17. package/data/decoders/string.mjs +10 -0
  18. package/data/decoders/type.d.ts +7 -0
  19. package/data/decoders.cjs +11 -0
  20. package/data/decoders.mjs +3 -0
  21. package/data/model/credentials.cjs +114 -0
  22. package/data/model/credentials.d.ts +61 -0
  23. package/data/model/credentials.mjs +109 -0
  24. package/data/model/extra.cjs +88 -0
  25. package/data/model/extra.d.ts +5 -0
  26. package/data/model/extra.mjs +66 -0
  27. package/data/model/index.d.ts +6 -0
  28. package/data/model/model.cjs +541 -0
  29. package/data/model/model.d.ts +207 -0
  30. package/data/model/model.mjs +518 -0
  31. package/data/model/plugin.cjs +96 -0
  32. package/data/model/plugin.d.ts +38 -0
  33. package/data/model/plugin.mjs +72 -0
  34. package/data/model/property.cjs +310 -0
  35. package/data/model/property.d.ts +83 -0
  36. package/data/model/property.mjs +278 -0
  37. package/data/model/types.cjs +32 -0
  38. package/data/model/types.d.ts +28 -0
  39. package/data/model/types.mjs +32 -0
  40. package/data/model.cjs +43 -0
  41. package/data/model.mjs +6 -0
  42. package/data/signal.cjs +47 -0
  43. package/data/signal.d.ts +17 -0
  44. package/data/signal.mjs +45 -0
  45. package/data/store/index.d.ts +4 -0
  46. package/data/store/json.cjs +58 -0
  47. package/data/store/json.d.ts +18 -0
  48. package/data/store/json.mjs +56 -0
  49. package/data/store/manager.cjs +206 -0
  50. package/data/store/manager.d.ts +44 -0
  51. package/data/store/manager.mjs +203 -0
  52. package/data/store/web.cjs +92 -0
  53. package/data/store/web.d.ts +25 -0
  54. package/data/store/web.mjs +89 -0
  55. package/data/value.cjs +66 -0
  56. package/data/value.mjs +64 -0
  57. package/data-structures/array/CyclicalList.cjs +57 -0
  58. package/data-structures/array/CyclicalList.d.ts +12 -0
  59. package/data-structures/array/CyclicalList.mjs +55 -0
  60. package/data-structures/array/Pair.cjs +30 -0
  61. package/data-structures/array/Pair.d.ts +17 -0
  62. package/data-structures/array/Pair.mjs +27 -0
  63. package/data-structures/array/Stack.cjs +96 -0
  64. package/data-structures/array/Stack.d.ts +41 -0
  65. package/data-structures/array/Stack.mjs +94 -0
  66. package/data-structures/array/Triple.cjs +34 -0
  67. package/data-structures/array/Triple.d.ts +19 -0
  68. package/data-structures/array/Triple.mjs +31 -0
  69. package/data-structures/array/Tuple.d.ts +8 -0
  70. package/data-structures/array/index.d.ts +5 -0
  71. package/data-structures/array.cjs +15 -0
  72. package/data-structures/array.mjs +4 -0
  73. package/data-structures/object/BufferQueue.cjs +98 -0
  74. package/data-structures/object/BufferQueue.d.ts +21 -0
  75. package/data-structures/object/BufferQueue.mjs +77 -0
  76. package/data-structures/object/CyclicalObject.cjs +155 -0
  77. package/data-structures/object/CyclicalObject.d.ts +74 -0
  78. package/data-structures/object/CyclicalObject.mjs +134 -0
  79. package/data-structures/object/Queue.cjs +178 -0
  80. package/data-structures/object/Queue.d.ts +31 -0
  81. package/data-structures/object/Queue.mjs +176 -0
  82. package/data-structures/object/SimpleQueue.cjs +53 -0
  83. package/data-structures/object/SimpleQueue.d.ts +13 -0
  84. package/data-structures/object/SimpleQueue.mjs +51 -0
  85. package/data-structures/object/TimeableObject.cjs +73 -0
  86. package/data-structures/object/TimeableObject.d.ts +30 -0
  87. package/data-structures/object/TimeableObject.mjs +71 -0
  88. package/data-structures/object/WaitableObject.cjs +61 -0
  89. package/data-structures/object/WaitableObject.d.ts +29 -0
  90. package/data-structures/object/WaitableObject.mjs +59 -0
  91. package/data-structures/object/WatchableObject.cjs +251 -0
  92. package/data-structures/object/WatchableObject.d.ts +88 -0
  93. package/data-structures/object/WatchableObject.mjs +230 -0
  94. package/data-structures/object/index.d.ts +7 -0
  95. package/data-structures/object.cjs +23 -0
  96. package/data-structures/object.mjs +7 -0
  97. package/errors/AbortError.cjs +9 -0
  98. package/errors/AbortError.d.ts +5 -0
  99. package/errors/AbortError.mjs +7 -0
  100. package/errors/CallExecutionError.cjs +13 -0
  101. package/errors/CallExecutionError.d.ts +7 -0
  102. package/errors/CallExecutionError.mjs +11 -0
  103. package/errors/FutureCancelled.cjs +11 -0
  104. package/errors/FutureCancelled.d.ts +6 -0
  105. package/errors/FutureCancelled.mjs +9 -0
  106. package/errors/FutureError.cjs +11 -0
  107. package/errors/FutureError.d.ts +6 -0
  108. package/errors/FutureError.mjs +9 -0
  109. package/errors/IllegalArgumentsError.cjs +13 -0
  110. package/errors/IllegalArgumentsError.d.ts +7 -0
  111. package/errors/IllegalArgumentsError.mjs +11 -0
  112. package/errors/IllegalOperationError.cjs +6 -0
  113. package/errors/IllegalOperationError.d.ts +2 -0
  114. package/errors/IllegalOperationError.mjs +4 -0
  115. package/errors/InvalidType.cjs +6 -0
  116. package/errors/InvalidType.d.ts +2 -0
  117. package/errors/InvalidType.mjs +4 -0
  118. package/errors/InvalidValue.cjs +11 -0
  119. package/errors/InvalidValue.d.ts +4 -0
  120. package/errors/InvalidValue.mjs +9 -0
  121. package/errors/MissingArgumentsError.cjs +13 -0
  122. package/errors/MissingArgumentsError.d.ts +7 -0
  123. package/errors/MissingArgumentsError.mjs +11 -0
  124. package/errors/NetworkError.cjs +13 -0
  125. package/errors/NetworkError.d.ts +8 -0
  126. package/errors/NetworkError.mjs +11 -0
  127. package/errors/NoSuchElement.cjs +6 -0
  128. package/errors/NoSuchElement.d.ts +2 -0
  129. package/errors/NoSuchElement.mjs +4 -0
  130. package/errors/NotImplemented.cjs +9 -0
  131. package/errors/NotImplemented.d.ts +5 -0
  132. package/errors/NotImplemented.mjs +7 -0
  133. package/errors/ParsingError.cjs +10 -0
  134. package/errors/ParsingError.d.ts +5 -0
  135. package/errors/ParsingError.mjs +8 -0
  136. package/errors/QueueEmptyError.cjs +6 -0
  137. package/errors/QueueEmptyError.d.ts +2 -0
  138. package/errors/QueueEmptyError.mjs +4 -0
  139. package/errors/QueueFullError.cjs +6 -0
  140. package/errors/QueueFullError.d.ts +2 -0
  141. package/errors/QueueFullError.mjs +4 -0
  142. package/errors/StackEmptyError.cjs +6 -0
  143. package/errors/StackEmptyError.d.ts +2 -0
  144. package/errors/StackEmptyError.mjs +4 -0
  145. package/errors/StackFullError.cjs +6 -0
  146. package/errors/StackFullError.d.ts +2 -0
  147. package/errors/StackFullError.mjs +4 -0
  148. package/errors/StreamEnded.cjs +6 -0
  149. package/errors/StreamEnded.d.ts +2 -0
  150. package/errors/StreamEnded.mjs +4 -0
  151. package/errors/TimeoutError.cjs +11 -0
  152. package/errors/TimeoutError.d.ts +6 -0
  153. package/errors/TimeoutError.mjs +9 -0
  154. package/errors/ValueDestroyedError.cjs +6 -0
  155. package/errors/ValueDestroyedError.d.ts +2 -0
  156. package/errors/ValueDestroyedError.mjs +4 -0
  157. package/errors/VerificationError.cjs +9 -0
  158. package/errors/VerificationError.d.ts +5 -0
  159. package/errors/VerificationError.mjs +7 -0
  160. package/errors/index.d.ts +18 -0
  161. package/errors.cjs +41 -0
  162. package/errors.mjs +18 -0
  163. package/files.cjs +201 -0
  164. package/files.d.ts +31 -0
  165. package/files.mjs +180 -0
  166. package/functools/index.d.ts +31 -0
  167. package/functools.cjs +371 -0
  168. package/functools.mjs +334 -0
  169. package/future/future.cjs +570 -0
  170. package/future/future.d.ts +208 -0
  171. package/future/future.mjs +548 -0
  172. package/future/group.cjs +180 -0
  173. package/future/group.d.ts +82 -0
  174. package/future/group.mjs +159 -0
  175. package/future/index.d.ts +3 -0
  176. package/future/result.cjs +19 -0
  177. package/future/result.d.ts +18 -0
  178. package/future/result.mjs +17 -0
  179. package/future.cjs +11 -0
  180. package/future.mjs +3 -0
  181. package/managers/context.d.ts +8 -0
  182. package/managers/index.d.ts +1 -0
  183. package/managers.cjs +2 -0
  184. package/managers.mjs +1 -0
  185. package/mocks.cjs +301 -0
  186. package/mocks.d.ts +125 -0
  187. package/mocks.mjs +293 -0
  188. package/networking/EventSourceService.cjs +104 -0
  189. package/networking/EventSourceService.d.ts +11 -0
  190. package/networking/EventSourceService.mjs +83 -0
  191. package/networking/NetworkService.cjs +394 -0
  192. package/networking/NetworkService.d.ts +97 -0
  193. package/networking/NetworkService.mjs +373 -0
  194. package/networking/SocketService.cjs +218 -0
  195. package/networking/SocketService.d.ts +23 -0
  196. package/networking/SocketService.mjs +197 -0
  197. package/networking/decorators/index.d.ts +4 -0
  198. package/networking/decorators/options.cjs +289 -0
  199. package/networking/decorators/options.d.ts +148 -0
  200. package/networking/decorators/options.mjs +242 -0
  201. package/networking/decorators/requests.cjs +80 -0
  202. package/networking/decorators/requests.d.ts +34 -0
  203. package/networking/decorators/requests.mjs +52 -0
  204. package/networking/decorators/socket.cjs +81 -0
  205. package/networking/decorators/socket.d.ts +13 -0
  206. package/networking/decorators/socket.mjs +51 -0
  207. package/networking/http.cjs +69 -0
  208. package/networking/http.d.ts +66 -0
  209. package/networking/http.mjs +69 -0
  210. package/networking/index.d.ts +39 -0
  211. package/networking/request.cjs +18 -0
  212. package/networking/request.d.ts +9 -0
  213. package/networking/request.mjs +16 -0
  214. package/networking/types.cjs +37 -0
  215. package/networking/types.d.ts +132 -0
  216. package/networking/types.mjs +35 -0
  217. package/networking.cjs +91 -0
  218. package/networking.mjs +33 -0
  219. package/package.json +1 -0
  220. package/reflection/injector.cjs +43 -0
  221. package/reflection/injector.d.ts +3 -0
  222. package/reflection/injector.mjs +40 -0
  223. package/socket.cjs +239 -0
  224. package/socket.d.ts +65 -0
  225. package/socket.mjs +237 -0
  226. package/store.cjs +12 -0
  227. package/store.mjs +3 -0
  228. package/stream/call.cjs +157 -0
  229. package/stream/call.d.ts +66 -0
  230. package/stream/call.mjs +154 -0
  231. package/stream/collector.cjs +217 -0
  232. package/stream/collector.d.ts +14 -0
  233. package/stream/collector.mjs +215 -0
  234. package/stream/index.d.ts +257 -0
  235. package/stream/observable.cjs +110 -0
  236. package/stream/observable.d.ts +65 -0
  237. package/stream/observable.mjs +106 -0
  238. package/stream/state.cjs +10 -0
  239. package/stream/state.d.ts +5 -0
  240. package/stream/state.mjs +8 -0
  241. package/stream.cjs +1153 -0
  242. package/stream.mjs +1132 -0
  243. package/synchronize.cjs +106 -0
  244. package/synchronize.d.ts +54 -0
  245. package/synchronize.mjs +103 -0
  246. package/typing/types.d.ts +24 -0
  247. package/typing.cjs +2 -0
  248. package/typing.mjs +1 -0
  249. package/utils/parser.cjs +37 -0
  250. package/utils/parser.d.ts +7 -0
  251. package/utils/parser.mjs +16 -0
@@ -0,0 +1,394 @@
1
+ 'use strict';
2
+
3
+ var _tslib = require('../_virtual/_tslib.cjs');
4
+ var model = require('../data/model/model.cjs');
5
+ var R = require('ramda');
6
+ var CallExecutionError = require('../errors/CallExecutionError.cjs');
7
+ require('../data/model/types.cjs');
8
+ var credentials = require('../data/model/credentials.cjs');
9
+ var types = require('./types.cjs');
10
+ var stream_call = require('../stream/call.cjs');
11
+ var options = require('./decorators/options.cjs');
12
+ var requests = require('./decorators/requests.cjs');
13
+ var http = require('./http.cjs');
14
+ require('uuid');
15
+ var request = require('./request.cjs');
16
+ var future = require('../future/future.cjs');
17
+ var stream = require('../stream.cjs');
18
+ var hash = require('object-hash');
19
+ var manager = require('../data/store/manager.cjs');
20
+ var stream_collector = require('../stream/collector.cjs');
21
+
22
+ function _interopNamespaceDefault(e) {
23
+ var n = Object.create(null);
24
+ if (e) {
25
+ Object.keys(e).forEach(function (k) {
26
+ if (k !== 'default') {
27
+ var d = Object.getOwnPropertyDescriptor(e, k);
28
+ Object.defineProperty(n, k, d.get ? d : {
29
+ enumerable: true,
30
+ get: function () { return e[k]; }
31
+ });
32
+ }
33
+ });
34
+ }
35
+ n.default = e;
36
+ return Object.freeze(n);
37
+ }
38
+
39
+ var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
40
+
41
+ /**
42
+ * A Snap Service for managing network requests
43
+ */
44
+ class NetworkService {
45
+ constructor({ baseUrl, retryLimit, cacheManager } = {}) {
46
+ var _a, _b, _c;
47
+ this.retryLimit = retryLimit;
48
+ this.__internal__ = (_a = this.__internal__) !== null && _a !== void 0 ? _a : { classConfig: {}, methodConfig: {} };
49
+ this.baseUrl = (_c = (_b = this.__internal__.classConfig.baseUrl) !== null && _b !== void 0 ? _b : baseUrl) !== null && _c !== void 0 ? _c : "";
50
+ this.cacheManager = cacheManager !== null && cacheManager !== void 0 ? cacheManager : new manager.CacheManager();
51
+ }
52
+ /**
53
+ * Converts the given value to a JSON object
54
+ * @param value Any data
55
+ * @private
56
+ */
57
+ convertToJSON(value) {
58
+ if (value instanceof model.Model) {
59
+ return value.json();
60
+ }
61
+ else if (value instanceof Array || Array.isArray(value)) {
62
+ return `[${value.map((v) => this.convertToJSON(v))}]`;
63
+ }
64
+ return typeof value === "string" ? value : JSON.stringify(value);
65
+ }
66
+ /**
67
+ * Internal method called to wrap all network request methods with actual networking functionalities
68
+ * @private
69
+ */
70
+ __setup__() {
71
+ const hostname = this.baseUrl;
72
+ const self = this;
73
+ R__namespace.forEach(([key, value]) => {
74
+ var _a, _b, _c, _d, _e, _f;
75
+ let headers = (_a = value.headers) !== null && _a !== void 0 ? _a : {};
76
+ const apiPath = (_b = value.apiPath) !== null && _b !== void 0 ? _b : "";
77
+ const oldMethod = self[key];
78
+ const auth = { global: (_f = (_d = (_c = value.authRefresher) === null || _c === void 0 ? void 0 : _c.global) !== null && _d !== void 0 ? _d : (_e = value.authenticator) === null || _e === void 0 ? void 0 : _e.global) !== null && _f !== void 0 ? _f : false };
79
+ if (value.requestType === undefined) {
80
+ this.__setup_authentication__(value, key, self[key], auth);
81
+ return;
82
+ }
83
+ const requestType = value.requestType;
84
+ const cacheInfo = value.cache;
85
+ let cache;
86
+ if (cacheInfo) {
87
+ cache = this.cacheManager.createCache(this.baseUrl, cacheInfo.persist, (resp) => _tslib.__awaiter(this, void 0, void 0, function* () {
88
+ return JSON.stringify({
89
+ headers: Object.fromEntries(resp.headers),
90
+ status: resp.status,
91
+ statusText: resp.statusText,
92
+ body: yield stream.Stream.of(resp.body).collect(stream_collector.Collectors.asList()),
93
+ });
94
+ }), (v) => {
95
+ const { headers, status, statusText, body } = JSON.parse(v);
96
+ return new Response(stream.Stream.of(body).readableStream, { status, statusText, headers });
97
+ }, undefined, cacheInfo.duration);
98
+ }
99
+ self[key] = (...args) => {
100
+ var _a;
101
+ let url = new URL((hostname.endsWith("/") ? hostname.substring(0, hostname.length) : hostname) +
102
+ (apiPath.startsWith("/") ? apiPath : "/" + apiPath));
103
+ return new stream_call.Call((signal) => _tslib.__awaiter(this, void 0, void 0, function* () {
104
+ headers = Object.assign(Object.assign({}, headers), this.__get_auth_headers__(auth));
105
+ const result = this.__constructor_call_args__(url, headers, value, args);
106
+ headers = result.headers;
107
+ url = result.url;
108
+ const body = result.body;
109
+ if (cache && cacheInfo.method === requests.CachingMethod.HIT_FIRST) {
110
+ let key = `${url.toString()}-${requestType.toString()}-${hash.sha1(headers)}`;
111
+ if (body) {
112
+ key += hash(body);
113
+ }
114
+ const result = yield cache.get(key).registerSignal(signal);
115
+ if (result) {
116
+ return result.clone();
117
+ }
118
+ }
119
+ const lookup = (retries = 0) => request.request(url, { headers, method: requestType.toString(), body }).thenApply(({ value: resp }) => _tslib.__awaiter(this, void 0, void 0, function* () {
120
+ let credentials;
121
+ if (resp.status === http.HTTPStatus.UNAUTHORIZED &&
122
+ !value.noAuth &&
123
+ self[key] !== auth.authenticator &&
124
+ self[key] !== auth.authRefresher &&
125
+ (credentials = yield this.__get_credentials__(auth))) {
126
+ return request.request(url, {
127
+ headers: Object.assign(Object.assign({}, headers), credentials.buildAuthHeaders()),
128
+ method: requestType.toString(),
129
+ body,
130
+ });
131
+ }
132
+ else if ((resp.status === http.HTTPStatus.GATEWAY_TIMEOUT || resp.status === http.HTTPStatus.SERVICE_UNAVAILABLE) &&
133
+ self.retryLimit &&
134
+ retries < self.retryLimit) {
135
+ return future.Future.sleep({ milliseconds: options.THROTTLE_DEFAULT_MS }).thenApply(() => lookup(retries + 1));
136
+ }
137
+ if (cache) {
138
+ let key = `${url.toString()}-${requestType.toString()}-${hash.sha1(headers)}`;
139
+ if (resp.ok) {
140
+ if (body) {
141
+ key += hash(body);
142
+ }
143
+ cache.set(key, resp.clone());
144
+ }
145
+ else if (cache && !resp.ok && cacheInfo.method === requests.CachingMethod.FALLBACK_ON_MISSING) {
146
+ return cache.get(key);
147
+ }
148
+ }
149
+ return resp;
150
+ }));
151
+ return lookup().registerSignal(signal);
152
+ }), oldMethod, value.responseClass, value.throttle, (_a = value.responseType) !== null && _a !== void 0 ? _a : types.ResponseType.NONE, value.responseArray);
153
+ };
154
+ this.__setup_authentication__(value, key, self[key], auth);
155
+ }, R__namespace.toPairs(this.__internal__.methodConfig));
156
+ }
157
+ __constructor_call_args__(url, headers, value, args) {
158
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
159
+ let body;
160
+ if (value.parameters) {
161
+ const paramHeaders = (_a = value.parameters.headers) !== null && _a !== void 0 ? _a : {};
162
+ const paramQueries = (_b = value.parameters.queries) !== null && _b !== void 0 ? _b : {};
163
+ const paramHeaderPairs = R__namespace.concat(R__namespace.toPairs(paramHeaders), R__namespace.map((k) => [k, paramHeaders[k]], Object.getOwnPropertySymbols(paramHeaders)));
164
+ const paramQueryPairs = R__namespace.concat(R__namespace.toPairs(paramQueries), R__namespace.map((k) => [k, paramQueries[k]], Object.getOwnPropertySymbols(paramQueries)));
165
+ R__namespace.forEach(([key, index]) => {
166
+ const arg = args[index];
167
+ if (arg === undefined) {
168
+ throw new CallExecutionError.CallExecutionError(`header parameter is missing at index ${index}`);
169
+ }
170
+ if (typeof key === "symbol" && typeof arg === "object") {
171
+ headers = Object.assign(Object.assign({}, headers), arg);
172
+ }
173
+ else {
174
+ headers[key] = headers[key] ? `${R__namespace.prop(key, headers)};${String(args[index])}` : String(args[index]);
175
+ }
176
+ }, paramHeaderPairs);
177
+ R__namespace.forEach(([key, index]) => {
178
+ if (args[index] === undefined) {
179
+ throw new CallExecutionError.CallExecutionError(`path parameter is missing at index ${index}`);
180
+ }
181
+ url.pathname = url.pathname.replace(encodeURI(`{${key}}`), encodeURI(args[index]));
182
+ }, R__namespace.toPairs((_c = value.parameters.pathVariables) !== null && _c !== void 0 ? _c : {}));
183
+ R__namespace.forEach(([key, index]) => {
184
+ const arg = args[index];
185
+ if (arg === undefined) {
186
+ throw new CallExecutionError.CallExecutionError(`query parameter is missing at index ${index}`);
187
+ }
188
+ if (typeof key === "symbol" && typeof arg === "object") {
189
+ R__namespace.forEach(([k, v]) => {
190
+ url.searchParams.set(k, v);
191
+ }, R__namespace.toPairs(arg));
192
+ }
193
+ else {
194
+ url.searchParams.set(key, arg);
195
+ }
196
+ }, paramQueryPairs);
197
+ if ((_d = value.parameters.body) === null || _d === void 0 ? void 0 : _d.type) {
198
+ switch (value.parameters.body.type) {
199
+ case types.BodyType.STRING: {
200
+ const index = (_e = value.parameters.body.parameterIndex) !== null && _e !== void 0 ? _e : -1;
201
+ if (args[index] === undefined) {
202
+ throw new CallExecutionError.CallExecutionError(`body parameter is missing at index ${index}`);
203
+ }
204
+ body = this.convertToJSON(args[index]);
205
+ break;
206
+ }
207
+ case types.BodyType.JSON: {
208
+ headers["Content-Type"] = (_f = headers["Content-Type"]) !== null && _f !== void 0 ? _f : "application/json";
209
+ const index = (_g = value.parameters.body.parameterIndex) !== null && _g !== void 0 ? _g : -1;
210
+ let data = {};
211
+ if (args[index] === undefined) {
212
+ const fields = value.parameters.body.fields;
213
+ if (!fields) {
214
+ throw new CallExecutionError.CallExecutionError(`body parameter is missing at index ${index}`);
215
+ }
216
+ R__namespace.forEach(([key, index]) => {
217
+ const arg = args[index];
218
+ if (arg === undefined) {
219
+ throw new CallExecutionError.CallExecutionError(`form field is missing at index ${index}`);
220
+ }
221
+ data[key] = arg;
222
+ }, R__namespace.toPairs((_h = value.parameters.body.fields) !== null && _h !== void 0 ? _h : {}));
223
+ R__namespace.forEach(([key, index]) => {
224
+ const arg = args[index];
225
+ if (arg !== undefined && arg !== null) {
226
+ data[key] = arg;
227
+ }
228
+ }, R__namespace.toPairs((_j = value.parameters.body.optionalFields) !== null && _j !== void 0 ? _j : {}));
229
+ }
230
+ else {
231
+ data = args[index];
232
+ }
233
+ body = this.convertToJSON(data);
234
+ break;
235
+ }
236
+ case types.BodyType.XML: {
237
+ const index = value.parameters.body.parameterIndex;
238
+ const arg = args[index];
239
+ if (arg === undefined) {
240
+ throw new CallExecutionError.CallExecutionError(`body parameter is missing at index ${index}`);
241
+ }
242
+ body = arg instanceof model.Model ? arg.xml() : new XMLSerializer().serializeToString(arg);
243
+ break;
244
+ }
245
+ case types.BodyType.FORMURLENCODED: {
246
+ headers["Content-Type"] = (_k = headers["Content-Type"]) !== null && _k !== void 0 ? _k : "application/x-www-form-urlencoded";
247
+ const formData = new URLSearchParams();
248
+ R__namespace.forEach(([key, index]) => {
249
+ const arg = args[index];
250
+ if (arg === undefined) {
251
+ throw new CallExecutionError.CallExecutionError(`form field is missing at index ${index}`);
252
+ }
253
+ formData.set(key, arg);
254
+ }, R__namespace.toPairs((_l = value.parameters.body.fields) !== null && _l !== void 0 ? _l : {}));
255
+ R__namespace.forEach(([key, index]) => {
256
+ const arg = args[index];
257
+ if (arg !== undefined && arg !== null) {
258
+ formData.set(key, arg);
259
+ }
260
+ }, R__namespace.toPairs((_m = value.parameters.body.optionalFields) !== null && _m !== void 0 ? _m : {}));
261
+ body = formData;
262
+ break;
263
+ }
264
+ case types.BodyType.MULTIPART: {
265
+ headers["Content-Type"] = (_o = headers["Content-Type"]) !== null && _o !== void 0 ? _o : "multipart/form-data";
266
+ const formData = new FormData();
267
+ R__namespace.forEach(([key, index]) => {
268
+ const arg = args[index];
269
+ if (arg === undefined) {
270
+ throw new CallExecutionError.CallExecutionError(`form part is missing at index ${index}`);
271
+ }
272
+ formData.append(key, arg);
273
+ }, R__namespace.toPairs((_p = value.parameters.body.parts) !== null && _p !== void 0 ? _p : {}));
274
+ body = formData;
275
+ break;
276
+ }
277
+ default:
278
+ throw new CallExecutionError.CallExecutionError(`Unsupported body type: ${(_q = value.parameters.body) === null || _q === void 0 ? void 0 : _q.type}`);
279
+ }
280
+ }
281
+ }
282
+ return { body, headers, url };
283
+ }
284
+ /**
285
+ * Retrieves the authentication headers
286
+ * @param auth AuthInstance
287
+ * @private
288
+ */
289
+ __get_auth_headers__(auth) {
290
+ const classRef = this.constructor;
291
+ if (!auth.global && classRef.credentials)
292
+ return classRef.credentials.buildAuthHeaders();
293
+ else if (auth.global && NetworkService.credentials)
294
+ return NetworkService.credentials.buildAuthHeaders();
295
+ return {};
296
+ }
297
+ /**
298
+ * Retrieves the auth credentials, and if no valid on exists, attempt to fetch it via the authenticator/authRefresher
299
+ * @param auth AuthInstance
300
+ * @private
301
+ */
302
+ __get_credentials__(auth) {
303
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
304
+ let result;
305
+ let credentials;
306
+ const classRef = this.constructor;
307
+ if (auth.global && NetworkService.credentials && auth.authRefresher) {
308
+ result = auth.authRefresher(NetworkService.credentials);
309
+ }
310
+ else if (!auth.global && classRef.credentials && auth.authRefresher) {
311
+ result = auth.authRefresher(classRef.credentials);
312
+ }
313
+ else if (auth.authenticator) {
314
+ result = auth.authenticator();
315
+ }
316
+ else {
317
+ return;
318
+ }
319
+ if (result instanceof stream_call.Call) {
320
+ credentials = yield result.execute();
321
+ }
322
+ else if (result instanceof Promise) {
323
+ credentials = yield result;
324
+ }
325
+ else {
326
+ credentials = result;
327
+ }
328
+ if (auth.global)
329
+ NetworkService.credentials = credentials;
330
+ else
331
+ classRef.credentials = credentials;
332
+ return credentials;
333
+ });
334
+ }
335
+ /**
336
+ * Configures authentication if none exist by assigning the authenticator to the correct scope
337
+ * (Global -> Service, Local -> this)
338
+ * @param config MethodConfiguration
339
+ * @param methodName name of the method that should hold the authenticator
340
+ * @param functor authenticator
341
+ * @param auth AuthInstance
342
+ * @private
343
+ */
344
+ __setup_authentication__(config, methodName, functor, auth) {
345
+ const classRef = this.constructor;
346
+ if (config.authenticator) {
347
+ if (config.authenticator.global && !NetworkService.authenticator) {
348
+ NetworkService.authenticator = functor;
349
+ }
350
+ else if (!config.authenticator.global && !classRef.authenticator) {
351
+ classRef.authenticator = functor;
352
+ }
353
+ const parentRef = config.authenticator.global ? NetworkService : classRef;
354
+ const oldMethod = this[methodName];
355
+ this[methodName] = (...args) => {
356
+ const result = oldMethod(...args);
357
+ if (result instanceof Promise) {
358
+ return result.then((v) => {
359
+ if (v instanceof credentials.Credentials) {
360
+ parentRef.credentials = v;
361
+ }
362
+ return v;
363
+ });
364
+ }
365
+ else if (result instanceof credentials.Credentials) {
366
+ parentRef.credentials = result;
367
+ }
368
+ return result;
369
+ };
370
+ }
371
+ if (config.authRefresher) {
372
+ if (config.authRefresher.global && !NetworkService.authRefresher) {
373
+ NetworkService.authRefresher = functor;
374
+ }
375
+ else if (!config.authRefresher.global && !classRef.authRefresher) {
376
+ classRef.authRefresher = functor;
377
+ }
378
+ }
379
+ if (NetworkService.authenticator) {
380
+ auth.authenticator = NetworkService.authenticator;
381
+ }
382
+ else if (classRef.authenticator) {
383
+ auth.authenticator = classRef.authenticator;
384
+ }
385
+ if (NetworkService.authRefresher) {
386
+ auth.authRefresher = NetworkService.authRefresher;
387
+ }
388
+ else if (classRef.authRefresher) {
389
+ auth.authRefresher = classRef.authRefresher;
390
+ }
391
+ }
392
+ }
393
+
394
+ exports.NetworkService = NetworkService;
@@ -0,0 +1,97 @@
1
+ import { GingerSnapProps } from "./index";
2
+ import { TimeableObject } from "../data-structures/object";
3
+ import { CacheManager } from "../data/store/manager";
4
+ /**
5
+ * A Snap Service for managing network requests
6
+ */
7
+ export declare class NetworkService {
8
+ /**
9
+ * BaseUrl used by this snap service
10
+ * @private
11
+ */
12
+ protected readonly baseUrl: string;
13
+ /**
14
+ * Request retry limit used by this snap service
15
+ * @private
16
+ */
17
+ private readonly retryLimit?;
18
+ protected readonly cacheManager: CacheManager;
19
+ /**
20
+ * Internal properties used to construct this snap service
21
+ * @private
22
+ */
23
+ private readonly __internal__;
24
+ /**
25
+ * Function that handles authentication refresh
26
+ * @private
27
+ */
28
+ private static authRefresher?;
29
+ /**
30
+ * Function that handles authentication
31
+ * @private
32
+ */
33
+ private static authenticator?;
34
+ /**
35
+ * Credentials used for authenticated network requests sent by this snap service
36
+ * @private
37
+ */
38
+ private static credentials?;
39
+ /**
40
+ * Context representing ResponseData created after the network request is complete.
41
+ * Value varies per network request method, and is scoped to the calling method
42
+ * @example
43
+ * ```ts
44
+ * class Snap extends Service {
45
+ * @GET("/data")
46
+ * @JSONResponse(Data)
47
+ * public getData(): Call<Data> {
48
+ * // do post processing with the Data model
49
+ * console.log('Data model is returned to this.context variable');
50
+ * console.log(this.context);
51
+ * }
52
+ * }
53
+ * ```
54
+ * @protected
55
+ */
56
+ protected context: any;
57
+ /**
58
+ * Cache containing the results from cached network lookups
59
+ * @protected
60
+ */
61
+ protected lookupCache: TimeableObject<string, any>;
62
+ constructor({ baseUrl, retryLimit, cacheManager }?: GingerSnapProps);
63
+ /**
64
+ * Converts the given value to a JSON object
65
+ * @param value Any data
66
+ * @private
67
+ */
68
+ private convertToJSON;
69
+ /**
70
+ * Internal method called to wrap all network request methods with actual networking functionalities
71
+ * @private
72
+ */
73
+ protected __setup__(): void;
74
+ private __constructor_call_args__;
75
+ /**
76
+ * Retrieves the authentication headers
77
+ * @param auth AuthInstance
78
+ * @private
79
+ */
80
+ private __get_auth_headers__;
81
+ /**
82
+ * Retrieves the auth credentials, and if no valid on exists, attempt to fetch it via the authenticator/authRefresher
83
+ * @param auth AuthInstance
84
+ * @private
85
+ */
86
+ private __get_credentials__;
87
+ /**
88
+ * Configures authentication if none exist by assigning the authenticator to the correct scope
89
+ * (Global -> Service, Local -> this)
90
+ * @param config MethodConfiguration
91
+ * @param methodName name of the method that should hold the authenticator
92
+ * @param functor authenticator
93
+ * @param auth AuthInstance
94
+ * @private
95
+ */
96
+ private __setup_authentication__;
97
+ }