mocksaml/pages/api/apps/index.ts
2022-02-05 22:35:56 +05:30

40 lines
883 B
TypeScript

import type { NextApiRequest, NextApiResponse } from 'next';
import { apps } from '../../../services';
import type { App, IdPMetadata } from '../../../types';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<App | App[] | IdPMetadata | null>
) {
switch (req.method) {
case 'GET':
return await getAllApps();
case 'POST':
return await createApp();
default:
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
// Get all apps
async function getAllApps() {
const appList = await apps.getAll();
return res.json(appList);
}
// Create a new app
async function createApp() {
const {
name,
acs_url,
entity_id,
description = null,
} = req.body;
const app = await apps.create(name, description, acs_url, entity_id);
return res.json(app);
}
}