-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from dnd-side-project/feature/70-fetch-to-ky
Replace Fetch -> Ky
- Loading branch information
Showing
93 changed files
with
339 additions
and
307 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,66 @@ | ||
import type { KyRequest, NormalizedOptions } from "ky"; | ||
import ky from "ky"; | ||
|
||
import { env } from "@/env"; | ||
import { getClientSideSession } from "@/lib/session"; | ||
|
||
import { getServerSideSession } from "./auth/auth"; | ||
import { FiestaResponse } from "./instance"; | ||
|
||
const addAuthTokenToHeader = async ( | ||
_request: KyRequest, | ||
_options: NormalizedOptions, | ||
) => { | ||
const isServer = typeof window === "undefined"; | ||
|
||
const session = isServer | ||
? await getServerSideSession() | ||
: await getClientSideSession(); | ||
|
||
if (session) { | ||
_request.headers.set("Authorization", `Bearer ${session.accessToken}`); | ||
} | ||
|
||
return _request; | ||
}; | ||
|
||
const createKyInstance = () => { | ||
return ky.create({ | ||
prefixUrl: env.NEXT_PUBLIC_BASE_URL, | ||
hooks: { | ||
beforeRequest: [addAuthTokenToHeader], | ||
}, | ||
retry: { | ||
limit: 2, | ||
methods: ["get", "post", "put", "patch", "delete"], | ||
statusCodes: [408, 413, 429, 500, 502, 503, 504], | ||
}, | ||
}); | ||
}; | ||
|
||
const Instance = createKyInstance(); | ||
|
||
const FiestaInstance = { | ||
get: <T>(url: string, options?: RequestInit) => | ||
Instance.get(url, options) | ||
.json<FiestaResponse<T>>() | ||
.then((res) => res.data), | ||
post: <T>(url: string, json?: unknown, options?: RequestInit) => | ||
Instance.post(url, { json, ...options }) | ||
.json<FiestaResponse<T>>() | ||
.then((res) => res.data), | ||
put: <T>(url: string, json?: unknown, options?: RequestInit) => | ||
Instance.put(url, { json, ...options }) | ||
.json<FiestaResponse<T>>() | ||
.then((res) => res.data), | ||
patch: <T>(url: string, json?: unknown, options?: RequestInit) => | ||
Instance.patch(url, { json, ...options }) | ||
.json<FiestaResponse<T>>() | ||
.then((res) => res.data), | ||
delete: <T>(url: string, options?: RequestInit) => | ||
Instance.delete(url, options) | ||
.json<FiestaResponse<T>>() | ||
.then((res) => res.data), | ||
}; | ||
|
||
export default FiestaInstance; |
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
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
Oops, something went wrong.