use lib/api defaultHandler

This commit is contained in:
Deepak Prabhakara 2024-04-29 22:29:49 +01:00
parent 8fbc1e8db7
commit 03e28973c0
22 changed files with 102 additions and 289 deletions

View File

@ -3,19 +3,14 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import stream from 'stream';
import { promisify } from 'util';
import { defaultHandler } from '@lib/api';
const pipeline = promisify(stream.pipeline);
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Display the metadata for the SAML federation

View File

@ -2,21 +2,12 @@ import { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${req.method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get SAML federated apps filtered by the product

View File

@ -1,20 +1,13 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
case 'DELETE':
return await handleDELETE(req, res);
case 'PATCH':
return await handlePATCH(req, res);
default:
res.setHeader('Allow', 'GET, DELETE, PATCH');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
PATCH: handlePATCH,
DELETE: handleDELETE,
});
}
// Get directory by id

View File

@ -1,20 +1,11 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${req.method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get webhook events

View File

@ -1,24 +1,13 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
case 'DELETE':
await handleDELETE(req, res);
break;
default:
res.setHeader('Allow', 'GET, DELETE');
res.status(405).json({ error: { message: `Method ${req.method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
DELETE: handleDELETE,
});
}
// Get webhook events

View File

@ -1,16 +1,11 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get a group by id

View File

@ -1,17 +1,12 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the list of members (user_id only) in a group

View File

@ -1,17 +1,12 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the groups

View File

@ -1,23 +1,12 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
try {
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the connections filtered by the product

View File

@ -1,32 +1,16 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import type { SetupLinkService } from '@boxyhq/saml-jackson';
import jackson from '@lib/jackson';
import { defaultHandler } from '@lib/api';
const service: SetupLinkService = 'dsync';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
case 'POST':
await handlePOST(req, res);
break;
case 'DELETE':
await handleDELETE(req, res);
break;
default:
res.setHeader('Allow', 'GET, POST, DELETE');
res.status(405).json({
error: { message: `Method ${req.method} Not Allowed` },
});
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
POST: handlePOST,
DELETE: handleDELETE,
});
}
// Get the setup link by ID or (tenant + product + service) combination

View File

@ -2,26 +2,14 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import type { SetupLinkService } from '@boxyhq/saml-jackson';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
const service: SetupLinkService = 'dsync';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
default:
res.setHeader('Allow', 'GET');
res.status(405).json({
error: { message: `Method ${req.method} Not Allowed` },
});
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the setup links filtered by the product

View File

@ -1,16 +1,11 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get a user by id

View File

@ -1,17 +1,12 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the users

View File

@ -1,20 +1,11 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${req.method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the saml trace by id

View File

@ -1,20 +1,11 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${req.method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the sso traces filtered by the product

View File

@ -1,15 +1,21 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
await defaultHandler(req, res, {
GET: handleGET,
DELETE: handleDELETE,
});
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
case 'DELETE':
await handleDelete(req, res);
await handleDELETE(req, res);
break;
default:
res.setHeader('Allow', 'GET,DELETE');
@ -36,7 +42,7 @@ const handleGET = async (req: NextApiRequest, res: NextApiResponse) => {
res.json(traces);
};
const handleDelete = async (req: NextApiRequest, res: NextApiResponse) => {
const handleDELETE = async (req: NextApiRequest, res: NextApiResponse) => {
const { adminController } = await jackson();
const { product } = req.query as {

View File

@ -1,23 +1,12 @@
import jackson from '@lib/jackson';
import { NextApiRequest, NextApiResponse } from 'next';
import type { GetConnectionsQuery } from '@boxyhq/saml-jackson';
import { defaultHandler } from '@lib/api';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
try {
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Check if a connection exists

View File

@ -1,23 +1,12 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
try {
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the connections filtered by the product

View File

@ -1,32 +1,16 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import type { SetupLinkService } from '@boxyhq/saml-jackson';
import jackson from '@lib/jackson';
import { defaultHandler } from '@lib/api';
const service: SetupLinkService = 'sso';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
case 'POST':
await handlePOST(req, res);
break;
case 'DELETE':
await handleDELETE(req, res);
break;
default:
res.setHeader('Allow', 'GET, POST, DELETE');
res.status(405).json({
error: { message: `Method ${req.method} Not Allowed` },
});
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
POST: handlePOST,
DELETE: handleDELETE,
});
}
// Get the setup link by ID or (tenant + product + service) combination

View File

@ -2,26 +2,14 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import type { SetupLinkService } from '@boxyhq/saml-jackson';
import jackson from '@lib/jackson';
import { parsePaginateApiParams } from '@lib/utils';
import { defaultHandler } from '@lib/api';
const service: SetupLinkService = 'sso';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
switch (req.method) {
case 'GET':
await handleGET(req, res);
break;
default:
res.setHeader('Allow', 'GET');
res.status(405).json({
error: { message: `Method ${req.method} Not Allowed` },
});
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
// Get the setup links filtered by the product

View File

@ -1,34 +1,25 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
try {
switch (method) {
case 'GET':
return await handleGET(req, res);
default:
res.setHeader('Allow', 'GET');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
GET: handleGET,
});
}
const handleGET = async (req: NextApiRequest, res: NextApiResponse) => {
const { connectionAPIController, directorySyncController } = await jackson();
const { connectionAPIController, directorySyncController, samlFederatedController } = await jackson();
const sso_connections_count = await connectionAPIController.getCount();
const dsync_connections_count = await directorySyncController.directories.getCount();
const idfed_apps_count = samlFederatedController.app.getCount();
return res.json({
data: {
sso_connections: sso_connections_count,
dsync_connections: dsync_connections_count,
idfed_apps: idfed_apps_count,
},
});
};

View File

@ -1,23 +1,12 @@
import { defaultHandler } from '@lib/api';
import jackson from '@lib/jackson';
import { NextApiRequest, NextApiResponse } from 'next';
import { IndexNames } from 'npm/src/controller/utils';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req;
try {
switch (method) {
case 'POST':
return await handlePOST(req, res);
default:
res.setHeader('Allow', 'POST');
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
}
} catch (error: any) {
const { message, statusCode = 500 } = error;
return res.status(statusCode).json({ error: { message } });
}
await defaultHandler(req, res, {
POST: handlePOST,
});
}
const handlePOST = async (req: NextApiRequest, res: NextApiResponse) => {