All files / src index.ts

0% Statements 0/14
0% Branches 0/2
0% Functions 0/2
0% Lines 0/14

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                                                                                                                                                               
// hono
import { HTTPException } from 'hono/http-exception';
import { cors } from 'hono/cors';
import type { ErrorResponseSchemaType } from 'validator';
import { Scalar } from '@scalar/hono-api-reference';
// hono object factory
import honoFactory from './presentation/factory/hono';
// better-auth object factory
import { authFactory } from './presentation/factory/auth';
// routes
import apiPublic from './presentation/api.public';
import apiProtected from './presentation/api.protected';
 
// hono instance
const app = honoFactory();
 
// cors settings
app.use(
  '/api/*',
  cors({
    origin: ['https://pay-crew2.yukiosada.work', 'http://localhost:5173'],
    allowHeaders: ['Content-Type', 'Authorization', 'baggage', 'sentry-trace'],
    allowMethods: ['POST', 'PUT', 'PATCH', 'GET', 'DELETE', 'OPTIONS'],
    exposeHeaders: ['Content-Length'],
    maxAge: 600,
    credentials: true,
  })
);
 
// public routes
app.route('/', apiPublic(authFactory));
// protected routes
app.route('/', apiProtected(authFactory));
 
// error handling
// `404 Not Found` handling
app.notFound((c) => {
  console.error(`Not Found: ${c.req.url}`);
  return c.json({ status: 404, message: 'Not Found' } satisfies ErrorResponseSchemaType, 404);
});
// General error handling (excluding `404 Not Found`)
app.onError((error, c) => {
  console.error(`Error: ${error.message}`);
  // HTTPExceptionの場合はそのステータスコードで返す
  if (error instanceof HTTPException) {
    return c.json(
      {
        status: error.status,
        message: error.message,
      } satisfies ErrorResponseSchemaType,
      error.status
    );
  }
  // その他のエラーは500で返す
  return c.json(
    {
      status: 500,
      message: 'Internal Server Error',
    } satisfies ErrorResponseSchemaType,
    500
  );
});
 
// OpenAPI Spec Endpoint
app.doc('/openapi', {
  openapi: '3.1.0',
  info: {
    title: 'Pay Crew2 API',
    version: '0.0.1',
    description: 'API documentation for Pay Crew2 backend',
  },
});
 
// Scalar Web UI Endpoint
// References
// https://guides.scalar.com/scalar/scalar-api-references/integrations/hono
app.get('/docs', Scalar({ url: '/openapi' }));
 
export default app;