-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.ts
208 lines (188 loc) · 5.43 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
DEFAULT_API_VERSION,
DEFAULT_PAGINATION_PAGE_COUNT,
DEFAULT_PAGINATION_PAGE_ITEMS_COUNT,
DEFAULT_ORDER,
DEFAULT_BATCH_SIZE,
} from '../config';
import {
Options,
ValidatedOptions,
PaginationOptions,
AdditionalEndpointOptions,
AllMethodOptions,
BlockfrostNetwork,
} from '../types';
import { RetryObject } from 'got';
import { RATE_LIMITER_DEFAULT_CONFIG } from './limiter';
export const isDebugEnabled = () => process.env.BLOCKFROST_DEBUG === 'true';
export const validateOptions = (options?: Options): ValidatedOptions => {
if (!options || (!options.customBackend && !options.projectId)) {
throw Error('Missing customBackend or projectId option');
}
if (!options.projectId && !options.customBackend) {
throw Error('Missing param projectId in options');
}
if (options.version && isNaN(options.version)) {
throw Error('Param version is not a number');
}
if (options.requestTimeout && isNaN(options.requestTimeout)) {
throw Error('Param requestTimeout is not a number');
}
const debug = options.debug ?? isDebugEnabled();
let rateLimiter: ValidatedOptions['rateLimiter'];
if (options.rateLimiter === false) {
rateLimiter = false;
} else if (
options.rateLimiter === true ||
options.rateLimiter === undefined
) {
rateLimiter = RATE_LIMITER_DEFAULT_CONFIG;
} else if (options.rateLimiter) {
// custom config
// eslint-disable-next-line prefer-destructuring
rateLimiter = options.rateLimiter;
}
return {
customBackend: options.customBackend,
projectId: options.projectId,
network: options.network ?? deriveNetworkOption(options.projectId),
rateLimiter,
version: options.version || DEFAULT_API_VERSION,
debug,
http2: options.http2 ?? false,
gotOptions: options.gotOptions || undefined,
requestTimeout: options.requestTimeout ?? 20000, // 20 seconds
// see: https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#retry
retrySettings: options.retrySettings ?? {
limit: 3, // retry count
methods: ['GET', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'], // no retry on POST
statusCodes: [408, 413, 429, 500, 502, 503, 504, 520, 521, 522, 524],
errorCodes: [
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ECONNREFUSED',
'EPIPE',
'ENOTFOUND',
'ENETUNREACH',
'EAI_AGAIN',
'EPROTO',
],
calculateDelay: (retryObject: RetryObject) => {
return retryObject.computedValue !== 0 ? 1000 : 0;
},
// maxRetryAfter: undefined,
// backoffLimit: Number.POSITIVE_INFINITY,
// noise: 100
},
};
};
const deriveNetworkOption = (
projectId: string | undefined,
): BlockfrostNetwork | undefined => {
if (!projectId) return undefined;
if (projectId.startsWith('mainnet')) {
return 'mainnet';
} else if (projectId.startsWith('preview')) {
return 'preview';
} else if (projectId.startsWith('preprod')) {
return 'preprod';
} else if (projectId.startsWith('sanchonet')) {
return 'sanchonet';
} else if (projectId.startsWith('ipfs')) {
return 'ipfs';
} else {
console.warn(
'WARNING: Old token was used without network parameter. Switching to mainnet network',
);
return 'mainnet';
}
};
export const getAdditionalParams = (
options?: AdditionalEndpointOptions,
): AdditionalEndpointOptions => {
if (!options) {
return {
from: undefined,
to: undefined,
};
}
return {
from: options.from || undefined,
to: options.to || undefined,
};
};
export const getPaginationOptions = (
options?: PaginationOptions,
): PaginationOptions => {
if (!options) {
return {
page: DEFAULT_PAGINATION_PAGE_COUNT,
count: DEFAULT_PAGINATION_PAGE_ITEMS_COUNT,
order: DEFAULT_ORDER,
};
}
return {
page: options.page || DEFAULT_PAGINATION_PAGE_COUNT,
count: options.count || DEFAULT_PAGINATION_PAGE_ITEMS_COUNT,
order: options.order || DEFAULT_ORDER,
};
};
export const getAllMethodOptions = (
options?: AllMethodOptions,
): { batchSize: number; order: 'asc' | 'desc' } => {
if (!options) {
return {
batchSize: DEFAULT_BATCH_SIZE,
order: DEFAULT_ORDER,
};
}
return {
batchSize: options.batchSize || DEFAULT_PAGINATION_PAGE_COUNT,
order: options.order || DEFAULT_ORDER,
};
};
export const paginateMethod = async <
T extends (
pagination: PaginationOptions,
additionalOptions?: AdditionalEndpointOptions,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => any,
>(
fn: T,
allMethodOptions: AllMethodOptions | undefined,
additionalOptions?: AdditionalEndpointOptions,
): Promise<ReturnType<T>> => {
const res = [];
let page = 1;
const count = DEFAULT_PAGINATION_PAGE_ITEMS_COUNT;
const options = getAllMethodOptions(allMethodOptions);
const getSlice = () => {
const promises = [...Array(options.batchSize).keys()].map(i =>
fn(
{
page: page + i,
count,
order: options.order,
},
{
from: additionalOptions?.from,
to: additionalOptions?.to,
},
),
);
page += options.batchSize;
return promises;
};
// eslint-disable-next-line no-constant-condition
while (true) {
const pages = await Promise.all(getSlice());
for (const p of pages) {
res.push(...p);
if (p.length < count) {
return res as ReturnType<T>; // yikes
}
}
}
};