-
import { route, routeOperation, TypedNextResponse } from 'next-rest-framework';
import { z } from 'zod';
import { db } from '@/libs/DB';
import { authors } from '@/models/BooksSchema.tables';
import { AddAuthorSchema } from '@/validations/AuthorValidation';
// Example dynamic app router route handler with GET/DELETE handlers.
export const { POST } = route({
createTodo: routeOperation({
method: 'POST',
// Optional OpenAPI operation documentation.
openApiOperation: {
tags: ['author'],
},
})
// Input schema for strictly-typed request, request validation and OpenAPI documentation.
.input({
contentType: 'application/json',
body: AddAuthorSchema,
})
// Output schema for strictly-typed responses and OpenAPI documentation.
.outputs([
{
status: 201,
contentType: 'application/json',
schema: z.object({
id: z.number().optional(),
}),
},
{
status: 401,
contentType: 'application/json',
schema: z.string(),
},
{
status: 422,
contentType: 'application/json',
schema: z.string(),
},
{
status: 500,
contentType: 'application/json',
schema: z.object({}),
},
])
.middleware(
// Optional middleware logic executed before request validation.
(req) => {
if (!req.headers.get('authorization')) {
return TypedNextResponse.json('Unauthorized', {
status: 401,
});
}
return undefined;
},
)
.handler(async (req) => {
try {
const body = await req.json(); // Strictly-typed request.
const author = await db.insert(authors).values(body).returning();
return TypedNextResponse.json(
{
id: author[0]?.id,
},
{
status: 201,
},
);
} catch (error) {
if (error instanceof z.ZodError) {
return TypedNextResponse.json(error.format(), { status: 422 });
}
return TypedNextResponse.json({}, { status: 500 });
}
}),
}); and the errors I'm having are Exported variable 'POST' has or is using name 'INTERNALS' from external module "/home/proj/node_modules/next-rest-framework/dist/index" but cannot be named.ts(4023) any insights? When using example app for todos everything is fine, but that app using monorepo using pnpm if I recall correctly? When I hover over type of POST in todo app, I get this result const POST: {
(req: NextRequest, context: {
params: BaseParams;
}): Promise<TypedNextResponseType<unknown, number, AnyContentTypeWithAutocompleteForMostCommonOnes> | NextResponse<...>>;
_getPathsForRoute(route: string): Promise<...>;
} but when I hover over POST in my code, I just get those 2 errors instead |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I believe you're asking on the wrong repo. Your example is using https://github.com/blomqma/next-rest-framework/tree/master/packages/next-rest-framework; |
Beta Was this translation helpful? Give feedback.
I believe you're asking on the wrong repo. Your example is using https://github.com/blomqma/next-rest-framework/tree/master/packages/next-rest-framework;