@vitrical/utils
Advanced tools
Comparing version 1.0.3 to 1.0.4
19
api.js
@@ -50,3 +50,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.jsonRequest = exports.request = void 0; | ||
exports.formRequest = exports.jsonRequest = exports.request = void 0; | ||
var validation_1 = require("./validation"); | ||
@@ -97,5 +97,20 @@ var request = function (options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var jsonRequest = function (options) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2, (0, exports.request)(__assign(__assign({}, options), { headers: __assign(__assign({}, options.headers), { 'Content-Type': 'application/json' }) }))]; | ||
return [2, (0, exports.request)(__assign(__assign({}, options), { headers: __assign({ 'Content-Type': 'application/json' }, options.headers) }))]; | ||
}); }); }; | ||
exports.jsonRequest = jsonRequest; | ||
var formRequest = function (options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var formBody; | ||
return __generator(this, function (_a) { | ||
formBody = options.data | ||
? Object.entries(options.data) | ||
.map(function (_a) { | ||
var key = _a[0], value = _a[1]; | ||
return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent("".concat(value))); | ||
}) | ||
.join('&') | ||
: undefined; | ||
return [2, (0, exports.request)(__assign(__assign({}, options), { headers: __assign({ 'Content-Type': 'application/x-www-urlencoded' }, options.headers), data: formBody }))]; | ||
}); | ||
}); }; | ||
exports.formRequest = formRequest; | ||
//# sourceMappingURL=api.js.map |
25
api.ts
@@ -25,3 +25,3 @@ import { objectHasProperty } from './validation' | ||
method: DataMethod | ||
data?: { [key: string]: unknown } | string | ||
data?: any | ||
} | ||
@@ -94,2 +94,23 @@ | { | ||
export const jsonRequest = async (options: JSONRequestOptions): Promise<RequestResponse> => | ||
request({ ...options, headers: { ...options.headers, 'Content-Type': 'application/json' } }) | ||
request({ ...options, headers: { 'Content-Type': 'application/json', ...options.headers } }) | ||
export type FormRequestOptions = RequestOptions & { | ||
method: DataMethod | ||
data: { | ||
[key: string]: string | number | boolean | ||
} | ||
} | ||
export const formRequest = async (options: FormRequestOptions): Promise<RequestResponse> => { | ||
const formBody = options.data | ||
? Object.entries(options.data) | ||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(`${value}`)}`) | ||
.join('&') | ||
: undefined | ||
return request({ | ||
...options, | ||
headers: { 'Content-Type': 'application/x-www-urlencoded', ...options.headers }, | ||
data: formBody, | ||
}) | ||
} |
import { Request, Response, NextFunction } from 'express' | ||
import { isApiError, objectHasProperty } from './validation' | ||
import { isApiError, objectHasProperty, validateObject, ValidateObjectOptions } from './validation' | ||
@@ -46,1 +46,11 @@ export const handleExpressError = ( | ||
} | ||
export const validateBody = | ||
(schema: ValidateObjectOptions) => (req: Request, res: Response, next: NextFunction) => { | ||
const errors = validateObject(schema, req.body) | ||
if (errors.length === 0) { | ||
next() | ||
} else { | ||
next(`Errors present in the request body:\n${errors.reduce((a, b) => ` ${a}\n ${b}`)}`) | ||
} | ||
} |
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46479
859