- 
                Notifications
    You must be signed in to change notification settings 
- Fork 585
Migrate to ofetch #904
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
base: master
Are you sure you want to change the base?
Migrate to ofetch #904
Changes from 11 commits
1c2f3d9
              0304356
              60faabe
              bf0c869
              170b932
              b212168
              a910087
              3dc29cb
              6bc962f
              77a6c2f
              cfd5246
              fc18bab
              836dddf
              bc64032
              137ffa1
              2cb0b93
              925f2b8
              ba1889c
              83981d4
              1610b3d
              305935b
              bfad8f7
              3bb41c9
              3d1dc2a
              dbc44e0
              261a6e4
              58fc436
              520b95e
              96d7df1
              93f8069
              bec2e3a
              b2cc4b2
              cfb0e3e
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import fetch from 'cross-fetch'; | ||
| import { ofetch as fetch } from 'ofetch'; | ||
| import { Interface } from '@ethersproject/abi'; | ||
| import { Contract } from '@ethersproject/contracts'; | ||
| import { isAddress } from '@ethersproject/address'; | ||
|  | @@ -136,34 +136,29 @@ export async function multicall( | |
| } | ||
|  | ||
| export async function subgraphRequest(url: string, query, options: any = {}) { | ||
| const res = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| ...options?.headers | ||
| }, | ||
| body: JSON.stringify({ query: jsonToGraphQLQuery({ query }) }) | ||
| }); | ||
| let responseData: any = await res.text(); | ||
| try { | ||
| responseData = JSON.parse(responseData); | ||
| const init = { | ||
| method: 'POST', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| ...options?.headers | ||
| }, | ||
| timeout: 20e3, | ||
| body: { query: jsonToGraphQLQuery({ query }) } | ||
| }; | ||
|  | ||
| const body = await fetch(url, init); | ||
| return body.data; | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Errors found in subgraphRequest: URL: ${url}, Status: ${ | ||
| res.status | ||
| }, Response: ${responseData.substring(0, 400)}` | ||
| ); | ||
| } | ||
| if (responseData.errors) { | ||
| throw new Error( | ||
| `Errors found in subgraphRequest: URL: ${url}, Status: ${ | ||
| res.status | ||
| }, Response: ${JSON.stringify(responseData.errors).substring(0, 400)}` | ||
| return Promise.reject( | ||
| e.data?.error || { | ||
| code: e.status || 0, | ||
| message: e.statusText || e.toString(), | ||
| data: e.data || '' | ||
| } | ||
| ); | ||
| } | ||
| const { data } = responseData; | ||
| return data || {}; | ||
| } | ||
|  | ||
| export function getUrl(uri, gateway = gateways[0]) { | ||
|  | @@ -186,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).then((res) => res.json()); | ||
| return fetch(url, { | ||
| timeout: 30e3, | ||
| parseResponse: JSON.parse, | ||
|         
                  wa0x6e marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| ...options?.headers | ||
| } | ||
| }); | ||
| } | ||
|  | ||
| export async function ipfsGet( | ||
|  | @@ -195,7 +198,14 @@ export async function ipfsGet( | |
| protocolType = 'ipfs' | ||
| ) { | ||
| const url = `https://${gateway}/${protocolType}/${ipfsHash}`; | ||
| return fetch(url).then((res) => res.json()); | ||
| return fetch(url, { | ||
| timeout: 20e3, | ||
| parseResponse: JSON.parse, | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| }); | ||
| } | ||
|  | ||
| export async function sendTransaction( | ||
|  | @@ -234,25 +244,25 @@ export async function getScores( | |
| strategies, | ||
| addresses | ||
| }; | ||
| const res = await fetch(scoreApiUrl, { | ||
|  | ||
| const body = await fetch(scoreApiUrl, { | ||
| method: 'POST', | ||
| headers: scoreApiHeaders, | ||
| body: JSON.stringify({ params }) | ||
| timeout: 60e3, | ||
|         
                  Todmy marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| body: { params } | ||
| }); | ||
| const obj = await res.json(); | ||
|  | ||
| if (obj.error) { | ||
|         
                  Todmy marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| return Promise.reject(obj.error); | ||
| } | ||
|  | ||
| return options.returnValue === 'all' | ||
| ? obj.result | ||
| : obj.result[options.returnValue || 'scores']; | ||
| ? body.result | ||
| : body.result[options.returnValue || 'scores']; | ||
| } catch (e) { | ||
| if (e.errno) { | ||
| return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); | ||
| } | ||
| return Promise.reject(e); | ||
| return Promise.reject( | ||
| e.data?.error || { | ||
| code: e.status || 0, | ||
| message: e.statusText || e.toString(), | ||
|         
                  Todmy marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| data: e.data || '' | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|  | ||
|  | @@ -270,7 +280,8 @@ export async function getVp( | |
| const init = { | ||
| method: 'POST', | ||
| headers: scoreApiHeaders, | ||
| body: JSON.stringify({ | ||
| timeout: 60e3, | ||
| body: { | ||
| jsonrpc: '2.0', | ||
| method: 'get_vp', | ||
| params: { | ||
|  | @@ -281,19 +292,20 @@ export async function getVp( | |
| space, | ||
| delegation | ||
| } | ||
| }) | ||
| } | ||
| }; | ||
|  | ||
| try { | ||
| const res = await fetch(options.url, init); | ||
| const json = await res.json(); | ||
| if (json.error) return Promise.reject(json.error); | ||
| if (json.result) return json.result; | ||
| 
      Comment on lines
    
      -290
     to 
      -291
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume sometimes a successful response could look like this: {
  "error": "some error"
}Also, consider that the  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, no 200 response should ever return an object with the  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 What do you mean by that ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean after this line it returns nothing( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're reading the wrong commit, the line you're mentioning have been removed | ||
| const body = await fetch(options.url, init); | ||
| return body.result; | ||
| } catch (e) { | ||
| if (e.errno) { | ||
|         
                  Todmy marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); | ||
| } | ||
| return Promise.reject(e); | ||
| return Promise.reject( | ||
| e.data?.error || { | ||
| code: e.status || 0, | ||
| message: e.statusText || e.toString(), | ||
| data: e.data || '' | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|  | ||
|  | @@ -311,7 +323,8 @@ export async function validate( | |
| const init = { | ||
| method: 'POST', | ||
| headers: scoreApiHeaders, | ||
| body: JSON.stringify({ | ||
| timeout: 30e3, | ||
| body: { | ||
| jsonrpc: '2.0', | ||
| method: 'validate', | ||
| params: { | ||
|  | @@ -322,19 +335,20 @@ export async function validate( | |
| snapshot, | ||
| params | ||
| } | ||
| }) | ||
| } | ||
| }; | ||
|  | ||
| try { | ||
| const res = await fetch(options.url, init); | ||
| const json = await res.json(); | ||
| if (json.error) return Promise.reject(json.error); | ||
| return json.result; | ||
| const body = await fetch(options.url, init); | ||
| return body.result; | ||
| } catch (e) { | ||
| if (e.errno) { | ||
| return Promise.reject({ code: e.errno, message: e.toString(), data: '' }); | ||
| } | ||
| return Promise.reject(e); | ||
| return Promise.reject( | ||
| e.data?.error || { | ||
| code: e.status || 0, | ||
| message: e.statusText || e.toString(), | ||
| data: e.data || '' | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|  | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.