Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chowjustin committed Dec 16, 2024
2 parents 56935b6 + de603b0 commit c7f91a6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/dashboard/[id]/modal/editModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function EditModal({
children: (props: ModalReturnType) => JSX.Element;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
data: any;

url: string;
}) {
const { isOpen, onOpen, onClose, onOpenChange } = useDisclosure();
Expand Down
67 changes: 67 additions & 0 deletions src/lib/signerapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import axios, { AxiosError } from "axios";
import { GetServerSidePropsContext } from "next/types";
import Cookies from "universal-cookie";

import { getToken } from "@/lib/cookies";
import { UninterceptedApiError } from "@/types/api";
const context = <GetServerSidePropsContext>{};

export const baseURL = process.env.NEXT_PUBLIC_API_URL_SIGNER;

export const api = axios.create({
baseURL,
headers: {
"Content-Type": "multipart/form-data",
},

withCredentials: false,
});

api.defaults.withCredentials = false;
const isBrowser = typeof window !== "undefined";

api.interceptors.request.use(function (config) {
if (config.headers) {
let token: string | undefined;

if (!isBrowser) {
if (!context)
throw "Api Context not found. You must call `setApiContext(context)` before calling api on server-side";

const cookies = new Cookies(context.req?.headers.cookie);
token = cookies.get("@signify/token");
} else {
token = getToken();
}

config.headers.Authorization = token ? `Bearer ${token}` : "";
}

return config;
});

api.interceptors.response.use(
(config) => {
return config;
},
(error: AxiosError<UninterceptedApiError>) => {
// parse error
if (error.response?.data.message) {
return Promise.reject({
...error,
response: {
...error.response,
data: {
...error.response.data,
message:
typeof error.response.data.message === "string"
? error.response.data.message
: Object.values(error.response.data.message)[0][0],
},
},
});
}
return Promise.reject(error);
},
);
export default api;

0 comments on commit c7f91a6

Please sign in to comment.