Skip to content

Commit

Permalink
♻️ refactor: refactor async params route to adapt next15 breaking cha…
Browse files Browse the repository at this point in the history
…nge (lobehub#4905)

* ♻️ refactor: refactor async route params

* ✅ test: fix test

* revert

* fix tests

* Update index.ts
  • Loading branch information
arvinxx authored Dec 7, 2024
1 parent 757a28e commit 5d61950
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/app/(backend)/middleware/auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ vi.mock('@/utils/server/jwt', () => ({
describe('checkAuth', () => {
const mockHandler: RequestHandler = vi.fn();
const mockRequest = new Request('https://example.com');
const mockOptions = { params: { provider: 'mock' } };
const mockOptions = { params: Promise.resolve({ provider: 'mock' }) };

beforeEach(() => {
vi.clearAllMocks();
Expand Down
5 changes: 3 additions & 2 deletions src/app/(backend)/middleware/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getJWTPayload } from '@/utils/server/jwt';
import { checkAuthMethod } from './utils';

type CreateRuntime = (jwtPayload: JWTPayload) => AgentRuntime;
type RequestOptions = { createRuntime?: CreateRuntime; params: { provider: string } };
type RequestOptions = { createRuntime?: CreateRuntime; params: Promise<{ provider: string }> };

export type RequestHandler = (
req: Request,
Expand Down Expand Up @@ -56,7 +56,8 @@ export const checkAuth =

const error = errorContent || e;

return createErrorResponse(errorType, { error, ...res, provider: options.params?.provider });
const params = await options.params;
return createErrorResponse(errorType, { error, ...res, provider: params?.provider });
}

return handler(req, { ...options, jwtPayload });
Expand Down
6 changes: 5 additions & 1 deletion src/app/(backend)/webapi/assistant/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { AssistantStore } from '@/server/modules/AssistantStore';

export const runtime = 'edge';

export const GET = async (req: Request, { params }: { params: { id: string } }) => {
type Params = Promise<{ id: string }>;

export const GET = async (req: Request, segmentData: { params: Params }) => {
const params = await segmentData.params;

const { searchParams } = new URL(req.url);

const locale = searchParams.get('locale');
Expand Down
12 changes: 6 additions & 6 deletions src/app/(backend)/webapi/chat/[provider]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ afterEach(() => {
describe('POST handler', () => {
describe('init chat model', () => {
it('should initialize AgentRuntime correctly with valid authorization', async () => {
const mockParams = { provider: 'test-provider' };
const mockParams = Promise.resolve({ provider: 'test-provider' });

// 设置 getJWTPayload 和 initAgentRuntimeWithUserPayload 的模拟返回值
vi.mocked(getJWTPayload).mockResolvedValueOnce({
Expand All @@ -83,7 +83,7 @@ describe('POST handler', () => {
});

it('should return Unauthorized error when LOBE_CHAT_AUTH_HEADER is missing', async () => {
const mockParams = { provider: 'test-provider' };
const mockParams = Promise.resolve({ provider: 'test-provider' });
const requestWithoutAuthHeader = new Request(new URL('https://test.com'), {
method: 'POST',
body: JSON.stringify({ model: 'test-model' }),
Expand All @@ -110,7 +110,7 @@ describe('POST handler', () => {
azureApiVersion: 'v1',
});

const mockParams = { provider: 'test-provider' };
const mockParams = Promise.resolve({ provider: 'test-provider' });
// 设置 initAgentRuntimeWithUserPayload 的模拟返回值
vi.mocked(getAuth).mockReturnValue({} as any);
vi.mocked(checkAuthMethod).mockReset();
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('POST handler', () => {
});

it('should return InternalServerError error when throw a unknown error', async () => {
const mockParams = { provider: 'test-provider' };
const mockParams = Promise.resolve({ provider: 'test-provider' });
vi.mocked(getJWTPayload).mockRejectedValueOnce(new Error('unknown error'));

const response = await POST(request, { params: mockParams });
Expand All @@ -166,7 +166,7 @@ describe('POST handler', () => {
userId: 'abc',
});

const mockParams = { provider: 'test-provider' };
const mockParams = Promise.resolve({ provider: 'test-provider' });
const mockChatPayload = { message: 'Hello, world!' };
request = new Request(new URL('https://test.com'), {
headers: { [LOBE_CHAT_AUTH_HEADER]: 'Bearer some-valid-token' },
Expand All @@ -192,7 +192,7 @@ describe('POST handler', () => {
azureApiVersion: 'v1',
});

const mockParams = { provider: 'test-provider' };
const mockParams = Promise.resolve({ provider: 'test-provider' });
const mockChatPayload = { message: 'Hello, world!' };
request = new Request(new URL('https://test.com'), {
headers: { [LOBE_CHAT_AUTH_HEADER]: 'Bearer some-valid-token' },
Expand Down
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/chat/[provider]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getTracePayload } from '@/utils/trace';
export const runtime = 'edge';

export const POST = checkAuth(async (req: Request, { params, jwtPayload, createRuntime }) => {
const { provider } = params;
const { provider } = await params;

try {
// ============ 1. init chat model ============ //
Expand Down
4 changes: 3 additions & 1 deletion src/app/(backend)/webapi/chat/anthropic/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('Anthropic POST function tests', () => {
it('should call UniverseRoute with correct parameters', async () => {
const mockRequest = new Request('https://example.com', { method: 'POST' });
await POST(mockRequest);
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, { params: { provider: 'anthropic' } });
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, {
params: Promise.resolve({ provider: 'anthropic' }),
});
});
});
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/chat/anthropic/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const preferredRegion = [
];

export const POST = async (req: Request) =>
UniverseRoute(req, { params: { provider: 'anthropic' } });
UniverseRoute(req, { params: Promise.resolve({ provider: 'anthropic' }) });
4 changes: 3 additions & 1 deletion src/app/(backend)/webapi/chat/google/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('Google POST function tests', () => {
it('should call UniverseRoute with correct parameters', async () => {
const mockRequest = new Request('https://example.com', { method: 'POST' });
await POST(mockRequest);
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, { params: { provider: 'google' } });
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, {
params: Promise.resolve({ provider: 'google' }),
});
});
});
3 changes: 2 additions & 1 deletion src/app/(backend)/webapi/chat/google/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export const preferredRegion = [
'gru1',
];

export const POST = async (req: Request) => UniverseRoute(req, { params: { provider: 'google' } });
export const POST = async (req: Request) =>
UniverseRoute(req, { params: Promise.resolve({ provider: 'google' }) });
4 changes: 3 additions & 1 deletion src/app/(backend)/webapi/chat/minimax/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('Minimax POST function tests', () => {
it('should call UniverseRoute with correct parameters', async () => {
const mockRequest = new Request('https://example.com', { method: 'POST' });
await POST(mockRequest);
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, { params: { provider: 'minimax' } });
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, {
params: Promise.resolve({ provider: 'minimax' }),
});
});
});
3 changes: 2 additions & 1 deletion src/app/(backend)/webapi/chat/minimax/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { POST as UniverseRoute } from '../[provider]/route';

export const runtime = 'nodejs';

export const POST = async (req: Request) => UniverseRoute(req, { params: { provider: 'minimax' } });
export const POST = async (req: Request) =>
UniverseRoute(req, { params: Promise.resolve({ provider: 'minimax' }) });
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/chat/models/[provider]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const noNeedAPIKey = (provider: string) =>
[ModelProvider.OpenRouter, ModelProvider.TogetherAI].includes(provider as any);

export const GET = checkAuth(async (req, { params, jwtPayload }) => {
const { provider } = params;
const { provider } = await params;

try {
const hasDefaultApiKey = jwtPayload.apiKey || 'dont-need-api-key-for-model-list';
Expand Down
4 changes: 3 additions & 1 deletion src/app/(backend)/webapi/chat/openai/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('OpenAI POST function tests', () => {
it('should call UniverseRoute with correct parameters', async () => {
const mockRequest = new Request('https://example.com', { method: 'POST' });
await POST(mockRequest);
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, { params: { provider: 'openai' } });
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, {
params: Promise.resolve({ provider: 'openai' }),
});
});
});
3 changes: 2 additions & 1 deletion src/app/(backend)/webapi/chat/openai/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export const preferredRegion = [
'syd1',
];

export const POST = async (req: Request) => UniverseRoute(req, { params: { provider: 'openai' } });
export const POST = async (req: Request) =>
UniverseRoute(req, { params: Promise.resolve({ provider: 'openai' }) });
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/chat/wenxin/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Wenxin POST function tests', () => {
await POST(mockRequest);
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, {
createRuntime: expect.anything(),
params: { provider: 'wenxin' },
params: Promise.resolve({ provider: 'wenxin' }),
});
});
});
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/chat/wenxin/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const POST = async (req: Request) =>

return new AgentRuntime(instance);
},
params: { provider: ModelProvider.Wenxin },
params: Promise.resolve({ provider: ModelProvider.Wenxin }),
});
2 changes: 1 addition & 1 deletion src/app/(backend)/webapi/text-to-image/[provider]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const preferredRegion = [
// );

export const POST = checkAuth(async (req: Request, { params, jwtPayload }) => {
const { provider } = params;
const { provider } = await params;

try {
// ============ 1. init chat model ============ //
Expand Down

0 comments on commit 5d61950

Please sign in to comment.