-
Notifications
You must be signed in to change notification settings - Fork 381
chore(vue): Improve error message when Clerk plugin is not installed #6719
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
Conversation
🦋 Changeset detectedLatest commit: d2d3155 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughAdds a named-source parameter to useClerkContext with centralized error handling via errorThrower and updated docs link. Updates all callsites to pass their identifier. Makes plugin.install resilient to undefined options. Introduces a unit test for missing-plugin errors, plus Vitest setup/config. Adds a changeset for a patch release. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Dev as App Component
participant Hook as useAuth/useUser/...
participant Ctx as useClerkContext(source)
participant Vue as Vue inject()
participant Err as errorThrower
Dev->>Hook: call composable
Hook->>Ctx: useClerkContext("useAuth")
Ctx->>Vue: inject(ClerkContextKey)
alt Context found
Vue-->>Ctx: ctx
Ctx-->>Hook: ctx
Hook-->>Dev: computed refs
else Context missing
Ctx->>Err: throw("@clerk/vue: useAuth can only be used when the Vue plugin is installed. ...")
Err-->>Dev: Error thrown
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/elements
@clerk/clerk-expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/clerk-react
@clerk/react-router
@clerk/remix
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/themes
@clerk/types
@clerk/upgrade
@clerk/vue
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/vue/src/plugin.ts (1)
41-57
: Fix unsafe access when pluginOptions is undefinedYou guard
initialState
withpluginOptions || {}
, but still readpluginOptions.sdkMetadata
, which will throw ifpluginOptions
is undefined. Merge with a default and use optional chaining.Apply:
- const { initialState } = pluginOptions || {}; + const { initialState } = pluginOptions ?? {}; const options = { - ...pluginOptions, - sdkMetadata: pluginOptions.sdkMetadata || SDK_METADATA, + ...(pluginOptions ?? {}), + sdkMetadata: pluginOptions?.sdkMetadata ?? SDK_METADATA, } as LoadClerkJsScriptOptions;
🧹 Nitpick comments (4)
packages/vue/src/composables/useSignIn.ts (1)
33-39
: De-duplicate the hook name stringUse a single SOURCE constant for both telemetry and error provenance to prevent drift.
Apply:
export const useSignIn: UseSignIn = () => { - const { clerk, clientCtx } = useClerkContext('useSignIn'); + const SOURCE = 'useSignIn' as const; + const { clerk, clientCtx } = useClerkContext(SOURCE); const unwatch = watch(clerk, value => { if (value) { - value.telemetry?.record(eventMethodCalled('useSignIn')); + value.telemetry?.record(eventMethodCalled(SOURCE)); unwatch(); } });packages/vue/src/composables/useUser.ts (1)
35-47
: Optional: avoid hardcoded hook nameMirror the useSignIn pattern with a SOURCE constant to avoid copy-paste mistakes across files.
export const useUser: UseUser = () => { - const { userCtx } = useClerkContext('useUser'); + const SOURCE = 'useUser' as const; + const { userCtx } = useClerkContext(SOURCE);packages/vue/src/composables/__tests__/useClerkContext.test.ts (2)
9-23
: Solid negative-path coverage; minor resilience tweakTest is precise and hides Vue’s inject warning. Consider building the expected prefix from PACKAGE_NAME to avoid hardcoding '@clerk/vue'.
- expect(() => render(Component)).toThrow( - '@clerk/vue: useAuth can only be used when the Vue plugin is installed. Learn more: https://clerk.com/docs/references/vue/clerk-plugin', - ); + const prefix = `${globalThis.PACKAGE_NAME}: `; + expect(() => render(Component)).toThrow( + `${prefix}useAuth can only be used when the Vue plugin is installed. Learn more: https://clerk.com/docs/references/vue/clerk-plugin`, + );
25-45
: Prevent accidental network/script loads during testsWhen installing the real plugin, plugin.ts may attempt to load ClerkJS in JSDOM. Mock the browser loader to a resolved promise to keep tests hermetic.
Add near the top of this test file (before imports that use the plugin), or in a dedicated test setup:
vi.mock('../../utils/loadClerkJsScript', () => ({ loadClerkJsScript: () => Promise.resolve(), })); vi.mock('../../utils/inBrowser', () => ({ inBrowser: () => false, // or true if you prefer, since load is mocked anyway }));Alternatively, add
afterEach(() => vi.restoreAllMocks())
to the suite to ensure mocks/spies are reset.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (15)
.changeset/fuzzy-rivers-tell.md
(1 hunks)packages/vue/src/components/controlComponents.ts
(1 hunks)packages/vue/src/composables/__tests__/useClerkContext.test.ts
(1 hunks)packages/vue/src/composables/useAuth.ts
(1 hunks)packages/vue/src/composables/useClerk.ts
(1 hunks)packages/vue/src/composables/useClerkContext.ts
(1 hunks)packages/vue/src/composables/useOrganization.ts
(1 hunks)packages/vue/src/composables/useSession.ts
(1 hunks)packages/vue/src/composables/useSessionList.ts
(1 hunks)packages/vue/src/composables/useSignIn.ts
(1 hunks)packages/vue/src/composables/useSignUp.ts
(1 hunks)packages/vue/src/composables/useUser.ts
(1 hunks)packages/vue/src/plugin.ts
(1 hunks)packages/vue/vitest.config.ts
(1 hunks)packages/vue/vitest.setup.ts
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (9)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
**/*.{js,jsx,ts,tsx}
: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels
Files:
packages/vue/vitest.setup.ts
packages/vue/src/composables/useUser.ts
packages/vue/src/composables/__tests__/useClerkContext.test.ts
packages/vue/src/composables/useSession.ts
packages/vue/vitest.config.ts
packages/vue/src/composables/useSignIn.ts
packages/vue/src/composables/useSessionList.ts
packages/vue/src/composables/useClerk.ts
packages/vue/src/composables/useClerkContext.ts
packages/vue/src/composables/useSignUp.ts
packages/vue/src/composables/useOrganization.ts
packages/vue/src/plugin.ts
packages/vue/src/composables/useAuth.ts
packages/vue/src/components/controlComponents.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/vue/vitest.setup.ts
packages/vue/src/composables/useUser.ts
packages/vue/src/composables/__tests__/useClerkContext.test.ts
packages/vue/src/composables/useSession.ts
packages/vue/vitest.config.ts
packages/vue/src/composables/useSignIn.ts
packages/vue/src/composables/useSessionList.ts
packages/vue/src/composables/useClerk.ts
packages/vue/src/composables/useClerkContext.ts
packages/vue/src/composables/useSignUp.ts
packages/vue/src/composables/useOrganization.ts
packages/vue/src/plugin.ts
packages/vue/src/composables/useAuth.ts
packages/vue/src/components/controlComponents.ts
packages/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/vue/vitest.setup.ts
packages/vue/src/composables/useUser.ts
packages/vue/src/composables/__tests__/useClerkContext.test.ts
packages/vue/src/composables/useSession.ts
packages/vue/vitest.config.ts
packages/vue/src/composables/useSignIn.ts
packages/vue/src/composables/useSessionList.ts
packages/vue/src/composables/useClerk.ts
packages/vue/src/composables/useClerkContext.ts
packages/vue/src/composables/useSignUp.ts
packages/vue/src/composables/useOrganization.ts
packages/vue/src/plugin.ts
packages/vue/src/composables/useAuth.ts
packages/vue/src/components/controlComponents.ts
packages/**/*.{ts,tsx,d.ts}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Packages should export TypeScript types alongside runtime code
Files:
packages/vue/vitest.setup.ts
packages/vue/src/composables/useUser.ts
packages/vue/src/composables/__tests__/useClerkContext.test.ts
packages/vue/src/composables/useSession.ts
packages/vue/vitest.config.ts
packages/vue/src/composables/useSignIn.ts
packages/vue/src/composables/useSessionList.ts
packages/vue/src/composables/useClerk.ts
packages/vue/src/composables/useClerkContext.ts
packages/vue/src/composables/useSignUp.ts
packages/vue/src/composables/useOrganization.ts
packages/vue/src/plugin.ts
packages/vue/src/composables/useAuth.ts
packages/vue/src/components/controlComponents.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
**/*.{ts,tsx}
: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoidany
type - preferunknown
when type is uncertain, then narrow with type guards
Useinterface
for object shapes that might be extended
Usetype
for unions, primitives, and computed types
Preferreadonly
properties for immutable data structures
Useprivate
for internal implementation details
Useprotected
for inheritance hierarchies
Usepublic
explicitly for clarity in public APIs
Preferreadonly
for properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Useconst assertions
for literal types:as const
Usesatisfies
operator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports:import type { ... } from ...
Noany
types without justification
Proper error handling with typed errors
Consistent use ofreadonly
for immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)
Files:
packages/vue/vitest.setup.ts
packages/vue/src/composables/useUser.ts
packages/vue/src/composables/__tests__/useClerkContext.test.ts
packages/vue/src/composables/useSession.ts
packages/vue/vitest.config.ts
packages/vue/src/composables/useSignIn.ts
packages/vue/src/composables/useSessionList.ts
packages/vue/src/composables/useClerk.ts
packages/vue/src/composables/useClerkContext.ts
packages/vue/src/composables/useSignUp.ts
packages/vue/src/composables/useOrganization.ts
packages/vue/src/plugin.ts
packages/vue/src/composables/useAuth.ts
packages/vue/src/components/controlComponents.ts
**/*.{js,ts,tsx,jsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.
Files:
packages/vue/vitest.setup.ts
packages/vue/src/composables/useUser.ts
packages/vue/src/composables/__tests__/useClerkContext.test.ts
packages/vue/src/composables/useSession.ts
packages/vue/vitest.config.ts
packages/vue/src/composables/useSignIn.ts
packages/vue/src/composables/useSessionList.ts
packages/vue/src/composables/useClerk.ts
packages/vue/src/composables/useClerkContext.ts
packages/vue/src/composables/useSignUp.ts
packages/vue/src/composables/useOrganization.ts
packages/vue/src/plugin.ts
packages/vue/src/composables/useAuth.ts
packages/vue/src/components/controlComponents.ts
packages/**/*.{test,spec}.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Unit tests should use Jest or Vitest as the test runner.
Files:
packages/vue/src/composables/__tests__/useClerkContext.test.ts
**/__tests__/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/typescript.mdc)
**/__tests__/**/*.{ts,tsx}
: Create type-safe test builders/factories
Use branded types for test isolation
Implement proper mock types that match interfaces
Files:
packages/vue/src/composables/__tests__/useClerkContext.test.ts
.changeset/**
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Automated releases must use Changesets.
Files:
.changeset/fuzzy-rivers-tell.md
🧬 Code graph analysis (11)
packages/vue/src/composables/useUser.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/__tests__/useClerkContext.test.ts (2)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)packages/vue/src/plugin.ts (1)
clerkPlugin
(39-123)
packages/vue/src/composables/useSession.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/useSignIn.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/useSessionList.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/useClerk.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/useClerkContext.ts (2)
packages/vue/src/keys.ts (1)
ClerkInjectionKey
(5-5)packages/vue/src/errors/errorThrower.ts (1)
errorThrower
(6-6)
packages/vue/src/composables/useSignUp.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/useOrganization.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/composables/useAuth.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
packages/vue/src/components/controlComponents.ts (1)
packages/vue/src/composables/useClerkContext.ts (1)
useClerkContext
(6-16)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Integration Tests (billing, chrome)
🔇 Additional comments (14)
.changeset/fuzzy-rivers-tell.md (1)
1-6
: Changeset looks goodPatch bump and concise summary are appropriate for this scope.
packages/vue/vitest.config.ts (1)
8-8
: Good addition of setupFilesThe relative path is correct given file locations; this will ensure globals are stubbed before tests run.
packages/vue/vitest.setup.ts (2)
1-6
: LGTM on test setupUsing
vi.stubGlobal
is a clean way to supply PACKAGE_NAME/VERSION for runtime code in tests.
5-6
: resolveJsonModule already enabled
Thepackages/vue/tsconfig.json
hascompilerOptions.resolveJsonModule
set totrue
, so importingpackage.json
works as expected.packages/vue/src/composables/useClerk.ts (1)
21-22
: Passing source to useClerkContext is correctThis aligns with the improved error messaging and central handling.
packages/vue/src/composables/useOrganization.ts (1)
56-56
: Clearer, actionable error context — nice.Passing 'useOrganization' into useClerkContext improves the missing-plugin error and debuggability.
packages/vue/src/composables/useSignUp.ts (1)
33-33
: Good: scoped source name for error messaging.Supplying 'useSignUp' to useClerkContext aligns with the new API and yields clearer guidance when the plugin is absent.
packages/vue/src/composables/useAuth.ts (2)
77-77
: LGTM: explicit source improves diagnostics.useClerkContext('useAuth') matches the updated signature and enhances the error text.
77-77
: AlluseClerkContext
calls include a source string and the plugin docs link is present.packages/vue/src/composables/useSessionList.ts (1)
35-35
: Nice consistency with the new API.Providing 'useSessionList' to useClerkContext keeps errors specific and actionable.
packages/vue/src/composables/useSession.ts (1)
36-36
: Consistent, clearer errors — approved.useClerkContext('useSession') adheres to the refactor and improves DX on misconfiguration.
packages/vue/src/components/controlComponents.ts (1)
40-40
: Approve: No remaining zero-argument useClerkContext calls found
Search confirms there are no zero-argument calls touseClerkContext
in packages/vue.packages/vue/src/composables/useUser.ts (1)
35-35
: LGTMPassing 'useUser' to useClerkContext aligns with the new signature and improves error clarity.
packages/vue/src/composables/useClerkContext.ts (1)
6-16
: Add explicit return type for useClerkContextPer TS guidelines, public APIs should declare explicit return types. Derive the injected context type from the InjectionKey to avoid importing internal types.
import { inject } from 'vue'; +import type { InjectionKey } from 'vue'; import { errorThrower } from '../errors/errorThrower'; import { ClerkInjectionKey } from '../keys'; +type ClerkContext = typeof ClerkInjectionKey extends InjectionKey<infer T> ? T : never; -export function useClerkContext(source: string) { +export function useClerkContext(source: string): ClerkContext { const ctx = inject(ClerkInjectionKey); if (!ctx) { return errorThrower.throw( `${source} can only be used when the Vue plugin is installed. Learn more: https://clerk.com/docs/references/vue/clerk-plugin`, ); } return ctx; }errorThrower.throw is already typed to return
never
, so the annotated return type is satisfied.
export const useClerk = () => { | ||
const { clerk } = useClerkContext(); | ||
const { clerk } = useClerkContext('useClerk'); | ||
|
||
return clerk; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add an explicit return type for the public API
Per repo guidelines, declare the return type.
Example:
+import type { ShallowRef } from 'vue';
+import type { Clerk } from '@clerk/types';
-export const useClerk = () => {
+export const useClerk = (): ShallowRef<Clerk | null> => {
const { clerk } = useClerkContext('useClerk');
return clerk;
};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export const useClerk = () => { | |
const { clerk } = useClerkContext(); | |
const { clerk } = useClerkContext('useClerk'); | |
return clerk; | |
}; | |
import type { ShallowRef } from 'vue'; | |
import type { Clerk } from '@clerk/types'; | |
export const useClerk = (): ShallowRef<Clerk | null> => { | |
const { clerk } = useClerkContext('useClerk'); | |
return clerk; | |
}; |
🤖 Prompt for AI Agents
In packages/vue/src/composables/useClerk.ts around lines 20 to 24, the exported
useClerk function is missing an explicit return type; update the signature to
declare the return type (for example: export const useClerk = ():
ReturnType<typeof useClerkContext>['clerk'] => { ... }), or alternatively
import/declare the concrete Clerk type and use that, and ensure any necessary
type imports are added at the top of the file.
Description
Checklist
pnpm test
runs as expected.pnpm build
runs as expected.Type of change
Summary by CodeRabbit