@chainlink/external-adapter-framework 0.12.1 → 0.13.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.
- package/adapter/basic.js +3 -3
- package/adapter/basic.js.map +1 -1
- package/adapter/endpoint.d.ts +9 -1
- package/adapter/endpoint.js +23 -0
- package/adapter/endpoint.js.map +1 -1
- package/cache/index.d.ts +2 -2
- package/cache/index.js +1 -1
- package/cache/index.js.map +1 -1
- package/cache/local.js +7 -3
- package/cache/local.js.map +1 -1
- package/cache/metrics.d.ts +1 -1
- package/cache/metrics.js +9 -1
- package/cache/metrics.js.map +1 -1
- package/cache/redis.js +12 -4
- package/cache/redis.js.map +1 -1
- package/cache/response.d.ts +36 -0
- package/cache/response.js +58 -0
- package/cache/response.js.map +1 -0
- package/config/index.d.ts +6 -6
- package/config/index.js +6 -10
- package/config/index.js.map +1 -1
- package/index.js +4 -1
- package/index.js.map +1 -1
- package/metrics/util.d.ts +1 -1
- package/metrics/util.js +2 -2
- package/metrics/util.js.map +1 -1
- package/package.json +1 -1
- package/rate-limiting/metrics.js +1 -1
- package/rate-limiting/metrics.js.map +1 -1
- package/transports/abstract/streaming.d.ts +34 -0
- package/transports/abstract/streaming.js +44 -0
- package/transports/abstract/streaming.js.map +1 -0
- package/transports/abstract/subscription.d.ts +38 -0
- package/transports/abstract/subscription.js +63 -0
- package/transports/abstract/subscription.js.map +1 -0
- package/transports/batch-warming.d.ts +7 -12
- package/transports/batch-warming.js +42 -28
- package/transports/batch-warming.js.map +1 -1
- package/transports/index.d.ts +20 -13
- package/transports/index.js +0 -35
- package/transports/index.js.map +1 -1
- package/transports/metrics.d.ts +11 -4
- package/transports/metrics.js +30 -20
- package/transports/metrics.js.map +1 -1
- package/transports/rest.d.ts +10 -7
- package/transports/rest.js +22 -12
- package/transports/rest.js.map +1 -1
- package/transports/routing.d.ts +9 -3
- package/transports/routing.js +4 -4
- package/transports/routing.js.map +1 -1
- package/transports/sse.d.ts +8 -12
- package/transports/sse.js +29 -39
- package/transports/sse.js.map +1 -1
- package/transports/websocket.d.ts +14 -14
- package/transports/websocket.js +76 -60
- package/transports/websocket.js.map +1 -1
- package/util/request.d.ts +90 -11
package/transports/websocket.js
CHANGED
|
@@ -30,8 +30,8 @@ exports.WebSocketTransport = exports.WebSocketClassProvider = exports.WebSocket
|
|
|
30
30
|
const ws_1 = __importDefault(require("ws"));
|
|
31
31
|
exports.WebSocket = ws_1.default;
|
|
32
32
|
const util_1 = require("../util");
|
|
33
|
-
const _1 = require("./");
|
|
34
33
|
const transportMetrics = __importStar(require("./metrics"));
|
|
34
|
+
const streaming_1 = require("./abstract/streaming");
|
|
35
35
|
const logger = (0, util_1.makeLogger)('WebSocketTransport');
|
|
36
36
|
class WebSocketClassProvider {
|
|
37
37
|
static set(ctor) {
|
|
@@ -49,22 +49,15 @@ WebSocketClassProvider.ctor = ws_1.default;
|
|
|
49
49
|
*
|
|
50
50
|
* @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[WebsocketTransportGenerics]])
|
|
51
51
|
*/
|
|
52
|
-
class WebSocketTransport {
|
|
52
|
+
class WebSocketTransport extends streaming_1.StreamingTransport {
|
|
53
53
|
constructor(config) {
|
|
54
|
+
super();
|
|
54
55
|
this.config = config;
|
|
55
|
-
// The double sets serve to create a simple polling mechanism instead of needing a subscription
|
|
56
|
-
// This one would not; this is always local state
|
|
57
|
-
this.localSubscriptions = [];
|
|
58
56
|
this.currentUrl = '';
|
|
57
|
+
this.lastMessageReceivedAt = 0;
|
|
59
58
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
this.rateLimiter = dependencies.backgroundExecuteRateLimiter;
|
|
63
|
-
this.subscriptionSet = dependencies.subscriptionSetFactory.buildSet(endpointName);
|
|
64
|
-
}
|
|
65
|
-
async registerRequest(req, config) {
|
|
66
|
-
logger.debug(`Adding entry to subscription set (ttl: ${config.WS_SUBSCRIPTION_TTL}): [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
|
|
67
|
-
await this.subscriptionSet.add(req.requestContext.cacheKey, req.requestContext.data, config.WS_SUBSCRIPTION_TTL);
|
|
59
|
+
getSubscriptionTtlFromConfig(config) {
|
|
60
|
+
return config.WS_SUBSCRIPTION_TTL;
|
|
68
61
|
}
|
|
69
62
|
connectionClosed() {
|
|
70
63
|
return !this.wsConnection || this.wsConnection.readyState === ws_1.default.CLOSED;
|
|
@@ -75,11 +68,10 @@ class WebSocketTransport {
|
|
|
75
68
|
deserializeMessage(data) {
|
|
76
69
|
return JSON.parse(data.toString());
|
|
77
70
|
}
|
|
78
|
-
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this.wsConnection.addEventListener('open', async (event) => {
|
|
71
|
+
buildConnectionHandlers(context, connectionReadyResolve) {
|
|
72
|
+
return {
|
|
73
|
+
// Called when the WS connection is opened
|
|
74
|
+
open: async (event) => {
|
|
83
75
|
logger.debug(`Opened websocket connection. (event type ${event.type})`);
|
|
84
76
|
if (this.config.handlers.open) {
|
|
85
77
|
await this.config.handlers.open(this.wsConnection, context);
|
|
@@ -87,31 +79,64 @@ class WebSocketTransport {
|
|
|
87
79
|
}
|
|
88
80
|
// Record active ws connections by incrementing count on open
|
|
89
81
|
transportMetrics.wsConnectionActive.inc();
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
82
|
+
connectionReadyResolve(true);
|
|
83
|
+
},
|
|
84
|
+
// Called when any message is received by the open connection
|
|
85
|
+
message: async (event) => {
|
|
93
86
|
const parsed = this.deserializeMessage(event.data);
|
|
94
87
|
logger.trace(`Got ws message: ${event.data}`);
|
|
95
|
-
const
|
|
88
|
+
const providerDataReceived = Date.now();
|
|
89
|
+
const results = this.config.handlers.message(parsed, context)?.map((r) => {
|
|
90
|
+
const result = r;
|
|
91
|
+
const partialResponse = r.response;
|
|
92
|
+
result.response.timestamps = {
|
|
93
|
+
providerDataStreamEstablished: this.providerDataStreamEstablished,
|
|
94
|
+
providerDataReceived,
|
|
95
|
+
providerIndicatedTime: partialResponse.timestamps?.providerIndicatedTime,
|
|
96
|
+
};
|
|
97
|
+
return result;
|
|
98
|
+
});
|
|
96
99
|
if (Array.isArray(results)) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
+
// Updating the last message received time here, to only care about messages we use
|
|
101
|
+
this.lastMessageReceivedAt = Date.now();
|
|
102
|
+
logger.trace(`Writing ${results.length} responses to cache`);
|
|
103
|
+
await this.responseCache.write(results);
|
|
100
104
|
}
|
|
101
|
-
|
|
102
|
-
|
|
105
|
+
// Do this after writing so we get the values to the cache ASAP
|
|
106
|
+
// We're not calculating feedId or subscription because this is only a single message,
|
|
107
|
+
// and it could in theory contain more than one value to set to the cache
|
|
108
|
+
transportMetrics.wsMessageTotal
|
|
109
|
+
.labels({
|
|
110
|
+
direction: 'received',
|
|
111
|
+
})
|
|
112
|
+
.inc();
|
|
113
|
+
},
|
|
114
|
+
// Called when an error is thrown by the connection
|
|
115
|
+
error: async (event) => {
|
|
103
116
|
logger.debug(`Error occurred in web socket connection. Error: ${event.error} ; Message: ${event.message}`);
|
|
104
117
|
// Record connection error count
|
|
105
118
|
transportMetrics.wsConnectionErrors
|
|
106
119
|
.labels(transportMetrics.connectionErrorLabels(event.message))
|
|
107
120
|
.inc();
|
|
108
|
-
}
|
|
109
|
-
|
|
121
|
+
},
|
|
122
|
+
// Called when the WS connection closes for any reason
|
|
123
|
+
close: (event) => {
|
|
110
124
|
logger.debug(`Closed websocket connection. Code: ${event.code} ; reason: ${event.reason?.toString()}`);
|
|
111
125
|
// Record active ws connections by decrementing count on close
|
|
112
126
|
// Using URL in label since connection_key is removed from v3
|
|
113
127
|
transportMetrics.wsConnectionActive.dec();
|
|
114
|
-
}
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
establishWsConnection(context, url, options) {
|
|
132
|
+
return new Promise((resolve) => {
|
|
133
|
+
const ctor = WebSocketClassProvider.get();
|
|
134
|
+
const handlers = this.buildConnectionHandlers(context, resolve);
|
|
135
|
+
this.wsConnection = new ctor(url, undefined, options);
|
|
136
|
+
this.wsConnection.addEventListener('open', handlers.open);
|
|
137
|
+
this.wsConnection.addEventListener('message', handlers.message);
|
|
138
|
+
this.wsConnection.addEventListener('error', handlers.error);
|
|
139
|
+
this.wsConnection.addEventListener('close', handlers.close);
|
|
115
140
|
});
|
|
116
141
|
}
|
|
117
142
|
async sendMessages(context, subscribes, unsubscribes) {
|
|
@@ -122,59 +147,50 @@ class WebSocketTransport {
|
|
|
122
147
|
this.wsConnection.send(message);
|
|
123
148
|
}
|
|
124
149
|
}
|
|
125
|
-
|
|
126
|
-
async backgroundExecute(context) {
|
|
127
|
-
logger.debug('Starting background execute, getting subscriptions from sorted set');
|
|
128
|
-
const desiredSubs = await this.subscriptionSet.getAll();
|
|
129
|
-
// Keep track of active subscriptions for background execute
|
|
130
|
-
transportMetrics.bgExecuteSubscriptionSetCount
|
|
131
|
-
.labels({ endpoint: context.endpointName, transport_type: 'websocket' })
|
|
132
|
-
.set(desiredSubs.length);
|
|
133
|
-
logger.debug('Generating delta (subscribes & unsubscribes)');
|
|
134
|
-
const subscribes = desiredSubs.filter((s) => !this.localSubscriptions.map((ls) => JSON.stringify(ls)).includes(JSON.stringify(s)));
|
|
135
|
-
const unsubscribes = this.localSubscriptions.filter((s) => !desiredSubs.map((ds) => JSON.stringify(ds)).includes(JSON.stringify(s)));
|
|
136
|
-
logger.debug(`${subscribes.length} new subscriptions; ${unsubscribes.length} to unsubscribe`);
|
|
137
|
-
if (subscribes.length) {
|
|
138
|
-
logger.trace(`Will subscribe to: ${JSON.stringify(subscribes)}`);
|
|
139
|
-
}
|
|
140
|
-
if (unsubscribes.length) {
|
|
141
|
-
logger.trace(`Will unsubscribe to: ${JSON.stringify(unsubscribes)}`);
|
|
142
|
-
}
|
|
150
|
+
async streamHandler(context, subscriptions) {
|
|
143
151
|
// New subs && no connection -> connect -> add subs
|
|
144
152
|
// No new subs && no connection -> skip
|
|
145
153
|
// New subs && connection -> add subs
|
|
146
154
|
// No new subs && connection -> unsubs only
|
|
147
|
-
if (!
|
|
155
|
+
if (!subscriptions.new.length && !this.wsConnection) {
|
|
148
156
|
logger.debug('No entries in subscription set and no established connection, skipping');
|
|
149
|
-
return
|
|
157
|
+
return;
|
|
150
158
|
}
|
|
151
|
-
const urlFromConfig = await this.config.url(context,
|
|
159
|
+
const urlFromConfig = await this.config.url(context, subscriptions.desired);
|
|
152
160
|
const urlChanged = this.currentUrl !== urlFromConfig;
|
|
161
|
+
const timeSinceLastMessage = Date.now() - this.lastMessageReceivedAt;
|
|
162
|
+
const connectionUnresponsive = timeSinceLastMessage > context.adapterConfig.WS_SUBSCRIPTION_UNRESPONSIVE_TTL;
|
|
153
163
|
let connectionClosed = this.connectionClosed();
|
|
154
164
|
// Check if we should close the current connection
|
|
155
|
-
if (!connectionClosed && urlChanged) {
|
|
156
|
-
|
|
165
|
+
if (!connectionClosed && (urlChanged || connectionUnresponsive)) {
|
|
166
|
+
const reason = urlChanged
|
|
167
|
+
? `Websocket url has changed from ${this.currentUrl} to ${urlFromConfig}, closing connection...`
|
|
168
|
+
: `Last message was received ${timeSinceLastMessage} ago, exceeding the threshold of ${context.adapterConfig.WS_SUBSCRIPTION_UNRESPONSIVE_TTL}ms, closing connection...`;
|
|
169
|
+
logger.info(reason);
|
|
157
170
|
this.wsConnection.close();
|
|
158
171
|
connectionClosed = true;
|
|
172
|
+
// If the connection was closed, the new subscriptions should be the desired ones
|
|
173
|
+
subscriptions.new = subscriptions.desired;
|
|
174
|
+
if (subscriptions.new.length) {
|
|
175
|
+
logger.trace(`Connection will be reopened and will subscribe to new and resubscribe to existing: ${JSON.stringify(subscriptions.new)}`);
|
|
176
|
+
}
|
|
159
177
|
}
|
|
160
178
|
// Check if we need to open a new connection
|
|
161
|
-
if (connectionClosed &&
|
|
179
|
+
if (connectionClosed && subscriptions.desired.length) {
|
|
162
180
|
logger.debug('No established connection and new subscriptions available, connecting to WS');
|
|
163
181
|
const options = this.config.options && (await this.config.options(context));
|
|
164
182
|
this.currentUrl = urlFromConfig;
|
|
165
183
|
await this.establishWsConnection(context, urlFromConfig, options);
|
|
184
|
+
this.providerDataStreamEstablished = Date.now();
|
|
166
185
|
}
|
|
167
186
|
if (this.config.builders) {
|
|
168
187
|
logger.debug('Sending subs/unsubs if there are any');
|
|
169
188
|
const { subscribeMessage, unsubscribeMessage } = this.config.builders;
|
|
170
|
-
await this.sendMessages(context, subscribeMessage ?
|
|
189
|
+
await this.sendMessages(context, subscribeMessage ? subscriptions.new.map(subscribeMessage) : subscriptions.new, unsubscribeMessage ? subscriptions.stale.map(unsubscribeMessage) : subscriptions.stale);
|
|
171
190
|
}
|
|
172
191
|
// Record WS message and subscription metrics
|
|
173
|
-
transportMetrics.recordWsMessageMetrics(context,
|
|
174
|
-
|
|
175
|
-
this.localSubscriptions = desiredSubs;
|
|
176
|
-
logger.debug('Background execute complete');
|
|
177
|
-
return this.rateLimiter.msUntilNextExecution(context.endpointName);
|
|
192
|
+
transportMetrics.recordWsMessageMetrics(context, subscriptions.new, subscriptions.stale);
|
|
193
|
+
return;
|
|
178
194
|
}
|
|
179
195
|
}
|
|
180
196
|
exports.WebSocketTransport = WebSocketTransport;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.js","sourceRoot":"","sources":["../../../src/transports/websocket.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAsD;
|
|
1
|
+
{"version":3,"file":"websocket.js","sourceRoot":"","sources":["../../../src/transports/websocket.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAsD;AAc7C,oBAdF,YAAS,CAcE;AAXlB,kCAAoC;AAOpC,4DAA6C;AAC7C,oDAA6E;AAK7E,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,oBAAoB,CAAC,CAAA;AAQ/C,MAAa,sBAAsB;IAGjC,MAAM,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,CAAC,QAAQ,EAAE;YACxB,MAAM,CAAC,KAAK,CAAC,sCAAsC,CAAC,CAAA;YACpD,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAA;YACrE,MAAM,IAAI,CAAC,YAAY,CACrB,OAAO,EACP,gBAAgB,CAAC,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,GAAG,EAC9E,kBAAkB,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CACvF,CAAA;SACF;QAED,6CAA6C;QAC7C,gBAAgB,CAAC,sBAAsB,CAAC,OAAO,EAAE,aAAa,CAAC,GAAG,EAAE,aAAa,CAAC,KAAK,CAAC,CAAA;QAExF,OAAM;IACR,CAAC;CACF;AA9LD,gDA8LC"}
|
package/util/request.d.ts
CHANGED
|
@@ -76,18 +76,27 @@ export interface AdapterMetricsMeta {
|
|
|
76
76
|
export declare type AdapterRequestData = Record<string, unknown> & {
|
|
77
77
|
endpoint?: string;
|
|
78
78
|
};
|
|
79
|
+
export declare type ProviderResultGenerics = {
|
|
80
|
+
Request: RequestGenerics;
|
|
81
|
+
Response: ResponseGenerics;
|
|
82
|
+
};
|
|
79
83
|
/**
|
|
80
84
|
* Helper type to hold the value from responses from a provider, and the adapter params they correspond to.
|
|
81
85
|
*/
|
|
82
|
-
export
|
|
83
|
-
Request: RequestGenerics;
|
|
84
|
-
Response: ResponseGenerics;
|
|
85
|
-
}> {
|
|
86
|
+
export declare type ProviderResult<T extends ProviderResultGenerics> = {
|
|
86
87
|
/** The set of parameters that uniquely relate to the response */
|
|
87
88
|
params: T['Request']['Params'];
|
|
88
89
|
/** Value that will be included in the result property of the response */
|
|
89
|
-
|
|
90
|
-
}
|
|
90
|
+
response: PartialAdapterResponse<T['Response']>;
|
|
91
|
+
};
|
|
92
|
+
export declare type TimestampedProviderResult<T extends ProviderResultGenerics> = Pick<ProviderResult<T>, 'params'> & {
|
|
93
|
+
/** Value that will be included in the result property of the response, with timestamps applied */
|
|
94
|
+
response: TimestampedAdapterResponse<T['Response']>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Here we do actually want an object, since unknown could be a primitive.
|
|
98
|
+
* The rule itself does say "marginally better"
|
|
99
|
+
*/
|
|
91
100
|
export declare type EmptyObject = Object;
|
|
92
101
|
/**
|
|
93
102
|
* Helper struct type that provides detail about the incoming Adapter Request
|
|
@@ -113,20 +122,90 @@ export declare type ResponseGenerics = {
|
|
|
113
122
|
Result: string | number | null;
|
|
114
123
|
};
|
|
115
124
|
/**
|
|
116
|
-
*
|
|
125
|
+
* Details for timestamps to be included in the response
|
|
126
|
+
*/
|
|
127
|
+
declare type ResponseTimestamps = {
|
|
128
|
+
/** Time at which data was received from the provider */
|
|
129
|
+
providerDataReceived: number;
|
|
130
|
+
/** Time indicated by the provider representing the time at which this value was calculated/set/valid */
|
|
131
|
+
providerIndicatedTime: number | undefined;
|
|
132
|
+
} & ({
|
|
133
|
+
/**
|
|
134
|
+
* For sync protocols (request -\> response).
|
|
135
|
+
* Time at which data was requested from the provider.
|
|
136
|
+
*/
|
|
137
|
+
providerDataRequested: number;
|
|
138
|
+
providerDataStreamEstablished?: never;
|
|
139
|
+
} | {
|
|
140
|
+
/**
|
|
141
|
+
* For async protocols (subscription -\> n events received).
|
|
142
|
+
* Time at which a data stream was established for the provider.
|
|
143
|
+
*/
|
|
144
|
+
providerDataStreamEstablished: number;
|
|
145
|
+
providerDataRequested?: never;
|
|
146
|
+
});
|
|
147
|
+
/**
|
|
148
|
+
* Object with timestamps that will be present in both successful and provider error responses
|
|
149
|
+
*/
|
|
150
|
+
declare type TimestampedResponseObject = {
|
|
151
|
+
/** Timestamps relevant for data provider timings */
|
|
152
|
+
timestamps: ResponseTimestamps;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Response from the EA to send when manually storing an error in the cache
|
|
117
156
|
*/
|
|
118
|
-
|
|
119
|
-
/**
|
|
157
|
+
declare type ProviderErrorResponse = {
|
|
158
|
+
/** Status code for the errored response */
|
|
120
159
|
statusCode: number;
|
|
160
|
+
/** Error message that will be sent back from the adapter */
|
|
161
|
+
errorMessage: string;
|
|
162
|
+
} & {
|
|
163
|
+
data?: never;
|
|
164
|
+
result?: never;
|
|
165
|
+
meta?: never;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Provider error response with timestamps added
|
|
169
|
+
*/
|
|
170
|
+
export declare type TimestampedProviderErrorResponse = ProviderErrorResponse & TimestampedResponseObject;
|
|
171
|
+
/**
|
|
172
|
+
* The most basic data that needs to be manually set for an adapter response.
|
|
173
|
+
*/
|
|
174
|
+
export declare type PartialSuccessfulResponse<T extends ResponseGenerics> = {
|
|
121
175
|
/** Response data, holds "result" for Flux Monitor */
|
|
122
176
|
data: T['Data'];
|
|
123
177
|
/** Result value used for OCR */
|
|
124
178
|
result: T['Result'];
|
|
125
|
-
/**
|
|
126
|
-
|
|
179
|
+
/** Optionally, specify manually some of the timestamps here */
|
|
180
|
+
timestamps?: Pick<ResponseTimestamps, 'providerIndicatedTime'>;
|
|
181
|
+
} & {
|
|
182
|
+
errorMessage?: never;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Partial EA response, with timestamps added
|
|
186
|
+
*/
|
|
187
|
+
declare type TimestampedSuccessfulResponse<T extends ResponseGenerics> = PartialSuccessfulResponse<T> & TimestampedResponseObject;
|
|
188
|
+
/**
|
|
189
|
+
* Full EA successful response, with metadata and defaults added
|
|
190
|
+
*/
|
|
191
|
+
declare type SuccessfulResponse<T extends ResponseGenerics> = TimestampedSuccessfulResponse<T> & {
|
|
127
192
|
/** Metadata relevant to this request */
|
|
128
193
|
meta?: AdapterRequestMeta;
|
|
194
|
+
/** HTTP status code, by default will be set to 200 */
|
|
195
|
+
statusCode: number;
|
|
129
196
|
};
|
|
197
|
+
/**
|
|
198
|
+
* Response body from the EA, before timestamps, defaults and metadata are filled in
|
|
199
|
+
*/
|
|
200
|
+
export declare type PartialAdapterResponse<T extends ResponseGenerics = ResponseGenerics> = PartialSuccessfulResponse<T> | ProviderErrorResponse;
|
|
201
|
+
/**
|
|
202
|
+
* Response body from the EA with timestamps, before defaults and metadata are filled in
|
|
203
|
+
*/
|
|
204
|
+
export declare type TimestampedAdapterResponse<T extends ResponseGenerics = ResponseGenerics> = TimestampedSuccessfulResponse<T> | TimestampedProviderErrorResponse;
|
|
205
|
+
/**
|
|
206
|
+
* Shape of the response body from the adapter
|
|
207
|
+
*/
|
|
208
|
+
export declare type AdapterResponse<T extends ResponseGenerics = ResponseGenerics> = SuccessfulResponse<T> | TimestampedProviderErrorResponse;
|
|
130
209
|
export declare type SingleNumberResultResponse = {
|
|
131
210
|
Result: number;
|
|
132
211
|
Data: {
|