Skip to content
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

feat: added header helpers #20

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/solana-actions/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ export const MEMO_PROGRAM_ID = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr";
*/
export const BLINKS_QUERY_PARAM = "action";

/**
* Blockchain IDs for Solana from CAIP
*
* @see https://namespaces.chainagnostic.org/solana/caip10
*/
export const BLOCKCHAIN_IDS = {
mainnet: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
devnet: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
testnet: "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",
};

/**
* Standard headers for use within frameworks that use the native `HeadersInit` (like NextJS)
*
* Note: `Access-Control-Allow-Origin=*` should ONLY be set on your Actions API routes and `actions.json`.
* Setting "allow origin to any" on other routes on your server is bad practice and should be avoided.
*/
export const ACTIONS_CORS_HEADERS: HeadersInit = {
export const ACTIONS_CORS_HEADERS: Record<string, string> = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,POST,PUT,OPTIONS",
"Access-Control-Allow-Headers":
"Content-Type, Authorization, Content-Encoding, Accept-Encoding",
"Content-Type, Authorization, Content-Encoding, Accept-Encoding, X-Action-Version, X-Blockchain-Ids",
"Content-Type": "application/json",
};

Expand All @@ -42,5 +53,7 @@ export const ACTIONS_CORS_HEADERS_MIDDLEWARE = {
"Authorization",
"Content-Encoding",
"Accept-Encoding",
"X-Action-Version",
"X-Blockchain-Ids",
],
};
1 change: 1 addition & 0 deletions packages/solana-actions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./types.js";
export * from "./constants.js";
export * from "./utils.js";

export * from "./encodeURL.js";
export * from "./parseURL.js";
Expand Down
41 changes: 41 additions & 0 deletions packages/solana-actions/src/utils.ts
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();
};
}
1 change: 1 addition & 0 deletions packages/solana-actions/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"include": [],
"compilerOptions": {
"lib": ["esnext", "dom"],
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
Expand Down