@chainlink/external-adapter-framework 0.0.15 → 0.0.17
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.
- package/adapter.js +128 -0
- package/background-executor.js +45 -0
- package/cache/factory.js +58 -0
- package/cache/index.js +173 -0
- package/cache/local.js +83 -0
- package/cache/metrics.js +120 -0
- package/cache/redis.js +100 -0
- package/chainlink-external-adapter-framework-v0.0.6.tgz +0 -0
- package/config/index.js +366 -0
- package/config/provider-limits.js +74 -0
- package/examples/bank-frick/accounts.js +192 -0
- package/examples/bank-frick/config/index.js +54 -0
- package/examples/bank-frick/index.js +15 -0
- package/examples/bank-frick/util.js +39 -0
- package/examples/coingecko/batch-warming.js +53 -0
- package/examples/coingecko/index.js +11 -0
- package/examples/coingecko/rest.js +51 -0
- package/examples/coingecko/src/config/index.js +13 -0
- package/examples/coingecko/src/config/overrides.json +10826 -0
- package/examples/coingecko/src/cryptoUtils.js +41 -0
- package/examples/coingecko/src/endpoint/coins.js +33 -0
- package/examples/coingecko/src/endpoint/crypto-marketcap.js +46 -0
- package/examples/coingecko/src/endpoint/crypto-volume.js +46 -0
- package/examples/coingecko/src/endpoint/crypto.js +47 -0
- package/examples/coingecko/src/endpoint/dominance.js +26 -0
- package/examples/coingecko/src/endpoint/global-marketcap.js +26 -0
- package/examples/coingecko/src/endpoint/index.js +15 -0
- package/examples/coingecko/src/globalUtils.js +48 -0
- package/examples/coingecko/src/index.js +14 -0
- package/examples/coingecko/test/e2e/adapter.test.js +262 -0
- package/examples/coingecko/test/integration/adapter.test.js +264 -0
- package/examples/coingecko/test/integration/capturedRequests.json +1 -0
- package/examples/coingecko/test/integration/fixtures.js +41 -0
- package/examples/coingecko-old/batch-warming.js +53 -0
- package/examples/coingecko-old/index.js +11 -0
- package/examples/coingecko-old/rest.js +51 -0
- package/examples/ncfx/config/index.js +15 -0
- package/examples/ncfx/index.js +11 -0
- package/examples/ncfx/websocket.js +73 -0
- package/index.js +127 -0
- package/metrics/constants.js +25 -0
- package/metrics/index.js +122 -0
- package/metrics/util.js +9 -0
- package/package.json +5 -15
- package/rate-limiting/background/fixed-frequency.js +35 -0
- package/rate-limiting/index.js +84 -0
- package/rate-limiting/metrics.js +44 -0
- package/rate-limiting/request/simple-counting.js +62 -0
- package/test.js +6 -0
- package/transports/batch-warming.js +101 -0
- package/transports/index.js +87 -0
- package/transports/metrics.js +105 -0
- package/transports/rest.js +138 -0
- package/transports/util.js +86 -0
- package/transports/websocket.js +166 -0
- package/util/index.js +35 -0
- package/util/logger.js +62 -0
- package/util/recordRequests.js +45 -0
- package/util/request.js +2 -0
- package/util/subscription-set/expiring-sorted-set.js +47 -0
- package/util/subscription-set/subscription-set.js +19 -0
- package/util/test-payload-loader.js +83 -0
- package/validation/error.js +79 -0
- package/validation/index.js +91 -0
- package/validation/input-params.js +30 -0
- package/validation/override-functions.js +40 -0
- package/validation/overrideFunctions.js +40 -0
- package/validation/preset-tokens.json +23 -0
- package/validation/validator.js +303 -0
- package/.c8rc.json +0 -3
- package/.eslintignore +0 -10
- package/.eslintrc.js +0 -96
- package/.github/README.MD +0 -42
- package/.github/actions/setup/action.yaml +0 -13
- package/.github/workflows/label.yaml +0 -39
- package/.github/workflows/main.yaml +0 -39
- package/.github/workflows/publish.yaml +0 -17
- package/.prettierignore +0 -13
- package/.yarnrc +0 -0
- package/README.md +0 -103
- package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +0 -82953
- package/dist/examples/coingecko/test/integration/adapter.test.ts.js +0 -91672
- package/dist/main.js +0 -72703
- package/docker-compose.yaml +0 -35
- package/env.sh +0 -54
- package/env2.sh +0 -55
- package/jest.config.js +0 -5
- package/publish.sh +0 -0
- package/src/adapter.ts +0 -263
- package/src/background-executor.ts +0 -52
- package/src/cache/factory.ts +0 -26
- package/src/cache/index.ts +0 -258
- package/src/cache/local.ts +0 -73
- package/src/cache/metrics.ts +0 -112
- package/src/cache/redis.ts +0 -93
- package/src/config/index.ts +0 -517
- package/src/config/provider-limits.ts +0 -127
- package/src/examples/bank-frick/README.MD +0 -10
- package/src/examples/bank-frick/accounts.ts +0 -246
- package/src/examples/bank-frick/config/index.ts +0 -53
- package/src/examples/bank-frick/index.ts +0 -13
- package/src/examples/bank-frick/types.d.ts +0 -38
- package/src/examples/bank-frick/util.ts +0 -55
- package/src/examples/coingecko/src/config/index.ts +0 -12
- package/src/examples/coingecko/src/config/overrides.json +0 -10826
- package/src/examples/coingecko/src/cryptoUtils.ts +0 -88
- package/src/examples/coingecko/src/endpoint/coins.ts +0 -54
- package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +0 -66
- package/src/examples/coingecko/src/endpoint/crypto-volume.ts +0 -66
- package/src/examples/coingecko/src/endpoint/crypto.ts +0 -63
- package/src/examples/coingecko/src/endpoint/dominance.ts +0 -40
- package/src/examples/coingecko/src/endpoint/global-marketcap.ts +0 -40
- package/src/examples/coingecko/src/endpoint/index.ts +0 -6
- package/src/examples/coingecko/src/globalUtils.ts +0 -78
- package/src/examples/coingecko/src/index.ts +0 -17
- package/src/examples/coingecko/test/e2e/adapter.test.ts +0 -278
- package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +0 -15
- package/src/examples/coingecko/test/integration/adapter.test.ts +0 -281
- package/src/examples/coingecko/test/integration/capturedRequests.json +0 -1
- package/src/examples/coingecko/test/integration/fixtures.ts +0 -42
- package/src/examples/coingecko-old/batch-warming.ts +0 -79
- package/src/examples/coingecko-old/index.ts +0 -9
- package/src/examples/coingecko-old/rest.ts +0 -77
- package/src/examples/ncfx/config/index.ts +0 -12
- package/src/examples/ncfx/index.ts +0 -9
- package/src/examples/ncfx/websocket.ts +0 -99
- package/src/index.ts +0 -149
- package/src/metrics/constants.ts +0 -23
- package/src/metrics/index.ts +0 -115
- package/src/metrics/util.ts +0 -18
- package/src/rate-limiting/background/fixed-frequency.ts +0 -45
- package/src/rate-limiting/index.ts +0 -100
- package/src/rate-limiting/metrics.ts +0 -18
- package/src/rate-limiting/request/simple-counting.ts +0 -76
- package/src/transports/batch-warming.ts +0 -127
- package/src/transports/index.ts +0 -152
- package/src/transports/metrics.ts +0 -95
- package/src/transports/rest.ts +0 -168
- package/src/transports/util.ts +0 -63
- package/src/transports/websocket.ts +0 -245
- package/src/util/index.ts +0 -23
- package/src/util/logger.ts +0 -69
- package/src/util/recordRequests.ts +0 -47
- package/src/util/request.ts +0 -117
- package/src/util/subscription-set/expiring-sorted-set.ts +0 -54
- package/src/util/subscription-set/subscription-set.ts +0 -35
- package/src/util/test-payload-loader.ts +0 -87
- package/src/validation/error.ts +0 -116
- package/src/validation/index.ts +0 -110
- package/src/validation/input-params.ts +0 -45
- package/src/validation/override-functions.ts +0 -44
- package/src/validation/overrideFunctions.ts +0 -44
- package/src/validation/preset-tokens.json +0 -23
- package/src/validation/validator.ts +0 -384
- package/test/adapter.test.ts +0 -27
- package/test/background-executor.test.ts +0 -108
- package/test/cache/cache-key.test.ts +0 -114
- package/test/cache/helper.ts +0 -100
- package/test/cache/local.test.ts +0 -54
- package/test/cache/redis.test.ts +0 -89
- package/test/correlation.test.ts +0 -114
- package/test/index.test.ts +0 -37
- package/test/metrics/feed-id.test.ts +0 -38
- package/test/metrics/helper.ts +0 -14
- package/test/metrics/labels.test.ts +0 -36
- package/test/metrics/metrics.test.ts +0 -267
- package/test/metrics/redis-metrics.test.ts +0 -113
- package/test/metrics/warmer-metrics.test.ts +0 -193
- package/test/metrics/ws-metrics.test.ts +0 -225
- package/test/rate-limit-config.test.ts +0 -242
- package/test/smoke/smoke.test.ts +0 -166
- package/test/smoke/test-payload-fail.json +0 -3
- package/test/smoke/test-payload.js +0 -22
- package/test/smoke/test-payload.json +0 -7
- package/test/transports/batch.test.ts +0 -466
- package/test/transports/rest.test.ts +0 -242
- package/test/transports/websocket.test.ts +0 -183
- package/test/tsconfig.json +0 -5
- package/test/util.ts +0 -77
- package/test/validation.test.ts +0 -178
- package/test.sh +0 -20
- package/test2.sh +0 -2
- package/tsconfig.json +0 -28
- package/typedoc.json +0 -6
- package/webpack.config.js +0 -57
- package/yarn-error.log +0 -3778
|
@@ -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
|
-
}
|
package/test/adapter.test.ts
DELETED
|
@@ -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,108 +0,0 @@
|
|
|
1
|
-
import test from 'ava'
|
|
2
|
-
import FakeTimers from '@sinonjs/fake-timers'
|
|
3
|
-
import { expose } from '../src'
|
|
4
|
-
import { Adapter, AdapterContext } from '../src/adapter'
|
|
5
|
-
import { deferredPromise, NopTransport } from './util'
|
|
6
|
-
|
|
7
|
-
test('background executor calls transport function with background context', async (t) => {
|
|
8
|
-
const [promise, resolve] = deferredPromise<AdapterContext>()
|
|
9
|
-
|
|
10
|
-
const transport = new (class extends NopTransport {
|
|
11
|
-
async backgroundExecute(context: AdapterContext): Promise<number> {
|
|
12
|
-
resolve(context)
|
|
13
|
-
return 100
|
|
14
|
-
}
|
|
15
|
-
})()
|
|
16
|
-
|
|
17
|
-
const adapter: Adapter = {
|
|
18
|
-
name: 'test',
|
|
19
|
-
endpoints: [
|
|
20
|
-
{
|
|
21
|
-
name: 'test',
|
|
22
|
-
inputParameters: {},
|
|
23
|
-
transport,
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
name: 'skipped',
|
|
27
|
-
inputParameters: {},
|
|
28
|
-
transport: new NopTransport(), // Also add coverage for skipped executors
|
|
29
|
-
},
|
|
30
|
-
],
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
await expose(adapter)
|
|
34
|
-
const context = await promise
|
|
35
|
-
t.is(context.adapterEndpoint.name, 'test')
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
// Needs to be serial to avoid mocking global clock more than once at the same time
|
|
39
|
-
// TODO: Inject clock?
|
|
40
|
-
test.serial(
|
|
41
|
-
'background executor calls transport function second time after sleeping',
|
|
42
|
-
async (t) => {
|
|
43
|
-
const clock = FakeTimers.install()
|
|
44
|
-
let timesCalled = 0
|
|
45
|
-
|
|
46
|
-
const transport = new (class extends NopTransport {
|
|
47
|
-
async backgroundExecute(): Promise<number> {
|
|
48
|
-
timesCalled++
|
|
49
|
-
return 1000
|
|
50
|
-
}
|
|
51
|
-
})()
|
|
52
|
-
|
|
53
|
-
const adapter: Adapter = {
|
|
54
|
-
name: 'test',
|
|
55
|
-
endpoints: [
|
|
56
|
-
{
|
|
57
|
-
name: 'test',
|
|
58
|
-
inputParameters: {},
|
|
59
|
-
aliases: ['other'], // Add an alias to ensure it's not called once per alias
|
|
60
|
-
transport,
|
|
61
|
-
},
|
|
62
|
-
],
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
await expose(adapter)
|
|
66
|
-
t.is(timesCalled, 1)
|
|
67
|
-
await clock.tickAsync(999)
|
|
68
|
-
t.is(timesCalled, 1)
|
|
69
|
-
await clock.tickAsync(1)
|
|
70
|
-
t.is(timesCalled, 2)
|
|
71
|
-
|
|
72
|
-
clock.uninstall()
|
|
73
|
-
},
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
test.serial('background executor ends recursive chain on server close', async (t) => {
|
|
77
|
-
const clock = FakeTimers.install()
|
|
78
|
-
let timesCalled = 0
|
|
79
|
-
|
|
80
|
-
const transport = new (class extends NopTransport {
|
|
81
|
-
async backgroundExecute(): Promise<number> {
|
|
82
|
-
timesCalled++
|
|
83
|
-
return 1000
|
|
84
|
-
}
|
|
85
|
-
})()
|
|
86
|
-
|
|
87
|
-
const adapter: Adapter = {
|
|
88
|
-
name: 'test',
|
|
89
|
-
endpoints: [
|
|
90
|
-
{
|
|
91
|
-
name: 'test',
|
|
92
|
-
inputParameters: {},
|
|
93
|
-
transport,
|
|
94
|
-
},
|
|
95
|
-
],
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const server = await expose(adapter)
|
|
99
|
-
if (!server) {
|
|
100
|
-
throw 'Server did not start'
|
|
101
|
-
}
|
|
102
|
-
t.is(timesCalled, 1)
|
|
103
|
-
server.close()
|
|
104
|
-
await clock.tickAsync(999999)
|
|
105
|
-
t.is(timesCalled, 1) // The background process closed, so this was never called again
|
|
106
|
-
|
|
107
|
-
clock.uninstall()
|
|
108
|
-
})
|
|
@@ -1,114 +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
|
-
envDefaultOverrides: {
|
|
39
|
-
MAX_COMMON_KEY_SIZE: 100,
|
|
40
|
-
},
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
t.context.adapterEndpoint = adapter.endpoints[0]
|
|
44
|
-
const server = await expose(adapter)
|
|
45
|
-
if (!server) {
|
|
46
|
-
throw 'Server did not start'
|
|
47
|
-
}
|
|
48
|
-
t.context.serverAddress = `http://localhost:${(server.address() as AddressInfo).port}`
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
test.serial('no parameters returns default cache key', async (t) => {
|
|
52
|
-
const response = await axios.post(`${t.context.serverAddress}`, {})
|
|
53
|
-
t.is(response.data.result, BaseSettings.DEFAULT_CACHE_KEY.default)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test.serial('builds cache key correctly from input params', async (t) => {
|
|
57
|
-
t.context.adapterEndpoint.inputParameters = {
|
|
58
|
-
base: {
|
|
59
|
-
type: 'string',
|
|
60
|
-
required: true,
|
|
61
|
-
},
|
|
62
|
-
quote: {
|
|
63
|
-
type: 'string',
|
|
64
|
-
required: false,
|
|
65
|
-
},
|
|
66
|
-
factor: {
|
|
67
|
-
type: 'number',
|
|
68
|
-
required: true,
|
|
69
|
-
},
|
|
70
|
-
proper: {
|
|
71
|
-
type: 'boolean',
|
|
72
|
-
required: false,
|
|
73
|
-
},
|
|
74
|
-
details: {
|
|
75
|
-
type: 'object',
|
|
76
|
-
required: true,
|
|
77
|
-
},
|
|
78
|
-
nullable: {
|
|
79
|
-
type: 'object',
|
|
80
|
-
required: false,
|
|
81
|
-
},
|
|
82
|
-
}
|
|
83
|
-
const response = await axios.post(`${t.context.serverAddress}`, {
|
|
84
|
-
data: {
|
|
85
|
-
base: 'eth',
|
|
86
|
-
factor: 123,
|
|
87
|
-
proper: true,
|
|
88
|
-
details: {
|
|
89
|
-
asd: 'qwe',
|
|
90
|
-
zxc: 432,
|
|
91
|
-
},
|
|
92
|
-
nullable: null,
|
|
93
|
-
},
|
|
94
|
-
})
|
|
95
|
-
t.is(response.data.result, 'test-|base:eth|factor:123|proper:true|details:')
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
test.serial('cache key is truncated if over max size', async (t) => {
|
|
99
|
-
t.context.adapterEndpoint.inputParameters = {
|
|
100
|
-
base: {
|
|
101
|
-
type: 'string',
|
|
102
|
-
required: true,
|
|
103
|
-
},
|
|
104
|
-
}
|
|
105
|
-
const response = await axios.post(`${t.context.serverAddress}`, {
|
|
106
|
-
data: {
|
|
107
|
-
base: Array(100)
|
|
108
|
-
.fill(null)
|
|
109
|
-
.map((s, i) => `----------${i}`)
|
|
110
|
-
.join('|'),
|
|
111
|
-
},
|
|
112
|
-
})
|
|
113
|
-
t.is(response.data.result.length, 100 + 1 + 4) // Max common key + separator + endpoint name
|
|
114
|
-
})
|