Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

マイルストーン編集用のfetchロジックを作る #39

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bfae217
create useFetchMilestone
hirokiwa Mar 14, 2024
35a9d5c
➕ [Add] axios and protobuf
Najah7 Mar 14, 2024
e614041
✨ [Add] custom hooks to fetch data
Najah7 Mar 14, 2024
db61151
[Fix] token -> Beare
Najah7 Mar 15, 2024
45d3264
[Format]
Najah7 Mar 15, 2024
19a890a
🔧 add env NEXT_PUBLIC_BACKEND_DOMAIN
Shion1305 Mar 15, 2024
fb0fd9d
✨ (src/hooks) useApiClient
Shion1305 Mar 15, 2024
91e8f49
💄 future timeline ui
daiki0381 Mar 15, 2024
9861b40
✨ (src/hooks) header for useApiClient
Shion1305 Mar 15, 2024
01e07f6
💄 future timeline ui
daiki0381 Mar 15, 2024
f3d29d1
➕ axios
Shion1305 Mar 15, 2024
236219f
🔀 merge main
Shion1305 Mar 15, 2024
69a82d7
Merge pull request #49 from openhacku-team-a/feature/ui-future-timeline
daiki0381 Mar 15, 2024
4b8e2ad
✨ (hooks) useFetchUser
Shion1305 Mar 15, 2024
eacfad9
[Add] API callの関数をとりあえず実装。
Najah7 Mar 15, 2024
b76469b
[Fix] a little because of conflict
Najah7 Mar 15, 2024
76b7775
Merge branch '38-マイルストーン編集用のfetchロジックを作る' of https://github.com/openh…
Ishigami100 Mar 15, 2024
2b84d01
[Add] image uploader
Najah7 Mar 15, 2024
0e23201
memoizeを追加する
Ishigami100 Mar 15, 2024
86c23d6
firebaseのtokenを保持する
Ishigami100 Mar 15, 2024
a67ce40
Merge pull request #52 from openhacku-team-a/feture/upload-img-method
Najah7 Mar 15, 2024
7e73f68
refresh-tokenを登録する
Ishigami100 Mar 15, 2024
780967e
✨ del method in useApiPBClient.ts
Shion1305 Mar 15, 2024
65a4ff9
🚑️ hot fix for useFetchMilestone.ts
Shion1305 Mar 15, 2024
6f36a77
🚨 useApiPBClient.ts
Shion1305 Mar 15, 2024
a60bcfb
🚑️ useImageUploder.ts
Shion1305 Mar 15, 2024
7856026
🚑️ useFetchUser.ts
Shion1305 Mar 15, 2024
8c2ddeb
🚨 npm run format
Shion1305 Mar 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.local.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ NEXT_PUBLIC_FIREBASE_PROJECT_ID=
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=
NEXT_PUBLIC_FIREBASE_APP_ID=
NEXT_PUBLIC_BACKEND_DOMAIN=
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@protobuf-ts/plugin": "^2.9.4",
"axios": "^1.6.7",
"axios": "^1.6.8",
"firebase": "^10.8.1",
"next": "14.1.3",
"react": "^18",
Expand Down
64 changes: 64 additions & 0 deletions src/hooks/useApiPBClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import axios from "axios";

const useApiPBClient = () => {
interface BackendResponse {
data: any;
unauthorized: boolean;
error: any;
}

// get access token
const token = localStorage.getItem("token");
const headers = {
"Content-Type": "application/octet-stream",
...(token ? { Authorization: `Bearer ${token}` } : {}),
};

const get = async (
url: string,
params?: Record<string, any>,
): Promise<BackendResponse> => {
return await axios
.get(process.env.NEXT_PUBLIC_BACKEND_DOMAIN + url, { params, headers })
.then((resp) => {
if (resp.status < 210) {
return { data: resp.data, unauthorized: false, error: null };
}
if (resp.status === 401) {
return { data: null, unauthorized: true, error: resp.status };
}
return { data: null, unauthorized: false, error: resp.statusText };
});
};

const put = async (url: string, body: any): Promise<BackendResponse> => {
return await axios
.put(process.env.NEXT_PUBLIC_BACKEND_DOMAIN + url, body, { headers })
.then((resp) => {
if (resp.status < 210) {
return { data: resp.data, unauthorized: false, error: null };
}
if (resp.status === 401) {
return { data: null, unauthorized: true, error: resp.status };
}
return { data: null, unauthorized: false, error: resp.statusText };
});
};

const post = async (url: string, body: any): Promise<BackendResponse> => {
return await axios
.post(process.env.NEXT_PUBLIC_BACKEND_DOMAIN + url, body, { headers })
.then((resp) => {
if (resp.status < 210) {
return { data: resp.data, unauthorized: false, error: null };
}
if (resp.status === 401) {
return { data: null, unauthorized: true, error: resp.status };
}
return { data: null, unauthorized: false, error: resp.statusText };
});
};
return { get, put, post };
};

export default useApiPBClient;
63 changes: 63 additions & 0 deletions src/hooks/useFetchMilestone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"use client";
import axios from "axios";
import {
Milestone,
MilestoneCreateRequest,
MilestoneCreateResponse,
} from "../../proto/typescript/pb_out/main";

import useApiPBClient from "@/hooks/useApiPBClient";

const protobuf = require("protobufjs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const protobuf = require("protobufjs");


const useFetchMilestone = () => {
const client = useApiPBClient();

const create = async (
newMilestoen: MilestoneCreateRequest,
): Promise<MilestoneCreateResponse> => {
return await client.post("/milestone", newMilestoen).then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return MilestoneCreateResponse.fromBinary(resp.data);
});
};

const update = async (
newMilestoen: MilestoneCreateRequest,
): Promise<MilestoneUpdateResponse> => {
return await client
.put(`/milestone/${newMilestoen.milestone?.milestoneId}`, newMilestoen)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientなんですが 現状 /api/v1/ つけないといけない仕様になっています..

.then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return MilestoneUpdateResponse.fromBinary(resp.data);
});
};

const _delete = async (
milestoneId: string,
): Promise<MilestoneDeleteResponse> => {
return await client.delete(`/milestone/${milestoneId}`).then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return MilestoneDeleteResponse.fromBinary(resp.data);
});
};

return { create, update, _delete };
};

export default useFetchMilestone;
80 changes: 80 additions & 0 deletions src/hooks/useFetchUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"use client";
import axios from "axios";
import {
UserData,
UserInfoResponse,
UserInfosResponse,
} from "../../proto/typescript/pb_out/main";
import useApiPBClient from "@/hooks/useApiPBClient";

const protobuf = require("protobufjs");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const protobuf = require("protobufjs");


const useFetchUser = () => {
const client = useApiPBClient();

const me = async (): Promise<UserInfoResponse> => {
return await client.get(`/api/v1/user/info/me`).then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return UserInfoResponse.fromBinary(resp.data);
});
};

const get = async (userId: string): Promise<UserInfoResponse> => {
return await client.get(`/api/v1/user/info/${userId}`).then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return UserInfoResponse.fromBinary(resp.data);
});
};

const list = async (): Promise<UserInfosResponse> => {
return await client.get("/api/v1/user/info/").then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return UserInfosResponse.fromBinary(resp.data);
});
};

const create = async (newUser: UserData): Promise<UserInfoResponse> => {
return await client.put(`/user/${newUser.userId}`, newUser).then((resp) => {
if (resp.unauthorized) {
throw new Error("unauthorized");
}
if (resp.error) {
throw new Error(resp.error);
}
return UserInfoResponse.fromBinary(resp.data);
});
};

// const icon = async (userId: string, file: File) => {
// return await client.put(
// `/user/${userId}/icon`,
// ).then((resp) => {
// if (resp.unauthorized) {
// throw new Error("unauthorized");
// }
// if (resp.error) {
// throw new Error(resp.error);
// }
// return UserInfoResponse.fromBinary(resp.data)
// })
// }

return { me, get, list, create };
};

export default useFetchUser;
38 changes: 38 additions & 0 deletions src/hooks/useImageUploder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import axios from "axios";
const protobuf = require("protobufjs");

const useImageUploder = () => {
const upload = async (image: File): Promise<string | void> => {
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 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 };
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const protobuf = require("protobufjs");
const useImageUploder = () => {
const upload = async (image: File): Promise<string | void> => {
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 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 };
};
const useImageUploder = () => {
const upload = async (image: File): Promise<string | void> => {
const token = localStorage.getItem("token");
};
return { upload };
};


export default useImageUploder;
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
Loading