-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ff3898
commit 9da3b45
Showing
3 changed files
with
57 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ACTIONS_CORS_HEADERS, BLOCKCHAIN_IDS } from "./constants.js"; | ||
|
||
type HeaderHelperArgs = { | ||
headers?: typeof ACTIONS_CORS_HEADERS; | ||
chainId?: keyof typeof BLOCKCHAIN_IDS | string; | ||
actionVersion?: string | number; | ||
}; | ||
|
||
/** | ||
* Construct valid headers for use with Action APIs | ||
*/ | ||
export function actionHeaders({ | ||
headers, | ||
chainId, | ||
actionVersion, | ||
}: HeaderHelperArgs) { | ||
if (chainId) { | ||
headers = Object.assign({}, headers || {}, { | ||
"X-Blockchain-Ids": Object.hasOwn(BLOCKCHAIN_IDS, chainId) | ||
? BLOCKCHAIN_IDS[chainId as keyof typeof BLOCKCHAIN_IDS] | ||
: chainId, | ||
}); | ||
} | ||
if (actionVersion) { | ||
headers = Object.assign({}, headers || {}, { | ||
"X-Action-Version": actionVersion.toString(), | ||
}); | ||
} | ||
if (headers) return Object.assign({}, ACTIONS_CORS_HEADERS, headers); | ||
else return ACTIONS_CORS_HEADERS; | ||
} | ||
|
||
/** | ||
* Middleware to enable proper CORS headers for Action APIs | ||
*/ | ||
export function actionCorsMiddleware(args: HeaderHelperArgs) { | ||
return (_req: any, res: any, next: Function) => { | ||
res.set(actionHeaders(args)); | ||
next(); | ||
}; | ||
} |