@digione/node-custom-api 0.2.0-beta4 → 0.2.0-beta6
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/middlewares/paramQuery.d.ts +2 -2
- package/middlewares/paramQuery.js +17 -2
- package/package.json +1 -1
- package/utils/stream.d.ts +6 -3
- package/utils/stream.js +7 -7
- package/utils/validator.js +2 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Request, Response } from 'express';
|
|
2
2
|
export declare const getAuthParam: (req: Request, res: Response, next: any) => void;
|
|
3
3
|
export declare const getStremParam: (req: Request, res: Response, next: any) => void;
|
|
4
|
-
export declare const getQueryParam: (req: Request, res: Response, next: any) => void
|
|
4
|
+
export declare const getQueryParam: (req: Request, res: Response, next: any) => Promise<void | Response<any, Record<string, any>>>;
|
|
5
5
|
export declare const getQueryParamCUD: (req: Request, res: Response, next: any) => void;
|
|
6
|
-
export declare const getQueryParamFile: (req: Request, res: Response, next: any) => void
|
|
6
|
+
export declare const getQueryParamFile: (req: Request, res: Response, next: any) => Promise<void | Response<any, Record<string, any>>>;
|
|
7
7
|
export declare const changeQueryParam: (value?: {}, { skipSystem }?: {
|
|
8
8
|
skipSystem?: boolean;
|
|
9
9
|
}) => (req: Request, res: Response, next: any) => any;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.changeConfigParam = exports.changeBodyParam = exports.changeOptionParam = exports.changeQueryParam = exports.getQueryParamFile = exports.getQueryParamCUD = exports.getQueryParam = exports.getStremParam = exports.getAuthParam = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const helper_1 = require("../utils/helper");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
5
7
|
const _ = require("lodash");
|
|
6
8
|
const getAuthParam = (req, res, next) => {
|
|
7
9
|
res.locals.option['hostname'] = ((0, helper_1.getENV)('NODE_ENV') == 'development' ? 'http' : 'https') + '://' + req.get('host');
|
|
@@ -77,7 +79,8 @@ const getStremParam = (req, res, next) => {
|
|
|
77
79
|
});
|
|
78
80
|
};
|
|
79
81
|
exports.getStremParam = getStremParam;
|
|
80
|
-
const getQueryParam = (req, res, next) => {
|
|
82
|
+
const getQueryParam = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
+
let config = res.locals.config || {};
|
|
81
84
|
res.locals.option = res.locals.option || {};
|
|
82
85
|
res.locals.option['lang_code'] = 'en';
|
|
83
86
|
res.locals.option['currency'] = 'thb';
|
|
@@ -93,8 +96,20 @@ const getQueryParam = (req, res, next) => {
|
|
|
93
96
|
else {
|
|
94
97
|
res.locals.include = {};
|
|
95
98
|
}
|
|
99
|
+
if (config['maxQueryLimit']) {
|
|
100
|
+
req.query.limit = req.query.limit || config['maxQueryLimit'];
|
|
101
|
+
}
|
|
96
102
|
if (req.query.limit) {
|
|
97
103
|
res.locals.option['limit'] = Number(req.query.limit);
|
|
104
|
+
if (config['maxQueryLimit'] && res.locals.option['limit'] > config['maxQueryLimit']) {
|
|
105
|
+
try {
|
|
106
|
+
yield (0, errors_1.paramError)(`"limit" must be less than or equal to ${config['maxQueryLimit']}`, { field: "limit" });
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
let error = new errors_1.CustomError(err);
|
|
110
|
+
return res.status(error.code).send(error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
98
113
|
}
|
|
99
114
|
if (req.query.offset) {
|
|
100
115
|
res.locals.option['offset'] = Number(req.query.offset);
|
|
@@ -124,7 +139,7 @@ const getQueryParam = (req, res, next) => {
|
|
|
124
139
|
}
|
|
125
140
|
return next();
|
|
126
141
|
});
|
|
127
|
-
};
|
|
142
|
+
});
|
|
128
143
|
exports.getQueryParam = getQueryParam;
|
|
129
144
|
const getQueryParamCUD = (req, res, next) => {
|
|
130
145
|
let config = res.locals.config || {};
|
package/package.json
CHANGED
package/utils/stream.d.ts
CHANGED
|
@@ -76,13 +76,14 @@ export declare class StreamUtil {
|
|
|
76
76
|
include?: any;
|
|
77
77
|
include_only?: boolean;
|
|
78
78
|
}): any;
|
|
79
|
-
buildStream(slug: string, namespace: string, { query, builder, fetch, label, attribute, attributes, field_exclude, include, include_all }?: {
|
|
79
|
+
buildStream(slug: string, namespace: string, { query, builder, fetch, label, attribute, attributes, field_option, field_exclude, include, include_all }?: {
|
|
80
80
|
query?: boolean;
|
|
81
81
|
builder?: boolean;
|
|
82
82
|
fetch?: boolean;
|
|
83
83
|
label?: boolean;
|
|
84
84
|
attribute?: boolean;
|
|
85
85
|
attributes?: any;
|
|
86
|
+
field_option?: {};
|
|
86
87
|
field_exclude?: any[];
|
|
87
88
|
include?: {};
|
|
88
89
|
include_all?: boolean;
|
|
@@ -92,10 +93,11 @@ export declare class StreamUtil {
|
|
|
92
93
|
fetchFn: any;
|
|
93
94
|
attributes: any;
|
|
94
95
|
}>;
|
|
95
|
-
getBuilder(slug: string, namespace: string, { query, index, builder, field_exclude }?: {
|
|
96
|
+
getBuilder(slug: string, namespace: string, { query, index, builder, field_option, field_exclude }?: {
|
|
96
97
|
query?: boolean;
|
|
97
98
|
index?: string;
|
|
98
99
|
builder?: boolean;
|
|
100
|
+
field_option?: {};
|
|
99
101
|
field_exclude?: any[];
|
|
100
102
|
}): Promise<any>;
|
|
101
103
|
getValue(slug: string, namespace: string, value: any, { query, transform, spread, spread_all, raw, lang_code }?: {
|
|
@@ -305,7 +307,7 @@ export declare class StreamUtil {
|
|
|
305
307
|
stripUnknown?: boolean;
|
|
306
308
|
builder?: boolean;
|
|
307
309
|
}): Promise<Joi.ObjectSchema<any>>;
|
|
308
|
-
validate(slug: string, namespace: string, input: any, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage, builder, field_exclude }?: {
|
|
310
|
+
validate(slug: string, namespace: string, input: any, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage, builder, field_option, field_exclude }?: {
|
|
309
311
|
req?: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
310
312
|
isNew?: boolean;
|
|
311
313
|
default_value?: {};
|
|
@@ -315,6 +317,7 @@ export declare class StreamUtil {
|
|
|
315
317
|
inputLangSeparate?: boolean;
|
|
316
318
|
prefixMessage?: string;
|
|
317
319
|
builder?: boolean;
|
|
320
|
+
field_option?: {};
|
|
318
321
|
field_exclude?: any[];
|
|
319
322
|
}): Promise<any>;
|
|
320
323
|
validateByField(streams: Array<any>, input: any, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage }?: {
|
package/utils/stream.js
CHANGED
|
@@ -235,13 +235,13 @@ class StreamUtil {
|
|
|
235
235
|
}
|
|
236
236
|
return data;
|
|
237
237
|
}
|
|
238
|
-
buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_exclude = [], include = {}, include_all = false } = {}) {
|
|
238
|
+
buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_option = {}, field_exclude = [], include = {}, include_all = false } = {}) {
|
|
239
239
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
240
240
|
let build = this.builder[slug + "_" + namespace] || { stream: null, fields: null, fetchFn: [], attributes: null };
|
|
241
241
|
if (!build['fields']) {
|
|
242
242
|
let data = {};
|
|
243
243
|
if (query)
|
|
244
|
-
data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_exclude });
|
|
244
|
+
data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_option, field_exclude });
|
|
245
245
|
build['fields'] = data['fields'] || [];
|
|
246
246
|
build['stream'] = _.omit(data, ['fields']);
|
|
247
247
|
}
|
|
@@ -312,9 +312,9 @@ class StreamUtil {
|
|
|
312
312
|
return build;
|
|
313
313
|
});
|
|
314
314
|
}
|
|
315
|
-
getBuilder(slug, namespace, { query = true, index = "", builder = true, field_exclude = [] } = {}) {
|
|
315
|
+
getBuilder(slug, namespace, { query = true, index = "", builder = true, field_option = {}, field_exclude = [] } = {}) {
|
|
316
316
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
317
|
-
let result = yield this.buildStream(slug, namespace, { query, builder, field_exclude });
|
|
317
|
+
let result = yield this.buildStream(slug, namespace, { query, builder, field_option, field_exclude });
|
|
318
318
|
return index ? result[index] : result;
|
|
319
319
|
});
|
|
320
320
|
}
|
|
@@ -685,7 +685,7 @@ class StreamUtil {
|
|
|
685
685
|
if (field_data['choose_stream']) {
|
|
686
686
|
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_namespace'] });
|
|
687
687
|
if (stream) {
|
|
688
|
-
item['stream'] = yield this.findField(stream['stream_slug'], stream['stream_namespace'], { field_only: true, convert_option: true });
|
|
688
|
+
item['stream'] = yield this.findField(stream['stream_slug'], stream['stream_namespace'], { option, lang_code, field_only: true, convert_option: true });
|
|
689
689
|
}
|
|
690
690
|
}
|
|
691
691
|
}
|
|
@@ -2056,9 +2056,9 @@ class StreamUtil {
|
|
|
2056
2056
|
return yield this.getValidator(streams, { req, isNew, required, lang, stripUnknown });
|
|
2057
2057
|
});
|
|
2058
2058
|
}
|
|
2059
|
-
validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_exclude = [] } = {}) {
|
|
2059
|
+
validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_option = {}, field_exclude = [] } = {}) {
|
|
2060
2060
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
2061
|
-
let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_exclude });
|
|
2061
|
+
let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_option, field_exclude });
|
|
2062
2062
|
return this.validateByField(streams, input, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage });
|
|
2063
2063
|
});
|
|
2064
2064
|
}
|
package/utils/validator.js
CHANGED
|
@@ -104,8 +104,9 @@ const fileService = (setup, { userfiles = false, required = false, fileSize = 0,
|
|
|
104
104
|
}
|
|
105
105
|
let option = setup(validate);
|
|
106
106
|
let upload = userfiles ? option.array("userfiles", maxCount) : option.any();
|
|
107
|
+
let schema = userfiles ? { tags: Joi.array().allow('', null).items(Joi.object().keys({ slug: Joi.string(), type: Joi.string() })) } : { tags: Joi.any() };
|
|
107
108
|
return upload(req, res, (err) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
-
return (0, exports.validateService)(Joi.object().keys(
|
|
109
|
+
return (0, exports.validateService)(Joi.object().keys(schema).unknown(), { field_omit: [] })(req, res, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
109
110
|
try {
|
|
110
111
|
let field = userfiles ? "userfiles" : undefined;
|
|
111
112
|
if (err) {
|