Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dump-validators by not using OpenAPI post-request middleware #949

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion internals/scripts/dump_validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getOasisscanAPIs } from '../../src/vendors/oasisscan'

// Abusing jest to handle typescript and run this
test('dump validators', async () => {
const { getAllValidators } = getOasisscanAPIs('https://api.oasisscan.com/mainnet/')
const { getAllValidators } = getOasisscanAPIs('https://api.oasisscan.com/mainnet')
const validators = await getAllValidators()

const json = {
Expand Down
4 changes: 2 additions & 2 deletions src/vendors/__tests__/throwAPIErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('throwAPIErrors', () => {
jest.spyOn(window, 'fetch').mockImplementationOnce(async () => {
throw new TypeError('Failed to fetch')
})
const fetchFail = getOasisscanAPIs('https://api.oasisscan.com/mainnet/').getAccount('oasis1')
const fetchFail = getOasisscanAPIs('https://api.oasisscan.com/mainnet').getAccount('oasis1')
await expect(fetchFail).rejects.toThrowError(WalletError)
await expect(fetchFail).rejects.toHaveProperty('type', WalletErrors.IndexerAPIError)
})
Expand All @@ -26,7 +26,7 @@ describe('throwAPIErrors', () => {
},
)
})
const http502 = getOasisscanAPIs('https://api.oasisscan.com/mainnet/').getAccount('oasis1')
const http502 = getOasisscanAPIs('https://api.oasisscan.com/mainnet').getAccount('oasis1')
await expect(http502).rejects.toThrowError(WalletError)
await expect(http502).rejects.toHaveProperty('type', WalletErrors.IndexerAPIError)
})
Expand Down
16 changes: 4 additions & 12 deletions src/vendors/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,14 @@ export const throwAPIErrors: ConfigurationParameters = {
fetchApi: async (info, init) => {
try {
const response = await window.fetch(info, init)
// eslint-disable-next-line no-throw-literal
if (response.status < 200 || response.status >= 300) throw `status ${response.status}` // Re-thrown below
return response
} catch (err: any) {
const url = new Request(info).url
throw new WalletError(WalletErrors.IndexerAPIError, `Request failed ${url} with ${err}`, err)
}
},
middleware: [
{
post: async ({ response }) => {
if (response.status < 200 || response.status >= 300) {
throw new WalletError(
WalletErrors.IndexerAPIError,
`Request failed ${response.url} with status ${response.status}`,
)
}
},
},
],
// Don't use post-request middleware! It clones the response. Same issue in Ky:
// https://github.com/sindresorhus/ky/pull/356
}
Loading