mocksaml/pages/api/apps/index.ts

42 lines
955 B
TypeScript
Raw Normal View History

2022-01-13 18:13:25 +00:00
import { promises as fs } from 'fs';
2022-01-13 17:07:44 +00:00
import type { NextApiRequest, NextApiResponse } from 'next';
2022-01-13 18:13:25 +00:00
import path from 'path';
2022-02-05 17:04:55 +00:00
import { apps, metadata } from '../../../services';
2022-01-13 17:07:44 +00:00
import type { App, IdPMetadata } from '../../../types';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<App | App[] | IdPMetadata | null>
) {
2022-02-05 17:04:55 +00:00
switch (req.method) {
case 'GET':
return await getAllApps();
case 'POST':
return await createApp();
default:
return res.status(405).end(`Method ${req.method} Not Allowed`);
2022-01-13 17:07:44 +00:00
}
2022-02-05 17:05:37 +00:00
// Get all apps
2022-02-05 17:04:55 +00:00
async function getAllApps() {
const appList = await apps.getAll();
return res.json(appList);
}
2022-02-05 17:05:37 +00:00
// Create a new app
2022-02-05 17:04:55 +00:00
async function createApp() {
2022-01-13 17:07:44 +00:00
const {
2022-02-05 17:04:55 +00:00
name,
2022-01-13 17:50:16 +00:00
acs_url,
entity_id,
2022-01-13 17:07:44 +00:00
description = null,
} = req.body;
2022-02-05 17:04:55 +00:00
const app = await apps.create(name, description, acs_url, entity_id);
2022-01-13 17:07:44 +00:00
2022-02-05 17:04:55 +00:00
return res.json(app);
2022-01-13 17:07:44 +00:00
}
}