Skip to content

Commit

Permalink
Merge pull request #115 from times-yasunori/refactor-entries-id
Browse files Browse the repository at this point in the history
単体表示ページは、単体取得APIを利用するようにリファクタリング
  • Loading branch information
tomoya authored Oct 12, 2024
2 parents 22c78db + 0ac0cfd commit 97aa68b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/web/app/routes/entries.$id/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("entryLoader", () => {

test("returns 404 if APIs return 400 error", async () => {
mockServer.use(
http.get("https://api.yasunori.dev/awesome", () => {
http.get("https://api.yasunori.dev/awesome/1", () => {
return new HttpResponse(null, {
status: 400,
});
Expand Down
10 changes: 2 additions & 8 deletions packages/web/app/routes/entries.$id/loader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import type { LoaderFunction } from "@remix-run/cloudflare";
import { fetchAwesomeYasunori } from "../../shared/fetch-awesome-yasunori";
import { fetchAwesomeYasunoriEntry } from "../../shared/fetch-awesome-yasunori-entry";

export const entryLoader = (async ({ params }) => {
const id = Number(params.id);
if (Number.isNaN(id)) {
throw new Response(null, { status: 404 });
}

// TODO: APIが単体リソースを返すようになったらそれを直接取得する
const entries = await fetchAwesomeYasunori();
if (!entries) {
throw new Response(null, { status: 404 });
}

const entry = entries.find((d) => d.id === id);
const entry = await fetchAwesomeYasunoriEntry(params.id as string);
if (!entry) {
throw new Response(null, { status: 404 });
}
Expand Down
9 changes: 9 additions & 0 deletions packages/web/app/shared/fetch-awesome-yasunori-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { yasunoriApiClient } from "./yasunori-api";

export async function fetchAwesomeYasunoriEntry(id: string) {
const res = await yasunoriApiClient.awesome[":id"].$get({ param: { id } });
if (!res.ok) {
return null;
}
return await res.json();
}
19 changes: 19 additions & 0 deletions packages/web/mocks/server/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { http, HttpResponse } from "msw";

export const awesomeIdHandlers = [
http.get("https://api.yasunori.dev/awesome/1", () => {
const response = {
at: "vim-jp radioお便り",
content:
"tomoyaさん、ありすえさんこんにちは。\nはじめまして、yasunoriの母です。\n\nyasunoriがソフトウェアエンジニアを志してから様子がおかしくなってしまいました。\n家ですれ違う時「Vim....Vim....」という独り言をずっと唱えていたり、部屋からは「設定させていただきありがとうございます!!」という大声が聞こえてきたり、\n「会合があるから東京に行ってくる、帰りは遅くなる」と言い残して出て行き、帰ってくると満面の笑みで「Vimはいいぞ」と一言言って自室に篭るようになりました。\n\ntomoyaさんありすえさんもVimコミュニティの人達だと伺いましたが、息子の身に一体何が起きてしまったのか教えていただけると幸いです。\n",
date: "2024-06-25",
id: 1,
meta: "",
senpan: "takeokunn",
title: "yasunoriの母",
};
return HttpResponse.json(response);
}),
http.get("https://api.yasunori.dev/awesome/0", () => {
return HttpResponse.json(null, { status: 404 });
}),
];

export const awesomeHandler = http.get(
"https://api.yasunori.dev/awesome",
() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/web/mocks/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setupServer } from "msw/node";
import { awesomeHandler } from "./handlers";
import { awesomeHandler, awesomeIdHandlers } from "./handlers";

const handlers = [awesomeHandler];
const handlers = [awesomeHandler, ...awesomeIdHandlers];

const server = setupServer(...handlers);

Expand Down

0 comments on commit 97aa68b

Please sign in to comment.