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 auth handler types when using custom handlers #1327

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"expect-type": "^0.16.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-environment-node-single-context": "^27.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HandleLogout } from './logout';
import { HandleCallback } from './callback';
import { HandleProfile } from './profile';
import { HandlerError } from '../utils/errors';
import { AppRouteHandlerFn, AppRouteHandlerFnContext, Handler } from './router-helpers';
import { AppRouteHandlerFn, AppRouteHandlerFnContext, AppRouterHandler, PageRouterHandler } from './router-helpers';
import { isRequest } from '../utils/req-helpers';

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ export type Handlers = ApiHandlers | ErrorHandlers;
/**
* @ignore
*/
type ApiHandlers = { [key: string]: Handler };
type ApiHandlers = { [key: string]: PageRouterHandler | AppRouterHandler };

/**
* @ignore
Expand Down
3 changes: 1 addition & 2 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ export {
AfterRefetchAppRoute
} from './profile';
export { default as handlerFactory, Handlers, HandleAuth, AppRouterOnError, PageRouterOnError } from './auth';
export { AppRouteHandlerFn } from './router-helpers';
export { AppRouteHandlerFnContext } from './router-helpers';
export { AppRouteHandlerFnContext, PageRouterHandler, AppRouterHandler } from './router-helpers';
3 changes: 3 additions & 0 deletions src/handlers/router-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export type AuthHandler<Opts> = Handler<Opts> & {
(options?: Opts): Handler<Opts>;
};

export type PageRouterHandler = (req: NextRequest, ctx: AppRouteHandlerFnContext) => Promise<Response> | Response;
export type AppRouterHandler = (req: NextApiRequest, res: NextApiResponse) => Promise<unknown> | unknown;

export type Handler<Opts = any> = {
(req: NextRequest, ctx: AppRouteHandlerFnContext, options?: Opts): Promise<Response> | Response;
(req: NextApiRequest, res: NextApiResponse, options?: Opts): Promise<unknown> | unknown;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/with-page-auth-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type PageRoute<P, Q extends ParsedUrlQuery = ParsedUrlQuery> = (
* @category Server
*/
export type AppRouterPageRouteOpts = {
params?: { slug: string };
params?: Record<string, string | string[]>;
searchParams?: { [key: string]: string | string[] | undefined };
};

Expand Down
5 changes: 4 additions & 1 deletion src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ export {
AfterRefetchPageRoute,
AfterRefetchAppRoute,
AppRouterOnError,
PageRouterOnError
PageRouterOnError,
AppRouteHandlerFnContext,
AppRouterHandler,
PageRouterHandler
} from './handlers';

export {
Expand Down
64 changes: 64 additions & 0 deletions tests/types.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';
import { NextApiRequest, NextApiResponse } from 'next';
import { NextRequest, NextResponse } from 'next/server';
import { expectTypeOf } from 'expect-type';
import { handleAuth, HandlerError, AppRouteHandlerFnContext, withPageAuthRequired } from '../src';

describe('types', () => {
test('should allow customisation of page router auth handlers', () => {
expectTypeOf(handleAuth).toBeCallableWith({
login(_req: NextApiRequest, _res: NextApiResponse) {}
});
});

test('should allow customisation of page router error handler', () => {
expectTypeOf(handleAuth).toBeCallableWith({
onError(_req: NextApiRequest, _res: NextApiResponse, _err: HandlerError) {}
});
});

test('should allow customisation of app router auth handlers', () => {
expectTypeOf(handleAuth).toBeCallableWith({
login(_req: NextRequest) {
return new NextResponse();
}
});
});

test('should allow customisation of app router auth handlers with context', () => {
expectTypeOf(handleAuth).toBeCallableWith({
login(_req: NextRequest, _ctx: AppRouteHandlerFnContext) {
return new NextResponse();
}
});
});

test('should allow customisation of app router auth handlers with context literal', () => {
expectTypeOf(handleAuth).toBeCallableWith({
login(_req: NextRequest, _ctx: { params: Record<string, string | string[]> }) {
return new NextResponse();
}
});
});

test('should allow withPageAuthRequired in app router', () => {
async function Page() {
return <span>Foo</span>;
}
expectTypeOf(withPageAuthRequired).toBeCallableWith(Page);
});

test('should allow withPageAuthRequired in app router with opts', () => {
async function Page() {
return <span>Foo</span>;
}
expectTypeOf(withPageAuthRequired).toBeCallableWith(Page, { returnTo: 'foo' });
});

test('should allow custom params in withPageAuthRequired', () => {
async function Page({ params }: { params?: Record<string, string | string[]> }) {
return <span>{typeof params}</span>;
}
expectTypeOf(withPageAuthRequired).toBeCallableWith(Page);
});
});