2
0
Files
bot/apps/builder/pages/api/integrations/google-sheets/spreadsheets.ts

33 lines
1.3 KiB
TypeScript
Raw Normal View History

import { NextApiRequest, NextApiResponse } from 'next'
import { drive } from '@googleapis/drive'
2022-05-13 09:18:25 -07:00
import { OAuth2Client } from 'googleapis-common'
import { getAuthenticatedGoogleClient } from 'libs/google-sheets'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
import { setUser, withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from 'services/api/utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
setUser({ email: user.email ?? undefined, id: user.id })
if (req.method === 'GET') {
const credentialsId = req.query.credentialsId as string | undefined
if (!credentialsId) return badRequest(res)
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
if (!auth)
return res.status(404).send("Couldn't find credentials in database")
2022-02-25 18:30:14 +01:00
const response = await drive({
version: 'v3',
2022-05-13 09:18:25 -07:00
auth: auth.client as unknown as OAuth2Client,
}).files.list({
q: "mimeType='application/vnd.google-apps.spreadsheet'",
fields: 'nextPageToken, files(id, name)',
})
2022-02-25 18:30:14 +01:00
return res.send(response.data)
}
return methodNotAllowed(res)
}
2022-02-14 10:57:57 +01:00
export default withSentry(handler)