javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Export common types from all `/nextjs` packages

Open mikestopcontinues opened this issue 2 years ago • 3 comments

Package

  • [ ] @clerk/clerk-js
  • [ ] @clerk/clerk-react
  • [x] @clerk/nextjs
  • [ ] @clerk/clerk-remix
  • [ ] @clerk/clerk-expo
  • [ ] @clerk/backend-core
  • [ ] @clerk/clerk-sdk-node
  • [ ] @clerk/edge
  • [ ] other:

Description

As it stands, it's very difficult to get the types for common objects (like clerk, user, and session) out of /nextjs, /nextjs/api, and /nextjs/ssr. In the latter case, it's next to impossible. Here's one example:

import type {useUser} from '@clerk/nextjs';
type UserResource = NonNullable<ReturnType<typeof useUser>['user']>;

I think it would be helpful if all of the common return types were exported, especially since they seem to vary across packages.

Thanks!

mikestopcontinues avatar Mar 17 '22 14:03 mikestopcontinues

Let me tack on here that the types for withAuth and requireAuth are a little messy, both from /nextjs/api, but also up in /node-sdk.

At minimum, it would make a big difference if handler: Function was replaced with the res, req, next defs from just a few lines down.

Even better would be the generics used over in /nextjs/ssr so that (1) it could be extended by the nextjs package, and (2) so that session can be added.

mikestopcontinues avatar Mar 17 '22 15:03 mikestopcontinues

Hello there - many thanks for the feedback!

Every Clerk package depends on the public @clerk/types package - if you're using @clerk/nextjs this will also be installed. Could you try importing UserResource from @clerk/types instead? Eg:

import { UserResource } from "@clerk/types";

Your comments about withAuth/requireAuth are valid. Improving type support for the nextjs package was on our roadmap, but let me see if we can publish a first batch of improvements with the next beta release :)

nikosdouvlis avatar Mar 17 '22 23:03 nikosdouvlis

Hi @nikosdouvlis,

Thanks! I can get UserResource out of types. That said, I do still think it should be exported by the package that uses it.

Similar topic: where's the user and session objects that get passed in when using withServerSideAuth (e.g. User)? It's not the same as UserResource. Maybe a better question: Should they be the same shape?

Thanks again!

mikestopcontinues avatar Mar 19 '22 14:03 mikestopcontinues

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

clerk-cookie avatar Jun 14 '23 18:06 clerk-cookie

This issue was closed because it has been stalled for 10 days with no activity.

clerk-cookie avatar Jun 25 '23 00:06 clerk-cookie

@mikestopcontinues The objects passed in withServerSideAuth come from the @clerk/backend package types. The are 2 different types of resources for user for example, User and UserResource. The User type is related to the shape generated from an API call from your application server to Clerk server to retrieve an object with information about a user which contains private metadata and other private data that are meant to be used by the server of the application. The UserResource is related to the shape generated from the API call from the end-user Browser to the Clerk server to retrieve information based on the end-user session which will not contain private metadata for example. Based on the above we explicitly use different shapes in the withServerSideAuth and in the hooks exposed by our packages to mistakenly expose or expect the wrong data.

Example for user object

  • User https://github.com/clerkinc/javascript/blob/6fa67499b9c2ba578be811c35e21124f88facc8b/packages/backend/src/api/resources/User.ts#L7
  • UserResource : https://github.com/clerkinc/javascript/blob/6fa67499b9c2ba578be811c35e21124f88facc8b/packages/types/src/user.ts#L47

dimkl avatar Jun 26 '23 16:06 dimkl

Thanks, @dimkl. I appreciate the context, and I should've probably closed this issue a while back.

mikestopcontinues avatar Jun 26 '23 17:06 mikestopcontinues

while UserResource can be found from @clerk/types.. where can User be found? - the type coming from {currentUser}: '@clerk/nextjs/server' for example

@dimkl @mikestopcontinues

Thanks

danajerban avatar May 27 '24 00:05 danajerban

@danajerban The User, Organization, .... (other resources) are exported in from the @clerk/nextjs/server in our core-2 (>= 5) version.

dimkl avatar May 29 '24 11:05 dimkl

@dimkl thank you and apologies for commenting everywhere for this 😆

danajerban avatar May 29 '24 11:05 danajerban