Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have 2 ts(4023) errors for POST operation exported from route #156

Open
llamerr opened this issue Apr 12, 2024 · 5 comments
Open

I have 2 ts(4023) errors for POST operation exported from route #156

llamerr opened this issue Apr 12, 2024 · 5 comments

Comments

@llamerr
Copy link

llamerr commented Apr 12, 2024

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)
Exported variable 'POST' has or is using name 'NrfOasData' 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? Not sure if it can be related to that in any way (shared TS types or something?)

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

@llamerr llamerr changed the title I have 2 ts(4023) errors for POST operation exported from route #1008 I have 2 ts(4023) errors for POST operation exported from route Apr 12, 2024
@llamerr
Copy link
Author

llamerr commented Apr 24, 2024

So I finally fixed all other not related errors in my test project and can confirm that it generates docs without problems, despite showing that typescript error in editor. When I moved my test project into next-rest-framework project inside apps, that error stopped appearing, so it's resolved when used as sub-project inside pnpm monorepo

williamstein added a commit to sagemathinc/cocalc that referenced this issue May 1, 2024
@williamstein
Copy link

I'm also hitting this NrfOasData issue

pages/api/v2/compute/create-server.ts:50:1 - error TS4082: Default export of the module has or is using private name 'NrfOasData'.

 50 export default apiRoute({
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 51   createServer: apiRouteOperation({
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... 
 76     .handler(handle),
    ~~~~~~~~~~~~~~~~~~~~~
 77 });
    ~~~

I'm working around it for now by applying the following horrible ugly hack, which is to patch the declaration file using sed before using typescript the first time, via this (idempotent) package.json script:

...
  "scripts": {
    "patch-next-rest-framework": "sed -i '/^interface NrfOasData {/s/^interface/export interface/' node_modules/next-rest-framework/dist/index.d.ts",
...

@blomqma
Copy link
Owner

blomqma commented May 4, 2024

Can you still reproduce this with v6.0.0?

@llamerr
Copy link
Author

llamerr commented May 4, 2024

Can you still reproduce this with v6.0.0?

Yes, just upgraded it and can still see those errors. I also see schema property on outputs is highlighted now, so I guess it was updated properly. Changed it to body in my code.

@j4tmr
Copy link

j4tmr commented Jul 23, 2024

tsconfig

"compilerOptions": {
  
}

"composite": false,
"declaration": true,
"declarationMap": true,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants