Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1c2f3d9
chore: migrate fetch lib from cross-fetch to ofetch
wa0x6e Sep 20, 2023
0304356
feat: add timeout to fetch requests
wa0x6e Sep 21, 2023
60faabe
chore: fix tests
wa0x6e Sep 21, 2023
bf0c869
Merge branch 'master' into migrate-ofetch
wa0x6e Sep 21, 2023
170b932
chore: add more tests
wa0x6e Sep 21, 2023
b212168
chore: add more test
wa0x6e Sep 21, 2023
a910087
Merge branch 'master' into migrate-ofetch
wa0x6e Sep 22, 2023
3dc29cb
chore: remove uneeded JSON.stringify
wa0x6e Sep 22, 2023
6bc962f
Merge branch 'migrate-ofetch' of https://github.com/snapshot-labs/sna…
wa0x6e Sep 22, 2023
77a6c2f
chore: split long line
wa0x6e Sep 22, 2023
cfd5246
fix: handle errors when parsing json response
wa0x6e Sep 22, 2023
fc18bab
fix: migrate fetch request to ofetch API
wa0x6e Sep 22, 2023
836dddf
chore: bump version
wa0x6e Sep 25, 2023
bc64032
chore: add more todo test
wa0x6e Sep 27, 2023
137ffa1
chore: rename test function
wa0x6e Sep 27, 2023
2cb0b93
chore: use .js extension for test files
wa0x6e Sep 27, 2023
925f2b8
chore: improve/add more tests
wa0x6e Sep 27, 2023
ba1889c
chore: add more tests
wa0x6e Sep 28, 2023
83981d4
Merge branch 'master' into migrate-ofetch
wa0x6e Sep 28, 2023
1610b3d
chore: fix tests
wa0x6e Sep 28, 2023
305935b
chore: fix tests
wa0x6e Sep 28, 2023
bfad8f7
fix: add more tests
wa0x6e Sep 28, 2023
3bb41c9
chore: fix tests
wa0x6e Sep 28, 2023
3d1dc2a
chore: remove redundant function calls
wa0x6e Sep 29, 2023
dbc44e0
chore: fix test
wa0x6e Sep 29, 2023
261a6e4
chore: add retry to flaky tests
wa0x6e Sep 29, 2023
58fc436
chore: improve tests
wa0x6e Sep 29, 2023
520b95e
Merge branch 'master' into migrate-ofetch
wa0x6e Oct 1, 2023
96d7df1
Merge branch 'master' into migrate-ofetch
wa0x6e Oct 2, 2023
93f8069
chore: fix tests for node 18
wa0x6e Oct 3, 2023
bec2e3a
Merge branch 'master' into migrate-ofetch
ChaituVR Oct 5, 2023
b2cc4b2
Merge branch 'master' into migrate-ofetch
wa0x6e Oct 9, 2023
cfb0e3e
chore: bump feat version instead of major
wa0x6e Oct 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/sign/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ export default class Client {
'Content-Type': 'application/json'
},
timeout: 20e3,
body: JSON.stringify(envelop)
body: envelop
};
return new Promise((resolve, reject) => {
fetch(address, init)
.then((res) => {
if (res.ok) return resolve(res.json());
throw res;
})
.catch((e) => e.json().then((json) => reject(json)));
});

try {
return await fetch(address, init);
} catch (e) {
const isSequencerError =
e.data?.hasOwnProperty('error') &&
e.data?.hasOwnProperty('error_description');
return Promise.reject(isSequencerError ? e.data : e);
}
}

async space(web3: Web3Provider | Wallet, address: string, message: Space) {
Expand Down
36 changes: 27 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ export async function subgraphRequest(url: string, query, options: any = {}) {
body: { query: jsonToGraphQLQuery({ query }) }
};

return (await fetch(url, init)).data;
const body = await fetch(url, init);
return body.data;
} catch (e) {
return Promise.reject(
e.data?.error || {
Expand Down Expand Up @@ -180,7 +181,15 @@ export function getUrl(uri, gateway = gateways[0]) {

export async function getJSON(uri, options: any = {}) {
const url = getUrl(uri, options.gateways);
return fetch(url, { timeout: 30e3 }).then((res) => res.json());
return fetch(url, {
timeout: 30e3,
parseResponse: JSON.parse,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...options?.headers
}
});
}

export async function ipfsGet(
Expand All @@ -189,7 +198,14 @@ export async function ipfsGet(
protocolType = 'ipfs'
) {
const url = `https://${gateway}/${protocolType}/${ipfsHash}`;
return fetch(url, { timeout: 20e3 }).then((res) => res.json());
return fetch(url, {
timeout: 20e3,
parseResponse: JSON.parse,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
}

export async function sendTransaction(
Expand Down Expand Up @@ -229,16 +245,16 @@ export async function getScores(
addresses
};

const res = await fetch(scoreApiUrl, {
const body = await fetch(scoreApiUrl, {
method: 'POST',
headers: scoreApiHeaders,
timeout: 60e3,
body: JSON.stringify({ params })
body: { params }
});

return options.returnValue === 'all'
? res.result
: res.result[options.returnValue || 'scores'];
? body.result
: body.result[options.returnValue || 'scores'];
} catch (e) {
return Promise.reject(
e.data?.error || {
Expand Down Expand Up @@ -280,7 +296,8 @@ export async function getVp(
};

try {
return (await fetch(options.url, init)).result;
const body = await fetch(options.url, init);
return body.result;
} catch (e) {
return Promise.reject(
e.data?.error || {
Expand Down Expand Up @@ -322,7 +339,8 @@ export async function validate(
};

try {
return (await fetch(options.url, init)).result;
const body = await fetch(options.url, init);
return body.result;
} catch (e) {
return Promise.reject(
e.data?.error || {
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/sign/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, test, expect } from 'vitest';
import Client from '../../../src/sign/';

describe('Client', () => {
describe('send()', () => {
describe('on error', () => {
const payload = { address: '', sig: '', data: '' };

test('should return the error from sequencer as a Promise rejection', async () => {
expect.assertions(1);
const client = new Client();
await expect(client.send(payload)).to.rejects.toEqual({
error: 'client_error',
error_description: 'wrong envelope format'
});
});

test.each([
['ENOTFOUND', 'https://unknown.snapshot.org'],
['404 Not Found', 'https://httpstat.us/404']
])(
'should return the network error (%s) as Promise rejection',
async (code, url) => {
expect.assertions(1);
const client = new Client(url);
await expect(() => client.send(payload)).rejects.toThrowError(code);
}
);
});
});
});
63 changes: 58 additions & 5 deletions test/e2e/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, describe } from 'vitest';
import { getScores, getVp, validate } from '../../src/utils';
import { getScores, getVp, validate, getJSON, ipfsGet } from '../../src/utils';

describe('getScores', () => {
test('should return a score', async () => {
Expand All @@ -22,7 +22,7 @@ describe('getScores', () => {
'0xeF8305E140ac520225DAf050e2f71d5fBcC543e7': 0.041582733391515345
}
]);
});
}, 20e3);

test('should return a promise rejection on error from score-api', async () => {
expect.assertions(1);
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('getScores', () => {
test('should return a promise rejection with JSON-RPC format on network error (not found)', async () => {
expect.assertions(1);
await expect(
getScores('test.eth', [], '1', [''], 'latest', 'https://google.com')
getScores('test.eth', [], '1', [''], 'latest', 'https://hub.snapshot.org')
).to.rejects.toEqual(
expect.objectContaining({
code: 404
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('getVp', () => {
expect.assertions(1);
await expect(
getVp(...defaultOptions, {
url: 'https://google.com'
url: 'https://httpstat.us/405'
})
).to.rejects.toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('validate', () => {
expect.assertions(1);
await expect(
validate(...defaultOptions, {
url: 'https://google.com'
url: 'https://httpstat.us/405'
})
).to.rejects.toEqual(
expect.objectContaining({
Expand All @@ -197,3 +197,56 @@ describe('validate', () => {
);
});
});

describe('getJSON', () => {
test('should return a JSON', async () => {
expect.assertions(1);
expect(await getJSON('https://hub.snapshot.org')).toEqual(
expect.objectContaining({ name: 'snapshot-hub' })
);
});

test('should throw an error when the response is not a JSON file', async () => {
expect.assertions(1);
await expect(() => getJSON('https://snapshot.org')).rejects.toThrowError(
/Unexpected.*JSON/
);
});

test('should throw an error on network error (no response)', async () => {
expect.assertions(1);
await expect(() =>
getJSON('https://score-null.snapshot.org')
).rejects.toThrowError('ENOTFOUND');
});

test('should throw an error on network error (not found)', async () => {
expect.assertions(1);
await expect(() => getJSON('https://httpstat.us/405')).rejects.toThrowError(
'405 Method Not Allowed'
);
});
});

describe('ipfsGet', () => {
const cid = 'bafkreibatgmdqdxsair3j52zfhtntegshtypq2qbex3fgtorwx34kzippe';

test('should return a JSON', async () => {
expect.assertions(1);
expect(await ipfsGet('pineapple.fyi', cid)).toEqual({ name: 'Vitalik' });
});

test('should throw an error on network error (no response)', async () => {
expect.assertions(1);
await expect(() =>
ipfsGet('score-null.snapshot.org', cid)
).rejects.toThrowError('ENOTFOUND');
});

test('should throw an error on network error (not found)', async () => {
expect.assertions(1);
await expect(() => ipfsGet('httpstat.us/404', cid)).rejects.toThrowError(
'404 Not Found'
);
});
});