-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ง ใๅใๅใใ API ใจใฎ้ไฟก
- Loading branch information
1 parent
98a9af0
commit 2219ebb
Showing
13 changed files
with
129 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
[tools] | ||
node = "21" | ||
bun = "1.1.0" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,60 @@ | ||
/* eslint-disable no-console */ | ||
import type { AppType } from "@api/index"; | ||
import { hc } from "hono/client"; | ||
import { hc, type InferResponseType } from "hono/client"; | ||
import { ResultAsync, err } from "neverthrow"; | ||
import type { ContactFormData } from "./contact"; | ||
|
||
const ENDPOINT = import.meta.env.PUBLIC_CLIENT_API_ENDPOINT; | ||
// const ACCESS_TOKEN = import.meta.env.PUBLIC_CLIENT_API_ACCESS_TOKEN; | ||
const ACCESS_TOKEN = import.meta.env.PUBLIC_CLIENT_API_ACCESS_TOKEN; | ||
|
||
if (ENDPOINT == null) { | ||
throw new Error("PUBLIC_CLIENT_API_ENDPOINT is not defined"); | ||
} | ||
|
||
export const api = hc<AppType>(ENDPOINT); | ||
|
||
type ContactResponse = InferResponseType< | ||
(typeof api)["v1"]["contact"]["$post"] | ||
>; | ||
export function postContactFormData(data: ContactFormData): ResultAsync< | ||
ContactResponse, | ||
{ | ||
code: "NETWORK_ERROR" | "API_ERROR"; | ||
error: Error; | ||
} | ||
> { | ||
return ResultAsync.fromPromise( | ||
api.v1.contact.$post( | ||
{ | ||
json: data, | ||
}, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${ACCESS_TOKEN}`, | ||
}, | ||
}, | ||
), | ||
(e) => { | ||
if (e instanceof Error) { | ||
return { code: "NETWORK_ERROR", error: e } as const; | ||
} | ||
throw e; | ||
}, | ||
).andThen((res) => { | ||
if (!res.ok) { | ||
return err({ | ||
code: "API_ERROR", | ||
error: new Error(res.statusText), | ||
} as const); | ||
} | ||
|
||
return ResultAsync.fromPromise( | ||
res.json(), | ||
() => | ||
({ | ||
code: "API_ERROR", | ||
error: new Error("Failed to parse response"), | ||
}) as const, | ||
).map((json) => json); | ||
}); | ||
} |
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,9 @@ | ||
import type { Result, ResultAsync } from "neverthrow"; | ||
|
||
// ref: neverthrow/dist/index.d.ts L53-56 | ||
export type InferOkTypes<R> = R extends Result<infer T, unknown> ? T : never; | ||
export type InferErrTypes<R> = R extends Result<unknown, infer E> ? E : never; | ||
export type InferAsyncOkTypes<R> = | ||
R extends ResultAsync<infer T, unknown> ? T : never; | ||
export type InferAsyncErrTypes<R> = | ||
R extends ResultAsync<unknown, infer E> ? E : never; |
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