@@ -2,11 +2,9 @@ import { useQueryClient } from '@tanstack/react-query';
22import { omit , pickBy } from 'es-toolkit' ;
33import React from 'react' ;
44
5- import type { ApiName } from './types' ;
65import type { CsrfData } from 'types/client/account' ;
76import type { ChainConfig } from 'types/multichain' ;
87
9- import config from 'configs/app' ;
108import isBodyAllowed from 'lib/api/isBodyAllowed' ;
119import isNeedProxy from 'lib/api/isNeedProxy' ;
1210import { getResourceKey } from 'lib/api/useApiQuery' ;
@@ -18,22 +16,6 @@ import buildUrl from './buildUrl';
1816import getResourceParams from './getResourceParams' ;
1917import type { ResourceName , ResourcePathParams } from './resources' ;
2018
21- function needCredentials ( apiName : ApiName ) {
22- if ( ! [ 'general' ] . includes ( apiName ) ) {
23- return false ;
24- }
25-
26- // currently, the cookies are used only for the following features
27- if (
28- config . features . account . isEnabled ||
29- config . UI . views . token . hideScamTokensEnabled
30- ) {
31- return true ;
32- }
33-
34- return false ;
35- }
36-
3719export interface Params < R extends ResourceName > {
3820 pathParams ?: ResourcePathParams < R > ;
3921 queryParams ?: Record < string , string | Array < string > | number | boolean | undefined | null > ;
@@ -53,21 +35,46 @@ export default function useApiFetch() {
5335 { pathParams, queryParams, fetchParams, logError, chain } : Params < R > = { } ,
5436 ) => {
5537 const apiToken = cookies . get ( cookies . NAMES . API_TOKEN ) ;
38+ const apiTempToken = cookies . get ( cookies . NAMES . API_TEMP_TOKEN ) ;
39+ const showScamTokens = cookies . get ( cookies . NAMES . SHOW_SCAM_TOKENS ) === 'true' ;
40+
5641 const { api, apiName, resource } = getResourceParams ( resourceName , chain ) ;
5742 const url = buildUrl ( resourceName , pathParams , queryParams , undefined , chain ) ;
5843 const withBody = isBodyAllowed ( fetchParams ?. method ) ;
5944 const headers = pickBy ( {
6045 'x-endpoint' : isNeedProxy ( ) ? api . endpoint : undefined ,
6146 Authorization : [ 'admin' , 'contractInfo' ] . includes ( apiName ) ? apiToken : undefined ,
62- 'x-csrf-token' : withBody && csrfToken ? csrfToken : undefined ,
47+ ...( apiName === 'general' ? {
48+ 'api-v2-temp-token' : apiTempToken ,
49+ 'show-scam-tokens' : showScamTokens ? 'true' : undefined ,
50+ 'x-csrf-token' : withBody && csrfToken ? csrfToken : undefined ,
51+ } : { } ) ,
6352 ...resource . headers ,
6453 ...fetchParams ?. headers ,
6554 } , Boolean ) as HeadersInit ;
6655
6756 return fetch < SuccessType , ErrorType > (
6857 url ,
6958 {
70- credentials : needCredentials ( apiName ) ? 'include' : 'same-origin' ,
59+ // Things to remember:
60+ //
61+ // A: Currently, we use only one API-related cookie, "_explorer_key," which is for the account feature.
62+ // We include credentials only for core API requests.
63+ // Note that some APIs may share the same origin with the core API, but they don't require credentials (e.g the Stats API).
64+ //
65+ // B: We cannot limit the routes for which credentials should be sent exclusively to the "/account/**" routes.
66+ // This is because the watchlist names and private tags preloading will not function on the API side.
67+ // Therefore, we include credentials for all core API routes.
68+ //
69+ // C: We cannot include credentials in cross-origin requests.
70+ // In this case, we must explicitly list all the origins allowed to make requests in the "Access-Control-Allow-Origin" header,
71+ // which is challenging for our devops and backend teams. Thus, we do not use the "include" option here.
72+ // And because of this, the account feature in cross-origin setup will not work.
73+ //
74+ // Considering all of the above, we use:
75+ // - The "same-origin" option for all core API requests
76+ // - The "omit" option for all other requests
77+ credentials : apiName === 'general' ? 'same-origin' : 'omit' ,
7178 headers,
7279 ...( fetchParams ? omit ( fetchParams , [ 'headers' ] ) : { } ) ,
7380 } ,
0 commit comments