Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/utils/check-context/check-context.util.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ type User = {
name: string
}

type Message = {
id: number
text: string
}

const app = feathers<{
users: MemoryService<User>
messages: MemoryService
messages: MemoryService<Message>
}>()

type App = typeof app
type Ctx = HookContext<App, MemoryService<User>>
type AppCtx = HookContext<App>
type UserCtx = HookContext<App, MemoryService<User>>

const context = {} as Ctx
const context = {} as UserCtx
const appContext = {} as AppCtx

it('options overload accepts valid options', () => {
checkContext(context, { type: 'before' })
Expand All @@ -34,6 +41,22 @@ it('options overload rejects invalid type', () => {
checkContext(context, { type: 'invalid' })
})

it('options overload accepts valid path for app-level context', () => {
checkContext(appContext, { path: 'users' })
checkContext(appContext, { path: 'messages' })
checkContext(appContext, { path: ['users', 'messages'] })
})

it('options overload rejects invalid path for service-specific context', () => {
// @ts-expect-error "messages" is not valid when context is narrowed to MemoryService<User>
checkContext(context, { path: 'messages' })
})

it('options overload rejects invalid path for app-level context', () => {
// @ts-expect-error "nonExistent" is not a valid service path
checkContext(appContext, { path: 'nonExistent' })
})

it('positional overload accepts valid args', () => {
checkContext(context, 'before')
checkContext(context, ['before', 'after'])
Expand Down
15 changes: 10 additions & 5 deletions src/utils/check-context/check-context.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
type IsContextOptions,
} from '../../predicates/is-context/is-context.predicate.js'

export type CheckContextOptions = IsContextOptions & {
label?: string
}
export type CheckContextOptions<H extends HookContext = HookContext> =
IsContextOptions<H> & {
label?: string
}

/**
* Validates that the hook context matches the expected type(s) and method(s).
Expand All @@ -30,7 +31,7 @@ export type CheckContextOptions = IsContextOptions & {
*/
export function checkContext<H extends HookContext = HookContext>(
context: H,
options: CheckContextOptions,
options: CheckContextOptions<NoInfer<H>>,
): void
export function checkContext<H extends HookContext = HookContext>(
context: H,
Expand All @@ -40,7 +41,11 @@ export function checkContext<H extends HookContext = HookContext>(
): void
export function checkContext<H extends HookContext = HookContext>(
context: H,
typeOrOptions?: HookType | HookType[] | CheckContextOptions | null,
typeOrOptions?:
| HookType
| HookType[]
| CheckContextOptions<NoInfer<H>>
| null,
methods?: MethodName | MethodName[] | null,
label = 'anonymous',
): void {
Expand Down
Loading