Skip to content

Commit

Permalink
Merge pull request #258 from skip-mev/proxy-skip-api-url-and-set-header
Browse files Browse the repository at this point in the history
proxy skip api url and set header authorization key
  • Loading branch information
codingki authored Apr 29, 2024
2 parents 761a4f5 + 95a76f4 commit 49a7f27
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ declare namespace NodeJS {
readonly WALLETCONNECT_VERIFY_KEY?: string;
readonly WORD_PHRASE_KEY?: string;
readonly NEXT_PUBLIC_IS_TESTNET?: boolean;

readonly SKIP_API_KEY?: string;
}
}

Expand Down
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ let nextConfig = {
source: "/api/rpc/(.*)",
destination: "/api/rpc/handler",
},
{
source: "/api/skip/(.*)",
destination: "/api/skip/handler",
},
],
transpilePackages:
process.env.NODE_ENV === "test"
Expand Down
34 changes: 34 additions & 0 deletions src/pages/api/skip/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest } from "next";
import { PageConfig } from "next";

import { API_URL } from "@/constants/api";

export const config: PageConfig = {
api: {
externalResolver: true,
bodyParser: false,
},
runtime: "edge",
};

export default async function handler(req: NextApiRequest) {
try {
const splitter = "/api/skip/";

const [...args] = req.url!.split(splitter).pop()!.split("/");
const uri = [API_URL, ...args].join("/");
const headers = new Headers();
if (process.env.SKIP_API_KEY) {
headers.set("authorization", process.env.SKIP_API_KEY);
}
return fetch(uri, {
body: req.body,
method: req.method,
headers,
});
} catch (error) {
const data = JSON.stringify({ error });
return new Response(data, { status: 500 });
}
}
4 changes: 2 additions & 2 deletions src/solve/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createContext, ReactNode } from "react";
import { WalletClient } from "viem";

import { chainIdToName } from "@/chains/types";
import { API_URL, appUrl } from "@/constants/api";
import { appUrl } from "@/constants/api";
import { trackWallet } from "@/context/track-wallet";
import { config } from "@/lib/wagmi";
import { gracefullyConnect, isWalletClientUsingLedger } from "@/utils/wallet";
Expand All @@ -19,7 +19,7 @@ export function SkipProvider({ children }: { children: ReactNode }) {

const skipClient = new SkipRouter({
clientID: process.env.NEXT_PUBLIC_CLIENT_ID,
apiURL: API_URL,
apiURL: `${appUrl}/api/skip`,
getCosmosSigner: async (chainID) => {
const chainName = chainIdToName[chainID];
if (!chainName) {
Expand Down

0 comments on commit 49a7f27

Please sign in to comment.