Skip to content

Commit

Permalink
Merge branch 'main' into feat/api/notify-newcomer
Browse files Browse the repository at this point in the history
  • Loading branch information
wiyco authored Nov 27, 2024
2 parents 4c50bad + 0ec3d30 commit b29b333
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function RootLayout({
children: React.ReactNode;
}>) {
return (
<html>
<html suppressHydrationWarning>
<body>
<Providers
nextThemeProviderProps={{
Expand Down
3 changes: 3 additions & 0 deletions src/constants/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const MARKDOWN_DIR_PATH = process.env.MARKDOWN_DIR_PATH || "markdowns";
export const MARKDOWN_FILE_EXTENSION = "md";

export const OCCUPATIONAL_STATUS = [
"IPUT_TOKYO_STUDENT",
"COCOON_TOWER_STUDENT",
Expand Down
1 change: 0 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./config";
export * from "./languages";
export * from "./metadata";
export * from "./paths";
2 changes: 0 additions & 2 deletions src/constants/paths.ts

This file was deleted.

35 changes: 31 additions & 4 deletions src/server/api/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import { z } from "zod";
/**
* APIレスポンス (エラー)
*/
export type APIErrorResponse = {
type: "error";
error: {
message: string;
code?: string;
details?: unknown;
};
status: number;
};

/**
* [JavaScript `Error()` constructor](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) を拡張したAPIエラー用のクラス
* @example
* ```ts
* try {
* if (true) {
* // カスタムエラーメッセージのレスポンスを返す
* throw new APIError("エラーメッセージ", 403, "ERROR_CODE");
* }
* } catch (error) {
* return handleAPIError(error);
* // => { type: "error", error: { message: "エラーメッセージ", code: "ERROR_CODE" }, status: 403 }
* }
* ```
*/
export class APIError extends Error {
constructor(
message: string,
Expand All @@ -25,16 +50,17 @@ export class APIError extends Error {
* // API処理
* } catch (error) {
* return handleAPIError(error);
* // APIError => { error: { message: error.message, code: error.code }, status: error.status }
* // ZodError => { error: { message: "入力値が不正です", details: error.errors }, status: 400 }
* // OtherError => { error: { message: "サーバーエラーが発生しました" }, status: 500 }
* // APIError => { type: "error", error: { message: error.message, code: error.code }, status: error.status }
* // ZodError => { type: "error", error: { message: "入力値が不正です", details: error.errors }, status: 400 }
* // OtherError => { type: "error", error: { message: "サーバーエラーが発生しました" }, status: 500 }
* }
* ```
*/
export function handleAPIError(error: unknown) {
if (error instanceof APIError) {
return NextResponse.json(
{
type: "error",
error: {
message: error.message,
code: error.code,
Expand All @@ -47,6 +73,7 @@ export function handleAPIError(error: unknown) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{
type: "error",
error: {
message: "入力値が不正です",
details: error.errors,
Expand All @@ -58,7 +85,7 @@ export function handleAPIError(error: unknown) {

console.error(error);
return NextResponse.json(
{ error: { message: "サーバーエラーが発生しました" } },
{ type: "error", error: { message: "サーバーエラーが発生しました" } },
{ status: 500 }
);
}
12 changes: 6 additions & 6 deletions src/server/api/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { NextResponse } from "next/server";
/**
* APIレスポンス (成功)
*/
export type APIResponse<T> = {
export type APISuccessResponse<T> = {
type: "success";
data: T;
status: number;
};
Expand All @@ -15,12 +16,11 @@ export type APIResponse<T> = {
* @example
* ```ts
* const response = createAPIResponse({ message: "Hello, World!" });
* console.log(response);
* // => { data: { message: "Hello, World!" }, status: 200 }
* // => { type: "success", data: { message: "Hello, World!" }, status: 200 }
* ```
*/
export function createAPIResponse<T>(data: T): APIResponse<T> {
return { data, status: 200 };
export function createAPIResponse<T>(data: T): APISuccessResponse<T> {
return { type: "success", data, status: 200 };
}

/**
Expand All @@ -32,7 +32,7 @@ export function createAPIResponse<T>(data: T): APIResponse<T> {
* try {
* // API処理
* return handleAPISuccess(data);
* // => { data: data, status: 200 }
* // => { type: "success", data: data, status: 200 }
* } catch (error) {
* return handleAPIError(error);
* }
Expand Down
4 changes: 2 additions & 2 deletions src/styles/globals.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@use "./tailwind";

main {
@apply bg-neutral-300 dark:bg-neutral-800;
body {
@apply bg-neutral-100 dark:bg-neutral-800;
}

0 comments on commit b29b333

Please sign in to comment.