Skip to content

Commit

Permalink
🎨 #38 存在しないcatIdが指定された場合は404を返すように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
keitakn committed Jul 4, 2023
1 parent 8b835f8 commit 2a1cee2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/app/chat/[catId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extractCatNameById, type CatId } from '@/features';
import { extractCatNameById, isCatId, type CatId } from '@/features';
import type { Metadata, ResolvingMetadata } from 'next';
import { Noto_Sans_JP } from 'next/font/google';
import { notFound } from 'next/navigation';
import type { JSX, ReactNode } from 'react';

const font = Noto_Sans_JP({
Expand All @@ -20,6 +21,10 @@ export const generateMetadata = async (
// eslint-disable-next-line
parent: ResolvingMetadata
): Promise<Metadata> => {
if (!isCatId(params.catId)) {
notFound();
}

return {
title: `AI Cat ${extractCatNameById(params.catId)}ちゃん🐱`,
description: `ねこのAI(${extractCatNameById(
Expand Down
6 changes: 6 additions & 0 deletions src/features/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ export const extractCatNameById = (catId: CatId): CatName => {
throw new ExhaustiveError(catId);
}
};

export const isCatId = (value: unknown): value is CatId => {
const result = catIds.find((element) => element === value);

return result !== undefined;
};
2 changes: 1 addition & 1 deletion src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export type {
FetchCatMessage,
} from './cat';

export { extractCatNameById } from './cat';
export { extractCatNameById, isCatId } from './cat';

0 comments on commit 2a1cee2

Please sign in to comment.