All files / src index.ts

0% Statements 0/15
0% Branches 0/4
0% Functions 0/3
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                               
import { OpenAPIHono } from '@hono/zod-openapi';
import { HTTPException } from 'hono/http-exception';
import { cors } from 'hono/cors';
import type { ErrorResponseSchemaType } from 'validator';
import { Scalar } from '@scalar/hono-api-reference';
import type { Bindings } from './presentation/share/binding';
import { default as root } from './presentation';
 
const app = new OpenAPIHono<{
  Bindings: Bindings;
}>({
  // Open API Honoのインスタンスを生成
  // ZodのバリデーションエラーをHTTPExceptionで投げるように設定
  // result.successがfalseの場合はZodErrorが入っている
  defaultHook: (result) => {
    if (!result.success) {
      console.error(result.error);
      throw new HTTPException(400, {
        message: 'Zod Validation Error',
      });
    }
  },
});
 
// CORSの設定
app.use(
  '*',
  cors({
    origin: ['https://pay-crew.yukiosada.work', 'http://localhost:5173'],
    allowHeaders: ['Content-Type', 'baggage', 'sentry-trace'],
    allowMethods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'],
  })
);
 
// 404のエラーハンドリング
app.notFound((c) => {
  console.error(`Not Found: ${c.req.url}`);
  return c.json({ status: 404, message: 'Not Found' } satisfies ErrorResponseSchemaType, 404);
});
 
// 404以外のエラーハンドリング
app.onError((error, c) => {
  if (error instanceof HTTPException) {
    return c.json(
      {
        status: error.status,
        message: error.message,
      } satisfies ErrorResponseSchemaType,
      error.status
    );
  }
  return c.json(
    {
      status: 500,
      message: 'Internal Server Error',
    } satisfies ErrorResponseSchemaType,
    500
  );
});
 
// OpenAPI Spec Endpoint
app.doc('/openapi', {
  openapi: '3.1.0',
  info: {
    title: 'Echo API',
    version: '1.0.0',
    description: '受け取った入力値をそのまま応答するAPI',
  },
});
 
// Scalar Web UI Endpoint
// References
// https://guides.scalar.com/scalar/scalar-api-references/integrations/hono
app.get('/docs', Scalar({ url: '/openapi' }));
 
// エンドポイントのルートの登録
app.route('/', root);
 
export default app;