1
+ // Type definitions for rest-io 4.0
2
+ // Project: https://github.com/EnoF/rest-io
3
+ // Definitions by: Andy Tang <https://github.com/EnoF>, Stefan Schacherl <https://github.com/TheBay0r>
4
+ // Definitions: https://github.com/borisyankov/DefinitelyTyped
5
+
6
+ /// <reference path="../express/express.d.ts" />
7
+ /// <reference path="../mongoose/mongoose.d.ts" />
8
+
9
+
10
+ declare module 'rest-io' {
11
+ import { Router , Application , Response , Request } from 'express' ;
12
+
13
+ import { Mongoose , Schema , Model , Document , Types , Promise } from 'mongoose' ;
14
+
15
+ function restIO ( app : Application , config ?: IRestIOConfig ) : RestIO ;
16
+
17
+ export interface RestIO {
18
+ resource : ResourceModule
19
+ }
20
+
21
+ export interface IRestIOConfig {
22
+ resources : string ;
23
+ db ?: Mongoose ;
24
+ }
25
+
26
+ export interface ResourceModule {
27
+ Resource : Resource ;
28
+ AuthorizedResource : AuthorizedResource ;
29
+ authorizedResource : AuthorizedResourceModule ;
30
+ UserResource : UserResource ;
31
+ SubResource : SubResource ;
32
+ }
33
+
34
+ export class Resource {
35
+ baseUrl : string ;
36
+ url : string ;
37
+ parameterizedUrl : string ;
38
+ model : Model < Document > ;
39
+ resDef : IResource ;
40
+ parentResource : Resource ;
41
+ router : Router ;
42
+ app : Application ;
43
+ db : Mongoose ;
44
+ paramId : string ;
45
+ parentRef : string ;
46
+ populate : string ;
47
+
48
+ constructor ( resDef : IResource ) ;
49
+
50
+ createModel ( resDef : IResource ) : Model < Document > ;
51
+
52
+ toClassName ( name : string ) : string ;
53
+
54
+ setupRoutes ( ) : void ;
55
+
56
+ getAll ( req : Request , res : Response ) : void ;
57
+
58
+ buildParentSearch ( req : Request ) : any ;
59
+
60
+ getById ( req : Request , res : Response ) : void ;
61
+
62
+ create ( req : Request , res : Response ) : void ;
63
+
64
+ update ( req : Request , res : Response ) : void ;
65
+
66
+ del ( req : Request , res : Response ) : void ;
67
+
68
+ errorHandler ( err : Error , res : Response ) : void ;
69
+ }
70
+
71
+ export interface IResource {
72
+ name : string ;
73
+ model : any ;
74
+ parentRef ?: string ;
75
+ populate ?: string ;
76
+ plural ?: string ;
77
+ parentResource ?: Resource ;
78
+ }
79
+
80
+ export interface AuthorizedResourceModule {
81
+ AuthorizedResource : AuthorizedResource
82
+ ROLES : Roles
83
+ }
84
+
85
+ export interface Roles {
86
+ USER : string ;
87
+ SUPER_USER : string ;
88
+ MODERATOR : string ;
89
+ ADMIN : string ;
90
+ }
91
+
92
+ export class AuthorizedResource extends Resource {
93
+ methodAccess : IMethodAccess ;
94
+
95
+ maxDays : number ;
96
+
97
+ permissions : IMethodAccess ;
98
+
99
+ isTokenExpired ( createdAt : Date ) : boolean ;
100
+
101
+ getRoles ( id : string ) : Promise < Document > ;
102
+
103
+ hasAuthorizedRole ( roles : Array < any > , authorizedRoles : Array < string > ) : boolean ;
104
+
105
+ hasAccessRightsDefined ( req : Request , authorizedRoles : Array < string > ) : boolean ;
106
+
107
+ isAuthorized ( req : Request , authorizedRoles : Array < string > ) : boolean ;
108
+
109
+ sendUnauthorized ( error : Error , res : Response ) : void ;
110
+ }
111
+
112
+ export interface IMethodAccess {
113
+ getAll : Array < string > ;
114
+ getById : Array < string > ;
115
+ create : Array < string > ;
116
+ update : Array < string > ;
117
+ del : Array < string > ;
118
+ }
119
+
120
+ export class UserResource extends AuthorizedResource {
121
+ ensureBaseUserModel ( model : any ) : void ;
122
+
123
+ createRoleModel ( ) : void ;
124
+
125
+ isSelf ( req : Request ) : boolean ;
126
+
127
+ login ( req : Request , res : Response ) : void ;
128
+ }
129
+
130
+ export class SubResource extends Resource {
131
+ constructor ( resDef : ISubResource ) ;
132
+
133
+ createProjectionQuery ( req : Request ) : any ;
134
+
135
+ createPullQuery ( req : Request ) : any ;
136
+
137
+ createFindQuery ( req : Request ) : any ;
138
+
139
+ createSubUpdateQuery ( req : Request ) : any ;
140
+ }
141
+
142
+ export interface ISubResource {
143
+ name : string ;
144
+ plural ?: string ;
145
+ parentResource : Resource ;
146
+ populate ?: string ;
147
+ }
148
+ }
0 commit comments