Skip to content

Commit

Permalink
types: RouterClient + Response
Browse files Browse the repository at this point in the history
  • Loading branch information
peterphanouvong committed Nov 12, 2023
1 parent 76d2972 commit afab467
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 21 deletions.
43 changes: 36 additions & 7 deletions src/handlers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {setup} from './setup';
import {register} from './register';
import AppRouterClient from '../routerClients/AppRouterClient';
import PagesRouterClient from '../routerClients/PagesRouterClient';
import {NextRequest} from 'next/server';
import RouterClient from '../routerClients/RouterClient';

/**
* @type {Record<string,(routerClient: RouterClient) => Promise<void>>}
*/
const routeMap = {
create_org: createOrg,
register,
Expand All @@ -17,6 +22,11 @@ const routeMap = {
kinde_callback: callback
};

/**
*
* @param {string} endpoint
* @returns
*/
const getRoute = (endpoint) => {
return routeMap[endpoint];
};
Expand All @@ -27,39 +37,58 @@ const getRoute = (endpoint) => {
export default (request, endpoint) => {
// For backwards compatibility in app router
if (typeof request == 'object' && typeof endpoint == 'string') {
// @ts-ignore
return appRouterHandler(request, {params: {kindeAuth: endpoint}});
}
/**
*
* @param {Request} [req]
* @param {Response} [res]
* @returns {Response}
* @param {import('next').NextApiRequest | Request} req
* @param {import('next').NextApiResponse | Response} res
*/
return async function handler(req, res) {
return isAppRouter(req)
? appRouterHandler(req, res)
: pagesRouterHandler(req, res);
? // @ts-ignore
appRouterHandler(req, res)
: // @ts-ignore
pagesRouterHandler(req, res);
};
};

/**
*
* @param {NextRequest} req
* @param {{params: {kindeAuth: string}}} res
* @returns
*/
const appRouterHandler = async (req, res) => {
const {params} = res;
let endpoint = params.kindeAuth;
endpoint = Array.isArray(endpoint) ? endpoint[0] : endpoint;
const route = getRoute(endpoint);

return route
? await route(new AppRouterClient(req, res))
? // @ts-ignore
await route(new AppRouterClient(req, res))
: new Response('This page could not be found.', {status: 404});
};

/**
*
* @param {import('next').NextApiRequest} req
* @param {import('next').NextApiResponse} res
* @returns
*/
const pagesRouterHandler = async (req, res) => {
let {
query: {kindeAuth: endpoint}
} = req;
endpoint = Array.isArray(endpoint) ? endpoint[0] : endpoint;
if (!endpoint) {
throw Error('Please check your Kinde setup');
}
const route = getRoute(endpoint);
return route
? await route(new PagesRouterClient(req, res))
? // @ts-ignore
await route(new PagesRouterClient(req, res))
: res.status(404).end();
};
36 changes: 32 additions & 4 deletions src/routerClients/AppRouterClient.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {createKindeServerClient} from '@kinde-oss/kinde-typescript-sdk';
import {cookies} from 'next/headers';
import {redirect} from 'next/navigation';
import {NextResponse} from 'next/server';
import {NextRequest} from 'next/server';
import {config} from '../config/index';
import {appRouterSessionManager} from '../session/sessionManager';
import RouterClient from './RouterClient';

export default class AppRouterClient {
// @ts-ignore
export default class AppRouterClient extends RouterClient {
/**
*
* @param {NextRequest} req
* @param {*} res
*/
constructor(req, res) {
super();
this.kindeClient = createKindeServerClient(
config.grantType,
config.clientOptions
Expand All @@ -17,22 +25,42 @@ export default class AppRouterClient {
this.searchParams = req.nextUrl.searchParams;
}

/**
*
* @param {string} url
* @returns
*/
redirect(url) {
return redirect(url);
}

/**
*
* @returns {URL}
*/
getUrl() {
return this.url;
}

/**
*
* @param {object} data
* @param {{status: number}} status
* @returns
*/
json(data, status) {
return NextResponse.json(data, status);
return Response.json(data, status);
}

error() {
return NextResponse.error;
return Response.error;
}

/**
*
* @param {string} key
* @returns
*/
getSearchParam(key) {
return this.req.nextUrl.searchParams.get(key);
}
Expand Down
34 changes: 29 additions & 5 deletions src/routerClients/PagesRouterClient.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import {createKindeServerClient} from '@kinde-oss/kinde-typescript-sdk';
import {config} from '../config/index';
import {sessionManager} from '../session/sessionManager';
import RouterClient from './RouterClient';

export default class PagesRouterClient {
// @ts-ignore
export default class PagesRouterClient extends RouterClient {
/**
*
* @param {import('next').NextApiRequest} req
* @param {import('next').NextApiResponse} res
*/
constructor(req, res) {
super();
this.kindeClient = createKindeServerClient(
config.grantType,
config.clientOptions
);
// @ts-ignore
this.url = new URL(config.redirectURL + req.url);
this.res = res;
this.req = req;
this.searchParams = this.url.searchParams;
this.sessionManager = sessionManager(req, res);
}

/**
*
* @param {string} url
* @returns
*/
redirect(url) {
return this.res.redirect(url.href ? url.href : url);
return this.res.redirect(url);
}

getUrl() {
return this.url;
}

json(data, status = 200) {
return this.res.status(status).json(data);
/**
*
* @param {object} data
* @param {{status: number}} status
* @returns
*/
json(data, status) {
return this.res.status(status.status).json(data);
}

/**
*
* @param {string} key
* @returns {string | null}
*/
getSearchParam(key) {
return this.url.searchParams.get(key);
}
Expand Down
49 changes: 44 additions & 5 deletions src/routerClients/RouterClient.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,62 @@
export default class AppRouterClient {
import {GrantType} from '@kinde-oss/kinde-typescript-sdk';
import {NextResponse} from 'next/server';

export default class RouterClient {
constructor() {
if (this.constructor == AppRouterClient) {
if (this.constructor == RouterClient) {
throw new Error("Abstract classes can't be instantiated.");
}
/** @type {import('../../types').KindeClient} */
this.kindeClient;
/** @type {URL} */
this.url;
/** @type {import('@kinde-oss/kinde-typescript-sdk').SessionManager} */
this.sessionManager;
/** @type {import('next').NextApiResponse | *} */
this.res;
/** @type {import('next').NextApiRequest | NextResponse | *} */
this.req;
/** @type {URLSearchParams} */
this.searchParams;
}

redirect() {
/**
*
* @param {string} url
* @returns
*/
redirect(url) {
throw new Error("Method 'redirect()' must be implemented.");
}

json() {
/**
*
* @param {object} data
* @param {{status: number}} [status]
* @returns
*/
json(data, status) {
throw new Error("Method 'json()' must be implemented.");
}

error() {
throw new Error("Method 'error()' must be implemented.");
}

getSearchParam() {
/**
*
* @returns {URL}
*/
getUrl() {
throw new Error("Method 'getUrl()' must be implemented.");
}

/**
*
* @param {string} key
* @returns {string | null}
*/
getSearchParam(key) {
throw new Error("Method 'getSearchParam()' must be implemented.");
}
}

0 comments on commit afab467

Please sign in to comment.