Skip to content

Commit e22b836

Browse files
ChaituVRwa0x6e
andauthored
Fix: Pass api key in headers and detect html error (#999)
* Fix: Pass api key in headers and detect html error * fix tests * Change to normal function * Update test/e2e/utils/getScores.spec.ts Co-authored-by: Wan <[email protected]> * Update formatScoreAPIUrl to include options.path * v0.11.23 --------- Co-authored-by: Wan <[email protected]>
1 parent ab51c3d commit e22b836

File tree

4 files changed

+131
-59
lines changed

4 files changed

+131
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snapshot-labs/snapshot.js",
3-
"version": "0.11.22",
3+
"version": "0.11.23",
44
"repository": "snapshot-labs/snapshot.js",
55
"license": "MIT",
66
"main": "dist/snapshot.cjs.js",

src/utils.spec.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ describe('utils', () => {
7878

7979
describe('when passing valid args', () => {
8080
test('send a JSON-RPC payload to score-api', async () => {
81+
const result = { result: 'OK' };
8182
fetch.mockReturnValue({
82-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
83+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
8384
});
8485

8586
expect(_validate({})).resolves;
8687
expect(fetch).toHaveBeenCalledWith(
87-
'https://score.snapshot.org',
88+
'https://score.snapshot.org/',
8889
expect.objectContaining({
8990
body: JSON.stringify({
9091
jsonrpc: '2.0',
@@ -96,13 +97,14 @@ describe('utils', () => {
9697
});
9798

9899
test('send a POST request with JSON content-type', async () => {
100+
const result = { result: 'OK' };
99101
fetch.mockReturnValue({
100-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
102+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
101103
});
102104

103105
expect(_validate({})).resolves;
104106
expect(fetch).toHaveBeenCalledWith(
105-
'https://score.snapshot.org',
107+
'https://score.snapshot.org/',
106108
expect.objectContaining({
107109
method: 'POST',
108110
headers: {
@@ -114,23 +116,28 @@ describe('utils', () => {
114116
});
115117

116118
test('can customize the score-api url', () => {
119+
const result = { result: 'OK' };
117120
fetch.mockReturnValue({
118-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
121+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
119122
});
120123

121124
expect(
122125
_validate({ options: { url: 'https://snapshot.org/?apiKey=xxx' } })
123126
).resolves;
124127
expect(fetch).toHaveBeenCalledWith(
125-
'https://snapshot.org/?apiKey=xxx',
126-
expect.anything()
128+
'https://snapshot.org/',
129+
expect.objectContaining({
130+
headers: expect.objectContaining({
131+
'X-API-KEY': 'xxx'
132+
})
133+
})
127134
);
128135
});
129136

130137
test('returns the JSON-RPC result property', () => {
131138
const result = { result: 'OK' };
132139
fetch.mockReturnValue({
133-
json: () => new Promise((resolve) => resolve(result))
140+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
134141
});
135142

136143
expect(_validate({})).resolves.toEqual('OK');
@@ -141,7 +148,7 @@ describe('utils', () => {
141148
test('rejects with the JSON-RPC error object', () => {
142149
const result = { error: { message: 'Oh no' } };
143150
fetch.mockReturnValue({
144-
json: () => new Promise((resolve) => resolve(result))
151+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
145152
});
146153

147154
expect(_validate({})).rejects.toEqual(result.error);
@@ -152,7 +159,7 @@ describe('utils', () => {
152159
test('rejects with the error', () => {
153160
const result = new Error('Oh no');
154161
fetch.mockReturnValue({
155-
json: () => {
162+
text: () => {
156163
throw result;
157164
}
158165
});
@@ -196,7 +203,7 @@ describe('utils', () => {
196203
network ?? payload.network,
197204
addresses ?? payload.addresses,
198205
snapshot ?? payload.snapshot,
199-
scoreApiUrl ?? 'https://score.snapshot.org',
206+
scoreApiUrl ?? 'https://score.snapshot.org/',
200207
options ?? {}
201208
);
202209
}
@@ -239,8 +246,9 @@ describe('utils', () => {
239246

240247
describe('when passing valid args', () => {
241248
test('send a JSON-RPC payload to score-api', async () => {
249+
const result = { result: 'OK' };
242250
fetch.mockReturnValue({
243-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
251+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
244252
});
245253

246254
expect(_getScores({})).resolves;
@@ -253,8 +261,9 @@ describe('utils', () => {
253261
});
254262

255263
test('send a POST request with JSON content-type', async () => {
264+
const result = { result: 'OK' };
256265
fetch.mockReturnValue({
257-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
266+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
258267
});
259268

260269
expect(_getScores({})).resolves;
@@ -270,23 +279,29 @@ describe('utils', () => {
270279
);
271280
});
272281

273-
test('can customize the score-api url', () => {
282+
test('can customize the score-api url and if apiKey should be passed in headers', () => {
283+
const result = { result: 'OK' };
274284
fetch.mockReturnValue({
275-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
285+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
276286
});
277287

278288
expect(_getScores({ scoreApiUrl: 'https://snapshot.org/?apiKey=xxx' }))
279289
.resolves;
280290
expect(fetch).toHaveBeenCalledWith(
281-
'https://snapshot.org/api/scores?apiKey=xxx',
282-
expect.anything()
291+
'https://snapshot.org/api/scores',
292+
expect.objectContaining({
293+
headers: expect.objectContaining({
294+
'X-API-KEY': 'xxx'
295+
})
296+
})
283297
);
284298
});
285299

286300
test('returns the JSON-RPC result scores property', () => {
287301
const result = { scores: 'SCORES', other: 'Other' };
288302
fetch.mockReturnValue({
289-
json: () => new Promise((resolve) => resolve({ result }))
303+
text: () =>
304+
new Promise((resolve) => resolve(JSON.stringify({ result })))
290305
});
291306

292307
expect(_getScores({})).resolves.toEqual('SCORES');
@@ -295,7 +310,8 @@ describe('utils', () => {
295310
test('returns the JSON-RPC all properties', () => {
296311
const result = { scores: 'SCORES', other: 'Other' };
297312
fetch.mockReturnValue({
298-
json: () => new Promise((resolve) => resolve({ result }))
313+
text: () =>
314+
new Promise((resolve) => resolve(JSON.stringify({ result })))
299315
});
300316

301317
expect(
@@ -308,7 +324,7 @@ describe('utils', () => {
308324
test('rejects with the JSON-RPC error object', () => {
309325
const result = { error: { message: 'Oh no' } };
310326
fetch.mockReturnValue({
311-
json: () => new Promise((resolve) => resolve(result))
327+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
312328
});
313329

314330
expect(_getScores({})).rejects.toEqual(result.error);
@@ -319,7 +335,7 @@ describe('utils', () => {
319335
test('rejects with the error', () => {
320336
const result = new Error('Oh no');
321337
fetch.mockReturnValue({
322-
json: () => {
338+
text: () => {
323339
throw result;
324340
}
325341
});
@@ -391,13 +407,14 @@ describe('utils', () => {
391407

392408
describe('when passing valid args', () => {
393409
test('send a JSON-RPC payload to score-api', async () => {
410+
const result = { result: 'OK' };
394411
fetch.mockReturnValue({
395-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
412+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
396413
});
397414

398415
expect(_getVp({})).resolves;
399416
expect(fetch).toHaveBeenCalledWith(
400-
'https://score.snapshot.org',
417+
'https://score.snapshot.org/',
401418
expect.objectContaining({
402419
body: JSON.stringify({
403420
jsonrpc: '2.0',
@@ -409,13 +426,14 @@ describe('utils', () => {
409426
});
410427

411428
test('send a POST request with JSON content-type', async () => {
429+
const result = { result: 'OK' };
412430
fetch.mockReturnValue({
413-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
431+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
414432
});
415433

416434
expect(_getVp({})).resolves;
417435
expect(fetch).toHaveBeenCalledWith(
418-
'https://score.snapshot.org',
436+
'https://score.snapshot.org/',
419437
expect.objectContaining({
420438
method: 'POST',
421439
headers: {
@@ -427,21 +445,23 @@ describe('utils', () => {
427445
});
428446

429447
test('can customize the score-api url', () => {
448+
const result = { result: 'OK' };
430449
fetch.mockReturnValue({
431-
json: () => new Promise((resolve) => resolve({ result: 'OK' }))
450+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
432451
});
433452

434453
expect(_getVp({ options: { url: 'https://snapshot.org' } })).resolves;
435454
expect(fetch).toHaveBeenCalledWith(
436-
'https://snapshot.org',
455+
'https://snapshot.org/',
437456
expect.anything()
438457
);
439458
});
440459

441460
test('returns the JSON-RPC result property', () => {
442461
const result = { data: 'OK' };
443462
fetch.mockReturnValue({
444-
json: () => new Promise((resolve) => resolve({ result }))
463+
text: () =>
464+
new Promise((resolve) => resolve(JSON.stringify({ result })))
445465
});
446466

447467
expect(_getVp({})).resolves.toEqual(result);
@@ -452,7 +472,7 @@ describe('utils', () => {
452472
test('rejects with the JSON-RPC error object', () => {
453473
const result = { error: { message: 'Oh no' } };
454474
fetch.mockReturnValue({
455-
json: () => new Promise((resolve) => resolve(result))
475+
text: () => new Promise((resolve) => resolve(JSON.stringify(result)))
456476
});
457477

458478
expect(_getVp({})).rejects.toEqual(result.error);
@@ -463,7 +483,7 @@ describe('utils', () => {
463483
test('rejects with the error', () => {
464484
const result = new Error('Oh no');
465485
fetch.mockReturnValue({
466-
json: () => {
486+
text: () => {
467487
throw result;
468488
}
469489
});

0 commit comments

Comments
 (0)