Skip to content

Commit

Permalink
🔧 Added tests for Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dmdin committed May 21, 2024
1 parent a93ae07 commit 4b4599e
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 171 deletions.
16 changes: 10 additions & 6 deletions src/middlewares/sveltekit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Middleware } from '../types';
import type { Middleware, Event } from '../types';


/**
Expand All @@ -13,19 +13,23 @@ import type { Middleware } from '../types';
* composer.use(sveltekitMiddleware());
* ```
*/
export function sveltekitMiddleware() {
const middleware: Middleware = async (
event: Record<string, unknown> | { request: Request; locals: unknown },
ctx: Record<string, unknown>,

type SvelteKitEvent = Event & { request: Request; locals: object }
type Locals = Record<string, unknown>

export function sveltekitMiddleware () {
const middleware: Middleware<SvelteKitEvent, Locals, Locals > = async (
event: SvelteKitEvent,
ctx: Locals,
next: CallableFunction
) => {
if (!event?.request || !event?.locals) {
throw TypeError('Use this middleware only with SvelteKit. Pass RequestEvent to exec function');
}
// @ts-expect-error json method is defined by Request object
ctx.body = await event.request.json();
Object.assign(ctx, event.locals);
next();

};
return middleware
}
Expand Down
Loading

0 comments on commit 4b4599e

Please sign in to comment.