@ewb/mongoose-query 1.0.0
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/README.md +70 -0
- package/lib/ExpressHandler.d.ts +30 -0
- package/lib/ExpressHandler.js +215 -0
- package/lib/ExpressHandler.js.map +1 -0
- package/lib/ModelQuery.d.ts +24 -0
- package/lib/ModelQuery.js +80 -0
- package/lib/ModelQuery.js.map +1 -0
- package/lib/Query.d.ts +77 -0
- package/lib/Query.js +307 -0
- package/lib/Query.js.map +1 -0
- package/lib/QueryError.d.ts +50 -0
- package/lib/QueryError.js +62 -0
- package/lib/QueryError.js.map +1 -0
- package/lib/QueryExpressHandler.d.ts +50 -0
- package/lib/QueryExpressHandler.js +383 -0
- package/lib/QueryExpressHandler.js.map +1 -0
- package/lib/QueryExpressSubHandler.d.ts +20 -0
- package/lib/QueryExpressSubHandler.js +137 -0
- package/lib/QueryExpressSubHandler.js.map +1 -0
- package/lib/QueryHandler.d.ts +59 -0
- package/lib/QueryHandler.js +297 -0
- package/lib/QueryHandler.js.map +1 -0
- package/lib/QueryHistory.d.ts +76 -0
- package/lib/QueryHistory.js +154 -0
- package/lib/QueryHistory.js.map +1 -0
- package/lib/QueryModel.d.ts +80 -0
- package/lib/QueryModel.js +201 -0
- package/lib/QueryModel.js.map +1 -0
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +6 -0
- package/lib/constants.js.map +1 -0
- package/lib/declarations.d.ts +16 -0
- package/lib/declarations.js +3 -0
- package/lib/declarations.js.map +1 -0
- package/lib/error/ExpressError.d.ts +44 -0
- package/lib/error/ExpressError.js +39 -0
- package/lib/error/ExpressError.js.map +1 -0
- package/lib/error/QueryError.d.ts +47 -0
- package/lib/error/QueryError.js +50 -0
- package/lib/error/QueryError.js.map +1 -0
- package/lib/error/QueryExpressError.d.ts +46 -0
- package/lib/error/QueryExpressError.js +39 -0
- package/lib/error/QueryExpressError.js.map +1 -0
- package/lib/error/V1Error.d.ts +12 -0
- package/lib/error/V1Error.js +27 -0
- package/lib/error/V1Error.js.map +1 -0
- package/lib/error/error.handler.d.ts +3 -0
- package/lib/error/error.handler.js +30 -0
- package/lib/error/error.handler.js.map +1 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +32 -0
- package/lib/index.js.map +1 -0
- package/lib/statics.d.ts +17 -0
- package/lib/statics.js +35 -0
- package/lib/statics.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/utils.d.ts +3 -0
- package/lib/utils.js +44 -0
- package/lib/utils.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryModel = exports.toValidTextRegexp = exports.toValidSortRegexp = exports.toValidSelectRegexp = void 0;
|
|
4
|
+
const mongoose_1 = require("mongoose");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const toValidSelectRegexp = (select) => new RegExp(`^${String(select)}(\..|$)`);
|
|
7
|
+
exports.toValidSelectRegexp = toValidSelectRegexp;
|
|
8
|
+
const toValidSortRegexp = (sort) => new RegExp(`^(\\+|-)?(${String(sort)}|text)$`);
|
|
9
|
+
exports.toValidSortRegexp = toValidSortRegexp;
|
|
10
|
+
const toValidTextRegexp = (str) => typeof str === 'string' ? new RegExp(str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i') : '';
|
|
11
|
+
exports.toValidTextRegexp = toValidTextRegexp;
|
|
12
|
+
class QueryModel {
|
|
13
|
+
constructor(opts = {}) {
|
|
14
|
+
this.opts = opts;
|
|
15
|
+
}
|
|
16
|
+
get publicFields() {
|
|
17
|
+
return this.opts.editFields;
|
|
18
|
+
}
|
|
19
|
+
get privateFields() {
|
|
20
|
+
return this.opts.privateFields;
|
|
21
|
+
}
|
|
22
|
+
get populate() {
|
|
23
|
+
return this.opts.populate;
|
|
24
|
+
}
|
|
25
|
+
get subs() {
|
|
26
|
+
return this.opts.subs;
|
|
27
|
+
}
|
|
28
|
+
get updateDateKey() {
|
|
29
|
+
return this.opts.updateDateKey;
|
|
30
|
+
}
|
|
31
|
+
get query() {
|
|
32
|
+
return this.opts.query;
|
|
33
|
+
}
|
|
34
|
+
createSearchQuery(query) {
|
|
35
|
+
return query.createQuery({ noRoot: false, query: this.opts.query });
|
|
36
|
+
}
|
|
37
|
+
setPopulate(modelQuery, query) {
|
|
38
|
+
let populate = [];
|
|
39
|
+
if (query.populate.length > 0 && this.opts.populate) {
|
|
40
|
+
populate = this.getValidPopulate(query.populate).map(x => (Object.assign(Object.assign({}, x), { select: x.select.join(' ') })));
|
|
41
|
+
}
|
|
42
|
+
if (query.rootPopulate.length > 0) {
|
|
43
|
+
populate.push(...query.rootPopulate);
|
|
44
|
+
}
|
|
45
|
+
if (populate.length > 0) {
|
|
46
|
+
modelQuery.populate(populate);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
getValidPopulate(queryPopulate, sub) {
|
|
50
|
+
let checkPop = this.opts.populate;
|
|
51
|
+
if (this.opts.subs && sub) {
|
|
52
|
+
const found = this.opts.subs.find(x => x.sub === sub);
|
|
53
|
+
if (!found || !found.populate || found.populate.length === 0)
|
|
54
|
+
return [];
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
checkPop = found.populate;
|
|
57
|
+
}
|
|
58
|
+
if (!checkPop)
|
|
59
|
+
return [];
|
|
60
|
+
return queryPopulate.map(pop => {
|
|
61
|
+
var _a;
|
|
62
|
+
const found = checkPop.find(x => x.path === pop.path);
|
|
63
|
+
if (!found)
|
|
64
|
+
return null;
|
|
65
|
+
const select = ((_a = pop.select) === null || _a === void 0 ? void 0 : _a.filter(x => found.select.includes(x))) || [];
|
|
66
|
+
return Object.assign({ path: pop.path, select: select.length > 0 ? select : found.select }, found.populate ? { populate: found.populate } : {});
|
|
67
|
+
}).filter(Boolean);
|
|
68
|
+
}
|
|
69
|
+
setLimitAndSkip(modelQuery, query) {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
if (query.export)
|
|
72
|
+
return;
|
|
73
|
+
modelQuery.skip((_a = query.skip) !== null && _a !== void 0 ? _a : 0);
|
|
74
|
+
modelQuery.limit((_c = (_b = query.limit) !== null && _b !== void 0 ? _b : this.opts.limit) !== null && _c !== void 0 ? _c : constants_1.DEFAULT_LIMIT);
|
|
75
|
+
}
|
|
76
|
+
setSelect(modelQuery, query, overrideValid) {
|
|
77
|
+
const valids = overrideValid || this.opts.select;
|
|
78
|
+
let selects = valids || [];
|
|
79
|
+
if (valids && valids.length > 0 && query.select.length > 0) {
|
|
80
|
+
selects = query.select.filter(select => valids.some(valid => String(select).match(exports.toValidSelectRegexp(valid))));
|
|
81
|
+
}
|
|
82
|
+
else if (this.opts.defaultSelect) {
|
|
83
|
+
selects = this.opts.defaultSelect;
|
|
84
|
+
}
|
|
85
|
+
if (selects.length > 0) {
|
|
86
|
+
modelQuery.select(selects.join(' '));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
setSort(modelQuery, query) {
|
|
90
|
+
if (query.sort.length > 0) {
|
|
91
|
+
if (this.opts.sort) {
|
|
92
|
+
const validSorts = query.sort.filter(x => this.opts.sort.some(y => x.match(exports.toValidSortRegexp(y))));
|
|
93
|
+
modelQuery.sort(sortArrayToSortObject(validSorts, query.hasTextSearch));
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
modelQuery.sort(sortArrayToSortObject(query.sort, query.hasTextSearch));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
setProjection(modelQuery, query) {
|
|
101
|
+
if (query.projection) {
|
|
102
|
+
modelQuery.projection(query.projection);
|
|
103
|
+
query.projection = undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
getSubPopulateLookup(query, sub) {
|
|
107
|
+
if (!this.opts.subs)
|
|
108
|
+
return [];
|
|
109
|
+
const validPopulate = this.opts.subs.find(x => x.sub === sub);
|
|
110
|
+
if (!validPopulate)
|
|
111
|
+
return [];
|
|
112
|
+
const populate = query.populate;
|
|
113
|
+
const $lookups = [];
|
|
114
|
+
if (populate.length > 0) {
|
|
115
|
+
for (const pop of populate) {
|
|
116
|
+
const found = validPopulate.populate.find(x => x.path === pop.path);
|
|
117
|
+
if (!found)
|
|
118
|
+
continue;
|
|
119
|
+
const select = pop.select
|
|
120
|
+
? found.select.filter(x => pop.select.includes(String(x)))
|
|
121
|
+
: found.select;
|
|
122
|
+
$lookups.push(...[{
|
|
123
|
+
$lookup: {
|
|
124
|
+
from: found.lookupRef || `${pop.path}s`,
|
|
125
|
+
localField: `${sub}.${pop.path}`,
|
|
126
|
+
foreignField: '_id',
|
|
127
|
+
as: pop.path
|
|
128
|
+
}
|
|
129
|
+
}, ...pop.select ? [{
|
|
130
|
+
$project: {
|
|
131
|
+
_id: 1,
|
|
132
|
+
[sub]: 1,
|
|
133
|
+
count: 1,
|
|
134
|
+
[pop.path]: select.reduce((obj, item) => (Object.assign(Object.assign({}, obj), { [item]: 1 })), {})
|
|
135
|
+
}
|
|
136
|
+
}] : []]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return $lookups;
|
|
140
|
+
}
|
|
141
|
+
getSubFilterConditionAndMatch(subQuery, sub) {
|
|
142
|
+
const cond = {};
|
|
143
|
+
let $match = {};
|
|
144
|
+
for (const key in subQuery) {
|
|
145
|
+
const v = subQuery[key];
|
|
146
|
+
if (key !== '$and' && !Array.isArray(v)) {
|
|
147
|
+
const $and = [];
|
|
148
|
+
const [newItem, toMatch] = queryToSubFilter(key, subQuery[key]);
|
|
149
|
+
if (newItem.length > 0) {
|
|
150
|
+
$and.push(...newItem);
|
|
151
|
+
}
|
|
152
|
+
$match = Object.assign(Object.assign({}, $match), toMatch);
|
|
153
|
+
cond['$and'] = $and;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const $filter = { input: `$${sub}`, as: 'subdoc', cond };
|
|
157
|
+
return { $filter, $match };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.QueryModel = QueryModel;
|
|
161
|
+
function queryToSubFilter(key, value) {
|
|
162
|
+
if (!value)
|
|
163
|
+
return [[], {}];
|
|
164
|
+
if (typeof value === 'object') {
|
|
165
|
+
if ('$in' in value && value.$in) {
|
|
166
|
+
return [[{ $in: [`$$subdoc.${key}`, value.$in.map(toCorrectIdValue)] }], {}];
|
|
167
|
+
}
|
|
168
|
+
if ('$nin' in value && value.$nin) {
|
|
169
|
+
return [value.$nin.map(x => ({
|
|
170
|
+
$ne: [`$$subdoc.${key}`, toCorrectIdValue(x)]
|
|
171
|
+
})), {}];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return [[], { [key]: toCorrectIdValue(value) }];
|
|
175
|
+
}
|
|
176
|
+
function toCorrectIdValue(v) {
|
|
177
|
+
if (!v)
|
|
178
|
+
return v;
|
|
179
|
+
if (mongoose_1.isValidObjectId(v))
|
|
180
|
+
return mongoose_1.Types.ObjectId(v);
|
|
181
|
+
return v;
|
|
182
|
+
}
|
|
183
|
+
function sortArrayToSortObject(arr, hasText) {
|
|
184
|
+
const sortObj = {};
|
|
185
|
+
for (const item of arr) {
|
|
186
|
+
if (item === 'text' && hasText) {
|
|
187
|
+
sortObj['score'] = { $meta: 'textScore' };
|
|
188
|
+
}
|
|
189
|
+
else if (item[0] === '-') {
|
|
190
|
+
sortObj[item.substr(1)] = -1;
|
|
191
|
+
}
|
|
192
|
+
else if (item[0] === '+') {
|
|
193
|
+
sortObj[item.substr(1)] = 1;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
sortObj[item] = 1;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return sortObj;
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=QueryModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryModel.js","sourceRoot":"","sources":["../src/QueryModel.ts"],"names":[],"mappings":";;;AAAA,u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,MAAM,OAAO,GAAQ,EAAE,CAAC;IACxB,KAAI,MAAM,IAAI,IAAI,GAAG,EAAE;QACrB,IAAI,IAAI,KAAK,MAAM,IAAI,OAAO,EAAE;YAC9B,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC;SAC3C;aAAM,IAAG,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;YACzB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;SAC9B;aAAM,IAAG,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;YACzB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;SAC7B;aAAM;YACL,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SACnB;KACF;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AACa,QAAA,aAAa,GAAG,EAAE,CAAC;AACnB,QAAA,YAAY,GAAG,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Types } from 'mongoose';
|
|
2
|
+
import { Query } from './Query';
|
|
3
|
+
import { QueryHistory } from './QueryHistory';
|
|
4
|
+
declare global {
|
|
5
|
+
namespace Express {
|
|
6
|
+
interface Request {
|
|
7
|
+
id: string;
|
|
8
|
+
mongoosePostBody: {
|
|
9
|
+
[key: string]: string | Types.ObjectId;
|
|
10
|
+
};
|
|
11
|
+
mongooseQuery: Query<any>;
|
|
12
|
+
mongooseHistory?: QueryHistory<any>;
|
|
13
|
+
mongooseExportJson?: any[];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declarations.js","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare enum ExpressErrorCode {
|
|
2
|
+
Default = "default",
|
|
3
|
+
ServerError = "server-error",
|
|
4
|
+
NotFound = "not-found",
|
|
5
|
+
BadRequest = "bad-request"
|
|
6
|
+
}
|
|
7
|
+
export interface IExpressErrorRequest extends Request {
|
|
8
|
+
id: string;
|
|
9
|
+
}
|
|
10
|
+
export interface AptlyError {
|
|
11
|
+
id: string;
|
|
12
|
+
links?: {
|
|
13
|
+
self?: AptlyErrorLink | string;
|
|
14
|
+
};
|
|
15
|
+
status: string;
|
|
16
|
+
code: ExpressErrorCode;
|
|
17
|
+
title?: string;
|
|
18
|
+
detail?: string;
|
|
19
|
+
source?: {
|
|
20
|
+
pointer?: string;
|
|
21
|
+
parameter?: string;
|
|
22
|
+
header?: string;
|
|
23
|
+
body?: string;
|
|
24
|
+
};
|
|
25
|
+
meta?: object;
|
|
26
|
+
}
|
|
27
|
+
export interface AptlyErrorLink {
|
|
28
|
+
href: string;
|
|
29
|
+
title?: string;
|
|
30
|
+
describedBy?: string;
|
|
31
|
+
meta: {
|
|
32
|
+
count: number;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export declare class ExpressError<R extends IExpressErrorRequest> {
|
|
36
|
+
private req;
|
|
37
|
+
readonly code: ExpressErrorCode;
|
|
38
|
+
readonly optional: Pick<AptlyError, 'meta' | 'title' | 'detail' | 'links' | 'source'>;
|
|
39
|
+
readonly status: number;
|
|
40
|
+
constructor(req: R, code: ExpressErrorCode, optional?: Pick<AptlyError, 'meta' | 'title' | 'detail' | 'links' | 'source'>);
|
|
41
|
+
toJSON(): AptlyError;
|
|
42
|
+
static catch<R extends IExpressErrorRequest>(req: R, e: Error | ExpressError<R>): ExpressError<R>;
|
|
43
|
+
static getStatusFromCode(code: ExpressErrorCode): 404 | 400 | 500;
|
|
44
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpressError = exports.ExpressErrorCode = void 0;
|
|
4
|
+
var ExpressErrorCode;
|
|
5
|
+
(function (ExpressErrorCode) {
|
|
6
|
+
ExpressErrorCode["Default"] = "default";
|
|
7
|
+
ExpressErrorCode["ServerError"] = "server-error";
|
|
8
|
+
ExpressErrorCode["NotFound"] = "not-found";
|
|
9
|
+
ExpressErrorCode["BadRequest"] = "bad-request";
|
|
10
|
+
})(ExpressErrorCode = exports.ExpressErrorCode || (exports.ExpressErrorCode = {}));
|
|
11
|
+
class ExpressError {
|
|
12
|
+
constructor(req, code, optional = {}) {
|
|
13
|
+
this.req = req;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.optional = optional;
|
|
16
|
+
this.status = ExpressError.getStatusFromCode(code);
|
|
17
|
+
}
|
|
18
|
+
toJSON() {
|
|
19
|
+
return Object.assign({ id: this.req.id, status: String(this.status), code: this.code }, this.optional);
|
|
20
|
+
}
|
|
21
|
+
static catch(req, e) {
|
|
22
|
+
if (e instanceof ExpressError) {
|
|
23
|
+
return e;
|
|
24
|
+
}
|
|
25
|
+
console.error(e);
|
|
26
|
+
return new ExpressError(req, ExpressErrorCode.ServerError, {
|
|
27
|
+
detail: e.message
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
static getStatusFromCode(code) {
|
|
31
|
+
switch (code) {
|
|
32
|
+
case ExpressErrorCode.NotFound: return 404;
|
|
33
|
+
case ExpressErrorCode.BadRequest: return 400;
|
|
34
|
+
default: return 500;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.ExpressError = ExpressError;
|
|
39
|
+
//# sourceMappingURL=ExpressError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpressError.js","sourceRoot":"","sources":["../../src/error/ExpressError.ts"],"names":[],"mappings":";;;AACA,IAAY,gBAKX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{ Request } from 'express';
|
|
2
|
+
export * from './error.handler';
|
|
3
|
+
export declare enum QueryErrorCode {
|
|
4
|
+
Default = "default",
|
|
5
|
+
ServerError = "server-error",
|
|
6
|
+
NotFound = "not-found",
|
|
7
|
+
BadRequest = "bad-request"
|
|
8
|
+
}
|
|
9
|
+
export interface IQueryErrorRequest extends Request {
|
|
10
|
+
id: string;
|
|
11
|
+
}
|
|
12
|
+
export interface IQueryError {
|
|
13
|
+
id: string;
|
|
14
|
+
links?: {
|
|
15
|
+
self?: IQueryErrorLink | string;
|
|
16
|
+
};
|
|
17
|
+
status: string;
|
|
18
|
+
code: QueryErrorCode;
|
|
19
|
+
title?: string;
|
|
20
|
+
detail?: string;
|
|
21
|
+
source?: {
|
|
22
|
+
pointer?: string;
|
|
23
|
+
parameter?: string;
|
|
24
|
+
header?: string;
|
|
25
|
+
body?: string;
|
|
26
|
+
};
|
|
27
|
+
meta?: object;
|
|
28
|
+
}
|
|
29
|
+
export interface IQueryErrorLink {
|
|
30
|
+
href: string;
|
|
31
|
+
title?: string;
|
|
32
|
+
describedBy?: string;
|
|
33
|
+
meta: {
|
|
34
|
+
count: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
declare type Optional = Pick<IQueryError, 'meta' | 'title' | 'detail' | 'links' | 'source'>;
|
|
38
|
+
export declare class QueryError<R extends IQueryErrorRequest> {
|
|
39
|
+
private req;
|
|
40
|
+
readonly code: QueryErrorCode;
|
|
41
|
+
readonly optional: Optional;
|
|
42
|
+
readonly status: number;
|
|
43
|
+
constructor(req: R, code: QueryErrorCode, optional?: Optional);
|
|
44
|
+
toJSON(): IQueryError;
|
|
45
|
+
static catch<R extends IQueryErrorRequest>(req: R, e: Error | QueryError<R>): QueryError<R>;
|
|
46
|
+
static getStatusFromCode(code: QueryErrorCode): 500 | 404 | 400;
|
|
47
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.QueryError = exports.QueryErrorCode = void 0;
|
|
14
|
+
__exportStar(require("./error.handler"), exports);
|
|
15
|
+
var QueryErrorCode;
|
|
16
|
+
(function (QueryErrorCode) {
|
|
17
|
+
QueryErrorCode["Default"] = "default";
|
|
18
|
+
QueryErrorCode["ServerError"] = "server-error";
|
|
19
|
+
QueryErrorCode["NotFound"] = "not-found";
|
|
20
|
+
QueryErrorCode["BadRequest"] = "bad-request";
|
|
21
|
+
})(QueryErrorCode = exports.QueryErrorCode || (exports.QueryErrorCode = {}));
|
|
22
|
+
class QueryError {
|
|
23
|
+
constructor(req, code, optional = {}) {
|
|
24
|
+
this.req = req;
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.optional = optional;
|
|
27
|
+
this.status = QueryError.getStatusFromCode(code);
|
|
28
|
+
}
|
|
29
|
+
toJSON() {
|
|
30
|
+
return Object.assign({ id: this.req.id, status: String(this.status), code: this.code }, this.optional);
|
|
31
|
+
}
|
|
32
|
+
static catch(req, e) {
|
|
33
|
+
if (e instanceof QueryError) {
|
|
34
|
+
return e;
|
|
35
|
+
}
|
|
36
|
+
console.error(e);
|
|
37
|
+
return new QueryError(req, QueryErrorCode.ServerError, {
|
|
38
|
+
detail: e.message
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
static getStatusFromCode(code) {
|
|
42
|
+
switch (code) {
|
|
43
|
+
case QueryErrorCode.NotFound: return 404;
|
|
44
|
+
case QueryErrorCode.BadRequest: return 400;
|
|
45
|
+
default: return 500;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.QueryError = QueryError;
|
|
50
|
+
//# sourceMappingURL=QueryError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryError.js","sourceRoot":"","sources":["../../src/error/QueryError.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAEA,kDAAgC;AAEhC,IAAY,cAKX;AALD,WAAY,cAAc;IACxB,qCAAmB,CAAA;IACnB,8CAA4B,CAAA;IAC5B,wCAAsB,CAAA;IACtB,4CAA0B,CAAA;AAC5B,CAAC,EALW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAKzB;AAkCD,MAAa,UAAU;IAGrB,YACU,GAAM,EACL,IAAoB,EACpB,WAAqB,EAAE;QAFxB,QAAG,GAAH,GAAG,CAAG;QACL,SAAI,GAAJ,IAAI,CAAgB;QACpB,aAAQ,GAAR,QAAQ,CAAe;QAEhC,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACnD,CAAC;IAEM,MAAM;QACX,uBACE,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EACf,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAC3B,IAAI,EAAE,IAAI,CAAC,IAAI,IACZ,IAAI,CAAC,QAAQ,EAChB;IACJ,CAAC;IAED,MAAM,CAAC,KAAK,CACV,GAAM,EAAE,CAAwB;QAEhC,IAAI,CAAC,YAAY,UAAU,EAAE;YAC3B,OAAO,CAAC,CAAC;SACV;QACD,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjB,OAAO,IAAI,UAAU,CAAC,GAAG,EAAE,cAAc,CAAC,WAAW,EAAE;YACrD,MAAM,EAAE,CAAC,CAAC,OAAO;SAClB,CAAC,CAAC;IACL,CAAC;IAED,MAAM,CAAC,iBAAiB,CAAC,IAAoB;QAC3C,QAAQ,IAAI,EAAE;YACZ,KAAK,cAAc,CAAC,QAAQ,CAAC,CAAC,OAAO,GAAG,CAAC;YACzC,KAAK,cAAc,CAAC,UAAU,CAAC,CAAC,OAAO,GAAG,CAAC;YAC3C,OAAO,CAAC,CAAC,OAAO,GAAG,CAAC;SACrB;IACH,CAAC;CACF;AAvCD,gCAuCC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare enum QueryExpressErrorCode {
|
|
2
|
+
Default = "default",
|
|
3
|
+
ServerError = "server-error",
|
|
4
|
+
NotFound = "not-found",
|
|
5
|
+
BadRequest = "bad-request"
|
|
6
|
+
}
|
|
7
|
+
export interface IQueryExpressErrorRequest extends Request {
|
|
8
|
+
id: string;
|
|
9
|
+
}
|
|
10
|
+
export interface IQueryExpressError {
|
|
11
|
+
id: string;
|
|
12
|
+
links?: {
|
|
13
|
+
self?: IQueryExpressErrorLink | string;
|
|
14
|
+
};
|
|
15
|
+
status: string;
|
|
16
|
+
code: QueryExpressErrorCode;
|
|
17
|
+
title?: string;
|
|
18
|
+
detail?: string;
|
|
19
|
+
source?: {
|
|
20
|
+
pointer?: string;
|
|
21
|
+
parameter?: string;
|
|
22
|
+
header?: string;
|
|
23
|
+
body?: string;
|
|
24
|
+
};
|
|
25
|
+
meta?: object;
|
|
26
|
+
}
|
|
27
|
+
export interface IQueryExpressErrorLink {
|
|
28
|
+
href: string;
|
|
29
|
+
title?: string;
|
|
30
|
+
describedBy?: string;
|
|
31
|
+
meta: {
|
|
32
|
+
count: number;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
declare type Optional = Pick<IQueryExpressError, 'meta' | 'title' | 'detail' | 'links' | 'source'>;
|
|
36
|
+
export declare class QueryExpressError<R extends IQueryExpressErrorRequest> {
|
|
37
|
+
private req;
|
|
38
|
+
readonly code: QueryExpressErrorCode;
|
|
39
|
+
readonly optional: Optional;
|
|
40
|
+
readonly status: number;
|
|
41
|
+
constructor(req: R, code: QueryExpressErrorCode, optional?: Optional);
|
|
42
|
+
toJSON(): IQueryExpressError;
|
|
43
|
+
static catch<R extends IQueryExpressErrorRequest>(req: R, e: Error | QueryExpressError<R>): QueryExpressError<R>;
|
|
44
|
+
static getStatusFromCode(code: QueryExpressErrorCode): 404 | 400 | 500;
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryExpressError = exports.QueryExpressErrorCode = void 0;
|
|
4
|
+
var QueryExpressErrorCode;
|
|
5
|
+
(function (QueryExpressErrorCode) {
|
|
6
|
+
QueryExpressErrorCode["Default"] = "default";
|
|
7
|
+
QueryExpressErrorCode["ServerError"] = "server-error";
|
|
8
|
+
QueryExpressErrorCode["NotFound"] = "not-found";
|
|
9
|
+
QueryExpressErrorCode["BadRequest"] = "bad-request";
|
|
10
|
+
})(QueryExpressErrorCode = exports.QueryExpressErrorCode || (exports.QueryExpressErrorCode = {}));
|
|
11
|
+
class QueryExpressError {
|
|
12
|
+
constructor(req, code, optional = {}) {
|
|
13
|
+
this.req = req;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.optional = optional;
|
|
16
|
+
this.status = QueryExpressError.getStatusFromCode(code);
|
|
17
|
+
}
|
|
18
|
+
toJSON() {
|
|
19
|
+
return Object.assign({ id: this.req.id, status: String(this.status), code: this.code }, this.optional);
|
|
20
|
+
}
|
|
21
|
+
static catch(req, e) {
|
|
22
|
+
if (e instanceof QueryExpressError) {
|
|
23
|
+
return e;
|
|
24
|
+
}
|
|
25
|
+
console.error(e);
|
|
26
|
+
return new QueryExpressError(req, QueryExpressErrorCode.ServerError, {
|
|
27
|
+
detail: e.message
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
static getStatusFromCode(code) {
|
|
31
|
+
switch (code) {
|
|
32
|
+
case QueryExpressErrorCode.NotFound: return 404;
|
|
33
|
+
case QueryExpressErrorCode.BadRequest: return 400;
|
|
34
|
+
default: return 500;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.QueryExpressError = QueryExpressError;
|
|
39
|
+
//# sourceMappingURL=QueryExpressError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryExpressError.js","sourceRoot":"","sources":["../../src/error/QueryExpressError.ts"],"names":[],"mappings":";;;AACA,IAAY,qBAKX;AALD,WAAY,qBAAqB;IAC/B,4CAAmB,CAAA;IACnB,qDAA4B,CAAA;IAC5B,+CAAsB,CAAA;IACtB,mDAA0B,CAAA;AAC5B,CAAC,EALW,qBAAqB,GAArB,6BAAqB,KAArB,6BAAqB,QAKhC;AAkCD,MAAa,iBAAiB;IAG5B,YACU,GAAM,EACL,IAA2B,EAC3B,WAAqB,EAAE;QAFxB,QAAG,GAAH,GAAG,CAAG;QACL,SAAI,GAAJ,IAAI,CAAuB;QAC3B,aAAQ,GAAR,QAAQ,CAAe;QAEhC,IAAI,CAAC,MAAM,GAAG,iBAAiB,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAC1D,CAAC;IAEM,MAAM;QACX,uBACE,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EACf,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAC3B,IAAI,EAAE,IAAI,CAAC,IAAI,IACZ,IAAI,CAAC,QAAQ,EAChB;IACJ,CAAC;IAED,MAAM,CAAC,KAAK,CACV,GAAM,EAAE,CAA+B;QAEvC,IAAI,CAAC,YAAY,iBAAiB,EAAE;YAClC,OAAO,CAAC,CAAC;SACV;QACD,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjB,OAAO,IAAI,iBAAiB,CAAC,GAAG,EAAE,qBAAqB,CAAC,WAAW,EAAE;YACnE,MAAM,EAAE,CAAC,CAAC,OAAO;SAClB,CAAC,CAAC;IACL,CAAC;IAED,MAAM,CAAC,iBAAiB,CAAC,IAA2B;QAClD,QAAQ,IAAI,EAAE;YACZ,KAAK,qBAAqB,CAAC,QAAQ,CAAC,CAAC,OAAO,GAAG,CAAC;YAChD,KAAK,qBAAqB,CAAC,UAAU,CAAC,CAAC,OAAO,GAAG,CAAC;YAClD,OAAO,CAAC,CAAC,OAAO,GAAG,CAAC;SACrB;IACH,CAAC;CACF;AAvCD,8CAuCC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { StatusCodes } from 'http-status-codes';
|
|
2
|
+
import { AptlyError, AptlyErrorCode } from '@aptly-as/types';
|
|
3
|
+
import { V1Request } from '../../types-v2';
|
|
4
|
+
export declare class V1Error {
|
|
5
|
+
private req;
|
|
6
|
+
readonly status: StatusCodes;
|
|
7
|
+
readonly code: AptlyErrorCode;
|
|
8
|
+
readonly optional: Pick<AptlyError, 'meta' | 'title' | 'detail' | 'links' | 'source'>;
|
|
9
|
+
constructor(req: V1Request, status: StatusCodes, code: AptlyErrorCode, optional?: Pick<AptlyError, 'meta' | 'title' | 'detail' | 'links' | 'source'>);
|
|
10
|
+
toJSON(): AptlyError;
|
|
11
|
+
static catch(req: V1Request, e: Error | V1Error): V1Error;
|
|
12
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.V1Error = void 0;
|
|
4
|
+
const http_status_codes_1 = require("http-status-codes");
|
|
5
|
+
const types_1 = require("@aptly-as/types");
|
|
6
|
+
class V1Error {
|
|
7
|
+
constructor(req, status, code, optional = {}) {
|
|
8
|
+
this.req = req;
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.optional = optional;
|
|
12
|
+
}
|
|
13
|
+
toJSON() {
|
|
14
|
+
return Object.assign({ id: this.req.id, status: String(this.status), code: this.code }, this.optional);
|
|
15
|
+
}
|
|
16
|
+
static catch(req, e) {
|
|
17
|
+
if (e instanceof V1Error) {
|
|
18
|
+
return e;
|
|
19
|
+
}
|
|
20
|
+
console.error(e);
|
|
21
|
+
return new V1Error(req, http_status_codes_1.StatusCodes.INTERNAL_SERVER_ERROR, types_1.AptlyErrorCode.ServerError, {
|
|
22
|
+
detail: e.message
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.V1Error = V1Error;
|
|
27
|
+
//# sourceMappingURL=V1Error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"V1Error.js","sourceRoot":"","sources":["../../src/error/V1Error.ts"],"names":[],"mappings":";;;AAAA,yDAAgD;AAChD,2CAA6D;AAG7D,MAAa,OAAO;IAClB,YACU,GAAc,EACb,MAAmB,EACnB,IAAoB,EACpB,WAA+E,EAAE;QAHlF,QAAG,GAAH,GAAG,CAAW;QACb,WAAM,GAAN,MAAM,CAAa;QACnB,SAAI,GAAJ,IAAI,CAAgB;QACpB,aAAQ,GAAR,QAAQ,CAAyE;IACzF,CAAC;IAEG,MAAM;QACX,uBACE,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EACf,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAC3B,IAAI,EAAE,IAAI,CAAC,IAAI,IACZ,IAAI,CAAC,QAAQ,EAChB;IACJ,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,GAAc,EAAE,CAAkB;QAC7C,IAAI,CAAC,YAAY,OAAO,EAAE;YACxB,OAAO,CAAC,CAAC;SACV;QACD,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjB,OAAO,IAAI,OAAO,CAAC,GAAG,EAAE,+BAAW,CAAC,qBAAqB,EAAE,sBAAc,CAAC,WAAW,EAAE;YACrF,MAAM,EAAE,CAAC,CAAC,OAAO;SAClB,CAAC,CAAC;IACL,CAAC;CACF;AA1BD,0BA0BC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { NextFunction, Response, Request } from 'express';
|
|
2
|
+
import { IQueryErrorExpressRequest, QueryError, QueryErrorCode } from '../QueryError';
|
|
3
|
+
export declare function queryErrorHandler<R extends IQueryErrorExpressRequest & Request, C extends QueryErrorCode>(err: Error | QueryError<R, C> | unknown, req: R, res: Response, next: NextFunction): void | Response<any, Record<string, any>>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.queryErrorHandler = void 0;
|
|
4
|
+
const QueryError_1 = require("../QueryError");
|
|
5
|
+
function queryErrorHandler(err, req, res, next) {
|
|
6
|
+
if (res.headersSent) {
|
|
7
|
+
return next(err);
|
|
8
|
+
}
|
|
9
|
+
res.status(500);
|
|
10
|
+
if (err instanceof QueryError_1.QueryError) {
|
|
11
|
+
if (err.status)
|
|
12
|
+
res.status(err.status);
|
|
13
|
+
return res.json(err);
|
|
14
|
+
}
|
|
15
|
+
else if (err instanceof Error) {
|
|
16
|
+
return res.json(new QueryError_1.QueryError(req, QueryError_1.QueryErrorCode.ServerError, {
|
|
17
|
+
detail: err.message,
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
else if (err && typeof err === 'object') {
|
|
21
|
+
return res.json(new QueryError_1.QueryError(req, QueryError_1.QueryErrorCode.ServerError, {
|
|
22
|
+
detail: JSON.stringify(err),
|
|
23
|
+
}).toJSON());
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return res.json(new QueryError_1.QueryError(req, QueryError_1.QueryErrorCode.ServerError).toJSON());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.queryErrorHandler = queryErrorHandler;
|
|
30
|
+
//# sourceMappingURL=error.handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.handler.js","sourceRoot":"","sources":["../../src/error/error.handler.ts"],"names":[],"mappings":";;;AACA,8CAIuB;AAEvB,SAAgB,iBAAiB,CAI/B,GAAuC,EACvC,GAAM,EACN,GAAa,EACb,IAAkB;IAElB,IAAI,GAAG,CAAC,WAAW,EAAE;QACnB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;KAClB;IAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAChB,IAAI,GAAG,YAAY,uBAAU,EAAE;QAC7B,IAAI,GAAG,CAAC,MAAM;YAAE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACvC,OAAO,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACtB;SAAM,IAAI,GAAG,YAAY,KAAK,EAAE;QAC/B,OAAO,GAAG,CAAC,IAAI,CACb,IAAI,uBAAU,CAAC,GAAG,EAAE,2BAAc,CAAC,WAAW,EAAE;YAC9C,MAAM,EAAE,GAAG,CAAC,OAAO;SACpB,CAAC,CACH,CAAC;KACH;SAAM,IAAI,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;QACzC,OAAO,GAAG,CAAC,IAAI,CACb,IAAI,uBAAU,CAAC,GAAG,EAAE,2BAAc,CAAC,WAAW,EAAE;YAC9C,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC;SAC5B,CAAC,CAAC,MAAM,EAAE,CACZ,CAAC;KACH;SAAM;QACL,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,uBAAU,CAAC,GAAG,EAAE,2BAAc,CAAC,WAAW,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;KAC3E;AACH,CAAC;AAhCD,8CAgCC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import './declarations';
|
|
2
|
+
import { Request, NextFunction, Response } from 'express';
|
|
3
|
+
export * from './QueryError';
|
|
4
|
+
export * from './QueryExpressHandler';
|
|
5
|
+
export * from './Query';
|
|
6
|
+
export * from './QueryHandler';
|
|
7
|
+
export * from './QueryHistory';
|
|
8
|
+
export * from './QueryModel';
|
|
9
|
+
export * from './statics';
|
|
10
|
+
export default function mongooseQuery(req: Request, _res: Response, next: NextFunction): void;
|