@chainlink/external-adapter-framework 0.0.6 → 0.0.8

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 (237) hide show
  1. package/adapter.d.ts +88 -0
  2. package/adapter.js +112 -0
  3. package/background-executor.d.ts +11 -0
  4. package/background-executor.js +45 -0
  5. package/cache/factory.d.ts +6 -0
  6. package/cache/factory.js +57 -0
  7. package/cache/index.d.ts +90 -0
  8. package/cache/index.js +169 -0
  9. package/cache/local.d.ts +23 -0
  10. package/cache/local.js +83 -0
  11. package/cache/metrics.d.ts +27 -0
  12. package/cache/metrics.js +120 -0
  13. package/cache/redis.d.ts +16 -0
  14. package/cache/redis.js +100 -0
  15. package/chainlink-external-adapter-framework-v0.0.8.tgz +0 -0
  16. package/config/index.d.ts +195 -0
  17. package/config/index.js +365 -0
  18. package/config/provider-limits.d.ts +31 -0
  19. package/config/provider-limits.js +75 -0
  20. package/examples/coingecko/batch-warming.d.ts +2 -0
  21. package/examples/coingecko/batch-warming.js +52 -0
  22. package/examples/coingecko/index.d.ts +2 -0
  23. package/examples/coingecko/index.js +10 -0
  24. package/examples/coingecko/rest.d.ts +2 -0
  25. package/examples/coingecko/rest.js +50 -0
  26. package/examples/ncfx/config/index.d.ts +12 -0
  27. package/examples/ncfx/config/index.js +15 -0
  28. package/examples/ncfx/index.d.ts +2 -0
  29. package/examples/ncfx/index.js +10 -0
  30. package/examples/ncfx/websocket.d.ts +36 -0
  31. package/examples/ncfx/websocket.js +72 -0
  32. package/index.d.ts +12 -0
  33. package/index.js +92 -0
  34. package/metrics/constants.d.ts +16 -0
  35. package/metrics/constants.js +25 -0
  36. package/metrics/index.d.ts +15 -0
  37. package/metrics/index.js +123 -0
  38. package/metrics/util.d.ts +3 -0
  39. package/metrics/util.js +9 -0
  40. package/package/adapter.d.ts +88 -0
  41. package/package/adapter.js +112 -0
  42. package/package/background-executor.d.ts +11 -0
  43. package/package/background-executor.js +45 -0
  44. package/package/cache/factory.d.ts +6 -0
  45. package/package/cache/factory.js +57 -0
  46. package/package/cache/index.d.ts +90 -0
  47. package/package/cache/index.js +169 -0
  48. package/package/cache/local.d.ts +23 -0
  49. package/package/cache/local.js +83 -0
  50. package/package/cache/metrics.d.ts +27 -0
  51. package/package/cache/metrics.js +120 -0
  52. package/package/cache/redis.d.ts +16 -0
  53. package/package/cache/redis.js +100 -0
  54. package/package/config/index.d.ts +195 -0
  55. package/package/config/index.js +365 -0
  56. package/package/config/provider-limits.d.ts +31 -0
  57. package/package/config/provider-limits.js +75 -0
  58. package/package/examples/coingecko/batch-warming.d.ts +2 -0
  59. package/package/examples/coingecko/batch-warming.js +52 -0
  60. package/package/examples/coingecko/index.d.ts +2 -0
  61. package/package/examples/coingecko/index.js +10 -0
  62. package/package/examples/coingecko/rest.d.ts +2 -0
  63. package/package/examples/coingecko/rest.js +50 -0
  64. package/package/examples/ncfx/config/index.d.ts +12 -0
  65. package/package/examples/ncfx/config/index.js +15 -0
  66. package/package/examples/ncfx/index.d.ts +2 -0
  67. package/package/examples/ncfx/index.js +10 -0
  68. package/package/examples/ncfx/websocket.d.ts +36 -0
  69. package/package/examples/ncfx/websocket.js +72 -0
  70. package/package/index.d.ts +12 -0
  71. package/package/index.js +92 -0
  72. package/package/metrics/constants.d.ts +16 -0
  73. package/package/metrics/constants.js +25 -0
  74. package/package/metrics/index.d.ts +15 -0
  75. package/package/metrics/index.js +123 -0
  76. package/package/metrics/util.d.ts +3 -0
  77. package/package/metrics/util.js +9 -0
  78. package/package/package.json +69 -0
  79. package/package/rate-limiting/background/fixed-frequency.d.ts +10 -0
  80. package/package/rate-limiting/background/fixed-frequency.js +37 -0
  81. package/package/rate-limiting/index.d.ts +54 -0
  82. package/package/rate-limiting/index.js +63 -0
  83. package/package/rate-limiting/metrics.d.ts +3 -0
  84. package/package/rate-limiting/metrics.js +44 -0
  85. package/package/rate-limiting/request/simple-counting.d.ts +20 -0
  86. package/package/rate-limiting/request/simple-counting.js +62 -0
  87. package/package/test.d.ts +1 -0
  88. package/package/test.js +6 -0
  89. package/package/transports/batch-warming.d.ts +34 -0
  90. package/package/transports/batch-warming.js +101 -0
  91. package/package/transports/index.d.ts +87 -0
  92. package/package/transports/index.js +87 -0
  93. package/package/transports/metrics.d.ts +21 -0
  94. package/package/transports/metrics.js +105 -0
  95. package/package/transports/rest.d.ts +43 -0
  96. package/package/transports/rest.js +129 -0
  97. package/package/transports/util.d.ts +8 -0
  98. package/package/transports/util.js +85 -0
  99. package/package/transports/websocket.d.ts +80 -0
  100. package/package/transports/websocket.js +169 -0
  101. package/package/util/expiring-sorted-set.d.ts +21 -0
  102. package/package/util/expiring-sorted-set.js +47 -0
  103. package/package/util/index.d.ts +11 -0
  104. package/package/util/index.js +35 -0
  105. package/package/util/logger.d.ts +42 -0
  106. package/package/util/logger.js +62 -0
  107. package/package/util/request.d.ts +55 -0
  108. package/package/util/request.js +2 -0
  109. package/package/validation/error.d.ts +50 -0
  110. package/package/validation/error.js +79 -0
  111. package/package/validation/index.d.ts +5 -0
  112. package/package/validation/index.js +86 -0
  113. package/package/validation/input-params.d.ts +15 -0
  114. package/package/validation/input-params.js +30 -0
  115. package/package/validation/override-functions.d.ts +3 -0
  116. package/package/validation/override-functions.js +40 -0
  117. package/package/validation/preset-tokens.json +23 -0
  118. package/package/validation/validator.d.ts +47 -0
  119. package/package/validation/validator.js +303 -0
  120. package/package.json +1 -1
  121. package/rate-limiting/background/fixed-frequency.d.ts +10 -0
  122. package/rate-limiting/background/fixed-frequency.js +37 -0
  123. package/rate-limiting/index.d.ts +54 -0
  124. package/rate-limiting/index.js +63 -0
  125. package/rate-limiting/metrics.d.ts +3 -0
  126. package/rate-limiting/metrics.js +44 -0
  127. package/rate-limiting/request/simple-counting.d.ts +20 -0
  128. package/rate-limiting/request/simple-counting.js +62 -0
  129. package/test.d.ts +1 -0
  130. package/test.js +6 -0
  131. package/transports/batch-warming.d.ts +34 -0
  132. package/transports/batch-warming.js +101 -0
  133. package/transports/index.d.ts +87 -0
  134. package/transports/index.js +87 -0
  135. package/transports/metrics.d.ts +21 -0
  136. package/transports/metrics.js +105 -0
  137. package/transports/rest.d.ts +43 -0
  138. package/transports/rest.js +129 -0
  139. package/transports/util.d.ts +8 -0
  140. package/transports/util.js +85 -0
  141. package/transports/websocket.d.ts +80 -0
  142. package/transports/websocket.js +169 -0
  143. package/util/expiring-sorted-set.d.ts +21 -0
  144. package/util/expiring-sorted-set.js +47 -0
  145. package/util/index.d.ts +11 -0
  146. package/util/index.js +35 -0
  147. package/util/logger.d.ts +42 -0
  148. package/util/logger.js +62 -0
  149. package/util/request.d.ts +55 -0
  150. package/util/request.js +2 -0
  151. package/validation/error.d.ts +50 -0
  152. package/validation/error.js +79 -0
  153. package/validation/index.d.ts +5 -0
  154. package/validation/index.js +86 -0
  155. package/validation/input-params.d.ts +15 -0
  156. package/validation/input-params.js +30 -0
  157. package/validation/override-functions.d.ts +3 -0
  158. package/validation/override-functions.js +40 -0
  159. package/validation/preset-tokens.json +23 -0
  160. package/validation/validator.d.ts +47 -0
  161. package/validation/validator.js +303 -0
  162. package/.c8rc.json +0 -3
  163. package/.eslintignore +0 -9
  164. package/.eslintrc.js +0 -96
  165. package/.github/README.MD +0 -17
  166. package/.github/actions/setup/action.yaml +0 -13
  167. package/.github/workflows/main.yaml +0 -39
  168. package/.github/workflows/publish.yaml +0 -17
  169. package/.prettierignore +0 -13
  170. package/.yarnrc +0 -0
  171. package/README.md +0 -103
  172. package/docker-compose.yaml +0 -35
  173. package/src/adapter.ts +0 -236
  174. package/src/background-executor.ts +0 -53
  175. package/src/cache/factory.ts +0 -28
  176. package/src/cache/index.ts +0 -236
  177. package/src/cache/local.ts +0 -73
  178. package/src/cache/metrics.ts +0 -112
  179. package/src/cache/redis.ts +0 -93
  180. package/src/config/index.ts +0 -501
  181. package/src/config/provider-limits.ts +0 -130
  182. package/src/examples/coingecko/batch-warming.ts +0 -79
  183. package/src/examples/coingecko/index.ts +0 -9
  184. package/src/examples/coingecko/rest.ts +0 -77
  185. package/src/examples/ncfx/config/index.ts +0 -12
  186. package/src/examples/ncfx/index.ts +0 -9
  187. package/src/examples/ncfx/websocket.ts +0 -100
  188. package/src/index.ts +0 -106
  189. package/src/metrics/constants.ts +0 -23
  190. package/src/metrics/index.ts +0 -116
  191. package/src/metrics/util.ts +0 -11
  192. package/src/rate-limiting/background/fixed-frequency.ts +0 -47
  193. package/src/rate-limiting/index.ts +0 -100
  194. package/src/rate-limiting/metrics.ts +0 -18
  195. package/src/rate-limiting/request/simple-counting.ts +0 -76
  196. package/src/test.ts +0 -5
  197. package/src/transports/batch-warming.ts +0 -121
  198. package/src/transports/index.ts +0 -173
  199. package/src/transports/metrics.ts +0 -95
  200. package/src/transports/rest.ts +0 -161
  201. package/src/transports/util.ts +0 -63
  202. package/src/transports/websocket.ts +0 -238
  203. package/src/util/expiring-sorted-set.ts +0 -52
  204. package/src/util/index.ts +0 -20
  205. package/src/util/logger.ts +0 -69
  206. package/src/util/request.ts +0 -115
  207. package/src/validation/error.ts +0 -116
  208. package/src/validation/index.ts +0 -101
  209. package/src/validation/input-params.ts +0 -45
  210. package/src/validation/override-functions.ts +0 -44
  211. package/src/validation/preset-tokens.json +0 -23
  212. package/src/validation/validator.ts +0 -384
  213. package/test/adapter.test.ts +0 -27
  214. package/test/background-executor.test.ts +0 -109
  215. package/test/cache/cache-key.test.ts +0 -96
  216. package/test/cache/helper.ts +0 -101
  217. package/test/cache/local.test.ts +0 -54
  218. package/test/cache/redis.test.ts +0 -89
  219. package/test/correlation.test.ts +0 -114
  220. package/test/index.test.ts +0 -37
  221. package/test/metrics/feed-id.test.ts +0 -33
  222. package/test/metrics/helper.ts +0 -14
  223. package/test/metrics/labels.test.ts +0 -36
  224. package/test/metrics/metrics.test.ts +0 -267
  225. package/test/metrics/redis-metrics.test.ts +0 -113
  226. package/test/metrics/warmer-metrics.test.ts +0 -192
  227. package/test/metrics/ws-metrics.test.ts +0 -225
  228. package/test/rate-limit-config.test.ts +0 -243
  229. package/test/transports/batch.test.ts +0 -465
  230. package/test/transports/rest.test.ts +0 -242
  231. package/test/transports/websocket.test.ts +0 -183
  232. package/test/tsconfig.json +0 -5
  233. package/test/util.ts +0 -76
  234. package/test/validation.test.ts +0 -169
  235. package/test.sh +0 -20
  236. package/tsconfig.json +0 -24
  237. package/typedoc.json +0 -6
@@ -1,384 +0,0 @@
1
- import { AdapterError, AdapterErrorResponse } from './error'
2
- import { InputParameter, InputParameters, Override, baseInputParameters } from './input-params'
3
- import { isArray, isObject } from '../util'
4
- import presetTokens from './preset-tokens.json'
5
-
6
- export type OverrideType = 'overrides' | 'tokenOverrides' | 'includes'
7
-
8
- type InputType = {
9
- id?: string
10
- data?: any
11
- }
12
-
13
- export interface ValidatorOptions {
14
- shouldThrowError?: boolean
15
- includes?: any[]
16
- overrides?: any
17
- }
18
-
19
- // Includes is an alternative symbol mapping that can be used to represent
20
- // The original request, such as wrapped tokens on DEXes.
21
- export type IncludePair = {
22
- from: string // From symbol
23
- to: string // To symbol
24
- adapters?: string[] // Array of adapters this applies to
25
- inverse?: boolean // If the inverse should be calculated instead
26
- tokens?: boolean // If the token addresses should be used instead
27
- }
28
-
29
- export type Includes = {
30
- from: string
31
- to: string
32
- includes: IncludePair[]
33
- }
34
-
35
- // Don't want to get requester in just yet, only copying the static method 'errored'
36
- const requesterErrored = (
37
- jobRunID = '1',
38
- error: AdapterError | Error | string | undefined = undefined,
39
- statusCode = 500,
40
- feedID: string | undefined = undefined,
41
- ): AdapterErrorResponse => {
42
- if (error instanceof AdapterError) {
43
- error.jobRunID = jobRunID
44
- if (feedID) {
45
- error.feedID = feedID
46
- }
47
- return error.toJSONResponse()
48
- }
49
- if (error instanceof Error) {
50
- return new AdapterError({
51
- jobRunID,
52
- statusCode,
53
- message: error.message,
54
- cause: error,
55
- feedID,
56
- }).toJSONResponse()
57
- }
58
- return new AdapterError({ jobRunID, statusCode, message: error, feedID }).toJSONResponse()
59
- }
60
-
61
- export class Validator {
62
- input: InputType
63
- inputConfigs: InputParameters
64
- inputOptions: Record<string, any[]>
65
- validatorOptions: ValidatorOptions
66
- validated: any
67
- error: AdapterError | undefined
68
- errored: AdapterErrorResponse | undefined
69
- constructor(
70
- input: InputType = { id: '1', data: {} },
71
- inputConfigs = {},
72
- inputOptions = {},
73
- validatorOptions: ValidatorOptions = {},
74
- ) {
75
- this.input = { ...input }
76
- if (!this.input.id) {
77
- this.input.id = '1'
78
- } // TODO Please remove these once "no any" strict typing is enabled
79
- if (!this.input.data) {
80
- this.input.data = {}
81
- }
82
- this.inputConfigs = { ...baseInputParameters, ...inputConfigs }
83
- this.inputOptions = { ...inputOptions }
84
- this.validatorOptions = {
85
- shouldThrowError: true,
86
- includes: [],
87
- overrides: {},
88
- ...validatorOptions,
89
- }
90
- this.validated = { id: this.input.id, data: {} }
91
- this.validateInput()
92
- this.validateOverrides('overrides', this.validatorOptions.overrides)
93
- this.validateOverrides('tokenOverrides', presetTokens)
94
- this.validateIncludeOverrides()
95
- this.checkDuplicateInputParams(inputConfigs)
96
- }
97
-
98
- validateInput(): void {
99
- try {
100
- for (const key in this.inputConfigs) {
101
- this.validateObjectParam(key, this.validatorOptions.shouldThrowError)
102
- }
103
- } catch (e) {
104
- this.parseError(e)
105
- }
106
- }
107
-
108
- validateOverrides(path: 'overrides' | 'tokenOverrides', preset: Record<string, any>): void {
109
- try {
110
- if (!this.input.data?.[path]) {
111
- this.validated[path] = this.formatOverride(preset)
112
- return
113
- }
114
- this.validated[path] = this.formatOverride({ ...preset, ...this.input.data[path] })
115
- } catch (e) {
116
- this.parseError(e)
117
- }
118
- }
119
-
120
- checkDuplicateInputParams(inputConfig: InputParameters): void {
121
- let aliases: string[] = []
122
- for (const key in inputConfig) {
123
- const param = inputConfig[key]
124
- if (Array.isArray(param)) {
125
- aliases = aliases.concat(param)
126
- } else if (typeof inputConfig === 'boolean') {
127
- return
128
- } else {
129
- aliases.push(key)
130
- if (typeof param === 'object' && 'aliases' in param && Array.isArray(param.aliases)) {
131
- aliases = aliases.concat(param.aliases)
132
- }
133
- }
134
- }
135
- if (aliases.length !== new Set(aliases).size) {
136
- this.throwInvalid('Duplicate Input Aliases')
137
- }
138
- }
139
-
140
- validateIncludeOverrides(): void {
141
- try {
142
- this.validated.includes = this.formatIncludeOverrides([
143
- ...(Array.isArray(this.input.data?.includes) ? this.input.data.includes : []),
144
- ...(this.validatorOptions.includes || []),
145
- ])
146
- } catch (e) {
147
- this.parseError(e)
148
- }
149
- }
150
-
151
- parseError(error: unknown): void {
152
- if (!(error instanceof Error)) {
153
- return
154
- }
155
- const message = 'Error validating input.'
156
- if (error instanceof AdapterError) {
157
- this.error = error
158
- } else {
159
- this.error = new AdapterError({
160
- jobRunID: this.validated.id,
161
- statusCode: 400,
162
- message,
163
- cause: error,
164
- })
165
- }
166
- this.errored = requesterErrored(this.validated.id, this.error)
167
- if (this.validatorOptions.shouldThrowError) {
168
- throw this.error
169
- }
170
- }
171
-
172
- // OverrideSymbol = (adapter: string, symbol?: string | string[]): string | string[] => {
173
- // Const defaultSymbol = symbol || this.validated.data.base
174
- // If (!defaultSymbol) this.throwInvalid(`Required parameter not supplied: base`)
175
-
176
- // // TODO: Will never be reached, because the presetSymbols are used as default overrides
177
- // If (!this.validated.overrides) return defaultSymbol
178
-
179
- // If (!Array.isArray(defaultSymbol))
180
- // Return (
181
- // This.validated.overrides.get(adapter.toLowerCase())?.get(defaultSymbol.toLowerCase()) ||
182
- // DefaultSymbol
183
- // )
184
- // Const multiple: string[] = []
185
- // For (const sym of defaultSymbol) {
186
- // Const overrided = this.validated.overrides.get(adapter.toLowerCase())?.get(sym.toLowerCase())
187
- // If (!overrided) multiple.push(sym)
188
- // Else multiple.push(overrided)
189
- // }
190
- // Return multiple
191
- // }
192
-
193
- // OverrideToken = (symbol: string, network = 'ethereum'): string | undefined => {
194
- // Return this.validated.tokenOverrides?.get(network.toLowerCase())?.get(symbol.toLowerCase())
195
- // }
196
-
197
- // OverrideIncludes = (from: string, to: string): IncludePair | undefined => {
198
- // // Search through `presetIncludes` to find matching override for adapter and to/from pairing.
199
- // Const pairs = (
200
- // This.validated.includes?.filter(
201
- // (val: string | Includes) => typeof val !== 'string',
202
- // ) as Includes[]
203
- // ).filter(
204
- // (pair) =>
205
- // Pair.from.toLowerCase() === from.toLowerCase() &&
206
- // Pair.to.toLowerCase() === to.toLowerCase(),
207
- // )
208
- // If (!pairs || !pairs[0] || !pairs[0].includes || !pairs[0].includes[0]) {
209
- // Return
210
- // }
211
- // Return pairs[0].includes[0]
212
- // }
213
-
214
- // OverrideReverseLookup = (adapter: string, type: OverrideType, symbol: string): string => {
215
- // Const overrides: Map<string, string> | undefined = this.validated?.[type]?.get(
216
- // Adapter.toLowerCase(),
217
- // )
218
- // If (!overrides) return symbol
219
- // Let originalSymbol: string | undefined
220
- // Overrides.forEach((overridden, original) => {
221
- // If (overridden.toLowerCase() === symbol.toLowerCase()) originalSymbol = original
222
- // })
223
- // Return originalSymbol || symbol
224
- // }
225
-
226
- formatOverride = (param: any): Override => {
227
- const _throwInvalid = () =>
228
- this.throwInvalid(`Parameter supplied with wrong format: "override"`)
229
-
230
- if (!isObject(param)) {
231
- _throwInvalid()
232
- }
233
-
234
- const _isValid = Object.values(param).every(isObject)
235
- if (!_isValid) {
236
- _throwInvalid()
237
- }
238
-
239
- const _keyToLowerCase = (entry: [string, any]): [string, any] => {
240
- return [entry[0].toLowerCase(), entry[1]]
241
- }
242
- return new Map(
243
- Object.entries(param)
244
- .map(_keyToLowerCase)
245
- .map(([key, value]) => [key, new Map(Object.entries(value).map(_keyToLowerCase))]),
246
- )
247
- }
248
-
249
- formatIncludeOverrides = (param: any): Override => {
250
- const _throwInvalid = () =>
251
- this.throwInvalid(`Parameter supplied with wrong format: "includes"`)
252
- if (!isArray(param)) {
253
- _throwInvalid()
254
- }
255
-
256
- const _isValid = Object.values(param).every((val) => isObject(val) || typeof val === 'string')
257
- if (!_isValid) {
258
- _throwInvalid()
259
- }
260
-
261
- return param
262
- }
263
-
264
- throwInvalid = (message: string): void => {
265
- throw new AdapterError({ jobRunID: this.validated.id, statusCode: 400, message })
266
- }
267
-
268
- validateObjectParam(key: string, shouldThrowError = true): void {
269
- const inputConfig = this.inputConfigs[key] as InputParameter
270
- const usedKey = this.getUsedKey(key, inputConfig.aliases ?? [])
271
-
272
- const param = usedKey
273
- ? this.input.data[usedKey as string] ?? inputConfig.default
274
- : inputConfig.default
275
-
276
- if (shouldThrowError) {
277
- const paramIsDefined = !(param === undefined || param === null || param === '')
278
-
279
- if (inputConfig.required && !paramIsDefined) {
280
- this.throwInvalid(`Required parameter ${key} must be non-null and non-empty`)
281
- }
282
-
283
- if (paramIsDefined) {
284
- if (inputConfig.type) {
285
- const primitiveTypes = ['boolean', 'number', 'bigint', 'string']
286
-
287
- if (![...primitiveTypes, 'array', 'object'].includes(inputConfig.type)) {
288
- this.throwInvalid(`${key} parameter has unrecognized type ${inputConfig.type}`)
289
- }
290
-
291
- if (primitiveTypes.includes(inputConfig.type) && typeof param !== inputConfig.type) {
292
- this.throwInvalid(`${key} parameter must be of type ${inputConfig.type}`)
293
- }
294
-
295
- if (inputConfig.type === 'array' && (!Array.isArray(param) || param.length === 0)) {
296
- this.throwInvalid(`${key} parameter must be a non-empty array`)
297
- }
298
-
299
- if (
300
- inputConfig.type === 'object' &&
301
- (!param ||
302
- Array.isArray(param) ||
303
- typeof param !== inputConfig.type ||
304
- Object.keys(param).length === 0)
305
- ) {
306
- this.throwInvalid(`${key} parameter must be an object with at least one property`)
307
- }
308
- }
309
-
310
- // If (inputConfig.options) {
311
- // Const tolcase = (o: any) => (typeof o === 'string' ? o.toLowerCase() : o)
312
-
313
- // Const formattedOptions = inputConfig.options.map(tolcase)
314
- // Const formattedParam = tolcase(param)
315
-
316
- // If (!formattedOptions.includes(formattedParam))
317
- // This.throwInvalid(
318
- // `${key} parameter '${formattedParam}' is not in the set of available options: ${formattedOptions.join(
319
- // ',',
320
- // )}`,
321
- // )
322
- // }
323
-
324
- // For (const dependency of inputConfig.dependsOn ?? []) {
325
- // Const usedDependencyKey = this.getUsedKey(
326
- // Dependency,
327
- // (this.inputConfigs[dependency] as InputParameter).aliases ?? [],
328
- // )
329
- // If (!usedDependencyKey) this.throwInvalid(`${key} dependency ${dependency} not supplied`)
330
- // }
331
-
332
- // For (const exclusive of inputConfig.exclusive ?? []) {
333
- // Const usedExclusiveKey = this.getUsedKey(
334
- // Exclusive,
335
- // (this.inputConfigs[exclusive] as InputParameter).aliases ?? [],
336
- // )
337
- // If (usedExclusiveKey)
338
- // This.throwInvalid(`${key} cannot be supplied concurrently with ${exclusive}`)
339
- // }
340
- }
341
- }
342
-
343
- this.validated.data[key] = param
344
- }
345
-
346
- validateOptionalParam(param: any, key: string, options: any[]): void {
347
- if (param && options) {
348
- if (!Array.isArray(options)) {
349
- this.throwInvalid(`Parameter options for ${key} must be of an Array type`)
350
- }
351
- if (!options.includes(param)) {
352
- this.throwInvalid(`${param} is not a supported ${key} option. Must be one of ${options}`)
353
- }
354
- }
355
- this.validated.data[key] = param
356
- }
357
-
358
- validateRequiredParam(param: any, key: string, options: any[]): void {
359
- if (typeof param === 'undefined' || param === '') {
360
- this.throwInvalid(`Required parameter not supplied: ${key}`)
361
- }
362
- if (options) {
363
- if (!Array.isArray(options)) {
364
- this.throwInvalid(`Parameter options for ${key} must be of an Array type`)
365
- }
366
- if (!options.includes(param)) {
367
- this.throwInvalid(
368
- `${param} is not a supported ${key} option. Must be one of ${options.join(' || ')}`,
369
- )
370
- }
371
- }
372
- this.validated.data[key] = param
373
- }
374
-
375
- getUsedKey(key: string, keyArray: string[]): string | undefined {
376
- const comparisonArray = [...keyArray]
377
- if (!comparisonArray.includes(key)) {
378
- comparisonArray.push(key)
379
- }
380
-
381
- const inputParamKeys = Object.keys(this.input.data)
382
- return inputParamKeys.find((k) => comparisonArray.includes(k))
383
- }
384
- }
@@ -1,27 +0,0 @@
1
- import test from 'ava'
2
- import { expose } from '../src'
3
- import { Adapter } from '../src/adapter'
4
- import { NopTransport } from './util'
5
-
6
- test('duplicate endpoint names throw error on startup', async (t) => {
7
- const adapter: Adapter = {
8
- name: 'test',
9
- endpoints: [
10
- {
11
- name: 'test',
12
- inputParameters: {},
13
- transport: new NopTransport(),
14
- },
15
- {
16
- name: 'another',
17
- aliases: ['test'],
18
- inputParameters: {},
19
- transport: new NopTransport(),
20
- },
21
- ],
22
- }
23
-
24
- await t.throwsAsync(async () => expose(adapter), {
25
- message: 'Duplicate endpoint / alias: "test"',
26
- })
27
- })
@@ -1,109 +0,0 @@
1
- import test from 'ava'
2
- import FakeTimers from '@sinonjs/fake-timers'
3
- import { expose } from '../src'
4
- import { Adapter } from '../src/adapter'
5
- import { AdapterContext } from '../src/transports'
6
- import { deferredPromise, NopTransport } from './util'
7
-
8
- test('background executor calls transport function with background context', async (t) => {
9
- const [promise, resolve] = deferredPromise<AdapterContext>()
10
-
11
- const transport = new (class extends NopTransport {
12
- async backgroundExecute(context: AdapterContext): Promise<number> {
13
- resolve(context)
14
- return 100
15
- }
16
- })()
17
-
18
- const adapter: Adapter = {
19
- name: 'test',
20
- endpoints: [
21
- {
22
- name: 'test',
23
- inputParameters: {},
24
- transport,
25
- },
26
- {
27
- name: 'skipped',
28
- inputParameters: {},
29
- transport: new NopTransport(), // Also add coverage for skipped executors
30
- },
31
- ],
32
- }
33
-
34
- await expose(adapter)
35
- const context = await promise
36
- t.is(context.adapterEndpoint.name, 'test')
37
- })
38
-
39
- // Needs to be serial to avoid mocking global clock more than once at the same time
40
- // TODO: Inject clock?
41
- test.serial(
42
- 'background executor calls transport function second time after sleeping',
43
- async (t) => {
44
- const clock = FakeTimers.install()
45
- let timesCalled = 0
46
-
47
- const transport = new (class extends NopTransport {
48
- async backgroundExecute(): Promise<number> {
49
- timesCalled++
50
- return 1000
51
- }
52
- })()
53
-
54
- const adapter: Adapter = {
55
- name: 'test',
56
- endpoints: [
57
- {
58
- name: 'test',
59
- inputParameters: {},
60
- aliases: ['other'], // Add an alias to ensure it's not called once per alias
61
- transport,
62
- },
63
- ],
64
- }
65
-
66
- await expose(adapter)
67
- t.is(timesCalled, 1)
68
- await clock.tickAsync(999)
69
- t.is(timesCalled, 1)
70
- await clock.tickAsync(1)
71
- t.is(timesCalled, 2)
72
-
73
- clock.uninstall()
74
- },
75
- )
76
-
77
- test.serial('background executor ends recursive chain on server close', async (t) => {
78
- const clock = FakeTimers.install()
79
- let timesCalled = 0
80
-
81
- const transport = new (class extends NopTransport {
82
- async backgroundExecute(): Promise<number> {
83
- timesCalled++
84
- return 1000
85
- }
86
- })()
87
-
88
- const adapter: Adapter = {
89
- name: 'test',
90
- endpoints: [
91
- {
92
- name: 'test',
93
- inputParameters: {},
94
- transport,
95
- },
96
- ],
97
- }
98
-
99
- const server = await expose(adapter)
100
- if (!server) {
101
- throw 'Server did not start'
102
- }
103
- t.is(timesCalled, 1)
104
- server.close()
105
- await clock.tickAsync(999999)
106
- t.is(timesCalled, 1) // The background process closed, so this was never called again
107
-
108
- clock.uninstall()
109
- })
@@ -1,96 +0,0 @@
1
- import untypedTest, { TestFn } from 'ava'
2
- import axios from 'axios'
3
- import { AddressInfo } from 'net'
4
- import { expose } from '../../src'
5
- import { Adapter, AdapterEndpoint } from '../../src/adapter'
6
- import { Cache } from '../../src/cache'
7
- import { BaseSettings } from '../../src/config'
8
- import { AdapterRequest, AdapterResponse } from '../../src/util'
9
- import { NopTransport } from '../util'
10
-
11
- const test = untypedTest as TestFn<{
12
- serverAddress: string
13
- cache: Cache
14
- adapterEndpoint: AdapterEndpoint
15
- }>
16
-
17
- test.beforeEach(async (t) => {
18
- const adapter: Adapter = {
19
- name: 'test',
20
- defaultEndpoint: 'test',
21
- endpoints: [
22
- {
23
- name: 'test',
24
- inputParameters: {},
25
- transport: new (class extends NopTransport {
26
- override async setup(
27
- req: AdapterRequest<unknown>,
28
- ): Promise<void | AdapterResponse<null>> {
29
- return {
30
- data: null,
31
- statusCode: 200,
32
- result: req.requestContext.cacheKey,
33
- }
34
- }
35
- })(),
36
- },
37
- ],
38
- }
39
-
40
- t.context.adapterEndpoint = adapter.endpoints[0]
41
- const server = await expose(adapter)
42
- if (!server) {
43
- throw 'Server did not start'
44
- }
45
- t.context.serverAddress = `http://localhost:${(server.address() as AddressInfo).port}`
46
- })
47
-
48
- test.serial('no parameters returns default cache key', async (t) => {
49
- const response = await axios.post(`${t.context.serverAddress}`, {})
50
- t.is(response.data.result, BaseSettings.DEFAULT_CACHE_KEY.default)
51
- })
52
-
53
- test.serial('builds cache key correctly from input params', async (t) => {
54
- t.context.adapterEndpoint.inputParameters = {
55
- base: {
56
- type: 'string',
57
- required: true,
58
- },
59
- quote: {
60
- type: 'string',
61
- required: false,
62
- },
63
- factor: {
64
- type: 'number',
65
- required: true,
66
- },
67
- proper: {
68
- type: 'boolean',
69
- required: false,
70
- },
71
- details: {
72
- type: 'object',
73
- required: true,
74
- },
75
- nullable: {
76
- type: 'object',
77
- required: false,
78
- },
79
- }
80
- const response = await axios.post(`${t.context.serverAddress}`, {
81
- data: {
82
- base: 'eth',
83
- factor: 123,
84
- proper: true,
85
- details: {
86
- asd: 'qwe',
87
- zxc: 432,
88
- },
89
- nullable: null,
90
- },
91
- })
92
- t.is(
93
- response.data.result,
94
- 'test-|base:eth|factor:123|proper:true|details:{"asd":"qwe","zxc":432}',
95
- )
96
- })