UNPKG

react-relay-network-modern

Version:

Network Layer for React Relay and Express (Batch Queries, AuthToken, Logging, Retry)

125 lines (124 loc) 6.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _RRNLError = _interopRequireDefault(require("./RRNLError")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function getFormDataInterface() { return typeof window !== 'undefined' && window.FormData || global && global.FormData; } var RelayRequest = /*#__PURE__*/function () { function RelayRequest(operation, variables, cacheConfig, uploadables) { _classCallCheck(this, RelayRequest); this.operation = operation; this.variables = variables; this.cacheConfig = cacheConfig; this.uploadables = uploadables; this.id = this.operation.id || this.operation.name || this._generateID(); var fetchOpts = { method: 'POST', headers: {}, body: this.prepareBody() }; this.controller = typeof window !== 'undefined' && window.AbortController ? new window.AbortController() : null; if (this.controller) fetchOpts.signal = this.controller.signal; this.fetchOpts = fetchOpts; } _createClass(RelayRequest, [{ key: "getBody", value: function getBody() { return this.fetchOpts.body; } }, { key: "prepareBody", value: function prepareBody() { var uploadables = this.uploadables; if (uploadables) { var _FormData_ = getFormDataInterface(); if (!_FormData_) { throw new _RRNLError["default"]('Uploading files without `FormData` interface does not supported.'); } var formData = new _FormData_(); formData.append('id', this.getID()); formData.append('query', this.getQueryString()); formData.append('variables', JSON.stringify(this.getVariables())); Object.keys(uploadables).forEach(function (key) { if (Object.prototype.hasOwnProperty.call(uploadables, key)) { formData.append(key, uploadables[key]); } }); return formData; } return JSON.stringify({ id: this.getID(), query: this.getQueryString(), variables: this.getVariables() }); } }, { key: "getID", value: function getID() { return this.id; } }, { key: "_generateID", value: function _generateID() { if (!this.constructor.lastGenId) { this.constructor.lastGenId = 0; } this.constructor.lastGenId += 1; return this.constructor.lastGenId.toString(); } }, { key: "getQueryString", value: function getQueryString() { return this.operation.text || ''; } }, { key: "getVariables", value: function getVariables() { return this.variables; } }, { key: "isMutation", value: function isMutation() { return this.operation.operationKind === 'mutation'; } }, { key: "isFormData", value: function isFormData() { var _FormData_ = getFormDataInterface(); return !!_FormData_ && this.fetchOpts.body instanceof _FormData_; } }, { key: "cancel", value: function cancel() { if (this.controller) { this.controller.abort(); return true; } return false; } }, { key: "clone", value: function clone() { // $FlowFixMe var newRequest = Object.assign(Object.create(Object.getPrototypeOf(this)), this); newRequest.fetchOpts = _objectSpread({}, this.fetchOpts); newRequest.fetchOpts.headers = _objectSpread({}, this.fetchOpts.headers); return newRequest; } }]); return RelayRequest; }(); exports["default"] = RelayRequest;