From 45d32642e89fcd8baba0fbaf69a282c24700c104 Mon Sep 17 00:00:00 2001 From: Najah7 Date: Sat, 16 Mar 2024 03:06:00 +0900 Subject: [PATCH] [Format] --- src/hooks/useFetchMilestone.ts | 133 +++++++++++++++------------- src/hooks/useFetchUser.ts | 156 +++++++++++++++++---------------- src/hooks/useImageUploder.ts | 59 ++++++------- tsconfig.json | 8 +- 4 files changed, 186 insertions(+), 170 deletions(-) diff --git a/src/hooks/useFetchMilestone.ts b/src/hooks/useFetchMilestone.ts index e38a503..3dd8927 100644 --- a/src/hooks/useFetchMilestone.ts +++ b/src/hooks/useFetchMilestone.ts @@ -1,4 +1,4 @@ -"use client" +"use client"; import axios from "axios"; const protobuf = require("protobufjs"); @@ -6,15 +6,16 @@ import { Milestone } from "../../proto/typescript/pb_out/main"; import mockMilestones from "@/sample-data/milestones.json"; const useFetchMilestone = () => { - - const create = async (newMileStone: Milestone): Promise => { + const create = async (newMileStone: Milestone): Promise => { // マイルストーンを新規作成する関数 // 新しいマイルストーンをサーバーに送信する (milestoneIdは空文字で送信) // サーバー側で付与されたmilestoneID または milestone 丸ごとを返す const token = localStorage.getItem("token"); - const MilestoneCreateRequest = protobuf.roots.default.lookupType("main.MilestoneCreateRequest"); - + const MilestoneCreateRequest = protobuf.roots.default.lookupType( + "main.MilestoneCreateRequest", + ); + const payload = { milestone: newMileStone }; const errMsg = MilestoneCreateRequest.verify(payload); @@ -23,57 +24,61 @@ const useFetchMilestone = () => { const milestone = MilestoneCreateRequest.create(payload); const buffer = MilestoneCreateRequest.encode(milestone).finish(); - await axios.post( - `${process.env.BACKEND_DOMAIN}/api/v1/milestone/create`, - buffer, - { - headers: { - Authorization: `token ${token}`, - }, - } - ) - .then((response) => { - const MilestoneCreateResponse = protobuf.roots.default.lookupType("main.MilestoneCreateResponse"); - const res = MilestoneCreateResponse.decode(new Uint8Array(response.data)); - return MilestoneCreateResponse.toObject(res); - }) - .catch((error) => { - console.log(error); - }) - } - + await axios + .post(`${process.env.BACKEND_DOMAIN}/api/v1/milestone/create`, buffer, { + headers: { + Authorization: `token ${token}`, + }, + }) + .then((response) => { + const MilestoneCreateResponse = protobuf.roots.default.lookupType( + "main.MilestoneCreateResponse", + ); + const res = MilestoneCreateResponse.decode( + new Uint8Array(response.data), + ); + return MilestoneCreateResponse.toObject(res); + }) + .catch((error) => { + console.log(error); + }); + }; + const update = async (newMileStone: Milestone): Promise => { // マイルストーンを更新する関数 // 更新したいマイルストーンをサーバーに送信 // 更新されたマイルストーン または ステータス または void を返す const token = localStorage.getItem("token"); - const MilestoneUpdateRequest = protobuf.roots.default.lookupType("main.MilestoneUpdateRequest"); + const MilestoneUpdateRequest = protobuf.roots.default.lookupType( + "main.MilestoneUpdateRequest", + ); const payload = { milestone: newMileStone }; const errMsg = MilestoneUpdateRequest.verify(payload); if (errMsg) throw Error(`MilestoneUpdateRequest: ${errMsg}`); const milestone = MilestoneUpdateRequest.create(payload); const buffer = MilestoneUpdateRequest.encode(milestone).finish(); - await axios.post( - `${process.env.BACKEND_DOMAIN}/api/v1/milestone/update`, - buffer, - { - headers: { - Authorization: `token ${token}`, - }, - } - ) - .then((response) => { - const MilestoneCreateResponse = protobuf.roots.default.lookupType("main.MilestoneCreateResponse"); - const res = MilestoneCreateResponse.decode(new Uint8Array(response.data)); - return res - }) - .catch((error) => { - console.log(error); - }) -} - + await axios + .post(`${process.env.BACKEND_DOMAIN}/api/v1/milestone/update`, buffer, { + headers: { + Authorization: `token ${token}`, + }, + }) + .then((response) => { + const MilestoneCreateResponse = protobuf.roots.default.lookupType( + "main.MilestoneCreateResponse", + ); + const res = MilestoneCreateResponse.decode( + new Uint8Array(response.data), + ); + return res; + }) + .catch((error) => { + console.log(error); + }); + }; + // because delete is a reserved word, we use _delete const _delete = async (milestoneId: string): Promise => { // マイルストーンを削除する関数 @@ -81,26 +86,30 @@ const useFetchMilestone = () => { // ステータス または void を返す const token = localStorage.getItem("token"); - await axios.delete( - `${process.env.BACKEND_DOMAIN}/api/v1/milestone/delete${milestoneId}`, - { + await axios + .delete( + `${process.env.BACKEND_DOMAIN}/api/v1/milestone/delete${milestoneId}`, + { headers: { - Authorization: `token ${token}`, + Authorization: `token ${token}`, }, - } - ) - .then((response) => { - const MilestoneDeleteResponse = protobuf.roots.default.lookupType("main.MilestoneDeleteResponse"); - const res = MilestoneDeleteResponse.decode(new Uint8Array(response.data)); - return res - }) - .catch((error) => { - console.log(error); - }) - - } + }, + ) + .then((response) => { + const MilestoneDeleteResponse = protobuf.roots.default.lookupType( + "main.MilestoneDeleteResponse", + ); + const res = MilestoneDeleteResponse.decode( + new Uint8Array(response.data), + ); + return res; + }) + .catch((error) => { + console.log(error); + }); + }; - return {create, update, _delete}; -} + return { create, update, _delete }; +}; export default useFetchMilestone; diff --git a/src/hooks/useFetchUser.ts b/src/hooks/useFetchUser.ts index 971e7a7..e743690 100644 --- a/src/hooks/useFetchUser.ts +++ b/src/hooks/useFetchUser.ts @@ -1,57 +1,59 @@ -"use client" +"use client"; import axios from "axios"; const protobuf = require("protobufjs"); import { UserData } from "../../proto/typescript/pb_out/main"; const useFetchUser = () => { + const get = async (userId: string): Promise => { + const token = localStorage.getItem("token"); - const get = async (userId: string): Promise => { - const token = localStorage.getItem("token"); - - await axios.get( - `${process.env.BACKEND_DOMAIN}/api/v1/user/info/${userId}`, - { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ) - .then((response) => { - const UserResponse = protobuf.roots.default.lookupType("main.UserResponse"); - const res = UserResponse.decode(new Uint8Array(response.data)); - return res - }) - .catch((error) => { - console.log(error); - }) - } + await axios + .get(`${process.env.BACKEND_DOMAIN}/api/v1/user/info/${userId}`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then((response) => { + const UserResponse = + protobuf.roots.default.lookupType("main.UserResponse"); + const res = UserResponse.decode(new Uint8Array(response.data)); + return res; + }) + .catch((error) => { + console.log(error); + }); + }; const list = async (): Promise => { const token = localStorage.getItem("token"); - await axios.get( + await axios + .get( `${process.env.BACKEND_DOMAIN}/api/v1/user/info`, // TODO: rename to /users { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ) - .then((response) => { - const UsersResponse = protobuf.roots.default.lookupType("main.UsersResponse"); - const res = UsersResponse.decode(new Uint8Array(response.data)); - return UsersResponse.toObject(res); - }) - .catch((error) => { - console.log(error); - }) - } - + headers: { + Authorization: `Bearer ${token}`, + }, + }, + ) + .then((response) => { + const UsersResponse = + protobuf.roots.default.lookupType("main.UsersResponse"); + const res = UsersResponse.decode(new Uint8Array(response.data)); + return UsersResponse.toObject(res); + }) + .catch((error) => { + console.log(error); + }); + }; + const create = async (newUser: UserData): Promise => { const token = localStorage.getItem("token"); - const UserCreateRequest = protobuf.roots.default.lookupType("main.UserCreateRequest"); + const UserCreateRequest = protobuf.roots.default.lookupType( + "main.UserCreateRequest", + ); const payload = { user: newUser }; const errMsg = UserCreateRequest.verify(payload); if (errMsg) throw Error(`UserCreateRequest: ${errMsg}`); @@ -59,57 +61,57 @@ const useFetchUser = () => { const user = UserCreateRequest.create(payload); const buffer = UserCreateRequest.encode(user).finish(); - await axios.post( - `${process.env.BACKEND_DOMAIN}/api/v1/user/create`, - buffer, - { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ) - .then((response) => { - const UserCreateResponse = protobuf.roots.default.lookupType("main.UserCreateResponse"); - const res = UserCreateResponse.decode(new Uint8Array(response.data)); - return res - }) - .catch((error) => { + await axios + .post(`${process.env.BACKEND_DOMAIN}/api/v1/user/create`, buffer, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then((response) => { + const UserCreateResponse = protobuf.roots.default.lookupType( + "main.UserCreateResponse", + ); + const res = UserCreateResponse.decode(new Uint8Array(response.data)); + return res; + }) + .catch((error) => { console.log(error); - }) - } + }); + }; const update = async (newUser: UserData): Promise => { const token = localStorage.getItem("token"); - const UserUpdateRequest = protobuf.roots.default.lookupType("main.UserUpdateRequest"); + const UserUpdateRequest = protobuf.roots.default.lookupType( + "main.UserUpdateRequest", + ); const payload = { user: newUser }; const errMsg = UserUpdateRequest.verify(payload); if (errMsg) throw Error(`UserUpdateRequest: ${errMsg}`); - + const user = UserUpdateRequest.create(payload); const buffer = UserUpdateRequest.encode(user).finish(); - await axios.post( - `${process.env.BACKEND_DOMAIN}/api/v1/user/update`, - buffer, - { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ) - .then((response) => { - const UserUpdateResponse = protobuf.roots.default.lookupType("main.UserUpdateResponse"); - const res = UserUpdateResponse.decode(new Uint8Array(response.data)); - return res - }) - .catch((error) => { + await axios + .post(`${process.env.BACKEND_DOMAIN}/api/v1/user/update`, buffer, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then((response) => { + const UserUpdateResponse = protobuf.roots.default.lookupType( + "main.UserUpdateResponse", + ); + const res = UserUpdateResponse.decode(new Uint8Array(response.data)); + return res; + }) + .catch((error) => { console.log(error); - }) - } - - return {get, list, create, update}; -} + }); + }; + + return { get, list, create, update }; +}; export default useFetchUser; diff --git a/src/hooks/useImageUploder.ts b/src/hooks/useImageUploder.ts index 017f767..c51ccdf 100644 --- a/src/hooks/useImageUploder.ts +++ b/src/hooks/useImageUploder.ts @@ -1,39 +1,38 @@ import axios from "axios"; const protobuf = require("protobufjs"); - const useImageUploder = () => { + const upload = async (image: File): Promise => { + const token = localStorage.getItem("token"); - const upload = async (image: File): Promise => { - const token = localStorage.getItem("token"); - - const ImageUploadRequest = protobuf.roots.default.lookupType("main.ImageUploadRequest"); - const payload = { image: image }; - const errMsg = ImageUploadRequest.verify(payload); - if (errMsg) throw Error(`ImageUploadRequest: ${errMsg}`); + const ImageUploadRequest = protobuf.roots.default.lookupType( + "main.ImageUploadRequest", + ); + const payload = { image: image }; + const errMsg = ImageUploadRequest.verify(payload); + if (errMsg) throw Error(`ImageUploadRequest: ${errMsg}`); - const imageUpload = ImageUploadRequest.create(payload); - const buffer = ImageUploadRequest.encode(imageUpload).finish(); - await axios.post( - `${process.env.BACKEND_DOMAIN}/api/v1/image/upload`, - buffer, - { - headers: { - Authorization: `token ${token}`, - }, - } - ) - .then((response) => { - const ImageUploadResponse = protobuf.roots.default.lookupType("main.ImageUploadResponse"); - const res = ImageUploadResponse.decode(new Uint8Array(response.data)); - return res - }) - .catch((error) => { - console.log(error); - }) - } + const imageUpload = ImageUploadRequest.create(payload); + const buffer = ImageUploadRequest.encode(imageUpload).finish(); + await axios + .post(`${process.env.BACKEND_DOMAIN}/api/v1/image/upload`, buffer, { + headers: { + Authorization: `token ${token}`, + }, + }) + .then((response) => { + const ImageUploadResponse = protobuf.roots.default.lookupType( + "main.ImageUploadResponse", + ); + const res = ImageUploadResponse.decode(new Uint8Array(response.data)); + return res; + }) + .catch((error) => { + console.log(error); + }); + }; - return { upload }; -} + return { upload }; +}; export default useImageUploder; diff --git a/tsconfig.json b/tsconfig.json index 7b28589..8c7d73a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,6 +21,12 @@ "@/*": ["./src/*"] } }, - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], + "include": [ + "next-env.d.ts", + "**/*.ts", + "**/*.tsx", + ".next/types/**/*.ts", + "src/app/api/tmp/route.ts" + ], "exclude": ["node_modules"] }