Skip to content

Commit

Permalink
Bug Fix: Pass Next server to tokenHandler to fix build issues with ex…
Browse files Browse the repository at this point in the history
…perimental-app-router (#1836)

* Bug Fix: Fix wierd issue with import in experimental-app-router

* Update Deps

* Tests: Fix unit tests in app router

* Update test in TokenHandler
  • Loading branch information
theodesp authored Mar 12, 2024
1 parent aee31a5 commit 42ded80
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 119 deletions.
174 changes: 67 additions & 107 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/experimental-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"concurrently": "^8.2.0",
"jest": "^29.6.2",
"jest-environment-jsdom": "^29.6.2",
"next": "^14.0.1",
"next": "^14.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^5.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { notFound } from 'next/navigation.js';
import * as server from 'next/server.js';
import { tokenHandler } from './tokenHandler.js';

export async function GetFn(req: Request) {
Expand All @@ -7,7 +8,7 @@ export async function GetFn(req: Request) {
switch (pathname) {
case '/api/faust/token/':
case '/api/faust/token': {
return tokenHandler(req);
return tokenHandler(req, server);
}
default: {
return notFound();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cookies } from 'next/headers.js';
import { NextResponse } from 'next/server.js';
import * as server from 'next/server.js';
import { getWpUrl, getWpSecret } from '../../faust-core-utils.js';

export type AuthorizeResponse = {
Expand All @@ -9,7 +9,7 @@ export type AuthorizeResponse = {
refreshTokenExpiration: number;
};

export async function tokenHandler(req: Request) {
export async function tokenHandler(req: Request, s: typeof server) {
try {
const secretKey = getWpSecret();

Expand Down Expand Up @@ -79,7 +79,7 @@ export async function tokenHandler(req: Request) {
* and expiration.
*/

const res = new NextResponse(JSON.stringify(data), {
const res = new s.NextResponse(JSON.stringify(data), {
status: 200,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as tokenHandler from '../../../src/server/routeHandler/tokenHandler';
jest.mock('next/headers.js');
import { cookies } from 'next/headers.js';
import fetchMock from 'fetch-mock';
import { NextRequest } from 'next/server';
import * as server from 'next/server';

// // https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-893763840
const nextHeaders = { cookies };
Expand All @@ -27,7 +27,7 @@ describe('tokenHandler', () => {
it('throws a 500 error if the secret key is not set', async () => {
const req = new Request('http://localhost:3000/api/faust/token');

const response = await tokenHandler.tokenHandler(req);
const response = await tokenHandler.tokenHandler(req, server);

expect(response.status).toBe(500);
expect(await response.json()).toStrictEqual({
Expand All @@ -49,7 +49,7 @@ describe('tokenHandler', () => {

const req = new Request('http://localhost:3000/api/faust/token');

const response = await tokenHandler.tokenHandler(req);
const response = await tokenHandler.tokenHandler(req, server);

expect(response.status).toBe(401);
expect(await response.json()).toStrictEqual({
Expand All @@ -74,11 +74,11 @@ describe('tokenHandler', () => {
status: 401,
});

const req = new NextRequest(
const req = new server.NextRequest(
new Request('http://localhost:3000/api/faust/token'),
);

const response = await tokenHandler.tokenHandler(req);
const response = await tokenHandler.tokenHandler(req, server);

expect(response.status).toBe(401);
expect(await response.json()).toStrictEqual({ error: 'Unauthorized' });
Expand Down Expand Up @@ -122,11 +122,11 @@ describe('tokenHandler', () => {
},
);

const req = new NextRequest(
const req = new server.NextRequest(
new Request('http://localhost:3000/api/faust/token?code=my-code'),
);

const response = await tokenHandler.tokenHandler(req);
const response = await tokenHandler.tokenHandler(req, server);

expect(response.status).toBe(200);
expect(await response.json()).toStrictEqual(validResponse);
Expand Down

0 comments on commit 42ded80

Please sign in to comment.