feathers-casl
Version:
Add access control with CASL to your feathers application.
40 lines (36 loc) • 1.31 kB
text/typescript
import { makeDefaultOptions as makeDefaultAuthorizeHookOptions } from './hooks/authorize/authorize.hook.utils.js'
import { makeDefaultOptions as makeDefaultChannelsOptions } from './channels/channels.utils.js'
import type { Application } from '@feathersjs/feathers'
import type { PartialDeep } from 'type-fest'
import type {
AuthorizeHookOptions,
ChannelOptions,
InitOptions,
} from './types.js'
export const initialize = (
options?: PartialDeep<InitOptions>,
): ((app: Application) => void) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
if (options?.version) {
// asserts that you call app.configure(casl({})) instead of app.configure(casl)
throw new Error(
"You passed 'feathers-casl' to app.configure() without a function. You probably wanted to call app.configure(casl({}))!",
)
}
options = {
defaultAdapter: options?.defaultAdapter || '@feathersjs/memory',
authorizeHook: makeDefaultAuthorizeHookOptions(
options?.authorizeHook as undefined | Partial<AuthorizeHookOptions>,
),
channels: makeDefaultChannelsOptions(
options?.channels as undefined | Partial<ChannelOptions>,
),
}
return (app: Application): void => {
if (app.get('casl')) {
return
}
app.set('casl', options)
}
}