소스 검색

feat: add GET /api/validate/:word endpoint

Returns {word, valid} — checks whether the input is a valid
5-letter word in the word bank. Works for both EN (/api/validate)
and RU (/ru/api/validate).
Oleg Panashchenko 1 주 전
부모
커밋
3bdad3a8dc
3개의 변경된 파일30개의 추가작업 그리고 0개의 파일을 삭제
  1. 2 0
      server/src/index.ts
  2. 22 0
      server/src/routes/validate.ts
  3. 6 0
      shared/src/api.ts

+ 2 - 0
server/src/index.ts

@@ -6,6 +6,7 @@ import type { WordBank } from '@wordle/shared';
 import { createDailyRouter } from './routes/daily.js';
 import { createGuessRouter } from './routes/guess.js';
 import { createPlayAgainRouter } from './routes/play-again.js';
+import { createValidateRouter } from './routes/validate.js';
 
 const __dirname = dirname(fileURLToPath(import.meta.url));
 
@@ -17,6 +18,7 @@ function loadBank(filename: string): WordBank {
 function mountApi(prefix: string, bank: WordBank) {
   return [
     [prefix + '/daily', createDailyRouter(bank)],
+    [prefix + '/validate', createValidateRouter(bank)],
     [prefix + '/guess', createGuessRouter(bank)],
     [prefix + '/play-again', createPlayAgainRouter(bank)],
   ] as const;

+ 22 - 0
server/src/routes/validate.ts

@@ -0,0 +1,22 @@
+import { Router, Request, Response } from 'express';
+import type { WordBank, ValidateResponse } from '@wordle/shared';
+
+export function createValidateRouter(wordBank: WordBank): Router {
+  const router = Router();
+
+  router.get('/:word', (req: Request, res: Response) => {
+    const word = req.params.word ?? '';
+
+    if (word.length !== 5) {
+      const response: ValidateResponse = { word, valid: false };
+      res.json(response);
+      return;
+    }
+
+    const valid = wordBank.guessable.includes(word.toLowerCase());
+    const response: ValidateResponse = { word, valid };
+    res.json(response);
+  });
+
+  return router;
+}

+ 6 - 0
shared/src/api.ts

@@ -36,6 +36,12 @@ export interface PlayAgainResponse {
   wordId: number;
 }
 
+/** GET /api/validate/:word response */
+export interface ValidateResponse {
+  word: string;
+  valid: boolean;
+}
+
 /** Standard API error shape */
 export interface ApiError {
   error: string;