feat: Add moer element

Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
2023-06-27 18:30:44 +02:00
parent 4cd2a365ae
commit d76f412b82
19 changed files with 540 additions and 90 deletions

20
src/middleware/apiAuth.ts Normal file
View File

@ -0,0 +1,20 @@
import { defineMiddleware } from "astro/middleware"
import { validateAuth } from '../libs/validateAuth'
// `context` and `next` are automatically typed
export default defineMiddleware(async (context, next) => {
if (!context.request.url.includes('api')) {
return next()
}
const auth = await validateAuth(context.request, {
name: 'slicing.slice',
api: true,
cookie: true
})
if (typeof auth === 'object') {
return auth
}
context.locals.authKey = auth
return next()
})

View File

@ -0,0 +1,18 @@
import { defineMiddleware } from "astro/middleware"
import RateLimiter from '../libs/RateLimiter'
// `context` and `next` are automatically typed
export default defineMiddleware(async ({ request, locals }, next) => {
if (!request.url.includes('api')) {
return next()
}
const limit = RateLimiter.getInstance().consume(locals.authKey as string)
if ('status' in limit) {
return limit
}
locals.responseBuilder.addHeaders(limit)
return next()
})

7
src/middleware/index.ts Normal file
View File

@ -0,0 +1,7 @@
import { sequence } from "astro/middleware"
import apiAuth from './apiAuth'
import apiRateLimit from './apiRateLimit'
import responseBuilder from './responseBuilder'
export const onRequest = sequence(responseBuilder, apiAuth, apiRateLimit)

View File

@ -0,0 +1,9 @@
import { defineMiddleware } from "astro/middleware"
import ResponseBuilder from '../libs/ResponseBuilder'
// `context` and `next` are automatically typed
export default defineMiddleware(async (context, next) => {
context.locals.responseBuilder = new ResponseBuilder()
return next()
})