forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-io.d.ts
148 lines (105 loc) · 3.39 KB
/
rest-io.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Type definitions for rest-io 4.0
// Project: https://github.com/EnoF/rest-io
// Definitions by: Andy Tang <https://github.com/EnoF>, Stefan Schacherl <https://github.com/TheBay0r>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../express/express.d.ts" />
/// <reference path="../mongoose/mongoose.d.ts" />
declare module 'rest-io' {
import {Router, Application, Response, Request} from 'express';
import {Mongoose, Schema, Model, Document, Types, Promise} from 'mongoose';
function restIO(app: Application, config?: IRestIOConfig): RestIO;
export interface RestIO {
resource: ResourceModule
}
export interface IRestIOConfig {
resources: string;
db?: Mongoose;
}
export interface ResourceModule {
Resource: Resource;
AuthorizedResource: AuthorizedResource;
authorizedResource: AuthorizedResourceModule;
UserResource: UserResource;
SubResource: SubResource;
}
export class Resource {
baseUrl: string;
url: string;
parameterizedUrl: string;
model: Model<Document>;
resDef: IResource;
parentResource: Resource;
router: Router;
app: Application;
db: Mongoose;
paramId: string;
parentRef: string;
populate: string;
constructor(resDef: IResource);
createModel(resDef: IResource): Model<Document>;
toClassName(name: string): string;
setupRoutes(): void;
getAll(req: Request, res: Response): void;
buildParentSearch(req: Request): any;
getById(req: Request, res: Response): void;
create(req: Request, res: Response): void;
update(req: Request, res: Response): void;
del(req: Request, res: Response): void;
errorHandler(err: Error, res: Response): void;
}
export interface IResource {
name: string;
model: any;
parentRef?: string;
populate?: string;
plural?: string;
parentResource?: Resource;
}
export interface AuthorizedResourceModule {
AuthorizedResource: AuthorizedResource
ROLES: Roles
}
export interface Roles {
USER: string;
SUPER_USER: string;
MODERATOR: string;
ADMIN: string;
}
export class AuthorizedResource extends Resource {
methodAccess: IMethodAccess;
maxDays: number;
permissions: IMethodAccess;
isTokenExpired(createdAt: Date): boolean;
getRoles(id: string): Promise<Document>;
hasAuthorizedRole(roles: Array<any>, authorizedRoles: Array<string>): boolean;
hasAccessRightsDefined(req: Request, authorizedRoles: Array<string>): boolean;
isAuthorized(req: Request, authorizedRoles: Array<string>): boolean;
sendUnauthorized(error: Error, res: Response): void;
}
export interface IMethodAccess {
getAll: Array<string>;
getById: Array<string>;
create: Array<string>;
update: Array<string>;
del: Array<string>;
}
export class UserResource extends AuthorizedResource {
ensureBaseUserModel(model: any): void;
createRoleModel(): void;
isSelf(req: Request): boolean;
login(req: Request, res: Response): void;
}
export class SubResource extends Resource {
constructor(resDef: ISubResource);
createProjectionQuery(req: Request): any;
createPullQuery(req: Request): any;
createFindQuery(req: Request): any;
createSubUpdateQuery(req: Request): any;
}
export interface ISubResource {
name: string;
plural?: string;
parentResource: Resource;
populate?: string;
}
}