Skip to content

Commit

Permalink
feat: added helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfrosty committed Aug 2, 2024
1 parent 7ff3898 commit 9da3b45
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
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();
};
}

0 comments on commit 9da3b45

Please sign in to comment.