feat: Add moer element
Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
20
src/middleware/apiAuth.ts
Normal file
20
src/middleware/apiAuth.ts
Normal 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()
|
||||
})
|
18
src/middleware/apiRateLimit.ts
Normal file
18
src/middleware/apiRateLimit.ts
Normal 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
7
src/middleware/index.ts
Normal 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)
|
9
src/middleware/responseBuilder.ts
Normal file
9
src/middleware/responseBuilder.ts
Normal 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()
|
||||
})
|
Reference in New Issue
Block a user