UNPKG

jarb-final-form

Version:
100 lines (99 loc) 2.97 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("./config"); let constraints = undefined; // Throw error when not 200 otherwise parse response. function tryParse(response) { if (response.status !== 200) { throw response; } else { return response.json(); } } /** * Loads the constraints from the back-end. * * The URL it will send the request to is defined by the 'constraintsUrl' * from the Config object. The HTTP method it uses is 'get'. * * It will also send the credentials if 'needsAuthentication' from * the Config object is set to true. * * The entire response will be written to the `constraints` variable. * Whatever the JSON response is will be the constraints. * * An example response: * * ```JSON * { * "SuperHero": { * "name": { * "javaType": "java.lang.String", * "types": ["text"], * "required": true, * "minimumLength": null, * "maximumLength": 50, * "fractionLength": null, * "radix": null, * "pattern": null, * "min": null, * "max": null, * "name": "name" * }, * "email": { * "javaType": "java.lang.String", * "types": ["email", "text"], * "required": true, * "minimumLength": null, * "maximumLength": 255, * "fractionLength": null, * "radix": null, * "pattern": null, * "min": null, * "max": null, * "name": "email" * } * } * } * ``` * * @returns {Promise} */ function loadConstraints() { return __awaiter(this, void 0, void 0, function* () { const { constraintsUrl, needsAuthentication } = config_1.getConfig(); const config = needsAuthentication ? { credentials: 'include' } : {}; const response = yield fetch(constraintsUrl, config); constraints = yield tryParse(response); }); } exports.loadConstraints = loadConstraints; /** * Sets the constraints. * * @param newConstraints The new constraints */ function setConstraints(newConstraints) { constraints = newConstraints; } exports.setConstraints = setConstraints; /** * Get the current constraints. * * @returns The current constraints */ function getConstraints() { return constraints; } exports.getConstraints = getConstraints;