Skip to content

Commit

Permalink
fix(src): replace AppRouteUserlandModule with looser type
Browse files Browse the repository at this point in the history
closes #1006
addresses #1005
  • Loading branch information
Xunnamius committed Mar 2, 2024
1 parent a9a69a3 commit 502e666
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'node:http';

import type { NextApiHandler } from 'next';
import type { NextRequest } from 'next/server';

// ? Next expects AsyncLocalStorage to be globally available IMMEDIATELY! So
// ? this line should happen before any imports of Next.js packages.
Expand Down Expand Up @@ -137,7 +138,15 @@ export interface NtarhInitAppRouter<NextResponseJsonType = unknown>
* documentation](https://nextjs.org/docs/app/building-your-application/routing/route-handlers)
* for details.
*/
appHandler: import('next/dist/server/future/route-modules/app-route/module').AppRouteUserlandModule;
appHandler: Omit<
import('next/dist/server/future/route-modules/app-route/module').AppRouteUserlandModule,
keyof import('next/dist/server/future/route-modules/app-route/module').AppRouteHandlers
> & {
[key in keyof import('next/dist/server/future/route-modules/app-route/module').AppRouteHandlers]?: (
req: NextRequest,
context?: any
) => any;
};
pagesHandler?: undefined;
/**
* `params` is passed directly to the handler and represents processed dynamic
Expand Down
25 changes: 24 additions & 1 deletion test/unit-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { testApiHandler } from 'universe/index';

import { parse, serialize } from 'cookie';
import { cookies, headers } from 'next/headers';
import { NextResponse } from 'next/server';
import { NextResponse, type NextRequest } from 'next/server';

import { withMockedOutput } from 'testverse/setup';

Expand Down Expand Up @@ -1442,6 +1442,29 @@ describe('::testApiHandler', () => {
}
});
});

it('does not throw any AppRouteUserlandModule-related type errors', async () => {
expect.hasAssertions();

const appHandler = {
async GET(req: NextRequest, { params }: { params: { recipeId: string } }) {
const { recipeId } = params;
expect(req).toBeDefined();
expect(recipeId).toBe('1');
return NextResponse.json({});
}
};

await testApiHandler({
rejectOnHandlerError: true,
// This test "fails" if there is a TS error (caught by CI/linting)
appHandler,
params: { recipeId: '1' },
async test({ fetch }) {
await expect(fetch()).resolves.toBeDefined();
}
});
});
});

describe('<pages router>', () => {
Expand Down

0 comments on commit 502e666

Please sign in to comment.