@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`
122 lines (121 loc) • 6.12 kB
JavaScript
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 _HttpReq_message, _HttpReq_option, _HttpReq_uri;
/**
* https://github.com/atmulyana/react-input-validator
*/
import { emptyString } from "javascript-common";
import messages from './messages';
import ValidationRuleAsync from './ValidationRuleAsync';
export class HttpReq extends ValidationRuleAsync {
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.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.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 : emptyString).toString());
data.set('name', (_c = this.name) !== null && _c !== void 0 ? _c : 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 = emptyString, hash = 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 : emptyString).toString());
params.set('name', (_e = this.name) !== null && _e !== void 0 ? _e : emptyString);
const getParams = params.toString();
__classPrivateFieldSet(this, _HttpReq_uri, uri
+ (getParams ? '?' + getParams : 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.httpReq.notOk);
}
else {
const responseData = JSON.parse(xhr.responseText);
resolve(responseData);
}
};
xhr.ontimeout = xhr.onerror = () => {
if (silentOnFailure)
resolve(true);
else
reject(messages.httpReq.disconnected);
};
xhr.send(reqData);
});
}
}
_HttpReq_message = new WeakMap(), _HttpReq_option = new WeakMap(), _HttpReq_uri = new WeakMap();
export const httpReq = (uri, option) => new HttpReq(uri, option);