All files / src/api/drive/folder list.ts

100% Statements 25/25
100% Branches 22/22
100% Functions 1/1
100% Lines 25/25

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104          53x                                                                     53x 202x 202x   202x 30x     172x 172x     172x               123x 15x 108x 93x   15x       123x   97x           75x     75x 60x 15x         45x 15x   30x 15x             30x          
import { createRoute, RouteHandler } from '@hono/zod-openapi'
import { ErrorSchema, FolderListRequestSchema, FolderListResponseSchema } from '../../../schema/drive'
import { getAccessToken, listFolders } from '../../../utils/googleDrive'
import { EnvHono } from '../../..'
 
export const listFoldersRoute = createRoute({
  method: 'get',
  path: '/list-folders',
  request: {
    query: FolderListRequestSchema
  },
  responses: {
    200: {
      content: {
        'application/json': {
          schema: FolderListResponseSchema
        }
      },
      description: 'Folders listed successfully'
    },
    400: {
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      },
      description: 'Bad request'
    },
    500: {
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      },
      description: 'Internal server error'
    }
  },
  tags: ['Folder API']
})
 
export const listFoldersHandler: RouteHandler<typeof listFoldersRoute, EnvHono> = async (c) => {
  try {
    const { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REFRESH_TOKEN, GOOGLE_DRIVE_DEFAULT_FOLDER_ID } = c.env
 
    if (!GOOGLE_CLIENT_ID || !GOOGLE_CLIENT_SECRET || !GOOGLE_REFRESH_TOKEN || !GOOGLE_DRIVE_DEFAULT_FOLDER_ID) {
      return c.json({ error: 'Missing required environment variables' }, 400)
    }
 
    const url = new URL(c.req.url)
    const parentId = url.searchParams.get('parentId')
 
    // アクセストークンを取得
    const accessToken = await getAccessToken({
      clientId: GOOGLE_CLIENT_ID,
      clientSecret: GOOGLE_CLIENT_SECRET,
      refreshToken: GOOGLE_REFRESH_TOKEN
    })
 
    // parentId が null string の場合は 'null' を、空文字や未定義の場合はデフォルトフォルダを使用
    let parentFolderId: string
    if (parentId === 'null') {
      parentFolderId = 'null'
    } else if (!parentId || parentId === '') {
      parentFolderId = GOOGLE_DRIVE_DEFAULT_FOLDER_ID
    } else {
      parentFolderId = parentId
    }
 
    // フォルダ一覧を取得
    const folders = await listFolders(parentFolderId, accessToken)
 
    return c.json({
      success: true,
      folders
    }, 200)
 
  } catch (error) {
    console.error('List folders error:', error)
 
    // エラーメッセージからHTTPステータスコードを判断
    if (error instanceof Error) {
      if (error.message.includes('Failed to refresh access token')) {
        return c.json({
          error: 'Failed to refresh access token',
          details: error.message.replace('Failed to refresh access token: ', '')
        }, 500)
      }
      if (error.message.includes('No access token')) {
        return c.json({ error: 'Failed to get access token' }, 500)
      }
      if (error.message.includes('Failed to list folders')) {
        return c.json({
          error: 'Failed to list folders',
          details: error.message.replace('Failed to list folders: ', '')
        }, 500)
      }
    }
 
    return c.json({
      error: 'Failed to list folders',
      details: error instanceof Error ? error.message : 'Unknown error'
    }, 500)
  }
}