express-ext 0.2.3 → 0.2.5
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/lib/GenericSearchController.js +37 -23
- package/lib/LoadSearchController.js +48 -31
- package/lib/LowCodeController.js +47 -27
- package/lib/SearchController.js +13 -10
- package/lib/http.js +9 -15
- package/lib/index.js +107 -66
- package/lib/search.js +149 -79
- package/package.json +1 -1
- package/src/GenericSearchController.ts +19 -10
- package/src/LoadSearchController.ts +37 -19
- package/src/LowCodeController.ts +34 -16
- package/src/SearchController.ts +13 -7
- package/src/http.ts +7 -7
- package/src/index.ts +88 -66
- package/src/search.ts +122 -36
package/src/SearchController.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {Request, Response} from 'express';
|
|
2
|
-
import {handleError, Log} from './http';
|
|
3
|
-
import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
import { handleError, Log } from './http';
|
|
3
|
+
import { buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult } from './search';
|
|
4
4
|
|
|
5
5
|
export class SearchController<T, S extends Filter> {
|
|
6
6
|
config?: SearchConfig;
|
|
@@ -8,7 +8,13 @@ export class SearchController<T, S extends Filter> {
|
|
|
8
8
|
fields?: string;
|
|
9
9
|
excluding?: string;
|
|
10
10
|
array?: string[];
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(
|
|
12
|
+
protected log: Log,
|
|
13
|
+
public find: (s: S, limit?: number, skip?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
14
|
+
config?: SearchConfig | boolean,
|
|
15
|
+
public dates?: string[],
|
|
16
|
+
public numbers?: string[],
|
|
17
|
+
) {
|
|
12
18
|
this.search = this.search.bind(this);
|
|
13
19
|
if (config) {
|
|
14
20
|
if (typeof config === 'boolean') {
|
|
@@ -30,8 +36,8 @@ export class SearchController<T, S extends Filter> {
|
|
|
30
36
|
const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
|
|
31
37
|
const l = getParameters(s, this.config);
|
|
32
38
|
const s2 = format(s, this.dates, this.numbers);
|
|
33
|
-
this.find(s2, l.limit, l.
|
|
34
|
-
.then(result => jsonResult(res, result, this.csv, l.fields, this.config))
|
|
35
|
-
.catch(err => handleError(err, res, this.log));
|
|
39
|
+
this.find(s2, l.limit, l.offsetOrNextPageToken, l.fields)
|
|
40
|
+
.then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
|
|
41
|
+
.catch((err) => handleError(err, res, this.log));
|
|
36
42
|
}
|
|
37
43
|
}
|
package/src/http.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type LogFunc = Log;
|
|
|
6
6
|
|
|
7
7
|
Object.defineProperty(Error.prototype, 'toJSON', {
|
|
8
8
|
value() {
|
|
9
|
-
const alt:any = {};
|
|
9
|
+
const alt: any = {};
|
|
10
10
|
Object.getOwnPropertyNames(this).forEach(function (key) {
|
|
11
11
|
// @ts-ignore
|
|
12
12
|
alt[key] = this[key];
|
|
@@ -15,7 +15,7 @@ Object.defineProperty(Error.prototype, 'toJSON', {
|
|
|
15
15
|
return alt;
|
|
16
16
|
},
|
|
17
17
|
configurable: true,
|
|
18
|
-
writable: true
|
|
18
|
+
writable: true,
|
|
19
19
|
});
|
|
20
20
|
export function handleError(err: any, res: Response, log?: (msg: string) => void) {
|
|
21
21
|
if (log) {
|
|
@@ -51,7 +51,7 @@ export function respondModel<T>(obj: T, res: Response): void {
|
|
|
51
51
|
res.status(404).json(null).end();
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
export function queryRequiredParams(req: Request, res: Response, name: string, split?: string): string[]|undefined {
|
|
54
|
+
export function queryRequiredParams(req: Request, res: Response, name: string, split?: string): string[] | undefined {
|
|
55
55
|
const v = req.query[name];
|
|
56
56
|
if (!v) {
|
|
57
57
|
res.status(400).end(`'${name}' cannot be empty`);
|
|
@@ -120,7 +120,7 @@ export function queryRequiredNumber(req: Request, res: Response, name: string):
|
|
|
120
120
|
const n = parseFloat(v);
|
|
121
121
|
return n;
|
|
122
122
|
}
|
|
123
|
-
export function queryNumber(req: Request, name: string, d
|
|
123
|
+
export function queryNumber(req: Request, name: string, d: number): number {
|
|
124
124
|
const field = req.query[name];
|
|
125
125
|
const v = field ? field.toString() : undefined;
|
|
126
126
|
if (!v || v.length === 0) {
|
|
@@ -219,7 +219,7 @@ export function getNumber(req: Request, name: string, d?: number): number | unde
|
|
|
219
219
|
const n = parseFloat(v);
|
|
220
220
|
return n;
|
|
221
221
|
}
|
|
222
|
-
export function getInteger(req: Request, name: string, d?: number): number|undefined {
|
|
222
|
+
export function getInteger(req: Request, name: string, d?: number): number | undefined {
|
|
223
223
|
const v = req.params[name];
|
|
224
224
|
if (!v || v.length === 0) {
|
|
225
225
|
return d;
|
|
@@ -231,7 +231,7 @@ export function getInteger(req: Request, name: string, d?: number): number|undef
|
|
|
231
231
|
const s = n.toFixed(0);
|
|
232
232
|
return parseFloat(s);
|
|
233
233
|
}
|
|
234
|
-
export function getRequiredDate(req: Request, res: Response, name: string): Date|undefined {
|
|
234
|
+
export function getRequiredDate(req: Request, res: Response, name: string): Date | undefined {
|
|
235
235
|
const v = req.params[name];
|
|
236
236
|
if (!v || v.length === 0) {
|
|
237
237
|
res.status(400).end(`'${name}' cannot be empty`);
|
|
@@ -244,7 +244,7 @@ export function getRequiredDate(req: Request, res: Response, name: string): Date
|
|
|
244
244
|
}
|
|
245
245
|
return date;
|
|
246
246
|
}
|
|
247
|
-
export function getDate(req: Request, name: string, d?: Date): Date|undefined {
|
|
247
|
+
export function getDate(req: Request, name: string, d?: Date): Date | undefined {
|
|
248
248
|
const v = req.params[name];
|
|
249
249
|
if (!v || v.length === 0) {
|
|
250
250
|
return d;
|
package/src/index.ts
CHANGED
|
@@ -1,46 +1,44 @@
|
|
|
1
|
-
import {NextFunction, Request, Response} from 'express';
|
|
2
|
-
import {GenericController} from './GenericController';
|
|
3
|
-
import {GenericSearchController} from './GenericSearchController';
|
|
4
|
-
import {HealthController} from './HealthController';
|
|
5
|
-
import {handleError, Log} from './http';
|
|
6
|
-
import {LoadController} from './LoadController';
|
|
7
|
-
import {LoadSearchController} from './LoadSearchController';
|
|
8
|
-
import {LogController} from './LogController';
|
|
9
|
-
import {Controller} from './LowCodeController';
|
|
10
|
-
import {
|
|
11
|
-
import {SearchController} from './SearchController';
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { GenericController } from './GenericController';
|
|
3
|
+
import { GenericSearchController } from './GenericSearchController';
|
|
4
|
+
import { HealthController } from './HealthController';
|
|
5
|
+
import { handleError, Log } from './http';
|
|
6
|
+
import { LoadController } from './LoadController';
|
|
7
|
+
import { LoadSearchController } from './LoadSearchController';
|
|
8
|
+
import { LogController } from './LogController';
|
|
9
|
+
import { Controller, Service } from './LowCodeController';
|
|
10
|
+
import { SearchController } from './SearchController';
|
|
12
11
|
|
|
13
|
-
export {HealthController as HealthHandler};
|
|
14
|
-
export {LogController as LogHandler};
|
|
15
|
-
export {LoadController as LoadHandler};
|
|
16
|
-
export {LoadController as ViewHandler};
|
|
12
|
+
export { HealthController as HealthHandler, LoadController as LoadHandler, LogController as LogHandler, LoadController as ViewHandler };
|
|
17
13
|
// export {LoadController as ViewController};
|
|
18
14
|
|
|
19
|
-
export {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
export {
|
|
16
|
+
GenericController as GenericHandler,
|
|
17
|
+
GenericSearchController as GenericSearchHandler,
|
|
18
|
+
Controller as Handler,
|
|
19
|
+
LoadSearchController as LoadSearchHandler,
|
|
20
|
+
Service as LowCodeService,
|
|
21
|
+
SearchController as SearchHandler,
|
|
22
|
+
};
|
|
25
23
|
|
|
26
|
-
export * from './health';
|
|
27
24
|
export * from './client';
|
|
25
|
+
export * from './edit';
|
|
26
|
+
export * from './GenericController';
|
|
27
|
+
export * from './GenericSearchController';
|
|
28
|
+
export * from './health';
|
|
28
29
|
export * from './HealthController';
|
|
29
|
-
export * from './LogController';
|
|
30
|
-
export * from './log';
|
|
31
30
|
export * from './http';
|
|
32
|
-
export * from './metadata';
|
|
33
|
-
export * from './view';
|
|
34
31
|
export * from './LoadController';
|
|
35
|
-
export * from './search_func';
|
|
36
|
-
export * from './search';
|
|
37
|
-
export * from './SearchController';
|
|
38
32
|
export * from './LoadSearchController';
|
|
39
|
-
export * from './
|
|
40
|
-
export * from './
|
|
41
|
-
export * from './GenericController';
|
|
42
|
-
export * from './GenericSearchController';
|
|
33
|
+
export * from './log';
|
|
34
|
+
export * from './LogController';
|
|
43
35
|
export * from './LowCodeController';
|
|
36
|
+
export * from './metadata';
|
|
37
|
+
export * from './resources';
|
|
38
|
+
export * from './search';
|
|
39
|
+
export * from './search_func';
|
|
40
|
+
export * from './SearchController';
|
|
41
|
+
export * from './view';
|
|
44
42
|
|
|
45
43
|
export interface AccessConfig {
|
|
46
44
|
origin?: string | string[];
|
|
@@ -87,7 +85,7 @@ export interface SavedService<T> {
|
|
|
87
85
|
}
|
|
88
86
|
export class SavedController<T> {
|
|
89
87
|
constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
|
|
90
|
-
this.id =
|
|
88
|
+
this.id = id && id.length > 0 ? id : 'id';
|
|
91
89
|
this.save = this.save.bind(this);
|
|
92
90
|
this.remove = this.remove.bind(this);
|
|
93
91
|
this.load = this.load.bind(this);
|
|
@@ -104,10 +102,12 @@ export class SavedController<T> {
|
|
|
104
102
|
res.status(400).end(`'${this.item}' cannot be empty`);
|
|
105
103
|
return;
|
|
106
104
|
}
|
|
107
|
-
this.service
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
this.service
|
|
106
|
+
.save(id, itemId)
|
|
107
|
+
.then((data) => {
|
|
108
|
+
res.status(200).json(data).end();
|
|
109
|
+
})
|
|
110
|
+
.catch((err) => handleError(err, res, this.log));
|
|
111
111
|
}
|
|
112
112
|
remove(req: Request, res: Response) {
|
|
113
113
|
const id = req.params[this.id];
|
|
@@ -120,10 +120,12 @@ export class SavedController<T> {
|
|
|
120
120
|
res.status(400).end(`'${this.item}' cannot be empty`);
|
|
121
121
|
return;
|
|
122
122
|
}
|
|
123
|
-
this.service
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
this.service
|
|
124
|
+
.remove(id, itemId)
|
|
125
|
+
.then((data) => {
|
|
126
|
+
res.status(200).json(data).end();
|
|
127
|
+
})
|
|
128
|
+
.catch((err) => handleError(err, res, this.log));
|
|
127
129
|
}
|
|
128
130
|
load(req: Request, res: Response) {
|
|
129
131
|
const id = req.params[this.id];
|
|
@@ -131,10 +133,12 @@ export class SavedController<T> {
|
|
|
131
133
|
res.status(400).end(`'${this.id}' cannot be empty`);
|
|
132
134
|
return;
|
|
133
135
|
}
|
|
134
|
-
this.service
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
this.service
|
|
137
|
+
.load(id)
|
|
138
|
+
.then((data) => {
|
|
139
|
+
res.status(200).json(data).end();
|
|
140
|
+
})
|
|
141
|
+
.catch((err) => handleError(err, res, this.log));
|
|
138
142
|
}
|
|
139
143
|
}
|
|
140
144
|
export interface FollowService {
|
|
@@ -145,7 +149,7 @@ export interface FollowService {
|
|
|
145
149
|
// tslint:disable-next-line:max-classes-per-file
|
|
146
150
|
export class FollowController {
|
|
147
151
|
constructor(public log: Log, public service: FollowService, public target: string, id: string) {
|
|
148
|
-
this.id =
|
|
152
|
+
this.id = id && id.length > 0 ? id : 'id';
|
|
149
153
|
this.follow = this.follow.bind(this);
|
|
150
154
|
this.unfollow = this.unfollow.bind(this);
|
|
151
155
|
this.checkFollow = this.checkFollow.bind(this);
|
|
@@ -162,9 +166,12 @@ export class FollowController {
|
|
|
162
166
|
res.status(400).end(`'${this.target}' cannot be empty`);
|
|
163
167
|
return;
|
|
164
168
|
}
|
|
165
|
-
this.service
|
|
166
|
-
|
|
167
|
-
|
|
169
|
+
this.service
|
|
170
|
+
.follow(id, target)
|
|
171
|
+
.then((count) => {
|
|
172
|
+
return res.status(200).json(count).end();
|
|
173
|
+
})
|
|
174
|
+
.catch((err) => handleError(err, res, this.log));
|
|
168
175
|
}
|
|
169
176
|
unfollow(req: Request, res: Response): void {
|
|
170
177
|
const id = req.params.id;
|
|
@@ -177,9 +184,12 @@ export class FollowController {
|
|
|
177
184
|
res.status(400).end(`'${this.target}' cannot be empty`);
|
|
178
185
|
return;
|
|
179
186
|
}
|
|
180
|
-
this.service
|
|
181
|
-
|
|
182
|
-
|
|
187
|
+
this.service
|
|
188
|
+
.unfollow(id, target)
|
|
189
|
+
.then((count) => {
|
|
190
|
+
return res.status(200).json(count).end();
|
|
191
|
+
})
|
|
192
|
+
.catch((err) => handleError(err, res, this.log));
|
|
183
193
|
}
|
|
184
194
|
checkFollow(req: Request, res: Response): void {
|
|
185
195
|
const id = req.params.id;
|
|
@@ -192,9 +202,12 @@ export class FollowController {
|
|
|
192
202
|
res.status(400).end(`'${this.target}' cannot be empty`);
|
|
193
203
|
return;
|
|
194
204
|
}
|
|
195
|
-
this.service
|
|
196
|
-
|
|
197
|
-
|
|
205
|
+
this.service
|
|
206
|
+
.checkFollow(id, target)
|
|
207
|
+
.then((count) => {
|
|
208
|
+
return res.status(200).json(count).end();
|
|
209
|
+
})
|
|
210
|
+
.catch((err) => handleError(err, res, this.log));
|
|
198
211
|
}
|
|
199
212
|
}
|
|
200
213
|
export interface ReactService {
|
|
@@ -205,7 +218,7 @@ export interface ReactService {
|
|
|
205
218
|
// tslint:disable-next-line:max-classes-per-file
|
|
206
219
|
export class UserReactionController {
|
|
207
220
|
constructor(public log: Log, public service: ReactService, public author: string, id: string, public reaction: string) {
|
|
208
|
-
this.id =
|
|
221
|
+
this.id = id && id.length > 0 ? id : 'id';
|
|
209
222
|
this.react = this.react.bind(this);
|
|
210
223
|
this.unreact = this.unreact.bind(this);
|
|
211
224
|
this.checkReaction = this.checkReaction.bind(this);
|
|
@@ -227,9 +240,12 @@ export class UserReactionController {
|
|
|
227
240
|
res.status(400).end(`'${this.reaction}' cannot be empty`);
|
|
228
241
|
return;
|
|
229
242
|
}
|
|
230
|
-
this.service
|
|
231
|
-
|
|
232
|
-
|
|
243
|
+
this.service
|
|
244
|
+
.react(id, author, reaction)
|
|
245
|
+
.then((count) => {
|
|
246
|
+
return res.status(200).json(count).end();
|
|
247
|
+
})
|
|
248
|
+
.catch((err) => handleError(err, res, this.log));
|
|
233
249
|
}
|
|
234
250
|
unreact(req: Request, res: Response): void {
|
|
235
251
|
const id = req.params.id;
|
|
@@ -247,9 +263,12 @@ export class UserReactionController {
|
|
|
247
263
|
res.status(400).end(`'${this.reaction}' cannot be empty`);
|
|
248
264
|
return;
|
|
249
265
|
}
|
|
250
|
-
this.service
|
|
251
|
-
|
|
252
|
-
|
|
266
|
+
this.service
|
|
267
|
+
.unreact(id, author, reaction)
|
|
268
|
+
.then((count) => {
|
|
269
|
+
return res.status(200).json(count).end();
|
|
270
|
+
})
|
|
271
|
+
.catch((err) => handleError(err, res, this.log));
|
|
253
272
|
}
|
|
254
273
|
checkReaction(req: Request, res: Response): void {
|
|
255
274
|
const id = req.params.id;
|
|
@@ -262,9 +281,12 @@ export class UserReactionController {
|
|
|
262
281
|
res.status(400).end(`'${this.author}' cannot be empty`);
|
|
263
282
|
return;
|
|
264
283
|
}
|
|
265
|
-
this.service
|
|
266
|
-
|
|
267
|
-
|
|
284
|
+
this.service
|
|
285
|
+
.checkReaction(id, author)
|
|
286
|
+
.then((count) => {
|
|
287
|
+
return res.status(200).json(count).end();
|
|
288
|
+
})
|
|
289
|
+
.catch((err) => handleError(err, res, this.log));
|
|
268
290
|
}
|
|
269
291
|
}
|
|
270
292
|
export const ReactController = UserReactionController;
|
package/src/search.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {Request, Response} from 'express';
|
|
2
|
-
import {minimizeArray} from './http';
|
|
3
|
-
import {Attribute, Attributes} from './metadata';
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
import { minimizeArray } from './http';
|
|
3
|
+
import { Attribute, Attributes } from './metadata';
|
|
4
4
|
|
|
5
5
|
export interface Filter {
|
|
6
6
|
fields?: string[];
|
|
@@ -29,6 +29,92 @@ export interface SearchResult<T> {
|
|
|
29
29
|
last?: boolean;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
export function getOffset(limit: number, page: number): number {
|
|
33
|
+
const offset = limit * (page - 1);
|
|
34
|
+
return offset < 0 ? 0 : offset;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function getPageTotal(pageSize?: number, total?: number): number {
|
|
38
|
+
if (!pageSize || pageSize <= 0) {
|
|
39
|
+
return 1;
|
|
40
|
+
} else {
|
|
41
|
+
if (!total) {
|
|
42
|
+
total = 0;
|
|
43
|
+
}
|
|
44
|
+
if (total % pageSize === 0) {
|
|
45
|
+
return Math.floor(total / pageSize);
|
|
46
|
+
}
|
|
47
|
+
return Math.floor(total / pageSize + 1);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function buildPages(pageSize?: number, total?: number): number[] {
|
|
51
|
+
const pageTotal = getPageTotal(pageSize, total);
|
|
52
|
+
if (pageTotal <= 1) {
|
|
53
|
+
return [1];
|
|
54
|
+
}
|
|
55
|
+
const arr: number[] = [];
|
|
56
|
+
for (let i = 1; i <= pageTotal; i++) {
|
|
57
|
+
arr.push(i);
|
|
58
|
+
}
|
|
59
|
+
return arr;
|
|
60
|
+
}
|
|
61
|
+
export function getPageQuery(query: string, page?: string): string {
|
|
62
|
+
const s = page && page.length > 0 ? page : 'page';
|
|
63
|
+
const i = query.indexOf(s + '=');
|
|
64
|
+
if (i < 0) {
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
const j = query.indexOf('&', i + s.length);
|
|
68
|
+
return j < 0 ? query.substring(i) : query.substring(i, j);
|
|
69
|
+
}
|
|
70
|
+
const PartialTrue = 'partial=true';
|
|
71
|
+
export function removePageQuery(query: string, page?: string, partialIsTrue?: string): string {
|
|
72
|
+
if (query.length == 0) {
|
|
73
|
+
return query;
|
|
74
|
+
}
|
|
75
|
+
const partialTrue = partialIsTrue && partialIsTrue.length > 0 ? partialIsTrue : PartialTrue;
|
|
76
|
+
const p1 = '&' + partialTrue;
|
|
77
|
+
const q1 = query.indexOf(p1);
|
|
78
|
+
if (q1 >= 0) {
|
|
79
|
+
query = query.substring(0, q1) + query.substring(q1 + partialTrue.length + 2);
|
|
80
|
+
} else {
|
|
81
|
+
const p2 = partialTrue + '&';
|
|
82
|
+
const q2 = query.indexOf(p2);
|
|
83
|
+
if (q2 >= 0) {
|
|
84
|
+
query = query.substring(0, q1) + query.substring(q1 + partialTrue.length + 2);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const pageQuery = getPageQuery(query, page);
|
|
88
|
+
if (pageQuery.length == 0) {
|
|
89
|
+
return query;
|
|
90
|
+
} else {
|
|
91
|
+
const x = pageQuery + '&';
|
|
92
|
+
if (query.indexOf(x) >= 0) {
|
|
93
|
+
return query.replace(x, '');
|
|
94
|
+
}
|
|
95
|
+
const x2 = '&' + pageQuery;
|
|
96
|
+
if (query.indexOf(x2) >= 0) {
|
|
97
|
+
return query.replace(x2, '');
|
|
98
|
+
}
|
|
99
|
+
return query.replace(pageQuery, '');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export function hasQuery(req: Request): boolean {
|
|
103
|
+
return req.url.indexOf('?') >= 0;
|
|
104
|
+
}
|
|
105
|
+
export function getQuery(url: string): string {
|
|
106
|
+
const i = url.indexOf('?');
|
|
107
|
+
return i < 0 ? '' : url.substring(i + 1);
|
|
108
|
+
}
|
|
109
|
+
export function buildPageQuery(query: string): string {
|
|
110
|
+
const qr = removePageQuery(query);
|
|
111
|
+
return qr.length == 0 ? qr : '&' + qr;
|
|
112
|
+
}
|
|
113
|
+
export function buildPageQueryFromUrl(url: string): string {
|
|
114
|
+
const query = getQuery(url);
|
|
115
|
+
return buildPageQuery(query);
|
|
116
|
+
}
|
|
117
|
+
|
|
32
118
|
export function jsonResult<T>(res: Response, result: SearchResult<T>, quick?: boolean, fields?: string[], config?: SearchConfig): void {
|
|
33
119
|
if (quick && fields && fields.length > 0) {
|
|
34
120
|
res.status(200).json(toCsv(fields, result)).end();
|
|
@@ -41,16 +127,16 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
|
|
|
41
127
|
return r;
|
|
42
128
|
}
|
|
43
129
|
const x: any = {};
|
|
44
|
-
const li =
|
|
130
|
+
const li = conf.list ? conf.list : 'list';
|
|
45
131
|
x[li] = minimizeArray(r.list);
|
|
46
|
-
const to =
|
|
132
|
+
const to = conf.total ? conf.total : 'total';
|
|
47
133
|
x[to] = r.total;
|
|
48
134
|
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
49
|
-
const t =
|
|
135
|
+
const t = conf.token ? conf.token : 'token';
|
|
50
136
|
x[t] = r.nextPageToken;
|
|
51
137
|
}
|
|
52
138
|
if (r.last) {
|
|
53
|
-
const l =
|
|
139
|
+
const l = conf.last ? conf.last : 'last';
|
|
54
140
|
x[l] = r.last;
|
|
55
141
|
}
|
|
56
142
|
return x;
|
|
@@ -71,7 +157,7 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
|
|
|
71
157
|
limit: conf.limit,
|
|
72
158
|
skip: conf.skip,
|
|
73
159
|
refId: conf.refId,
|
|
74
|
-
firstLimit: conf.firstLimit
|
|
160
|
+
firstLimit: conf.firstLimit,
|
|
75
161
|
};
|
|
76
162
|
if (!c.excluding || c.excluding.length === 0) {
|
|
77
163
|
c.excluding = 'excluding';
|
|
@@ -109,7 +195,7 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
|
|
|
109
195
|
return c;
|
|
110
196
|
}
|
|
111
197
|
export function fromRequest<S>(req: Request, arr?: string[]): S {
|
|
112
|
-
const s: any =
|
|
198
|
+
const s: any = req.method === 'GET' ? fromUrl(req, arr) : req.body;
|
|
113
199
|
return s;
|
|
114
200
|
}
|
|
115
201
|
export function buildArray(arr?: string[], s0?: string, s1?: string, s2?: string): string[] {
|
|
@@ -212,10 +298,10 @@ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: an
|
|
|
212
298
|
};
|
|
213
299
|
export interface Limit {
|
|
214
300
|
limit?: number;
|
|
215
|
-
|
|
216
|
-
|
|
301
|
+
offset?: number;
|
|
302
|
+
nextPageToken?: string;
|
|
217
303
|
fields?: string[];
|
|
218
|
-
|
|
304
|
+
offsetOrNextPageToken?: string | number;
|
|
219
305
|
}
|
|
220
306
|
export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
221
307
|
const o: any = obj;
|
|
@@ -231,7 +317,7 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
231
317
|
if (!refId) {
|
|
232
318
|
refId = o['nextPageToken'];
|
|
233
319
|
}
|
|
234
|
-
const r: Limit = {fields, refId};
|
|
320
|
+
const r: Limit = { fields, nextPageToken: refId };
|
|
235
321
|
let pageSize = o['limit'];
|
|
236
322
|
if (!pageSize) {
|
|
237
323
|
pageSize = o['pageSize'];
|
|
@@ -244,8 +330,8 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
244
330
|
if (skip && !isNaN(skip)) {
|
|
245
331
|
const iskip = Math.floor(parseFloat(skip));
|
|
246
332
|
if (iskip >= 0) {
|
|
247
|
-
r.
|
|
248
|
-
r.
|
|
333
|
+
r.offset = iskip;
|
|
334
|
+
r.offsetOrNextPageToken = r.offset;
|
|
249
335
|
deletePageInfo(o);
|
|
250
336
|
return r;
|
|
251
337
|
}
|
|
@@ -272,27 +358,27 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
272
358
|
if (firstPageSize && !isNaN(firstPageSize)) {
|
|
273
359
|
const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
|
|
274
360
|
if (ifirstPageSize > 0) {
|
|
275
|
-
r.
|
|
276
|
-
r.
|
|
361
|
+
r.offset = ipageSize * (ipageIndex - 2) + ifirstPageSize;
|
|
362
|
+
r.offsetOrNextPageToken = r.offset;
|
|
277
363
|
deletePageInfo(o);
|
|
278
364
|
return r;
|
|
279
365
|
}
|
|
280
366
|
}
|
|
281
|
-
r.
|
|
282
|
-
r.
|
|
367
|
+
r.offset = ipageSize * (ipageIndex - 1);
|
|
368
|
+
r.offsetOrNextPageToken = r.offset;
|
|
283
369
|
deletePageInfo(o);
|
|
284
370
|
return r;
|
|
285
371
|
}
|
|
286
|
-
r.
|
|
287
|
-
if (r.
|
|
288
|
-
r.
|
|
372
|
+
r.offset = 0;
|
|
373
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
374
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
289
375
|
}
|
|
290
376
|
deletePageInfo(o);
|
|
291
377
|
return r;
|
|
292
378
|
}
|
|
293
379
|
}
|
|
294
|
-
if (r.
|
|
295
|
-
r.
|
|
380
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
381
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
296
382
|
}
|
|
297
383
|
deletePageInfo(o);
|
|
298
384
|
return r;
|
|
@@ -312,7 +398,7 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
312
398
|
strRefId = 'refId';
|
|
313
399
|
}
|
|
314
400
|
const refId = o[strRefId];
|
|
315
|
-
const r: Limit = {fields, refId};
|
|
401
|
+
const r: Limit = { fields, nextPageToken: refId };
|
|
316
402
|
|
|
317
403
|
let strLimit = config.limit;
|
|
318
404
|
if (!strLimit || strLimit.length === 0) {
|
|
@@ -332,8 +418,8 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
332
418
|
if (skip && !isNaN(skip)) {
|
|
333
419
|
const iskip = Math.floor(parseFloat(skip));
|
|
334
420
|
if (iskip >= 0) {
|
|
335
|
-
r.
|
|
336
|
-
r.
|
|
421
|
+
r.offset = iskip;
|
|
422
|
+
r.offsetOrNextPageToken = r.offset;
|
|
337
423
|
deletePageInfo(o, arr);
|
|
338
424
|
return r;
|
|
339
425
|
}
|
|
@@ -356,27 +442,27 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
356
442
|
if (firstPageSize && !isNaN(firstPageSize)) {
|
|
357
443
|
const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
|
|
358
444
|
if (ifirstPageSize > 0) {
|
|
359
|
-
r.
|
|
360
|
-
r.
|
|
445
|
+
r.offset = ipageSize * (ipageIndex - 2) + ifirstPageSize;
|
|
446
|
+
r.offsetOrNextPageToken = r.offset;
|
|
361
447
|
deletePageInfo(o, arr);
|
|
362
448
|
return r;
|
|
363
449
|
}
|
|
364
450
|
}
|
|
365
|
-
r.
|
|
366
|
-
r.
|
|
451
|
+
r.offset = ipageSize * (ipageIndex - 1);
|
|
452
|
+
r.offsetOrNextPageToken = r.offset;
|
|
367
453
|
deletePageInfo(o, arr);
|
|
368
454
|
return r;
|
|
369
455
|
}
|
|
370
|
-
r.
|
|
371
|
-
if (r.
|
|
372
|
-
r.
|
|
456
|
+
r.offset = 0;
|
|
457
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
458
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
373
459
|
}
|
|
374
460
|
deletePageInfo(o, arr);
|
|
375
461
|
return r;
|
|
376
462
|
}
|
|
377
463
|
}
|
|
378
|
-
if (r.
|
|
379
|
-
r.
|
|
464
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
465
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
380
466
|
}
|
|
381
467
|
deletePageInfo(o, arr);
|
|
382
468
|
return r;
|