Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fix path type #3

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @triyanox/next-middleware

## 0.1.4

### Patch Changes

- fix: fix the `Path<T>` for `RuleFunction` #2

## 0.1.3

### Patch Changes
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ const isAdmin: RuleFunction<Data, typeof routes> = ({ data, next, redirect }) =>
}
};

const isOwnWorkspace: RuleFunction<Data, typeof routes, '/dashboard/workspaces/[workspaceId]/*'> = ({
const isOwnWorkspace: RuleFunction<Data, typeof routes, '/dashboard/workspaces/[workspaceId]'> = ({
data,
next,
redirect,
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@triyanox/next-middleware",
"version": "0.1.3",
"version": "0.1.4",
"description": "The cleanest way to protect your Next.js routes with a middleware",
"license": "MIT",
"publishConfig": {
23 changes: 10 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { NextRequest, NextResponse } from 'next/server';
import { NextRequest, NextResponse, NextFetchEvent } from 'next/server';

type Path<R extends string> = R | (R extends '/' ? `${R}*` : `${R}/*`);

/**
* The type of the routes object
*/
type Routes = Record<string, { path: string }>;

type Routes = Record<
string,
{
path: string;
}
>;
/**
* Extracts the parameters from a dynamic path
*/
@@ -15,40 +18,35 @@ type ExtractParams<R extends string> = string extends R
: R extends `${infer _Start}/[${infer Param}]${infer _Rest}`
? [Param, ...ExtractParams<_Rest>]
: [];

/**
* Checks if a path has parameters
*/
type HasParams<R extends string> = ExtractParams<R> extends [] ? false : true;

/**
* Extracts the parameters from a dynamic path if it has any
*/
type ParamsObject<R extends string> =
HasParams<R> extends true ? Record<ExtractParams<R>[number], string> : never;

/**
* This is the type of the function that will be used to define the rules for each path
*/
type RuleFunction<
T,
RS extends Routes,
R extends Path<keyof RS & string> = keyof RS & string,
R extends keyof RS & string = keyof RS & string,
> = (options: {
data: T;
path: R;
params: ParamsObject<R>;
next: () => void;
redirect: (path: keyof RS & string) => void;
}) => Promise<void> | void;

/**
* Authorization rules for each path
*/
type AuthRules<T, RS extends Routes, R extends keyof RS & string> = Partial<
Record<Path<R>, RuleFunction<T, RS, R>[]>
Record<Path<keyof RS & string>, RuleFunction<T, RS, R>[]>
>;

type BaseOptions<R extends Routes, T> = {
/**
* Function to fetch data so that it can be used in the authorization rules
@@ -57,7 +55,7 @@ type BaseOptions<R extends Routes, T> = {
/**
* Authorization rules for each path
*/
rules: AuthRules<T, R, keyof R & string>;
rules: AuthRules<T, R, any>;
/**
* Paths that requiring authentication
* Those paths will be the root paths of all the existing paths
@@ -68,7 +66,6 @@ type BaseOptions<R extends Routes, T> = {
*/
onError?: (req: NextRequest) => Promise<NextResponse> | NextResponse;
};

type MiddlewareOptions<R extends Routes, T> = BaseOptions<R, T>;

export type {