-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
190 additions
and
66 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum HttpMethod { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
DELETE = "DELETE", | ||
HEAD = "HEAD", | ||
CONNECT = "CONNECT", | ||
OPTIONS = "OPTIONS", | ||
TRACE = "TRACE", | ||
PATCH = "PATCH", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export interface IMiddlewareOptions { | ||
req: NextApiRequest; | ||
res: NextApiResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,67 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { createClient } from '@node-redis/client'; | ||
import { HttpMethod } from '@/enums/httpMethod.enum'; | ||
import { IMiddlewareOptions } from '@/interfaces/middleware.interface'; | ||
import { Middlewares } from '@/types/middleware.type'; | ||
|
||
const client = createClient(); | ||
|
||
const redisKey = 'apiKeys'; | ||
|
||
const middlewares: Middlewares = { | ||
[HttpMethod.GET]: getKeys, | ||
[HttpMethod.POST]: postKey, | ||
[HttpMethod.DELETE]: deleteKey, | ||
} | ||
|
||
|
||
|
||
async function getKeys(options: IMiddlewareOptions) { | ||
const { res } = options; | ||
const elements = await client.sMembers(redisKey); | ||
res.status(200).json(elements); | ||
} | ||
|
||
async function postKey(options: IMiddlewareOptions) { | ||
const { res } = options; | ||
await client.sAdd(redisKey, uuidv4()); | ||
res.status(200).end(); | ||
} | ||
|
||
async function deleteKey(options: IMiddlewareOptions) { | ||
const { res, req } = options; | ||
const { apiKey } = req.query; | ||
if (typeof apiKey === 'string') { | ||
await client.sRem(redisKey, apiKey); | ||
res.status(200).end(); | ||
} else { | ||
res.status(400).json({ message: 'Invalid request' }); | ||
} | ||
} | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!client.isOpen) { | ||
await client.connect(); | ||
} | ||
|
||
if (req.method === 'POST') { | ||
await client.sAdd(redisKey, uuidv4()); | ||
res.status(200).end(); | ||
const method = req.method as HttpMethod; | ||
|
||
} else if (req.method === 'GET') { | ||
const elements = await client.sMembers(redisKey); | ||
res.status(200).json(elements); | ||
} else if (req.method === 'DELETE') { | ||
const { apiKey } = req.query; | ||
if (typeof apiKey === 'string') { | ||
await client.sRem(redisKey, apiKey); | ||
res.status(200).end(); | ||
|
||
} else { | ||
res.status(400).json({ message: 'Invalid request' }); | ||
} | ||
} else { | ||
if (!Object.keys(middlewares).includes(method)) { | ||
res.setHeader('Allow', ['POST', 'GET', 'DELETE']); | ||
res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
|
||
const middleware = middlewares[method]; | ||
|
||
if (!middleware) { | ||
return res.status(500).end() | ||
} | ||
|
||
try { | ||
await middleware({ req, res }); | ||
} catch (error) { | ||
console.error(JSON.stringify(error)); | ||
return res.status(500).end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { WagonService } from '@/service/wagon.service'; | ||
import { createClient } from "@node-redis/client"; | ||
|
||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
const { query } = req; | ||
const apiKey = query.apiKey as string; | ||
const redisKey = 'apiKeys'; | ||
|
||
const client = createClient(); | ||
|
||
if (!client.isOpen) { | ||
await client.connect(); | ||
} | ||
|
||
const validation = await client.sIsMember(redisKey, apiKey); | ||
|
||
if (!apiKey || !validation) { | ||
return res.status(401).json({ error: 'Invalid API key' }); | ||
} | ||
|
||
const wagons = await WagonService.getAll(); | ||
return res.status(200).json(wagons); | ||
|
||
} catch (error) { | ||
console.error('Error retrieving wagon:', error); | ||
return res.status(500).json({ error: 'Internal server error' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { HttpMethod } from "@/enums/httpMethod.enum"; | ||
import { IMiddlewareOptions } from "@/interfaces/middleware.interface"; | ||
|
||
export type Middlewares = Partial< | ||
Record<HttpMethod, (options: IMiddlewareOptions) => any> | ||
> |