@guren/server 0.2.0-alpha.7 → 1.0.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Application-DtWDHXr1.d.ts +2110 -0
- package/dist/BroadcastManager-AkIWUGJo.d.ts +466 -0
- package/dist/CacheManager-BkvHEOZX.d.ts +244 -0
- package/dist/ConsoleKernel-CqCVrdZs.d.ts +207 -0
- package/dist/EventManager-CmIoLt7r.d.ts +207 -0
- package/dist/Gate-CNkBYf8m.d.ts +268 -0
- package/dist/HealthManager-DUyMIzsZ.d.ts +141 -0
- package/dist/I18nManager-Dtgzsf5n.d.ts +270 -0
- package/dist/LogManager-7mxnkaPM.d.ts +256 -0
- package/dist/MailManager-DpMvYiP9.d.ts +292 -0
- package/dist/Scheduler-BstvSca7.d.ts +469 -0
- package/dist/StorageManager-oZTHqaza.d.ts +337 -0
- package/dist/api-token-JOif2CtG.d.ts +1792 -0
- package/dist/app-key-CsBfRC_Q.d.ts +214 -0
- package/dist/auth/index.d.ts +418 -0
- package/dist/auth/index.js +6742 -0
- package/dist/authorization/index.d.ts +129 -0
- package/dist/authorization/index.js +621 -0
- package/dist/broadcasting/index.d.ts +233 -0
- package/dist/broadcasting/index.js +907 -0
- package/dist/cache/index.d.ts +233 -0
- package/dist/cache/index.js +817 -0
- package/dist/encryption/index.d.ts +222 -0
- package/dist/encryption/index.js +602 -0
- package/dist/events/index.d.ts +155 -0
- package/dist/events/index.js +330 -0
- package/dist/health/index.d.ts +185 -0
- package/dist/health/index.js +379 -0
- package/dist/i18n/index.d.ts +101 -0
- package/dist/i18n/index.js +597 -0
- package/dist/index-9_Jzj5jo.d.ts +7 -0
- package/dist/index.d.ts +2628 -619
- package/dist/index.js +22229 -3116
- package/dist/lambda/index.d.ts +156 -0
- package/dist/lambda/index.js +91 -0
- package/dist/logging/index.d.ts +50 -0
- package/dist/logging/index.js +557 -0
- package/dist/mail/index.d.ts +288 -0
- package/dist/mail/index.js +695 -0
- package/dist/mcp/index.d.ts +139 -0
- package/dist/mcp/index.js +382 -0
- package/dist/notifications/index.d.ts +271 -0
- package/dist/notifications/index.js +741 -0
- package/dist/queue/index.d.ts +423 -0
- package/dist/queue/index.js +958 -0
- package/dist/runtime/index.d.ts +93 -0
- package/dist/runtime/index.js +834 -0
- package/dist/scheduling/index.d.ts +41 -0
- package/dist/scheduling/index.js +836 -0
- package/dist/storage/index.d.ts +196 -0
- package/dist/storage/index.js +832 -0
- package/dist/vite/index.js +203 -3
- package/package.json +93 -6
- package/dist/chunk-FK2XQSBF.js +0 -160
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { C as Container } from './Application-DtWDHXr1.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parsed argument definition.
|
|
5
|
+
*/
|
|
6
|
+
interface ArgumentDefinition {
|
|
7
|
+
name: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
required: boolean;
|
|
10
|
+
array: boolean;
|
|
11
|
+
defaultValue?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Parsed option definition.
|
|
15
|
+
*/
|
|
16
|
+
interface OptionDefinition {
|
|
17
|
+
name: string;
|
|
18
|
+
shortcut?: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
requiresValue: boolean;
|
|
21
|
+
defaultValue?: string | boolean;
|
|
22
|
+
array: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parsed signature.
|
|
26
|
+
*/
|
|
27
|
+
interface ParsedSignature {
|
|
28
|
+
name: string;
|
|
29
|
+
arguments: ArgumentDefinition[];
|
|
30
|
+
options: OptionDefinition[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Command class type.
|
|
34
|
+
*/
|
|
35
|
+
interface CommandClass {
|
|
36
|
+
signature: string;
|
|
37
|
+
description: string;
|
|
38
|
+
new (container?: Container): CommandInstance;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Command instance type.
|
|
42
|
+
*/
|
|
43
|
+
interface CommandInstance {
|
|
44
|
+
setInput(argv: string[]): void;
|
|
45
|
+
setOutput(output: OutputInterface): void;
|
|
46
|
+
setKernel(kernel: {
|
|
47
|
+
handle(argv: string[]): Promise<number>;
|
|
48
|
+
}): void;
|
|
49
|
+
run(): Promise<number>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Output interface.
|
|
53
|
+
*/
|
|
54
|
+
interface OutputInterface {
|
|
55
|
+
info(message: string): void;
|
|
56
|
+
error(message: string): void;
|
|
57
|
+
warn(message: string): void;
|
|
58
|
+
success(message: string): void;
|
|
59
|
+
line(message: string): void;
|
|
60
|
+
newLine(count?: number): void;
|
|
61
|
+
table(headers: string[], rows: string[][]): void;
|
|
62
|
+
write(message: string): void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Input interface.
|
|
66
|
+
*/
|
|
67
|
+
interface InputInterface {
|
|
68
|
+
argument<T = string>(name: string): T;
|
|
69
|
+
arguments(): Record<string, string | string[]>;
|
|
70
|
+
option<T = string | boolean>(name: string): T | undefined;
|
|
71
|
+
options(): Record<string, string | boolean | string[]>;
|
|
72
|
+
hasOption(name: string): boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Console kernel options.
|
|
76
|
+
*/
|
|
77
|
+
interface ConsoleKernelOptions {
|
|
78
|
+
container?: Container;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Schedule definition for a command.
|
|
82
|
+
*/
|
|
83
|
+
interface ScheduledCommand {
|
|
84
|
+
command: string;
|
|
85
|
+
args?: string[];
|
|
86
|
+
expression: string;
|
|
87
|
+
timezone?: string;
|
|
88
|
+
description?: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Interactive prompt types.
|
|
92
|
+
*/
|
|
93
|
+
interface PromptInterface {
|
|
94
|
+
ask(question: string, defaultValue?: string): Promise<string>;
|
|
95
|
+
confirm(question: string, defaultValue?: boolean): Promise<boolean>;
|
|
96
|
+
choice<T extends string>(question: string, choices: T[], defaultValue?: T): Promise<T>;
|
|
97
|
+
secret(question: string): Promise<string>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Progress bar interface.
|
|
101
|
+
*/
|
|
102
|
+
interface ProgressInterface {
|
|
103
|
+
start(total: number): void;
|
|
104
|
+
advance(step?: number): void;
|
|
105
|
+
finish(): void;
|
|
106
|
+
setProgress(current: number): void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Console kernel for managing and executing commands.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const kernel = new ConsoleKernel()
|
|
115
|
+
*
|
|
116
|
+
* kernel.register(CreateUserCommand)
|
|
117
|
+
* kernel.register(SendNotificationsCommand)
|
|
118
|
+
*
|
|
119
|
+
* // Execute command
|
|
120
|
+
* const exitCode = await kernel.handle(['users:create', 'john@example.com', '--admin'])
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare class ConsoleKernel {
|
|
124
|
+
/**
|
|
125
|
+
* Registered commands.
|
|
126
|
+
*/
|
|
127
|
+
protected commands: Map<string, CommandClass>;
|
|
128
|
+
/**
|
|
129
|
+
* Service container.
|
|
130
|
+
*/
|
|
131
|
+
protected container?: Container;
|
|
132
|
+
/**
|
|
133
|
+
* Output instance.
|
|
134
|
+
*/
|
|
135
|
+
protected output: OutputInterface;
|
|
136
|
+
constructor(options?: ConsoleKernelOptions);
|
|
137
|
+
/**
|
|
138
|
+
* Register a command.
|
|
139
|
+
*/
|
|
140
|
+
register(command: CommandClass): this;
|
|
141
|
+
/**
|
|
142
|
+
* Register multiple commands.
|
|
143
|
+
*/
|
|
144
|
+
registerMany(commands: CommandClass[]): this;
|
|
145
|
+
/**
|
|
146
|
+
* Get all registered commands.
|
|
147
|
+
*/
|
|
148
|
+
getCommands(): Map<string, CommandClass>;
|
|
149
|
+
/**
|
|
150
|
+
* Get a command by name.
|
|
151
|
+
*/
|
|
152
|
+
getCommand(name: string): CommandClass | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* Check if a command is registered.
|
|
155
|
+
*/
|
|
156
|
+
hasCommand(name: string): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Set the output instance.
|
|
159
|
+
*/
|
|
160
|
+
setOutput(output: OutputInterface): this;
|
|
161
|
+
/**
|
|
162
|
+
* Get the output instance.
|
|
163
|
+
*/
|
|
164
|
+
getOutput(): OutputInterface;
|
|
165
|
+
/**
|
|
166
|
+
* Handle command execution.
|
|
167
|
+
*/
|
|
168
|
+
handle(argv?: string[]): Promise<number>;
|
|
169
|
+
/**
|
|
170
|
+
* Run a command.
|
|
171
|
+
*/
|
|
172
|
+
protected runCommand(CommandClass: CommandClass, args: string[]): Promise<number>;
|
|
173
|
+
/**
|
|
174
|
+
* Show help.
|
|
175
|
+
*/
|
|
176
|
+
protected showHelp(): void;
|
|
177
|
+
/**
|
|
178
|
+
* Show help for a specific command.
|
|
179
|
+
*/
|
|
180
|
+
protected showCommandHelp(commandName: string): void;
|
|
181
|
+
/**
|
|
182
|
+
* List all commands.
|
|
183
|
+
*/
|
|
184
|
+
protected listCommands(): void;
|
|
185
|
+
/**
|
|
186
|
+
* Group commands by namespace.
|
|
187
|
+
*/
|
|
188
|
+
protected groupCommands(): Map<string, Map<string, CommandClass>>;
|
|
189
|
+
/**
|
|
190
|
+
* Suggest similar commands.
|
|
191
|
+
*/
|
|
192
|
+
protected suggestCommands(input: string): void;
|
|
193
|
+
/**
|
|
194
|
+
* Calculate string similarity (simple Dice coefficient).
|
|
195
|
+
*/
|
|
196
|
+
protected similarity(a: string, b: string): number;
|
|
197
|
+
/**
|
|
198
|
+
* Call a command programmatically.
|
|
199
|
+
*/
|
|
200
|
+
call(commandName: string, args?: string[], silent?: boolean): Promise<number>;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Create a console kernel.
|
|
204
|
+
*/
|
|
205
|
+
declare function createConsoleKernel(options?: ConsoleKernelOptions): ConsoleKernel;
|
|
206
|
+
|
|
207
|
+
export { type ArgumentDefinition as A, ConsoleKernel as C, type InputInterface as I, type OptionDefinition as O, type ParsedSignature as P, type ScheduledCommand as S, type CommandInstance as a, type OutputInterface as b, type CommandClass as c, type ConsoleKernelOptions as d, type ProgressInterface as e, type PromptInterface as f, createConsoleKernel as g };
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for all application events.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* class UserRegistered extends Event {
|
|
7
|
+
* constructor(
|
|
8
|
+
* public readonly userId: string,
|
|
9
|
+
* public readonly email: string
|
|
10
|
+
* ) {
|
|
11
|
+
* super()
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* // Emit the event
|
|
16
|
+
* await events.emit(new UserRegistered('123', 'user@example.com'))
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
declare abstract class Event {
|
|
20
|
+
/**
|
|
21
|
+
* Timestamp when the event was created.
|
|
22
|
+
*/
|
|
23
|
+
readonly timestamp: Date;
|
|
24
|
+
/**
|
|
25
|
+
* Get the event name (class name by default).
|
|
26
|
+
* Override this to customize the event name.
|
|
27
|
+
*/
|
|
28
|
+
static get eventName(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Get the event name from an instance.
|
|
31
|
+
*/
|
|
32
|
+
get eventName(): string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Event class constructor type.
|
|
37
|
+
*/
|
|
38
|
+
interface EventClass<T extends Event = Event> {
|
|
39
|
+
new (...args: any[]): T;
|
|
40
|
+
eventName: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Event listener function type.
|
|
44
|
+
*/
|
|
45
|
+
type EventListener<T extends Event = Event> = (event: T) => void | Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Options for registering an event listener.
|
|
48
|
+
*/
|
|
49
|
+
interface ListenerOptions {
|
|
50
|
+
/**
|
|
51
|
+
* If true, the listener will be removed after the first invocation.
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
once?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Priority of the listener. Higher values are executed first.
|
|
57
|
+
* @default 0
|
|
58
|
+
*/
|
|
59
|
+
priority?: number;
|
|
60
|
+
/**
|
|
61
|
+
* If specified, the listener will be dispatched to a queue instead of being executed immediately.
|
|
62
|
+
* Requires the Queue system to be configured.
|
|
63
|
+
*/
|
|
64
|
+
queue?: string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Internal representation of a registered listener.
|
|
68
|
+
*/
|
|
69
|
+
interface RegisteredListener<T extends Event = Event> {
|
|
70
|
+
listener: EventListener<T>;
|
|
71
|
+
options: ListenerOptions;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Event subscription handle for unsubscribing.
|
|
75
|
+
*/
|
|
76
|
+
interface EventSubscription {
|
|
77
|
+
/**
|
|
78
|
+
* Unsubscribe the listener.
|
|
79
|
+
*/
|
|
80
|
+
unsubscribe(): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Event manager for registering listeners and emitting events.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const events = new EventManager()
|
|
89
|
+
*
|
|
90
|
+
* // Register a listener
|
|
91
|
+
* events.on(UserRegistered, async (event) => {
|
|
92
|
+
* console.log(`User ${event.userId} registered`)
|
|
93
|
+
* await sendWelcomeEmail(event.email)
|
|
94
|
+
* })
|
|
95
|
+
*
|
|
96
|
+
* // Register a one-time listener
|
|
97
|
+
* events.once(AppStarted, () => {
|
|
98
|
+
* console.log('App started!')
|
|
99
|
+
* })
|
|
100
|
+
*
|
|
101
|
+
* // Emit an event
|
|
102
|
+
* await events.emit(new UserRegistered('123', 'user@example.com'))
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare class EventManager {
|
|
106
|
+
private readonly listeners;
|
|
107
|
+
/**
|
|
108
|
+
* Queue dispatcher function (set when Queue system is integrated).
|
|
109
|
+
*/
|
|
110
|
+
private queueDispatcher?;
|
|
111
|
+
/**
|
|
112
|
+
* Register a listener for an event.
|
|
113
|
+
*
|
|
114
|
+
* @param event - Event class to listen for
|
|
115
|
+
* @param listener - Listener function
|
|
116
|
+
* @param options - Listener options
|
|
117
|
+
* @returns Subscription handle for unsubscribing
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* // Basic usage
|
|
122
|
+
* events.on(UserRegistered, (e) => console.log(e.email))
|
|
123
|
+
*
|
|
124
|
+
* // With options
|
|
125
|
+
* events.on(UserRegistered, (e) => sendEmail(e), { priority: 10 })
|
|
126
|
+
*
|
|
127
|
+
* // Unsubscribe later
|
|
128
|
+
* const sub = events.on(UserRegistered, handler)
|
|
129
|
+
* sub.unsubscribe()
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
on<T extends Event>(event: EventClass<T> | string, listener: EventListener<T>, options?: ListenerOptions): EventSubscription;
|
|
133
|
+
/**
|
|
134
|
+
* Register a one-time listener for an event.
|
|
135
|
+
* The listener will be automatically removed after the first invocation.
|
|
136
|
+
*
|
|
137
|
+
* @param event - Event class to listen for
|
|
138
|
+
* @param listener - Listener function
|
|
139
|
+
* @param options - Additional listener options (once is always true)
|
|
140
|
+
* @returns Subscription handle for unsubscribing
|
|
141
|
+
*/
|
|
142
|
+
once<T extends Event>(event: EventClass<T> | string, listener: EventListener<T>, options?: Omit<ListenerOptions, 'once'>): EventSubscription;
|
|
143
|
+
/**
|
|
144
|
+
* Remove a listener for an event.
|
|
145
|
+
*
|
|
146
|
+
* @param event - Event class
|
|
147
|
+
* @param listener - Listener to remove (if omitted, all listeners for the event are removed)
|
|
148
|
+
*/
|
|
149
|
+
off<T extends Event>(event: EventClass<T> | string, listener?: EventListener<T>): void;
|
|
150
|
+
/**
|
|
151
|
+
* Emit an event to all registered listeners.
|
|
152
|
+
* Listeners are executed in order of priority (highest first).
|
|
153
|
+
* Async listeners are awaited sequentially.
|
|
154
|
+
*
|
|
155
|
+
* @param event - Event instance to emit
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* await events.emit(new UserRegistered('123', 'user@example.com'))
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
emit<T extends Event>(event: T): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Emit an event to all registered listeners in parallel.
|
|
165
|
+
* Use this when listener execution order doesn't matter and you want faster execution.
|
|
166
|
+
*
|
|
167
|
+
* @param event - Event instance to emit
|
|
168
|
+
*/
|
|
169
|
+
emitParallel<T extends Event>(event: T): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* Check if an event has any listeners registered.
|
|
172
|
+
*
|
|
173
|
+
* @param event - Event class or event name
|
|
174
|
+
*/
|
|
175
|
+
hasListeners(event: EventClass | string): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Get all listeners for an event.
|
|
178
|
+
*
|
|
179
|
+
* @param event - Event class or event name
|
|
180
|
+
*/
|
|
181
|
+
getListeners<T extends Event>(event: EventClass<T> | string): EventListener<T>[];
|
|
182
|
+
/**
|
|
183
|
+
* Get the count of listeners for an event.
|
|
184
|
+
*
|
|
185
|
+
* @param event - Event class or event name
|
|
186
|
+
*/
|
|
187
|
+
listenerCount(event: EventClass | string): number;
|
|
188
|
+
/**
|
|
189
|
+
* Get all registered event names.
|
|
190
|
+
*/
|
|
191
|
+
eventNames(): string[];
|
|
192
|
+
/**
|
|
193
|
+
* Remove all listeners for all events.
|
|
194
|
+
*/
|
|
195
|
+
removeAllListeners(): void;
|
|
196
|
+
/**
|
|
197
|
+
* Set the queue dispatcher function.
|
|
198
|
+
* This is called by the Queue system when it's integrated.
|
|
199
|
+
*/
|
|
200
|
+
setQueueDispatcher(dispatcher: (queueName: string, eventName: string, event: Event) => Promise<void>): void;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Create a new EventManager instance.
|
|
204
|
+
*/
|
|
205
|
+
declare function createEventManager(): EventManager;
|
|
206
|
+
|
|
207
|
+
export { EventManager as E, type ListenerOptions as L, type RegisteredListener as R, Event as a, type EventClass as b, type EventListener as c, type EventSubscription as d, createEventManager as e };
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Context } from 'hono';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* User type for authorization.
|
|
5
|
+
*/
|
|
6
|
+
interface AuthUser {
|
|
7
|
+
id: string | number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Gate callback function.
|
|
11
|
+
*/
|
|
12
|
+
type GateCallback<Args extends unknown[] = unknown[]> = {
|
|
13
|
+
bivarianceHack(user: AuthUser | null, ...args: Args): boolean | Promise<boolean>;
|
|
14
|
+
}['bivarianceHack'];
|
|
15
|
+
/**
|
|
16
|
+
* Gate before callbacks can short-circuit authorization.
|
|
17
|
+
* Return true to allow, false to deny, or undefined to continue.
|
|
18
|
+
*/
|
|
19
|
+
type GateBeforeCallback<Args extends unknown[] = unknown[]> = {
|
|
20
|
+
bivarianceHack(user: AuthUser | null, ability: string, ...args: Args): boolean | undefined | Promise<boolean | undefined>;
|
|
21
|
+
}['bivarianceHack'];
|
|
22
|
+
/**
|
|
23
|
+
* Gate definition with optional callback.
|
|
24
|
+
*/
|
|
25
|
+
interface GateDefinition {
|
|
26
|
+
callback: GateCallback;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Policy method type.
|
|
30
|
+
*/
|
|
31
|
+
type PolicyMethod<T = unknown> = (user: AuthUser | null, model?: T, ...args: unknown[]) => boolean | Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Policy class interface.
|
|
34
|
+
*/
|
|
35
|
+
interface Policy {
|
|
36
|
+
/**
|
|
37
|
+
* Run before all other authorization checks.
|
|
38
|
+
* Return true to allow, false to deny, undefined to continue to specific check.
|
|
39
|
+
*/
|
|
40
|
+
before?(user: AuthUser | null, ability: string): boolean | undefined | Promise<boolean | undefined>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Policy class constructor.
|
|
44
|
+
*/
|
|
45
|
+
interface PolicyClass {
|
|
46
|
+
new (): Policy;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Authorization response.
|
|
50
|
+
*/
|
|
51
|
+
interface AuthorizationResponse {
|
|
52
|
+
allowed: boolean;
|
|
53
|
+
message?: string;
|
|
54
|
+
code?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gate options.
|
|
58
|
+
*/
|
|
59
|
+
interface GateOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Default user resolver from context.
|
|
62
|
+
*/
|
|
63
|
+
userResolver?: (ctx: Context) => AuthUser | null | Promise<AuthUser | null>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Authorization check options.
|
|
67
|
+
*/
|
|
68
|
+
interface AuthorizeOptions {
|
|
69
|
+
/**
|
|
70
|
+
* Custom message for denial.
|
|
71
|
+
*/
|
|
72
|
+
message?: string;
|
|
73
|
+
/**
|
|
74
|
+
* HTTP status code for denial.
|
|
75
|
+
*/
|
|
76
|
+
status?: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Policy registration.
|
|
80
|
+
*/
|
|
81
|
+
interface PolicyRegistration {
|
|
82
|
+
modelClass: unknown;
|
|
83
|
+
policyClass: PolicyClass;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Resource actions for policies.
|
|
87
|
+
*/
|
|
88
|
+
type ResourceAction = 'viewAny' | 'view' | 'create' | 'update' | 'delete' | 'restore' | 'forceDelete';
|
|
89
|
+
/**
|
|
90
|
+
* Response builder for authorization.
|
|
91
|
+
*/
|
|
92
|
+
interface ResponseBuilder {
|
|
93
|
+
allow(message?: string): AuthorizationResponse;
|
|
94
|
+
deny(message?: string, code?: string): AuthorizationResponse;
|
|
95
|
+
denyWithStatus(status: number, message?: string): AuthorizationResponse & {
|
|
96
|
+
status: number;
|
|
97
|
+
};
|
|
98
|
+
denyAsNotFound(message?: string): AuthorizationResponse & {
|
|
99
|
+
status: 404;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Response builder for authorization checks.
|
|
105
|
+
*/
|
|
106
|
+
declare const Response: ResponseBuilder;
|
|
107
|
+
/**
|
|
108
|
+
* Authorization gate for defining and checking abilities.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* const gate = new Gate()
|
|
113
|
+
*
|
|
114
|
+
* // Define abilities
|
|
115
|
+
* gate.define('edit-post', (user, post) => user.id === post.authorId)
|
|
116
|
+
* gate.define('admin', (user) => user.role === 'admin')
|
|
117
|
+
*
|
|
118
|
+
* // Check abilities
|
|
119
|
+
* if (await gate.allows('edit-post', post)) {
|
|
120
|
+
* // User can edit
|
|
121
|
+
* }
|
|
122
|
+
*
|
|
123
|
+
* // Or throw on denial
|
|
124
|
+
* await gate.authorize('edit-post', post) // throws if not allowed
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
declare class Gate {
|
|
128
|
+
/**
|
|
129
|
+
* Defined gates.
|
|
130
|
+
*/
|
|
131
|
+
protected gates: Map<string, GateDefinition>;
|
|
132
|
+
/**
|
|
133
|
+
* Registered policies.
|
|
134
|
+
*/
|
|
135
|
+
protected policies: Map<unknown, PolicyClass>;
|
|
136
|
+
/**
|
|
137
|
+
* Policy instances cache.
|
|
138
|
+
*/
|
|
139
|
+
protected policyInstances: Map<PolicyClass, Policy>;
|
|
140
|
+
/**
|
|
141
|
+
* Before callbacks.
|
|
142
|
+
*/
|
|
143
|
+
protected beforeCallbacks: GateBeforeCallback[];
|
|
144
|
+
/**
|
|
145
|
+
* After callbacks.
|
|
146
|
+
*/
|
|
147
|
+
protected afterCallbacks: Array<(user: AuthUser | null, ability: string, result: boolean, args: unknown[]) => void | Promise<void>>;
|
|
148
|
+
/**
|
|
149
|
+
* User resolver function.
|
|
150
|
+
*/
|
|
151
|
+
protected userResolver?: (ctx: Context) => AuthUser | null | Promise<AuthUser | null>;
|
|
152
|
+
/**
|
|
153
|
+
* Current user for checks.
|
|
154
|
+
*/
|
|
155
|
+
protected currentUser: AuthUser | null;
|
|
156
|
+
constructor(options?: GateOptions);
|
|
157
|
+
/**
|
|
158
|
+
* Define a new gate.
|
|
159
|
+
*/
|
|
160
|
+
define<Args extends unknown[]>(ability: string, callback: GateCallback<Args>): this;
|
|
161
|
+
/**
|
|
162
|
+
* Register a policy for a model class.
|
|
163
|
+
*/
|
|
164
|
+
policy(modelClass: unknown, policyClass: PolicyClass): this;
|
|
165
|
+
/**
|
|
166
|
+
* Register a before callback.
|
|
167
|
+
* Return true to allow, false to deny, undefined to continue.
|
|
168
|
+
*/
|
|
169
|
+
before<Args extends unknown[]>(callback: GateBeforeCallback<Args>): this;
|
|
170
|
+
/**
|
|
171
|
+
* Register an after callback.
|
|
172
|
+
*/
|
|
173
|
+
after(callback: (user: AuthUser | null, ability: string, result: boolean, args: unknown[]) => void | Promise<void>): this;
|
|
174
|
+
/**
|
|
175
|
+
* Set the current user for authorization checks.
|
|
176
|
+
*/
|
|
177
|
+
forUser(user: AuthUser | null): this;
|
|
178
|
+
/**
|
|
179
|
+
* Resolve the user from context.
|
|
180
|
+
*/
|
|
181
|
+
resolveUser(ctx: Context): Promise<AuthUser | null>;
|
|
182
|
+
/**
|
|
183
|
+
* Check if the user has the given ability.
|
|
184
|
+
*/
|
|
185
|
+
allows(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
186
|
+
/**
|
|
187
|
+
* Check if the user doesn't have the given ability.
|
|
188
|
+
*/
|
|
189
|
+
denies(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
190
|
+
/**
|
|
191
|
+
* Check if the user has any of the given abilities.
|
|
192
|
+
*/
|
|
193
|
+
any(abilities: string[], ...args: unknown[]): Promise<boolean>;
|
|
194
|
+
/**
|
|
195
|
+
* Check if the user has all of the given abilities.
|
|
196
|
+
*/
|
|
197
|
+
all(abilities: string[], ...args: unknown[]): Promise<boolean>;
|
|
198
|
+
/**
|
|
199
|
+
* Check if the user has none of the given abilities.
|
|
200
|
+
*/
|
|
201
|
+
none(abilities: string[], ...args: unknown[]): Promise<boolean>;
|
|
202
|
+
/**
|
|
203
|
+
* Authorize the ability or throw an exception.
|
|
204
|
+
*/
|
|
205
|
+
authorize(ability: string, ...args: unknown[]): Promise<void>;
|
|
206
|
+
/**
|
|
207
|
+
* Get the authorization response.
|
|
208
|
+
*/
|
|
209
|
+
inspect(ability: string, ...args: unknown[]): Promise<AuthorizationResponse>;
|
|
210
|
+
/**
|
|
211
|
+
* Check the ability with a specific user.
|
|
212
|
+
*/
|
|
213
|
+
check(ability: string, user: AuthUser | null, ...args: unknown[]): Promise<boolean>;
|
|
214
|
+
/**
|
|
215
|
+
* Check a policy for the given ability.
|
|
216
|
+
*/
|
|
217
|
+
protected checkPolicy(ability: string, user: AuthUser | null, model: unknown, additionalArgs: unknown[]): Promise<boolean | undefined>;
|
|
218
|
+
/**
|
|
219
|
+
* Get or create a policy instance.
|
|
220
|
+
*/
|
|
221
|
+
protected getPolicyInstance(policyClass: PolicyClass): Policy;
|
|
222
|
+
/**
|
|
223
|
+
* Run after callbacks.
|
|
224
|
+
*/
|
|
225
|
+
protected runAfterCallbacks(user: AuthUser | null, ability: string, result: boolean, args: unknown[]): Promise<void>;
|
|
226
|
+
/**
|
|
227
|
+
* Get all defined abilities.
|
|
228
|
+
*/
|
|
229
|
+
abilities(): string[];
|
|
230
|
+
/**
|
|
231
|
+
* Check if an ability is defined.
|
|
232
|
+
*/
|
|
233
|
+
has(ability: string): boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Get the policy for a model.
|
|
236
|
+
*/
|
|
237
|
+
getPolicyFor(model: unknown): Policy | undefined;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Create a new gate instance.
|
|
241
|
+
*/
|
|
242
|
+
declare function createGate(options?: GateOptions): Gate;
|
|
243
|
+
/**
|
|
244
|
+
* Set the global gate instance.
|
|
245
|
+
*/
|
|
246
|
+
declare function setGate(gate: Gate): void;
|
|
247
|
+
/**
|
|
248
|
+
* Get the global gate instance.
|
|
249
|
+
*/
|
|
250
|
+
declare function getGate(): Gate;
|
|
251
|
+
/**
|
|
252
|
+
* Define a gate on the global instance.
|
|
253
|
+
*/
|
|
254
|
+
declare function defineGate(ability: string, callback: GateCallback): void;
|
|
255
|
+
/**
|
|
256
|
+
* Check if the current user can perform an ability.
|
|
257
|
+
*/
|
|
258
|
+
declare function can(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
259
|
+
/**
|
|
260
|
+
* Check if the current user cannot perform an ability.
|
|
261
|
+
*/
|
|
262
|
+
declare function cannot(ability: string, ...args: unknown[]): Promise<boolean>;
|
|
263
|
+
/**
|
|
264
|
+
* Authorize or throw.
|
|
265
|
+
*/
|
|
266
|
+
declare function authorize(ability: string, ...args: unknown[]): Promise<void>;
|
|
267
|
+
|
|
268
|
+
export { type AuthUser as A, Gate as G, type PolicyClass as P, Response as R, type AuthorizationResponse as a, type AuthorizeOptions as b, type GateCallback as c, type GateDefinition as d, type GateOptions as e, type Policy as f, type PolicyMethod as g, type PolicyRegistration as h, type ResourceAction as i, type ResponseBuilder as j, authorize as k, can as l, cannot as m, createGate as n, defineGate as o, getGate as p, setGate as s };
|