@autofleet/sheilta 1.2.6 → 1.2.8
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/formatter/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
declare type OrderItem = string | [string, string];
|
|
2
2
|
declare type SequelizeOrder = string | OrderItem[];
|
|
3
|
+
export declare type FormatPayloadOptions = {
|
|
4
|
+
includeRawPayload?: boolean;
|
|
5
|
+
};
|
|
3
6
|
declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, }: {
|
|
4
7
|
order: any;
|
|
5
8
|
page?: number;
|
package/lib/formatter/index.js
CHANGED
|
@@ -24,7 +24,7 @@ const formatOrder = ({ order = [], associationModels = [], }) => (order.length =
|
|
|
24
24
|
const formatPage = page => page || utils_1.PAGE_DEFAULT;
|
|
25
25
|
const formatPerPage = perPage => perPage || utils_1.PER_PAGE_DEFAULT;
|
|
26
26
|
const formatInclude = (include, associationsMap = {}) => {
|
|
27
|
-
let formattedInclude = include.map(i => (Object.assign(Object.assign({}, i), { association: associationsMap[i.association || i.model], required: i.required !==
|
|
27
|
+
let formattedInclude = include.map(i => (Object.assign(Object.assign({}, i), { association: associationsMap[i.association || i.model], required: i.required !== false })));
|
|
28
28
|
formattedInclude = formattedInclude.map(i => lodash_1.default.omit(i, ['model']));
|
|
29
29
|
return formattedInclude;
|
|
30
30
|
};
|
|
@@ -190,4 +190,33 @@ describe('formatting test', () => {
|
|
|
190
190
|
expect(JSON.stringify(order)).toBe(JSON.stringify([['status.name', 'DESC']]));
|
|
191
191
|
});
|
|
192
192
|
});
|
|
193
|
+
describe('include formatting', () => {
|
|
194
|
+
it('include required should be true', () => {
|
|
195
|
+
const { include } = _1.default({
|
|
196
|
+
order: [],
|
|
197
|
+
include: [{
|
|
198
|
+
model: 'status',
|
|
199
|
+
}],
|
|
200
|
+
}, {
|
|
201
|
+
associations: {
|
|
202
|
+
status: {},
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
expect(include[0].required).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
it('include required should be false', () => {
|
|
208
|
+
const { include } = _1.default({
|
|
209
|
+
order: [],
|
|
210
|
+
include: [{
|
|
211
|
+
model: 'status',
|
|
212
|
+
required: false,
|
|
213
|
+
}],
|
|
214
|
+
}, {
|
|
215
|
+
associations: {
|
|
216
|
+
status: {},
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
expect(include[0].required).toBe(false);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
193
222
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import { FormatPayloadOptions } from '../formatter';
|
|
1
2
|
export declare const newQueryValidationMiddleware: (inner?: string) => (model: any) => (req: any, res: any, next: any) => Promise<any>;
|
|
2
|
-
export declare const newQueryFormatMiddleware: (inner?: string) => (model: any) => (req: any, res: any, next: any) => Promise<any>;
|
|
3
|
+
export declare const newQueryFormatMiddleware: (inner?: string) => (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<any>;
|
|
3
4
|
export declare const queryValidationMiddleware: (model: any) => (req: any, res: any, next: any) => Promise<any>;
|
|
4
|
-
export declare const queryFormatMiddleware: (model: any) => (req: any, res: any, next: any) => Promise<any>;
|
|
5
|
+
export declare const queryFormatMiddleware: (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<any>;
|
package/lib/middleware/index.js
CHANGED
|
@@ -24,6 +24,7 @@ const querySchema = joi_1.object({
|
|
|
24
24
|
page: joi_1.number(),
|
|
25
25
|
perPage: joi_1.number(),
|
|
26
26
|
include: joi_1.array().items(joi_1.any()),
|
|
27
|
+
searchTerm: joi_1.string(),
|
|
27
28
|
});
|
|
28
29
|
exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
30
|
const { query, attributes, order, page, perPage, include, } = req[inner];
|
|
@@ -54,7 +55,7 @@ exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, n
|
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
57
|
});
|
|
57
|
-
exports.newQueryFormatMiddleware = (inner = 'body') => model => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
58
|
+
exports.newQueryFormatMiddleware = (inner = 'body') => (model, options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
58
59
|
const { order, page, perPage, include, query, attributes, searchTerm, } = req[inner];
|
|
59
60
|
const { query: formattedQuery, order: formattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, } = formatter_1.default({
|
|
60
61
|
query,
|
|
@@ -70,6 +71,17 @@ exports.newQueryFormatMiddleware = (inner = 'body') => model => (req, res, next)
|
|
|
70
71
|
req[inner].page = formattedPage;
|
|
71
72
|
req[inner].perPage = formattedPerPage;
|
|
72
73
|
req[inner].include = formattedInclude;
|
|
74
|
+
if (options.includeRawPayload) {
|
|
75
|
+
req[inner].rawPayload = {
|
|
76
|
+
order,
|
|
77
|
+
page,
|
|
78
|
+
perPage,
|
|
79
|
+
include,
|
|
80
|
+
query,
|
|
81
|
+
attributes,
|
|
82
|
+
searchTerm,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
73
85
|
return next();
|
|
74
86
|
});
|
|
75
87
|
exports.queryValidationMiddleware = exports.newQueryValidationMiddleware();
|