@react-input-validator/rules
Version:
The validation rule objects used by the packages: `@react-input-validator/core`, `@react-input-validator/native` and `@react-input-validator/web`
130 lines (129 loc) • 6.66 kB
JavaScript
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _HttpReq_message, _HttpReq_option, _HttpReq_uri;
Object.defineProperty(exports, "__esModule", { value: true });
exports.httpReq = exports.HttpReq = void 0;
/**
* https://github.com/atmulyana/react-input-validator
*/
const javascript_common_1 = require("javascript-common");
const messages_1 = __importDefault(require("./messages"));
const ValidationRuleAsync_1 = __importDefault(require("./ValidationRuleAsync"));
class HttpReq extends ValidationRuleAsync_1.default {
constructor(uri, option) {
super();
_HttpReq_message.set(this, void 0);
_HttpReq_option.set(this, void 0);
_HttpReq_uri.set(this, void 0);
__classPrivateFieldSet(this, _HttpReq_uri, uri, "f");
__classPrivateFieldSet(this, _HttpReq_option, option, "f");
this.setPriority(1001);
}
get errorMessage() {
return __classPrivateFieldGet(this, _HttpReq_message, "f");
}
validate() {
__classPrivateFieldSet(this, _HttpReq_message, messages_1.default.invalid, "f");
return new Promise(($resolve, $reject) => {
var _a, _b, _c, _d, _e;
const resolve = (value) => {
this.isValid = false;
if (value === true)
this.isValid = true;
else if (typeof (value) == 'string')
__classPrivateFieldSet(this, _HttpReq_message, value, "f");
else if (value !== false)
__classPrivateFieldSet(this, _HttpReq_message, messages_1.default.httpReq.invalid, "f");
$resolve(this);
};
const reject = (value) => {
this.isValid = false;
$reject(value);
};
const xhr = new XMLHttpRequest(), { data, headers, silentOnFailure = false, timeout = 0, withCredentials = false, } = (_a = __classPrivateFieldGet(this, _HttpReq_option, "f")) !== null && _a !== void 0 ? _a : {};
let reqData, contentType, method;
if (data) {
method = 'POST';
if (data instanceof URLSearchParams) {
contentType = 'application/x-www-form-urlencoded';
data.set('value', ((_b = this.value) !== null && _b !== void 0 ? _b : javascript_common_1.emptyString).toString());
data.set('name', (_c = this.name) !== null && _c !== void 0 ? _c : javascript_common_1.emptyString);
reqData = data.toString();
}
else if (typeof (data) == 'object') {
contentType = 'application/json';
data['value'] = this.value;
data['name'] = this.name;
reqData = JSON.stringify(data);
}
}
else {
method = 'GET';
let i = __classPrivateFieldGet(this, _HttpReq_uri, "f").indexOf('#'), uri = __classPrivateFieldGet(this, _HttpReq_uri, "f"), search = javascript_common_1.emptyString, hash = javascript_common_1.emptyString;
if (i >= 0) {
hash = uri.substring(i);
uri = uri.substring(0, i);
}
i = uri.indexOf('?');
if (i >= 0) {
search = uri.substring(i);
uri = uri.substring(0, i);
}
const params = new URLSearchParams(search);
params.set('value', ((_d = this.value) !== null && _d !== void 0 ? _d : javascript_common_1.emptyString).toString());
params.set('name', (_e = this.name) !== null && _e !== void 0 ? _e : javascript_common_1.emptyString);
const getParams = params.toString();
__classPrivateFieldSet(this, _HttpReq_uri, uri
+ (getParams ? '?' + getParams : javascript_common_1.emptyString)
+ hash, "f");
}
xhr.open(method, __classPrivateFieldGet(this, _HttpReq_uri, "f"));
xhr.timeout = timeout;
xhr.withCredentials = withCredentials;
if (typeof (headers) == 'object' && headers) {
for (const headerName in headers) {
if (typeof (headers[headerName]) == 'string')
xhr.setRequestHeader(headerName, headers[headerName]);
}
}
if (contentType)
xhr.setRequestHeader('Content-Type', contentType);
xhr.onload = () => {
if (xhr.status < 200 || xhr.status > 299) {
if (silentOnFailure)
resolve(true);
else
reject(messages_1.default.httpReq.notOk);
}
else {
const responseData = JSON.parse(xhr.responseText);
resolve(responseData);
}
};
xhr.ontimeout = xhr.onerror = () => {
if (silentOnFailure)
resolve(true);
else
reject(messages_1.default.httpReq.disconnected);
};
xhr.send(reqData);
});
}
}
exports.HttpReq = HttpReq;
_HttpReq_message = new WeakMap(), _HttpReq_option = new WeakMap(), _HttpReq_uri = new WeakMap();
const httpReq = (uri, option) => new HttpReq(uri, option);
exports.httpReq = httpReq;