express-ext 0.2.3 → 0.2.4
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 +116 -66
- package/lib/search.js +135 -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 +96 -66
- package/src/search.ts +111 -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,52 @@
|
|
|
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';
|
|
42
|
+
|
|
43
|
+
export function hasQuery(req: Request): boolean {
|
|
44
|
+
return req.url.indexOf('?') >= 0;
|
|
45
|
+
}
|
|
46
|
+
export function getQuery(url: string): string {
|
|
47
|
+
const i = url.indexOf('?');
|
|
48
|
+
return i < 0 ? '' : url.substring(i + 1);
|
|
49
|
+
}
|
|
44
50
|
|
|
45
51
|
export interface AccessConfig {
|
|
46
52
|
origin?: string | string[];
|
|
@@ -87,7 +93,7 @@ export interface SavedService<T> {
|
|
|
87
93
|
}
|
|
88
94
|
export class SavedController<T> {
|
|
89
95
|
constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
|
|
90
|
-
this.id =
|
|
96
|
+
this.id = id && id.length > 0 ? id : 'id';
|
|
91
97
|
this.save = this.save.bind(this);
|
|
92
98
|
this.remove = this.remove.bind(this);
|
|
93
99
|
this.load = this.load.bind(this);
|
|
@@ -104,10 +110,12 @@ export class SavedController<T> {
|
|
|
104
110
|
res.status(400).end(`'${this.item}' cannot be empty`);
|
|
105
111
|
return;
|
|
106
112
|
}
|
|
107
|
-
this.service
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
113
|
+
this.service
|
|
114
|
+
.save(id, itemId)
|
|
115
|
+
.then((data) => {
|
|
116
|
+
res.status(200).json(data).end();
|
|
117
|
+
})
|
|
118
|
+
.catch((err) => handleError(err, res, this.log));
|
|
111
119
|
}
|
|
112
120
|
remove(req: Request, res: Response) {
|
|
113
121
|
const id = req.params[this.id];
|
|
@@ -120,10 +128,12 @@ export class SavedController<T> {
|
|
|
120
128
|
res.status(400).end(`'${this.item}' cannot be empty`);
|
|
121
129
|
return;
|
|
122
130
|
}
|
|
123
|
-
this.service
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
131
|
+
this.service
|
|
132
|
+
.remove(id, itemId)
|
|
133
|
+
.then((data) => {
|
|
134
|
+
res.status(200).json(data).end();
|
|
135
|
+
})
|
|
136
|
+
.catch((err) => handleError(err, res, this.log));
|
|
127
137
|
}
|
|
128
138
|
load(req: Request, res: Response) {
|
|
129
139
|
const id = req.params[this.id];
|
|
@@ -131,10 +141,12 @@ export class SavedController<T> {
|
|
|
131
141
|
res.status(400).end(`'${this.id}' cannot be empty`);
|
|
132
142
|
return;
|
|
133
143
|
}
|
|
134
|
-
this.service
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
144
|
+
this.service
|
|
145
|
+
.load(id)
|
|
146
|
+
.then((data) => {
|
|
147
|
+
res.status(200).json(data).end();
|
|
148
|
+
})
|
|
149
|
+
.catch((err) => handleError(err, res, this.log));
|
|
138
150
|
}
|
|
139
151
|
}
|
|
140
152
|
export interface FollowService {
|
|
@@ -145,7 +157,7 @@ export interface FollowService {
|
|
|
145
157
|
// tslint:disable-next-line:max-classes-per-file
|
|
146
158
|
export class FollowController {
|
|
147
159
|
constructor(public log: Log, public service: FollowService, public target: string, id: string) {
|
|
148
|
-
this.id =
|
|
160
|
+
this.id = id && id.length > 0 ? id : 'id';
|
|
149
161
|
this.follow = this.follow.bind(this);
|
|
150
162
|
this.unfollow = this.unfollow.bind(this);
|
|
151
163
|
this.checkFollow = this.checkFollow.bind(this);
|
|
@@ -162,9 +174,12 @@ export class FollowController {
|
|
|
162
174
|
res.status(400).end(`'${this.target}' cannot be empty`);
|
|
163
175
|
return;
|
|
164
176
|
}
|
|
165
|
-
this.service
|
|
166
|
-
|
|
167
|
-
|
|
177
|
+
this.service
|
|
178
|
+
.follow(id, target)
|
|
179
|
+
.then((count) => {
|
|
180
|
+
return res.status(200).json(count).end();
|
|
181
|
+
})
|
|
182
|
+
.catch((err) => handleError(err, res, this.log));
|
|
168
183
|
}
|
|
169
184
|
unfollow(req: Request, res: Response): void {
|
|
170
185
|
const id = req.params.id;
|
|
@@ -177,9 +192,12 @@ export class FollowController {
|
|
|
177
192
|
res.status(400).end(`'${this.target}' cannot be empty`);
|
|
178
193
|
return;
|
|
179
194
|
}
|
|
180
|
-
this.service
|
|
181
|
-
|
|
182
|
-
|
|
195
|
+
this.service
|
|
196
|
+
.unfollow(id, target)
|
|
197
|
+
.then((count) => {
|
|
198
|
+
return res.status(200).json(count).end();
|
|
199
|
+
})
|
|
200
|
+
.catch((err) => handleError(err, res, this.log));
|
|
183
201
|
}
|
|
184
202
|
checkFollow(req: Request, res: Response): void {
|
|
185
203
|
const id = req.params.id;
|
|
@@ -192,9 +210,12 @@ export class FollowController {
|
|
|
192
210
|
res.status(400).end(`'${this.target}' cannot be empty`);
|
|
193
211
|
return;
|
|
194
212
|
}
|
|
195
|
-
this.service
|
|
196
|
-
|
|
197
|
-
|
|
213
|
+
this.service
|
|
214
|
+
.checkFollow(id, target)
|
|
215
|
+
.then((count) => {
|
|
216
|
+
return res.status(200).json(count).end();
|
|
217
|
+
})
|
|
218
|
+
.catch((err) => handleError(err, res, this.log));
|
|
198
219
|
}
|
|
199
220
|
}
|
|
200
221
|
export interface ReactService {
|
|
@@ -205,7 +226,7 @@ export interface ReactService {
|
|
|
205
226
|
// tslint:disable-next-line:max-classes-per-file
|
|
206
227
|
export class UserReactionController {
|
|
207
228
|
constructor(public log: Log, public service: ReactService, public author: string, id: string, public reaction: string) {
|
|
208
|
-
this.id =
|
|
229
|
+
this.id = id && id.length > 0 ? id : 'id';
|
|
209
230
|
this.react = this.react.bind(this);
|
|
210
231
|
this.unreact = this.unreact.bind(this);
|
|
211
232
|
this.checkReaction = this.checkReaction.bind(this);
|
|
@@ -227,9 +248,12 @@ export class UserReactionController {
|
|
|
227
248
|
res.status(400).end(`'${this.reaction}' cannot be empty`);
|
|
228
249
|
return;
|
|
229
250
|
}
|
|
230
|
-
this.service
|
|
231
|
-
|
|
232
|
-
|
|
251
|
+
this.service
|
|
252
|
+
.react(id, author, reaction)
|
|
253
|
+
.then((count) => {
|
|
254
|
+
return res.status(200).json(count).end();
|
|
255
|
+
})
|
|
256
|
+
.catch((err) => handleError(err, res, this.log));
|
|
233
257
|
}
|
|
234
258
|
unreact(req: Request, res: Response): void {
|
|
235
259
|
const id = req.params.id;
|
|
@@ -247,9 +271,12 @@ export class UserReactionController {
|
|
|
247
271
|
res.status(400).end(`'${this.reaction}' cannot be empty`);
|
|
248
272
|
return;
|
|
249
273
|
}
|
|
250
|
-
this.service
|
|
251
|
-
|
|
252
|
-
|
|
274
|
+
this.service
|
|
275
|
+
.unreact(id, author, reaction)
|
|
276
|
+
.then((count) => {
|
|
277
|
+
return res.status(200).json(count).end();
|
|
278
|
+
})
|
|
279
|
+
.catch((err) => handleError(err, res, this.log));
|
|
253
280
|
}
|
|
254
281
|
checkReaction(req: Request, res: Response): void {
|
|
255
282
|
const id = req.params.id;
|
|
@@ -262,9 +289,12 @@ export class UserReactionController {
|
|
|
262
289
|
res.status(400).end(`'${this.author}' cannot be empty`);
|
|
263
290
|
return;
|
|
264
291
|
}
|
|
265
|
-
this.service
|
|
266
|
-
|
|
267
|
-
|
|
292
|
+
this.service
|
|
293
|
+
.checkReaction(id, author)
|
|
294
|
+
.then((count) => {
|
|
295
|
+
return res.status(200).json(count).end();
|
|
296
|
+
})
|
|
297
|
+
.catch((err) => handleError(err, res, this.log));
|
|
268
298
|
}
|
|
269
299
|
}
|
|
270
300
|
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,81 @@ 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 buildPageQuery(query: string): string {
|
|
103
|
+
const qr = removePageQuery(query);
|
|
104
|
+
return qr.length == 0 ? qr : '&' + qr;
|
|
105
|
+
}
|
|
106
|
+
|
|
32
107
|
export function jsonResult<T>(res: Response, result: SearchResult<T>, quick?: boolean, fields?: string[], config?: SearchConfig): void {
|
|
33
108
|
if (quick && fields && fields.length > 0) {
|
|
34
109
|
res.status(200).json(toCsv(fields, result)).end();
|
|
@@ -41,16 +116,16 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
|
|
|
41
116
|
return r;
|
|
42
117
|
}
|
|
43
118
|
const x: any = {};
|
|
44
|
-
const li =
|
|
119
|
+
const li = conf.list ? conf.list : 'list';
|
|
45
120
|
x[li] = minimizeArray(r.list);
|
|
46
|
-
const to =
|
|
121
|
+
const to = conf.total ? conf.total : 'total';
|
|
47
122
|
x[to] = r.total;
|
|
48
123
|
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
49
|
-
const t =
|
|
124
|
+
const t = conf.token ? conf.token : 'token';
|
|
50
125
|
x[t] = r.nextPageToken;
|
|
51
126
|
}
|
|
52
127
|
if (r.last) {
|
|
53
|
-
const l =
|
|
128
|
+
const l = conf.last ? conf.last : 'last';
|
|
54
129
|
x[l] = r.last;
|
|
55
130
|
}
|
|
56
131
|
return x;
|
|
@@ -71,7 +146,7 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
|
|
|
71
146
|
limit: conf.limit,
|
|
72
147
|
skip: conf.skip,
|
|
73
148
|
refId: conf.refId,
|
|
74
|
-
firstLimit: conf.firstLimit
|
|
149
|
+
firstLimit: conf.firstLimit,
|
|
75
150
|
};
|
|
76
151
|
if (!c.excluding || c.excluding.length === 0) {
|
|
77
152
|
c.excluding = 'excluding';
|
|
@@ -109,7 +184,7 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
|
|
|
109
184
|
return c;
|
|
110
185
|
}
|
|
111
186
|
export function fromRequest<S>(req: Request, arr?: string[]): S {
|
|
112
|
-
const s: any =
|
|
187
|
+
const s: any = req.method === 'GET' ? fromUrl(req, arr) : req.body;
|
|
113
188
|
return s;
|
|
114
189
|
}
|
|
115
190
|
export function buildArray(arr?: string[], s0?: string, s1?: string, s2?: string): string[] {
|
|
@@ -212,10 +287,10 @@ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: an
|
|
|
212
287
|
};
|
|
213
288
|
export interface Limit {
|
|
214
289
|
limit?: number;
|
|
215
|
-
|
|
216
|
-
|
|
290
|
+
offset?: number;
|
|
291
|
+
nextPageToken?: string;
|
|
217
292
|
fields?: string[];
|
|
218
|
-
|
|
293
|
+
offsetOrNextPageToken?: string | number;
|
|
219
294
|
}
|
|
220
295
|
export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
221
296
|
const o: any = obj;
|
|
@@ -231,7 +306,7 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
231
306
|
if (!refId) {
|
|
232
307
|
refId = o['nextPageToken'];
|
|
233
308
|
}
|
|
234
|
-
const r: Limit = {fields, refId};
|
|
309
|
+
const r: Limit = { fields, nextPageToken: refId };
|
|
235
310
|
let pageSize = o['limit'];
|
|
236
311
|
if (!pageSize) {
|
|
237
312
|
pageSize = o['pageSize'];
|
|
@@ -244,8 +319,8 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
244
319
|
if (skip && !isNaN(skip)) {
|
|
245
320
|
const iskip = Math.floor(parseFloat(skip));
|
|
246
321
|
if (iskip >= 0) {
|
|
247
|
-
r.
|
|
248
|
-
r.
|
|
322
|
+
r.offset = iskip;
|
|
323
|
+
r.offsetOrNextPageToken = r.offset;
|
|
249
324
|
deletePageInfo(o);
|
|
250
325
|
return r;
|
|
251
326
|
}
|
|
@@ -272,27 +347,27 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
272
347
|
if (firstPageSize && !isNaN(firstPageSize)) {
|
|
273
348
|
const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
|
|
274
349
|
if (ifirstPageSize > 0) {
|
|
275
|
-
r.
|
|
276
|
-
r.
|
|
350
|
+
r.offset = ipageSize * (ipageIndex - 2) + ifirstPageSize;
|
|
351
|
+
r.offsetOrNextPageToken = r.offset;
|
|
277
352
|
deletePageInfo(o);
|
|
278
353
|
return r;
|
|
279
354
|
}
|
|
280
355
|
}
|
|
281
|
-
r.
|
|
282
|
-
r.
|
|
356
|
+
r.offset = ipageSize * (ipageIndex - 1);
|
|
357
|
+
r.offsetOrNextPageToken = r.offset;
|
|
283
358
|
deletePageInfo(o);
|
|
284
359
|
return r;
|
|
285
360
|
}
|
|
286
|
-
r.
|
|
287
|
-
if (r.
|
|
288
|
-
r.
|
|
361
|
+
r.offset = 0;
|
|
362
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
363
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
289
364
|
}
|
|
290
365
|
deletePageInfo(o);
|
|
291
366
|
return r;
|
|
292
367
|
}
|
|
293
368
|
}
|
|
294
|
-
if (r.
|
|
295
|
-
r.
|
|
369
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
370
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
296
371
|
}
|
|
297
372
|
deletePageInfo(o);
|
|
298
373
|
return r;
|
|
@@ -312,7 +387,7 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
312
387
|
strRefId = 'refId';
|
|
313
388
|
}
|
|
314
389
|
const refId = o[strRefId];
|
|
315
|
-
const r: Limit = {fields, refId};
|
|
390
|
+
const r: Limit = { fields, nextPageToken: refId };
|
|
316
391
|
|
|
317
392
|
let strLimit = config.limit;
|
|
318
393
|
if (!strLimit || strLimit.length === 0) {
|
|
@@ -332,8 +407,8 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
332
407
|
if (skip && !isNaN(skip)) {
|
|
333
408
|
const iskip = Math.floor(parseFloat(skip));
|
|
334
409
|
if (iskip >= 0) {
|
|
335
|
-
r.
|
|
336
|
-
r.
|
|
410
|
+
r.offset = iskip;
|
|
411
|
+
r.offsetOrNextPageToken = r.offset;
|
|
337
412
|
deletePageInfo(o, arr);
|
|
338
413
|
return r;
|
|
339
414
|
}
|
|
@@ -356,27 +431,27 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
|
|
|
356
431
|
if (firstPageSize && !isNaN(firstPageSize)) {
|
|
357
432
|
const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
|
|
358
433
|
if (ifirstPageSize > 0) {
|
|
359
|
-
r.
|
|
360
|
-
r.
|
|
434
|
+
r.offset = ipageSize * (ipageIndex - 2) + ifirstPageSize;
|
|
435
|
+
r.offsetOrNextPageToken = r.offset;
|
|
361
436
|
deletePageInfo(o, arr);
|
|
362
437
|
return r;
|
|
363
438
|
}
|
|
364
439
|
}
|
|
365
|
-
r.
|
|
366
|
-
r.
|
|
440
|
+
r.offset = ipageSize * (ipageIndex - 1);
|
|
441
|
+
r.offsetOrNextPageToken = r.offset;
|
|
367
442
|
deletePageInfo(o, arr);
|
|
368
443
|
return r;
|
|
369
444
|
}
|
|
370
|
-
r.
|
|
371
|
-
if (r.
|
|
372
|
-
r.
|
|
445
|
+
r.offset = 0;
|
|
446
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
447
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
373
448
|
}
|
|
374
449
|
deletePageInfo(o, arr);
|
|
375
450
|
return r;
|
|
376
451
|
}
|
|
377
452
|
}
|
|
378
|
-
if (r.
|
|
379
|
-
r.
|
|
453
|
+
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
454
|
+
r.offsetOrNextPageToken = r.nextPageToken;
|
|
380
455
|
}
|
|
381
456
|
deletePageInfo(o, arr);
|
|
382
457
|
return r;
|