import { type PlatformaticContext, type WhereClause, } from '@platformatic/sql-mapper' import { type FastifyPluginAsync } from 'fastify' import { type FastifyUserPluginOptions } from 'fastify-user' import { FastifyError } from '@fastify/error' export type OperationFunction = (args: { user: T, ctx: PlatformaticContext, where: WhereClause }) => WhereClause export interface OperationChecks { checks: Record | WhereClause } export interface OperationFields { fields?: string[] } export type Operation = boolean | OperationFields & (OperationFunction | OperationChecks) export type DefaultsFunction = (args: { user: T, ctx: PlatformaticContext, input: Object[] }) => any export type Defaults = DefaultsFunction | Record interface AuthorizationRuleBase { role: string defaults?: Defaults find?: Operation save?: Operation delete?: Operation insert?: Operation updateMany?: Operation } export interface AuthorizationRuleEntity extends AuthorizationRuleBase { entity: string } export interface AuthorizationRuleEntities extends AuthorizationRuleBase { entities: string[] } export type AuthorizationRule = AuthorizationRuleEntity | AuthorizationRuleEntities export type SetupDBAuthorizationUserDecorator = () => Promise export type AddRulesForRoles = (rules: Iterable>) => void export type RoleMergeStrategy = 'first-match' | 'most-permissive' export interface DBAuthorizationPluginOptions extends FastifyUserPluginOptions { adminSecret?: string roleKey?: string isRolePath?: boolean anonymousRole?: string roleMergeStrategy?: RoleMergeStrategy rules?: Array> } export interface DBAuthorizationPluginInterface { addRulesForRoles: AddRulesForRoles } declare module 'fastify' { interface FastifyRequest { setupDBAuthorizationUser: SetupDBAuthorizationUserDecorator } } declare const auth: FastifyPluginAsync export default auth /** * All the errors thrown by the plugin. */ export namespace errors { export const Unauthorized: () => FastifyError export const UnauthorizedField: (field: string) => FastifyError export const MissingNotNullableError: (field: string, entity: string) => FastifyError }