-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
collection-hooks.d.ts
80 lines (78 loc) · 4.49 KB
/
collection-hooks.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
declare module 'meteor/matb33:collection-hooks' {
import { Meteor } from "meteor/meteor"
type Options = {
fetchPrevious?: boolean
[key: string]: any
}
type TGlobalOptions = {
all?: Options
insert?: Options
update?: Options
upsert?: Options
find?: Options
findOne?: Options
remove?: Options
}
interface CollectionHooks {
defaultUserId?: string
directEnv?: Meteor.EnvironmentVariable<any>
GlobalOptions?: TGlobalOptions
defaults?: {
all?: TGlobalOptions
before?: TGlobalOptions
after?: TGlobalOptions
}
}
}
declare module 'meteor/mongo' {
import {CollectionHooks} from "meteor/matb33:collection-hooks";
module Mongo {
type GenericFunction = (...args: any) => any
type THookThis<T, UnderlyingMethod extends GenericFunction> = {
_super: UnderlyingMethod,
context: ThisType<UnderlyingMethod>,
args: Parameters<UnderlyingMethod>
transform: (doc: T) => T
}
type THookThisWithId<T, UnderlyingMethod extends GenericFunction> = THookThis<T, UnderlyingMethod> & {
_id: string
}
type THookThisWithTransform<T, UnderlyingMethod extends GenericFunction> = THookThis<T, UnderlyingMethod> & {
transform: (doc: T) => T
}
type THookThisWithTransformAndPrevious<T, UnderlyingMethod extends GenericFunction> = THookThisWithTransform<T, UnderlyingMethod> & {
previous: T
}
type THookBeforeInsert<T,O = void> = (this: THookThis<T, Collection<T>["insert"]>, userId: string|undefined, doc: T) => O;
type THookAfterInsert<T,O = void> = (this: THookThisWithId<T, Collection<T>["insert"]>, userId: string|undefined, doc: T) => O;
type THookBeforeUpdate<T,O = void> = (this: THookThis<T, Collection<T>["update"]> & { previous: T, transform: (doc: T) => T }, userId: string|undefined, doc: T, fieldNames: string[], modifier: any, options: any) => O
type THookAfterUpdate<T,O = void> = (this: THookThisWithTransformAndPrevious<T, Collection<T>["update"]> & { previous: T, transform: (doc: T) => T }, userId: string|undefined, doc: T, fieldNames: string[], modifier: any, options: any) => O
type THookRemove<T,O = void> = (this: THookThisWithTransform<T, Collection<T>["remove"]>, userId: string|undefined, doc: T) => O
type THookUpsert<T,O = void> = (this: THookThis<T, Collection<T>["upsert"]>, userId: string|undefined, selector: any, modifier: any, options: any) => O
type THookBeforeFind<T,O = void> = (this: THookThis<T, Collection<T>["find"]>, userId: string|undefined, selector: any, options: any) => O
type THookAfterFind<T> = (this: THookThis<T, Collection<T>["find"]>, userId: string|undefined, selector: any, options: any, cursor: Cursor<T>) => void
type THookBeforeFindOne<T,O = void> = (this: THookThis<T, Collection<T>["findOne"]>, userId: string|undefined, selector: any, options: any) => O
type THookAfterFindOne<T> = (this: THookThis<T, Collection<T>["findOne"]>, userId: string|undefined, selector: any, options: any, doc: T) => void
type THandler<F> = {remove(): void, replace(callback: F, options: any): void}
interface Collection<T, U = T> {
hookOptions: CollectionHooks["GlobalOptions"]
direct: Pick<Collection<T, U>, "insert"|"insertAsync"|"update"|"updateAsync"|"find"|"findOne"|"findOneAsync"|"remove"|"removeAsync">
before: {
insert<Fn = THookBeforeInsert<T,void|false>>(fn: Fn): THandler<Fn>
update<Fn = THookBeforeUpdate<T,void|false>>(fn: Fn): THandler<Fn>
remove<Fn = THookRemove<T,void|false>>(fn: Fn): THandler<Fn>
upsert<Fn = THookUpsert<T,void|false>>(fn: Fn): THandler<Fn>
find<Fn = THookBeforeFind<T,void|false>>(fn: Fn): THandler<Fn>
findOne<Fn = THookBeforeFindOne<T,void|false>>(fn: Fn): THandler<Fn>
}
after: {
insert<Fn = THookAfterInsert<T>>(fn: Fn): THandler<Fn>
update<Fn = THookAfterUpdate<T>>(fn: Fn, options?: { fetchPrevious?: boolean }): THandler<Fn>
remove<Fn = THookRemove<T>>(fn: Fn): THandler<Fn>
upsert<Fn = THookUpsert<T>>(fn: Fn): THandler<Fn>
find<Fn = THookAfterFind<T>>(fn: Fn): THandler<Fn>
findOne<Fn = THookAfterFindOne<T>>(fn: Fn): THandler<Fn>
}
}
}
}