prettier
This commit is contained in:
1 parent
39ae2aed37
commit
1de7bc4146
126 files changed
+1240
-992
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
import { nanoid } from "nanoid";
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
export const addRequestId: NextMiddleware = async (_req, res, next) => {
|
||||
// Apply header
|
||||
res.setHeader("X-Response-ID", nanoid());
|
||||
// Let remaining middleware and API route execute
|
||||
await next();
|
||||
};
|
||||
// Apply header
|
||||
res.setHeader("Calcom-Response-ID", nanoid());
|
||||
// Let remaining middleware and API route execute
|
||||
await next();
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
import * as Sentry from "@sentry/nextjs";
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
export const captureErrors: NextMiddleware = async (_req, res, next) => {
|
||||
try {
|
||||
@@ -7,10 +7,8 @@ export const captureErrors: NextMiddleware = async (_req, res, next) => {
|
||||
// middleware and the API route handler
|
||||
await next();
|
||||
} catch (err) {
|
||||
const eventId = Sentry.captureException(err);
|
||||
console.log(eventId)
|
||||
Sentry.captureException(err);
|
||||
res.status(500);
|
||||
res.json({ error: err });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
export const httpMethod = (
|
||||
allowedHttpMethod: "GET" | "POST" | "PATCH" | "DELETE"
|
||||
): NextMiddleware => {
|
||||
export const httpMethod = (allowedHttpMethod: "GET" | "POST" | "PATCH" | "DELETE"): NextMiddleware => {
|
||||
return async function (req, res, next) {
|
||||
if (req.method === allowedHttpMethod || req.method == "OPTIONS") {
|
||||
await next();
|
||||
} else {
|
||||
res.status(404);
|
||||
res.status(405).json({ message: `Only ${allowedHttpMethod} Method allowed` });
|
||||
res.end();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const postOnly = httpMethod("POST");
|
||||
export const getOnly = httpMethod("GET");
|
||||
export const patchOnly = httpMethod("PATCH");
|
||||
export const deleteOnly = httpMethod("DELETE");
|
||||
export const HTTP_POST = httpMethod("POST");
|
||||
export const HTTP_GET = httpMethod("GET");
|
||||
export const HTTP_PATCH = httpMethod("PATCH");
|
||||
export const HTTP_DELETE = httpMethod("DELETE");
|
||||
+14
-11
@@ -1,20 +1,23 @@
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
// import { nanoid } from "nanoid";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
const dateInPast = function (firstDate: Date, secondDate: Date) {
|
||||
if (firstDate.setHours(0, 0, 0, 0) <= secondDate.setHours(0, 0, 0, 0)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (firstDate.setHours(0, 0, 0, 0) <= secondDate.setHours(0, 0, 0, 0)) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
const today = new Date();
|
||||
|
||||
export const verifyApiKey: NextMiddleware = async (req, res, next) => {
|
||||
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.apiKey as string } });
|
||||
if (!apiKey) {
|
||||
res.status(400).json({ error: 'Your api key is not valid' });
|
||||
throw new Error('No api key found');
|
||||
}
|
||||
if (apiKey.expiresAt && dateInPast(apiKey.expiresAt, today)) await next();
|
||||
else res.status(400).json({ error: 'Your api key is not valid' });
|
||||
const apiKey = await prisma.apiKey.findUnique({ where: { id: req.query.apiKey as string } });
|
||||
if (!apiKey) {
|
||||
res.status(400).json({ error: "Your api key is not valid" });
|
||||
throw new Error("No api key found");
|
||||
}
|
||||
if (apiKey.expiresAt && apiKey.userId && dateInPast(apiKey.expiresAt, today)) {
|
||||
res.setHeader("Calcom-User-ID", apiKey.userId);
|
||||
await next();
|
||||
} else res.status(400).json({ error: "Your api key is not valid" });
|
||||
};
|
||||
@@ -1,19 +1,22 @@
|
||||
import { label } from "next-api-middleware";
|
||||
|
||||
import { addRequestId } from "./addRequestid";
|
||||
import { captureErrors } from "./captureErrors";
|
||||
import { HTTP_POST, HTTP_DELETE, HTTP_PATCH, HTTP_GET, httpMethod } from "./httpMethods";
|
||||
import { verifyApiKey } from "./verifyApiKey";
|
||||
import { postOnly, deleteOnly, patchOnly, getOnly } from "./httpMethods";
|
||||
|
||||
const withMiddleware = label(
|
||||
{
|
||||
getOnly,
|
||||
patchOnly,
|
||||
postOnly,
|
||||
deleteOnly,
|
||||
HTTP_GET,
|
||||
HTTP_PATCH,
|
||||
HTTP_POST,
|
||||
HTTP_DELETE,
|
||||
addRequestId,
|
||||
verifyApiKey,
|
||||
sentry: captureErrors, // <-- Optionally alias middleware
|
||||
sentry: captureErrors,
|
||||
httpMethod: httpMethod("GET" || "DELETE" || "PATCH" || "POST"),
|
||||
},
|
||||
["sentry","verifyApiKey"] // <-- Provide a list of middleware to call automatically
|
||||
["sentry", "verifyApiKey", "httpMethod", "addRequestId"] // <-- Provide a list of middleware to call automatically
|
||||
);
|
||||
|
||||
export { withMiddleware };
|
||||
export { withMiddleware };
|
||||
Reference in new issue
Block a user