generated from multiversx/mx-template-dapp
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Work on gateway adapter * Logging in working * Create map * Edit configs * Abstract processing logic * Remove logs * Remove comment * Integrate network config * Cancel some requests * Minor fixes * Adapter for transactions working * Update CHANGELOG
- Loading branch information
1 parent
0e01b94
commit eb35bb6
Showing
18 changed files
with
356 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { EnvironmentsEnum } from 'types'; | ||
|
||
export * from './sharedConfig'; | ||
|
||
export const API_URL = ''; // either GATEWAY_URL or API_URL must be set | ||
export const GATEWAY_URL = 'https://devnet-gateway.multiversx.com'; | ||
export const sampleAuthenticatedDomains = [API_URL]; | ||
export const environment = EnvironmentsEnum.devnet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './sharedConfig'; | ||
|
||
export const API_URL = 'https://devnet-api.multiversx.com'; // replace here with actual sovereign URL | ||
export const GATEWAY_URL = ''; // either GATEWAY_URL or API_URL must be set | ||
export const sampleAuthenticatedDomains = [API_URL]; | ||
export const environment = 'sovereign'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import axios from 'axios'; | ||
import { API_URL, GATEWAY_URL } from 'config'; | ||
import { getGatewayConfigForCurrentRequest } from './helpers/getGatewayConfigForCurrentRequest'; | ||
import { getGatewayResponse } from './helpers/getGatewayResponse'; | ||
|
||
axios.interceptors.request.use( | ||
function (config) { | ||
if (!config.url || (API_URL && !GATEWAY_URL)) { | ||
return config; | ||
} | ||
|
||
const newConfig = getGatewayConfigForCurrentRequest(config); | ||
|
||
return newConfig; | ||
}, | ||
function (error) { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
axios.interceptors.response.use( | ||
async function (response) { | ||
const { config } = response; | ||
|
||
const isGatewayRequest = config.baseURL === GATEWAY_URL; | ||
const url = config.url || ''; | ||
|
||
if (!isGatewayRequest || !url) { | ||
return response; | ||
} | ||
|
||
const gatewayResponse = await getGatewayResponse(url, response); | ||
|
||
return gatewayResponse; | ||
}, | ||
function (error) { | ||
return Promise.reject(error); | ||
} | ||
); |
34 changes: 34 additions & 0 deletions
34
src/utils/adapter/gatewayAdapter/helpers/apiToGatewayEndpointMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
ACCOUNTS_ENDPOINT, | ||
ADDRESS_ENDPOINT, | ||
TRANSACTIONS_ENDPOINT, | ||
NETWORK_CONFIG_ENDPOINT, | ||
NFTS_ENDPOINT, | ||
TOKENS_ENDPOINT | ||
} from 'localConstants/sdkDapp'; | ||
|
||
export const gatewayEndpoints = { | ||
[ADDRESS_ENDPOINT]: ADDRESS_ENDPOINT, | ||
[NETWORK_CONFIG_ENDPOINT]: NETWORK_CONFIG_ENDPOINT, | ||
[TRANSACTIONS_ENDPOINT]: TRANSACTIONS_ENDPOINT, | ||
[TOKENS_ENDPOINT]: null, | ||
[NFTS_ENDPOINT]: null | ||
}; | ||
|
||
export const endpointMap = { | ||
[ACCOUNTS_ENDPOINT]: gatewayEndpoints[ADDRESS_ENDPOINT], | ||
[NETWORK_CONFIG_ENDPOINT]: gatewayEndpoints[NETWORK_CONFIG_ENDPOINT], | ||
[TRANSACTIONS_ENDPOINT]: gatewayEndpoints[TRANSACTIONS_ENDPOINT], | ||
// not configured | ||
[TOKENS_ENDPOINT]: gatewayEndpoints[TOKENS_ENDPOINT], | ||
[NFTS_ENDPOINT]: gatewayEndpoints[NFTS_ENDPOINT] | ||
}; | ||
|
||
export const apiRoutes: Record<keyof typeof endpointMap, string> = { | ||
[ACCOUNTS_ENDPOINT]: `/${ACCOUNTS_ENDPOINT}/:id`, | ||
[NETWORK_CONFIG_ENDPOINT]: `/${gatewayEndpoints[NETWORK_CONFIG_ENDPOINT]}`, | ||
[TRANSACTIONS_ENDPOINT]: `/${gatewayEndpoints[TRANSACTIONS_ENDPOINT]}`, | ||
// not configured | ||
[TOKENS_ENDPOINT]: `/${ACCOUNTS_ENDPOINT}/:id/${TOKENS_ENDPOINT}`, | ||
[NFTS_ENDPOINT]: `/${ACCOUNTS_ENDPOINT}/:id/${NFTS_ENDPOINT}` | ||
}; |
24 changes: 24 additions & 0 deletions
24
src/utils/adapter/gatewayAdapter/helpers/arraybufferToJSON.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AxiosResponse } from 'axios'; | ||
|
||
export const arraybufferToJSON = async <T extends AxiosResponse>( | ||
response: T | ||
) => { | ||
const needsParsing = response.config.responseType === 'arraybuffer'; | ||
|
||
if (!needsParsing) { | ||
return response.data; | ||
} | ||
|
||
const decoder = new TextDecoder('utf-8'); | ||
const text = decoder.decode(new Uint8Array(response.data)); | ||
|
||
try { | ||
const data = JSON.parse(text); | ||
return data; | ||
} catch (e) { | ||
// Handle JSON parse error if needed | ||
return Promise.reject( | ||
new Error('Failed to parse JSON from arraybuffer response.') | ||
); | ||
} | ||
}; |
69 changes: 69 additions & 0 deletions
69
src/utils/adapter/gatewayAdapter/helpers/getGatewayConfigForCurrentRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import axios, { InternalAxiosRequestConfig } from 'axios'; | ||
import { API_URL, GATEWAY_URL } from 'config'; | ||
import { matchPath } from 'types/sdkDapp.types'; | ||
import { apiRoutes, endpointMap } from './apiToGatewayEndpointMap'; | ||
|
||
export const getGatewayConfigForCurrentRequest = ( | ||
config: InternalAxiosRequestConfig<any> | ||
): InternalAxiosRequestConfig<any> => { | ||
const newConfig = config; | ||
|
||
const needsGateway = | ||
config.baseURL?.startsWith(API_URL) || config.url?.startsWith(API_URL); | ||
|
||
const isGatewayRequest = | ||
needsGateway && | ||
Object.keys(endpointMap).some((key) => config.url?.includes(key)); | ||
|
||
if (!isGatewayRequest) { | ||
return config; | ||
} | ||
|
||
config.baseURL = GATEWAY_URL; | ||
const configUrl = String(config.url); | ||
|
||
let url = configUrl.startsWith(API_URL) | ||
? configUrl.replace(API_URL, '') | ||
: configUrl; | ||
|
||
if ( | ||
newConfig.method?.toLowerCase() === 'post' && | ||
newConfig.url?.endsWith('transactions') | ||
) { | ||
newConfig.url = '/transaction/send'; | ||
return newConfig; | ||
} | ||
|
||
Object.entries(endpointMap).forEach(([key, value]) => { | ||
const matchesPath = matchPath( | ||
apiRoutes[key as keyof typeof apiRoutes], | ||
url | ||
); | ||
|
||
const needsReplacement = Boolean(matchesPath); | ||
|
||
if (!needsReplacement) { | ||
return; | ||
} | ||
|
||
if (key.includes('transactions') && url.includes('transactions')) { | ||
const hash = config.params?.hashes; | ||
url = `/transaction/${hash}`; | ||
} else { | ||
url = url.replace(`/${key}`, `/${value}`); | ||
} | ||
|
||
newConfig.url = url; | ||
|
||
if (value === null) { | ||
const source = axios.CancelToken.source(); | ||
newConfig.cancelToken = source.token; | ||
// Cancel the request | ||
source.cancel( | ||
`Request canceled: ${key} cannot be fetched from the gateway` | ||
); | ||
} | ||
}); | ||
|
||
return newConfig; | ||
}; |
50 changes: 50 additions & 0 deletions
50
src/utils/adapter/gatewayAdapter/helpers/getGatewayResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { AxiosResponse } from 'axios'; | ||
import { NETWORK_CONFIG_ENDPOINT } from 'localConstants'; | ||
import { matchPath } from 'types/sdkDapp.types'; | ||
import { gatewayEndpoints } from './apiToGatewayEndpointMap'; | ||
import { arraybufferToJSON } from './arraybufferToJSON'; | ||
import { jsonToArrayBuffer } from './jsonToArrayBuffer'; | ||
|
||
export const getGatewayResponse = async ( | ||
gatewayUrl: string, | ||
response: AxiosResponse<any, any> | ||
): Promise<AxiosResponse<any, any>> => { | ||
if (gatewayUrl.includes('/transaction/send')) { | ||
const transaction = await arraybufferToJSON(response); | ||
|
||
return { | ||
...response, | ||
data: jsonToArrayBuffer(transaction.data) | ||
}; | ||
} | ||
|
||
if (gatewayUrl.includes(`/${gatewayEndpoints.address}`)) { | ||
const account = await arraybufferToJSON(response); | ||
|
||
return { | ||
...response, | ||
data: jsonToArrayBuffer(account.data.account) | ||
}; | ||
} | ||
|
||
if (gatewayUrl.includes(`/${gatewayEndpoints[NETWORK_CONFIG_ENDPOINT]}`)) { | ||
const networkConfig = await arraybufferToJSON(response); | ||
return { | ||
...response, | ||
data: jsonToArrayBuffer(networkConfig.data.config) | ||
}; | ||
} | ||
|
||
const isFetchTransaction = matchPath('/transaction/:hash', gatewayUrl); | ||
|
||
if (Boolean(isFetchTransaction)) { | ||
const data = await arraybufferToJSON(response); | ||
|
||
return { | ||
...response, | ||
data: [{ ...data.data.transaction, txHash: data.data.transaction.hash }] | ||
}; | ||
} | ||
|
||
return response; | ||
}; |
6 changes: 6 additions & 0 deletions
6
src/utils/adapter/gatewayAdapter/helpers/jsonToArrayBuffer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const jsonToArrayBuffer = (json: Record<string, unknown>) => { | ||
const jsonString = JSON.stringify(json); | ||
const encoder = new TextEncoder(); | ||
const arrayBuffer = encoder.encode(jsonString); | ||
return arrayBuffer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './gatewayAdapter'; |
Oops, something went wrong.