policies.ts33 lines · main
1import { NextApiRequest, NextApiResponse } from 'next'
2
3import { fetchGet } from '@/data/fetchers'
4import { constructHeaders } from '@/lib/api/apiHelpers'
5import apiWrapper from '@/lib/api/apiWrapper'
6import { PG_META_URL } from '@/lib/constants'
7
8export default (req: NextApiRequest, res: NextApiResponse) =>
9 apiWrapper(req, res, handler, { withAuth: true })
10
11async function handler(req: NextApiRequest, res: NextApiResponse) {
12 const { method } = req
13
14 switch (method) {
15 case 'GET':
16 return handleGetAll(req, res)
17 default:
18 res.setHeader('Allow', ['GET'])
19 res.status(405).json({ error: { message: `Method ${method} Not Allowed` } })
20 }
21}
22
23const handleGetAll = async (req: NextApiRequest, res: NextApiResponse) => {
24 const headers = constructHeaders(req.headers)
25 const response = await fetchGet(`${PG_META_URL}/policies`, { headers })
26
27 if (response.error) {
28 const { code, message } = response.error
29 return res.status(code).json({ message })
30 } else {
31 return res.status(200).json(response)
32 }
33}