This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to use custom axios `interceptor` for sending 'x-client-id' headers - [x] Helpers `add9Rheader` / `register9R` (main + renderer thread) - [x] Use patched `@cosmos-client/core` https://github.com/veado/cosmos-client-ts.git#patch/v0.45.13 - [x] Move `ASGARDEX_IDENTIFIER` to `shared/const`
- Loading branch information
Showing
10 changed files
with
67 additions
and
37 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
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,40 @@ | ||
import { cosmosclient } from '@cosmos-client/core' | ||
import axios from 'axios' | ||
|
||
import { ASGARDEX_IDENTIFIER, NINE_REALMS_CLIENT_HEADER } from '../const' | ||
|
||
const { | ||
config: { globalAxios } | ||
} = cosmosclient | ||
|
||
/** | ||
* Middleware to add custom header to requests (9R endpoints only) | ||
* | ||
* @param request RequestArgs (rxjs/ajax) | AxiosRequestConfig (axios) | ||
* @returns RequestArgs (rxjs/ajax) | AxiosRequestConfig (axios) | ||
*/ | ||
export const add9Rheader = <T extends { url?: string; headers?: Object }>(request: T) => { | ||
try { | ||
// URL throws an `TypeError` if `url` is not available and 'unknown-url' is set | ||
// [TypeError: Invalid URL] input: 'unknown-url', code: 'ERR_INVALID_URL' } | ||
const url = new URL(request?.url ?? 'unknown-url') | ||
if (url.host.includes('ninerealms')) { | ||
const headers = request?.headers ?? {} | ||
// Add custom header to request before returning it | ||
return { ...request, headers: { ...headers, [`${NINE_REALMS_CLIENT_HEADER}`]: `${ASGARDEX_IDENTIFIER}` } } | ||
} | ||
} catch (error) { | ||
console.error(`Failed to add custom ${NINE_REALMS_CLIENT_HEADER} header`, error) | ||
} | ||
|
||
// If it errors, just return same request and keep it untouched (no change) | ||
return request | ||
} | ||
|
||
/** | ||
* Adds custom header to axios requests (9R endpoints only) | ||
*/ | ||
export const register9Rheader = () => { | ||
axios.interceptors.request.use(add9Rheader, (error) => Promise.reject(error)) | ||
globalAxios.interceptors.request.use(add9Rheader, (error) => Promise.reject(error)) | ||
} |
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