mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-07 12:57:47 +00:00
Add API for frameworks and examples (#3514)
* Add API for frameworks and examples * Adjust headers * Update frameworks list * Always use latest * Add types * Use now repo for downloading and listing * Use .existsSync * Remove unused packages * Use 307 for redirect * Add examples * Update tsconfig.json Co-Authored-By: Steven <steven@ceriously.com> * Make examples unique * Remove detectors from frameworks API * Use /api instead of Next.js * Install dependencies * Rename project * Change name * Empty * Change name * Update api/tsconfig.json Co-Authored-By: Steven <steven@ceriously.com> * Update examples Co-authored-by: Steven <steven@ceriously.com>
This commit is contained in:
29
api/_lib/util/with-api-handler.ts
Normal file
29
api/_lib/util/with-api-handler.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { NowRequest, NowResponse } from '@now/node';
|
||||
|
||||
type Handler = (req: NowRequest, res: NowResponse) => Promise<any>;
|
||||
|
||||
export function withApiHandler(handler: Handler): Handler {
|
||||
return async (req: NowRequest, res: NowResponse) => {
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET');
|
||||
res.setHeader(
|
||||
'Access-Control-Allow-Headers',
|
||||
'Authorization, Accept, Content-Type'
|
||||
);
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
return res.status(200).json({});
|
||||
}
|
||||
|
||||
if (req.method !== 'GET') {
|
||||
return res.status(404).json({
|
||||
error: {
|
||||
code: 'not_found',
|
||||
message: 'Only GET requests are supported for this endpoint.',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return handler(req, res);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user