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

100% Statements 20/20
95% Branches 19/20
100% Functions 1/1
100% Lines 20/20

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        53x                                                                                 53x 154x 154x   154x 48x     106x   106x                     37x 16x     21x           69x   69x 53x 5x         48x 16x   32x 16x             32x          
import { createRoute, RouteHandler } from '@hono/zod-openapi'
import { ErrorSchema, FolderCreateRequestSchema, FolderCreateResponseSchema } from '../../../schema/drive'
import { createFolder } from '../../../utils/googleDrive'
import { EnvHono } from '../../..'
export const createFolderRoute = createRoute({
  method: 'post',
  path: '/create-folder',
  request: {
    body: {
      content: {
        'application/json': {
          schema: FolderCreateRequestSchema
        }
      }
    }
  },
  responses: {
    200: {
      content: {
        'application/json': {
          schema: FolderCreateResponseSchema
        }
      },
      description: 'Folder created 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 createFolderHandler: RouteHandler<typeof createFolderRoute, 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 { name, parentId } = await c.req.json()
 
    const result = await createFolder(
      name,
      parentId,
      {
        clientId: GOOGLE_CLIENT_ID,
        clientSecret: GOOGLE_CLIENT_SECRET,
        refreshToken: GOOGLE_REFRESH_TOKEN
      },
      GOOGLE_DRIVE_DEFAULT_FOLDER_ID
    )
 
    if (!result.success) {
      return c.json({ error: result.error || 'Create folder failed' }, 400)
    }
 
    return c.json({
      success: true,
      folder: result.folder
    }, 200)
 
  } catch (error) {
    console.error('Create folder error:', error)
 
    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 create folder')) {
        return c.json({
          error: 'Failed to create folder',
          details: error.message.replace('Failed to create folder: ', '')
        }, 500)
      }
    }
 
    return c.json({
      error: 'Failed to create folder',
      details: error instanceof Error ? error.message : 'Unknown error'
    }, 500)
  }
}