-
Notifications
You must be signed in to change notification settings - Fork 1
/
vitest.setup.ts
479 lines (398 loc) · 16.5 KB
/
vitest.setup.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import { vi } from 'vitest';
import { promises as fs } from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
import { getProvider, isValidHttpUrl } from '@nangohq/shared';
import parseLinksHeader from 'parse-link-header';
import get from 'lodash-es/get.js';
import type { Pagination, CursorPagination, LinkPagination, OffsetPagination, OffsetCalculationMethod } from '@nangohq/types';
class NangoActionMock {
dirname: string;
name: string;
Model: string;
providerConfigKey: string;
log = vi.fn();
ActionError = vi.fn();
getConnection: ReturnType<typeof vi.fn>;
getMetadata: ReturnType<typeof vi.fn>;
updateMetadata: ReturnType<typeof vi.fn>;
paginate: ReturnType<typeof vi.fn>;
get: ReturnType<typeof vi.fn>;
post: ReturnType<typeof vi.fn>;
patch: ReturnType<typeof vi.fn>;
put: ReturnType<typeof vi.fn>;
delete: ReturnType<typeof vi.fn>;
proxy: ReturnType<typeof vi.fn>;
getWebhookURL: ReturnType<typeof vi.fn>;
constructor({ dirname, name, Model }: { dirname: string; name: string; Model: string }) {
this.dirname = dirname;
this.providerConfigKey = path.basename(path.dirname(dirname));
this.name = name;
this.Model = Model;
this.getConnection = vi.fn(this.getConnectionData.bind(this));
this.getMetadata = vi.fn(this.getMetadataData.bind(this));
this.paginate = vi.fn(this.getProxyPaginateData.bind(this));
this.get = vi.fn(this.proxyGetData.bind(this));
this.post = vi.fn(this.proxyPostData.bind(this));
this.patch = vi.fn(this.proxyPatchData.bind(this));
this.put = vi.fn(this.proxyPutData.bind(this));
this.delete = vi.fn(this.proxyDeleteData.bind(this));
this.proxy = vi.fn(this.proxyData.bind(this));
this.getWebhookURL = vi.fn(() => 'https://example.com/webhook');
this.updateMetadata = vi.fn();
}
private async getMockFile(fileName: string, throwOnMissing: boolean, identity?: ConfigIdentity) {
const filePath = path.resolve(this.dirname, `../mocks/${fileName}.json`);
try {
const fileContent = await fs.readFile(filePath, 'utf-8');
const data = JSON.parse(fileContent);
return data;
} catch (error) {
if (throwOnMissing) {
throw new Error(`Failed to load mock data from ${filePath}: ${error.message} ${identity ? JSON.stringify(identity, null, 2) : ''}`);
}
}
}
private async hashDirExists(hashDir: string) {
const filePath = path.resolve(this.dirname, `../mocks/${hashDir}/`);
try {
await fs.stat(filePath);
return true;
} catch (error) {
return false;
}
}
private async getCachedResponse(identity: ConfigIdentity) {
const dir = `nango/${identity.method}/proxy/${identity.endpoint}/${this.name}/`;
const hashBasedPath = `${dir}/${identity.requestIdentityHash}`;
if (await this.hashDirExists(dir)) {
const data = await this.getMockFile(hashBasedPath, true, identity);
return data;
} else {
return { response: await this.getMockFile(`nango/${identity.method}/proxy/${identity.endpoint}/${this.name}`, true, identity) };
}
}
public async getBatchSaveData(modelName: string) {
const data = await this.getMockFile(`${this.name}/${modelName}/batchSave`, true);
return data;
}
public async getBatchDeleteData(modelName: string) {
const data = await this.getMockFile(`${this.name}/${modelName}/batchDelete`, true);
return data;
}
public async getInput() {
const data = await this.getMockFile(`${this.name}/input`, false);
return data;
}
public async getOutput() {
const data = await this.getMockFile(`${this.name}/output`, true);
return data;
}
private async getConnectionData() {
const data = await this.getMockFile(`nango/getConnection`, true);
return data;
}
private async getMetadataData() {
const data = await this.getMockFile('nango/getMetadata', true);
return data;
}
private async *getProxyPaginateData(args: Configish) {
const providerConfig = getProvider(this.providerConfigKey);
if (!providerConfig) {
throw new Error(`Provider config not found for ${this.providerConfigKey}`);
}
args.method = args.method || 'get';
args.paginate = {
...providerConfig.proxy?.paginate,
...args.paginate
};
const paginateInBody = ['post', 'put', 'patch'].includes(args.method.toLowerCase());
const updatedBodyOrParams = paginateInBody ? (args.data as Record<string, any>) || {} : args.params || {};
if (args.paginate['limit']) {
const limitParameterName = args.paginate.limit_name_in_request!;
updatedBodyOrParams[limitParameterName] = args.paginate['limit'];
}
if (args.paginate?.type === 'cursor') {
yield* this.cursorPaginate(args, updatedBodyOrParams, paginateInBody);
} else if (args.paginate?.type === 'link') {
yield* this.linkPaginate(args, updatedBodyOrParams, paginateInBody);
} else if (args.paginate?.type === 'offset') {
yield* this.offsetPaginate(args, updatedBodyOrParams, paginateInBody);
} else {
throw new Error(`Invalid pagination type: ${args.paginate?.type}`);
}
}
private async *cursorPaginate(args: Configish, updatedBodyOrParams: Record<string, any>, paginateInBody: boolean) {
const cursorPagination = args.paginate as CursorPagination;
let nextCursor: string | number | undefined;
do {
if (typeof nextCursor !== 'undefined') {
updatedBodyOrParams[cursorPagination.cursor_name_in_request] = nextCursor;
}
if (paginateInBody) {
args.data = updatedBodyOrParams;
} else {
args.params = updatedBodyOrParams;
}
const response = await this.proxyData(args);
if (!response.headers) {
// use legacy method for cached responses
const data = response.data;
const paginate = args.paginate as Pagination;
if (Array.isArray(data)) {
yield data;
}
if (paginate && paginate.response_path) {
yield data[paginate.response_path];
} else {
// if not an array, return the first key that is an array
const keys = Object.keys(data);
for (const key of keys) {
if (Array.isArray(data[key])) {
yield data[key];
}
}
}
return;
}
const responseData = cursorPagination.response_path ? response.data[cursorPagination.response_path] : response.data;
if (!responseData || !responseData.length) {
return;
}
yield responseData;
nextCursor = response.data[cursorPagination.cursor_path_in_response];
if (typeof nextCursor === 'string') {
nextCursor = nextCursor.trim();
if (!nextCursor) {
nextCursor = undefined;
}
} else if (typeof nextCursor !== 'number') {
nextCursor = undefined;
}
} while (typeof nextCursor !== 'undefined');
}
private async *linkPaginate(args: Configish, updatedBodyOrParams: Record<string, any>, paginateInBody: boolean) {
const linkPagination = args.paginate as LinkPagination;
if (paginateInBody) {
args.data = updatedBodyOrParams;
} else {
args.params = updatedBodyOrParams;
}
while (true) {
const responseish = await this.proxyData(args);
// if this is a legacy cached response, use the legacy algorithm
if (!responseish.headers) {
const data = responseish.data;
const paginate = args.paginate as Pagination;
if (Array.isArray(data)) {
yield data;
}
if (paginate && paginate.response_path) {
yield data[paginate.response_path];
} else {
// if not an array, return the first key that is an array
const keys = Object.keys(data);
for (const key of keys) {
if (Array.isArray(data[key])) {
yield data[key];
}
}
}
return;
}
const data = responseish.data;
const responseData = linkPagination.response_path ? data[linkPagination.response_path] : data;
if (!responseData.length) {
return;
}
yield responseData;
const nextPageLink: string | undefined = this.getNextPageLinkFromBodyOrHeaders(linkPagination, responseish, args.paginate as Pagination);
if (!nextPageLink) {
return;
}
if (!isValidHttpUrl(nextPageLink)) {
// some providers only send path+query params in the link so we can immediately assign those to the endpoint
args.endpoint = nextPageLink;
} else {
const url: URL = new URL(nextPageLink);
args.endpoint = url.pathname + url.search;
}
args.params = {};
}
}
private getNextPageLinkFromBodyOrHeaders(linkPagination: LinkPagination, response: Responseish, paginationConfig: Pagination) {
if (linkPagination.link_rel_in_response_header) {
const linkHeader = parseLinksHeader(response.headers['link']);
return linkHeader?.[linkPagination.link_rel_in_response_header]?.url;
} else if (linkPagination.link_path_in_response_body) {
return get(response.data, linkPagination.link_path_in_response_body);
}
throw Error(`Either 'link_rel_in_response_header' or 'link_path_in_response_body' should be specified for '${paginationConfig.type}' pagination`);
}
private async *offsetPaginate(args: Configish, updatedBodyOrParams: Record<string, any>, paginateInBody: boolean) {
const offsetPagination = args.paginate as OffsetPagination;
const offsetParameterName: string = offsetPagination.offset_name_in_request;
const offsetCalculationMethod: OffsetCalculationMethod = offsetPagination.offset_calculation_method || 'by-response-size';
let offset = offsetPagination.offset_start_value || 0;
while (true) {
updatedBodyOrParams[offsetParameterName] = paginateInBody ? offset : String(offset);
if (paginateInBody) {
args.data = updatedBodyOrParams;
} else {
args.params = updatedBodyOrParams;
}
const response = await this.proxyData(args);
if (!response.headers) {
// use legacy method for cached responses
const data = response.data;
const paginate = args.paginate as Pagination;
if (Array.isArray(data)) {
yield data;
}
if (paginate && paginate.response_path) {
yield data[paginate.response_path];
} else {
// if not an array, return the first key that is an array
const keys = Object.keys(data);
for (const key of keys) {
if (Array.isArray(data[key])) {
yield data[key];
}
}
}
return;
}
const responseData = args.paginate?.response_path ? get(response.data, args.paginate?.response_path) : response.data;
if (!responseData || !responseData.length) {
return;
}
yield responseData;
if (args.paginate?.limit && responseData.length < args.paginate?.limit) {
return;
}
if (responseData.length < 1) {
// empty page, no more results
return;
}
if (offsetCalculationMethod === 'per-page') {
offset++;
} else {
offset += responseData.length;
}
}
}
private async proxyGetData(args: Configish) {
return this.proxyData({ ...args, method: 'get' });
}
private async proxyPostData(args: Configish) {
return this.proxyData({ ...args, method: 'post' });
}
private async proxyPatchData(args: Configish) {
return this.proxyData({ ...args, method: 'patch' });
}
private async proxyPutData(args: Configish) {
return this.proxyData({ ...args, method: 'put' });
}
private async proxyDeleteData(args: Configish) {
return this.proxyData({ ...args, method: 'delete' });
}
private async proxyData(args: Configish) {
const identity = computeConfigIdentity(args);
const cached = await this.getCachedResponse(identity);
return { data: cached.response, headers: cached.headers, status: cached.status };
}
}
class NangoSyncMock extends NangoActionMock {
lastSyncDate = null;
batchSave: ReturnType<typeof vi.fn>;
batchDelete: ReturnType<typeof vi.fn>;
constructor({ dirname, name, Model }: { dirname: string; name: string; Model: string }) {
super({ dirname, name, Model });
this.batchSave = vi.fn();
this.batchDelete = vi.fn();
}
}
const FILTER_HEADERS = ['authorization', 'user-agent', 'nango-proxy-user-agent', 'accept-encoding', 'retries', 'host', 'connection-id', 'provider-config-key'];
interface Configish {
endpoint: string;
params: Record<string, string | number>;
method: string;
headers?: Record<string, string>;
paginate?: Partial<CursorPagination> | Partial<LinkPagination> | Partial<OffsetPagination>;
data: unknown;
}
interface Responseish {
data: unknown;
headers: Record<string, string>;
status: number;
}
interface RequestIdentity {
method: string;
endpoint: string;
params: [string, unknown][];
headers: [string, unknown][];
data?: unknown;
}
interface ConfigIdentity {
method: string;
endpoint: string;
requestIdentityHash: string;
requestIdentity: RequestIdentity;
}
function computeConfigIdentity(config: Configish): ConfigIdentity {
const method = config.method?.toLowerCase() || 'get';
const params = sortEntries(Object.entries(config.params || {}));
const endpoint = config.endpoint.startsWith('/') ? config.endpoint.slice(1) : config.endpoint;
const dataIdentity = computeDataIdentity(config);
const filteredHeaders = Object.entries(config.headers || {}).filter(([key]) => !FILTER_HEADERS.includes(key.toLowerCase()));
sortEntries(filteredHeaders);
const headers = filteredHeaders;
const requestIdentity = {
method,
endpoint,
params,
headers,
data: dataIdentity
};
const requestIdentityHash = crypto.createHash('sha1').update(JSON.stringify(requestIdentity)).digest('hex');
return {
method,
endpoint,
requestIdentityHash,
requestIdentity
};
}
function sortEntries(entries: [string, unknown][]): [string, unknown][] {
return entries.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
}
function computeDataIdentity(config: Configish): string | undefined {
const data = config.data;
if (!data) {
return undefined;
}
let dataString = '';
if (typeof data === 'string') {
dataString = data;
} else if (Buffer.isBuffer(data)) {
dataString = data.toString('base64');
} else {
try {
dataString = JSON.stringify(data);
} catch (e) {
if (e instanceof Error) {
throw new Error(`Unable to compute request identity: ${e.message}`);
} else {
throw new Error('Unable to compute request identity');
}
}
}
if (dataString.length > 1000) {
return 'sha1:' + crypto.createHash('sha1').update(dataString).digest('hex');
} else {
return dataString;
}
}
globalThis.vitest = {
NangoActionMock,
NangoSyncMock
};