From 2a3c7e1bcf566fe823b43618a8541a6248017a8b Mon Sep 17 00:00:00 2001 From: Bruno Pinto Date: Mon, 15 Feb 2021 15:39:42 +0000 Subject: [PATCH] npm run dist Fix #39 --- dist/karet.xhr.cjs.js | 160 ++++++++++++++++++++-------------------- dist/karet.xhr.es.js | 110 ++++++++++++++-------------- dist/karet.xhr.js | 166 +++++++++++++++++++++--------------------- dist/karet.xhr.min.js | 2 +- docs/karet.xhr.js | 166 +++++++++++++++++++++--------------------- 5 files changed, 298 insertions(+), 306 deletions(-) diff --git a/dist/karet.xhr.cjs.js b/dist/karet.xhr.cjs.js index b5d32de..104c77e 100644 --- a/dist/karet.xhr.cjs.js +++ b/dist/karet.xhr.cjs.js @@ -148,6 +148,8 @@ var typeLoad = /*#__PURE__*/I.freeze({ type: LOAD }); var eventTypes = [LOADSTART, PROGRESS, LOAD, TIMEOUT, ERROR]; +var simpleSettings = [OVERRIDE_MIME_TYPE, RESPONSE_TYPE, TIMEOUT, WITH_CREDENTIALS]; + var hasKeys = function hasKeys(x) { return I.isFunction(x.keys); }; @@ -164,37 +166,32 @@ var headersMap = /*#__PURE__*/V.and( /*#__PURE__*/V.acceptWith(function (xs) { })); var performPlain = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? I.id : V.validate(V.freeFn(V.tuple(V.props({ - url: string, - method: V.optional(string), - user: V.optional(string), - password: V.optional(string), + body: V.optional(V.accept), headers: V.propsOr(headerValue, I.object0), + method: V.optional(string), overrideMimeType: V.optional(string), - body: V.optional(V.accept), + password: V.optional(string), responseType: V.optional(string), timeout: V.optional(number), + url: string, + user: V.optional(string), withCredentials: V.optional(boolean) })), V.accept)))(function perform(args) { return delayUnsub(K.stream(function (_ref) { var emit = _ref.emit, end = _ref.end; - var url = args.url; var method = args.method; var user = args.user; var password = args.password; var headers = args.headers; - var overrideMimeType = args[OVERRIDE_MIME_TYPE]; var body = args.body; - var responseType = args[RESPONSE_TYPE]; - var timeout = args[TIMEOUT]; - var withCredentials = args[WITH_CREDENTIALS]; var xhr = new XMLHttpRequest(); - var state = { xhr: xhr, up: typeInitial, down: typeInitial, map: I.id }; + var state = { down: typeInitial, map: I.id, up: typeInitial, xhr: xhr }; var update = function update(dir, type) { return function (event) { - if (type !== PROGRESS || state[dir].type !== LOAD) emit(state = L.set(dir, { type: type, event: event }, state)); + if (type !== PROGRESS || state[dir].type !== LOAD) emit(state = L.set(dir, { event: event, type: type }, state)); }; }; eventTypes.forEach(function (type) { @@ -207,17 +204,18 @@ var performPlain = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? I.id : xhr[ADD_EVENT_LISTENER](LOADEND, function (event) { end(emit(state = L.set(EVENT, event, state))); }); - xhr.open(isNil(method) ? 'GET' : method, url, true, isNil(user) ? null : user, isNil(password) ? null : password); - if (responseType) { - xhr[RESPONSE_TYPE] = responseType; - if (responseType === JSON_ && xhr[RESPONSE_TYPE] !== JSON_) state = L.set(MAP, tryParse, state); + xhr.open(isNil(method) ? 'GET' : method, args.url, true, isNil(user) ? null : user, isNil(password) ? null : password); + simpleSettings.forEach(function (key) { + var value = args[key]; + if (value) xhr[key] = value; + }); + if (args[RESPONSE_TYPE] === JSON_ && xhr[RESPONSE_TYPE] !== JSON_) { + // IE11 workaround + state = L.set(MAP, tryParse, state); } - if (timeout) xhr[TIMEOUT] = timeout; - if (withCredentials) xhr[WITH_CREDENTIALS] = withCredentials; for (var header in headers) { xhr.setRequestHeader(header, headers[header]); } - if (overrideMimeType) xhr[OVERRIDE_MIME_TYPE](overrideMimeType); xhr.send(isNil(body) ? null : body); return function () { if (!xhr[STATUS]) xhr.abort(); @@ -230,7 +228,7 @@ var toLowerKeyedObject = /*#__PURE__*/L.get([/*#__PURE__*/L.array( /*#__PURE__*/ var normalizeOptions = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? I.id : V.validate(V.modifyAfter(V.freeFn(V.tuple(V.or(string, V.propsOr(V.accept, { headers: V.optional(V.cases([isNil, V.accept], [I.isArray, headersArray], [hasKeys, headersMap], [V.propsOr(headerValue, I.object0)])) }))), V.accept), F.lift)))( /*#__PURE__*/L.transform( /*#__PURE__*/L.ifElse(I.isString, /*#__PURE__*/L.modifyOp(function (url) { - return { url: url, headers: I.object0 }; + return { headers: I.object0, url: url }; }), /*#__PURE__*/L.branch({ headers: /*#__PURE__*/L.cond([isNil, /*#__PURE__*/L.setOp(I.object0)], [I.isArray, /*#__PURE__*/L.modifyOp(toLowerKeyedObject)], [hasKeys, /*#__PURE__*/L.modifyOp( /*#__PURE__*/I.pipe2U(Array.from, toLowerKeyedObject))], [[L.keys, /*#__PURE__*/L.modifyOp(toLower)]]) })))); @@ -350,16 +348,16 @@ var performWith = /*#__PURE__*/I.curry(function performWith(defaults, overrides) }); var performJson = /*#__PURE__*/setName( /*#__PURE__*/performWith({ - responseType: JSON_, - headers: { 'Content-Type': 'application/json' } + headers: { 'Content-Type': 'application/json' }, + responseType: JSON_ }), 'performJson'); var typeIsSuccess = [TYPE$1, /*#__PURE__*/isOneOf([INITIAL, LOAD])]; var hasSucceeded = /*#__PURE__*/setName( /*#__PURE__*/L.and( /*#__PURE__*/L.branch({ + down: typeIsSuccess, event: [TYPE$1, /*#__PURE__*/L.is(LOADEND)], up: typeIsSuccess, - down: typeIsSuccess, xhr: [STATUS, isHttpSuccessU] })), 'hasSucceeded'); @@ -389,21 +387,21 @@ var mergeTypes = function mergeTypes(x, y) { var mergeEvents = function mergeEvents(x, y) { return { - type: mergeTypes(x[TYPE$1], y[TYPE$1]), event: { - total: (L.get(TOTAL, x[EVENT]) || 0) + (L.get(TOTAL, y[EVENT]) || 0), - loaded: (L.get(LOADED, x[EVENT]) || 0) + (L.get(LOADED, y[EVENT]) || 0) - } + loaded: (L.get(LOADED, x[EVENT]) || 0) + (L.get(LOADED, y[EVENT]) || 0), + total: (L.get(TOTAL, x[EVENT]) || 0) + (L.get(TOTAL, y[EVENT]) || 0) + }, + type: mergeTypes(x[TYPE$1], y[TYPE$1]) }; }; var mergeXHRs = function mergeXHRs(x, y) { return { + down: mergeEvents(x[DOWN], y[DOWN]), event: y[EVENT][TYPE$1] === LOADEND ? x[EVENT] : y[EVENT], - xhr: y[XHR], + map: y[MAP], up: mergeEvents(x[UP], y[UP]), - down: mergeEvents(x[DOWN], y[DOWN]), - map: y[MAP] + xhr: y[XHR] }; }; @@ -414,9 +412,10 @@ var getResponseHeader = /*#__PURE__*/I.always(null); var of = function of(response) { return { + down: typeLoad, event: typeLoadend, + map: I.id, up: typeInitial, - down: typeLoad, xhr: { getAllResponseHeaders: getAllResponseHeaders, getResponseHeader: getResponseHeader, @@ -424,8 +423,7 @@ var of = function of(response) { response: response, status: 200, statusText: 'OK' - }, - map: I.id + } }; }; @@ -466,10 +464,10 @@ var Parallel = /*#__PURE__*/I.Applicative(map, of, apParallel); var typeIsString = [TYPE$1, I.isString]; var isXHR = /*#__PURE__*/setName( /*#__PURE__*/L.and( /*#__PURE__*/L.branch({ - xhr: [READY_STATE, I.isNumber], - up: typeIsString, down: typeIsString, - map: I.isFunction + map: I.isFunction, + up: typeIsString, + xhr: [READY_STATE, I.isNumber] })), 'isXHR'); var IdentitySucceeded = /*#__PURE__*/I.IdentityOrU(isXHR, Succeeded); @@ -489,65 +487,65 @@ var tap = /*#__PURE__*/I.curryN(2, function tap(action) { }); }); -exports.perform = perform; -exports.upHasStarted = upHasStarted; -exports.upIsProgressing = upIsProgressing; -exports.upHasCompleted = upHasCompleted; -exports.upHasFailed = upHasFailed; -exports.upHasErrored = upHasErrored; -exports.upHasTimedOut = upHasTimedOut; -exports.upHasEnded = upHasEnded; -exports.upLoaded = upLoaded; -exports.upTotal = upTotal; -exports.upError = upError; -exports.downHasStarted = downHasStarted; -exports.downIsProgressing = downIsProgressing; +exports.IdentityParallel = IdentityParallel; +exports.IdentitySucceeded = IdentitySucceeded; +exports.Parallel = Parallel; +exports.Succeeded = Succeeded; +exports.allResponseHeaders = allResponseHeaders; +exports.ap = ap; +exports.apParallel = apParallel; +exports.apply = apply; +exports.chain = chain; +exports.downError = downError; exports.downHasCompleted = downHasCompleted; -exports.downHasFailed = downHasFailed; +exports.downHasEnded = downHasEnded; exports.downHasErrored = downHasErrored; +exports.downHasFailed = downHasFailed; +exports.downHasStarted = downHasStarted; exports.downHasTimedOut = downHasTimedOut; -exports.downHasEnded = downHasEnded; +exports.downIsProgressing = downIsProgressing; exports.downLoaded = downLoaded; exports.downTotal = downTotal; -exports.downError = downError; -exports.readyState = readyState; -exports.isStatusAvailable = isStatusAvailable; -exports.isDone = isDone; -exports.isProgressing = isProgressing; +exports.errors = errors; +exports.getJson = getJson; exports.hasErrored = hasErrored; +exports.hasFailed = hasFailed; +exports.hasSucceeded = hasSucceeded; exports.hasTimedOut = hasTimedOut; +exports.isDone = isDone; +exports.isHttpSuccess = isHttpSuccess; +exports.isProgressing = isProgressing; +exports.isStatusAvailable = isStatusAvailable; +exports.isXHR = isXHR; exports.loaded = loaded; -exports.total = total; -exports.errors = errors; +exports.map = map; +exports.of = of; +exports.perform = perform; +exports.performJson = performJson; +exports.performWith = performWith; +exports.readyState = readyState; exports.response = response; +exports.responseHeader = responseHeader; +exports.responseText = responseText; exports.responseType = responseType; exports.responseURL = responseURL; -exports.responseText = responseText; exports.responseXML = responseXML; +exports.result = result; exports.status = status; exports.statusIsHttpSuccess = statusIsHttpSuccess; exports.statusText = statusText; -exports.responseHeader = responseHeader; -exports.allResponseHeaders = allResponseHeaders; +exports.tap = tap; +exports.template = template; exports.timeout = timeout; +exports.total = total; +exports.upError = upError; +exports.upHasCompleted = upHasCompleted; +exports.upHasEnded = upHasEnded; +exports.upHasErrored = upHasErrored; +exports.upHasFailed = upHasFailed; +exports.upHasStarted = upHasStarted; +exports.upHasTimedOut = upHasTimedOut; +exports.upIsProgressing = upIsProgressing; +exports.upLoaded = upLoaded; +exports.upTotal = upTotal; exports.withCredentials = withCredentials; -exports.isHttpSuccess = isHttpSuccess; -exports.performWith = performWith; -exports.performJson = performJson; -exports.hasSucceeded = hasSucceeded; -exports.hasFailed = hasFailed; -exports.getJson = getJson; -exports.result = result; -exports.of = of; -exports.chain = chain; -exports.map = map; -exports.ap = ap; -exports.Succeeded = Succeeded; -exports.apParallel = apParallel; -exports.Parallel = Parallel; -exports.isXHR = isXHR; -exports.IdentitySucceeded = IdentitySucceeded; -exports.IdentityParallel = IdentityParallel; -exports.template = template; -exports.apply = apply; -exports.tap = tap; diff --git a/dist/karet.xhr.es.js b/dist/karet.xhr.es.js index b29eb13..8a838c0 100644 --- a/dist/karet.xhr.es.js +++ b/dist/karet.xhr.es.js @@ -1,8 +1,8 @@ import { lift, inTemplate } from 'karet.lift'; -import { inherit, curry, acyclicEqualsU, pipe2U, defineNameU, freeze, id, object0, isArray, isString, curryN, assign, always, Monad, Applicative, isNumber, isFunction, IdentityOrU } from 'infestines'; -import { Property, never, Observable, Stream, stream, constantError, constant } from 'kefir'; -import { set, get, array, cross, reread, identity, inverse, keyed, transform, ifElse, modifyOp, branch, cond, setOp, keys, sum, branches, collect, when, and as and$1, is, traverse } from 'kefir.partial.lenses'; -import { arrayId, tuple, and, acceptWith, validate, freeFn, props, optional, propsOr, accept, modifyAfter, or, cases } from 'partial.lenses.validation'; +import { inherit, defineNameU, pipe2U, curryN, curry, id, Monad, Applicative, isFunction, isNumber, IdentityOrU, acyclicEqualsU, freeze, object0, isArray, isString, assign, always } from 'infestines'; +import { Property, constantError, never as never$1, Observable, Stream, stream, constant } from 'kefir'; +import { get, collect, when, reread, and, branch, is as is$1, traverse, ifElse, set, transform, modifyOp, cond, setOp, keys, sum, branches, array, cross, identity, inverse, keyed } from 'kefir.partial.lenses'; +import { validate, freeFn, tuple, props, optional, accept, propsOr, modifyAfter, or, cases, arrayId, and as and$1, acceptWith } from 'partial.lenses.validation'; var TIMER = 't'; var SOURCE = 's'; @@ -63,7 +63,7 @@ var toProperty = function toProperty(x) { return x instanceof Stream ? x.toProperty() : x; }; -var never$1 = /*#__PURE__*/never(); +var never = /*#__PURE__*/never$1(); var skipDuplicates = /*#__PURE__*/curry(function skipDuplicates(eq, xs) { return isObservable(xs) ? xs.skipDuplicates(eq) : xs; @@ -144,6 +144,8 @@ var typeLoad = /*#__PURE__*/freeze({ type: LOAD }); var eventTypes = [LOADSTART, PROGRESS, LOAD, TIMEOUT, ERROR]; +var simpleSettings = [OVERRIDE_MIME_TYPE, RESPONSE_TYPE, TIMEOUT, WITH_CREDENTIALS]; + var hasKeys = function hasKeys(x) { return isFunction(x.keys); }; @@ -153,44 +155,39 @@ var isNil = function isNil(x) { }; var headerValue = accept; var headersArray = /*#__PURE__*/arrayId( /*#__PURE__*/tuple(string, headerValue)); -var headersMap = /*#__PURE__*/and( /*#__PURE__*/acceptWith(function (xs) { +var headersMap = /*#__PURE__*/and$1( /*#__PURE__*/acceptWith(function (xs) { return Array.from(xs); }), headersArray, /*#__PURE__*/acceptWith(function (xs) { return new Map(xs); })); var performPlain = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : validate(freeFn(tuple(props({ - url: string, - method: optional(string), - user: optional(string), - password: optional(string), + body: optional(accept), headers: propsOr(headerValue, object0), + method: optional(string), overrideMimeType: optional(string), - body: optional(accept), + password: optional(string), responseType: optional(string), timeout: optional(number), + url: string, + user: optional(string), withCredentials: optional(boolean) })), accept)))(function perform(args) { return delayUnsub(stream(function (_ref) { var emit = _ref.emit, end = _ref.end; - var url = args.url; var method = args.method; var user = args.user; var password = args.password; var headers = args.headers; - var overrideMimeType = args[OVERRIDE_MIME_TYPE]; var body = args.body; - var responseType = args[RESPONSE_TYPE]; - var timeout = args[TIMEOUT]; - var withCredentials = args[WITH_CREDENTIALS]; var xhr = new XMLHttpRequest(); - var state = { xhr: xhr, up: typeInitial, down: typeInitial, map: id }; + var state = { down: typeInitial, map: id, up: typeInitial, xhr: xhr }; var update = function update(dir, type) { return function (event) { - if (type !== PROGRESS || state[dir].type !== LOAD) emit(state = set(dir, { type: type, event: event }, state)); + if (type !== PROGRESS || state[dir].type !== LOAD) emit(state = set(dir, { event: event, type: type }, state)); }; }; eventTypes.forEach(function (type) { @@ -203,17 +200,18 @@ var performPlain = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : va xhr[ADD_EVENT_LISTENER](LOADEND, function (event) { end(emit(state = set(EVENT, event, state))); }); - xhr.open(isNil(method) ? 'GET' : method, url, true, isNil(user) ? null : user, isNil(password) ? null : password); - if (responseType) { - xhr[RESPONSE_TYPE] = responseType; - if (responseType === JSON_ && xhr[RESPONSE_TYPE] !== JSON_) state = set(MAP, tryParse, state); + xhr.open(isNil(method) ? 'GET' : method, args.url, true, isNil(user) ? null : user, isNil(password) ? null : password); + simpleSettings.forEach(function (key) { + var value = args[key]; + if (value) xhr[key] = value; + }); + if (args[RESPONSE_TYPE] === JSON_ && xhr[RESPONSE_TYPE] !== JSON_) { + // IE11 workaround + state = set(MAP, tryParse, state); } - if (timeout) xhr[TIMEOUT] = timeout; - if (withCredentials) xhr[WITH_CREDENTIALS] = withCredentials; for (var header in headers) { xhr.setRequestHeader(header, headers[header]); } - if (overrideMimeType) xhr[OVERRIDE_MIME_TYPE](overrideMimeType); xhr.send(isNil(body) ? null : body); return function () { if (!xhr[STATUS]) xhr.abort(); @@ -226,7 +224,7 @@ var toLowerKeyedObject = /*#__PURE__*/get([/*#__PURE__*/array( /*#__PURE__*/cros var normalizeOptions = /*#__PURE__*/(process.env.NODE_ENV === 'production' ? id : validate(modifyAfter(freeFn(tuple(or(string, propsOr(accept, { headers: optional(cases([isNil, accept], [isArray, headersArray], [hasKeys, headersMap], [propsOr(headerValue, object0)])) }))), accept), lift)))( /*#__PURE__*/transform( /*#__PURE__*/ifElse(isString, /*#__PURE__*/modifyOp(function (url) { - return { url: url, headers: object0 }; + return { headers: object0, url: url }; }), /*#__PURE__*/branch({ headers: /*#__PURE__*/cond([isNil, /*#__PURE__*/setOp(object0)], [isArray, /*#__PURE__*/modifyOp(toLowerKeyedObject)], [hasKeys, /*#__PURE__*/modifyOp( /*#__PURE__*/pipe2U(Array.from, toLowerKeyedObject))], [[keys, /*#__PURE__*/modifyOp(toLower)]]) })))); @@ -244,19 +242,19 @@ function tryParse(json) { var isOneOf = /*#__PURE__*/curry(function (values, value) { return values.includes(value); }); -var is$1 = /*#__PURE__*/curry(function (values, dir) { +var is = /*#__PURE__*/curry(function (values, dir) { return get([dir, TYPE$1, isOneOf(values)]); }); -var hasStartedOn = /*#__PURE__*/is$1(eventTypes); -var isProgressingOn = /*#__PURE__*/is$1([PROGRESS, LOADSTART]); +var hasStartedOn = /*#__PURE__*/is(eventTypes); +var isProgressingOn = /*#__PURE__*/is([PROGRESS, LOADSTART]); var load = [LOAD]; -var hasCompletedOn = /*#__PURE__*/is$1(load); -var hasErroredOn = /*#__PURE__*/is$1([ERROR]); -var hasTimedOutOn = /*#__PURE__*/is$1([TIMEOUT]); +var hasCompletedOn = /*#__PURE__*/is(load); +var hasErroredOn = /*#__PURE__*/is([ERROR]); +var hasTimedOutOn = /*#__PURE__*/is([TIMEOUT]); var ended = [LOAD, TIMEOUT, ERROR]; -var hasEndedOn = /*#__PURE__*/is$1(ended); +var hasEndedOn = /*#__PURE__*/is(ended); var failed = [ERROR, TIMEOUT]; -var hasFailedOn = /*#__PURE__*/is$1(failed); +var hasFailedOn = /*#__PURE__*/is(failed); var event = /*#__PURE__*/curry(function (prop, op, dir) { return op([dir, EVENT, prop]); @@ -301,7 +299,7 @@ var readyState = /*#__PURE__*/setName( /*#__PURE__*/get([XHR, READY_STATE]), REA var isStatusAvailable = /*#__PURE__*/setName( /*#__PURE__*/get([XHR, READY_STATE, function (state) { return 2 <= state; }]), 'isStatusAvailable'); -var isDone = /*#__PURE__*/setName( /*#__PURE__*/is$1([LOADEND], EVENT), 'isDone'); +var isDone = /*#__PURE__*/setName( /*#__PURE__*/is([LOADEND], EVENT), 'isDone'); var isProgressing = /*#__PURE__*/setName( /*#__PURE__*/isProgressingOn(either), 'isProgressing'); var hasErrored = /*#__PURE__*/setName( /*#__PURE__*/hasErroredOn(either), 'hasErrored'); var hasTimedOut = /*#__PURE__*/setName( /*#__PURE__*/hasTimedOutOn(either), 'hasTimedOut'); @@ -346,16 +344,16 @@ var performWith = /*#__PURE__*/curry(function performWith(defaults, overrides) { }); var performJson = /*#__PURE__*/setName( /*#__PURE__*/performWith({ - responseType: JSON_, - headers: { 'Content-Type': 'application/json' } + headers: { 'Content-Type': 'application/json' }, + responseType: JSON_ }), 'performJson'); var typeIsSuccess = [TYPE$1, /*#__PURE__*/isOneOf([INITIAL, LOAD])]; -var hasSucceeded = /*#__PURE__*/setName( /*#__PURE__*/and$1( /*#__PURE__*/branch({ - event: [TYPE$1, /*#__PURE__*/is(LOADEND)], - up: typeIsSuccess, +var hasSucceeded = /*#__PURE__*/setName( /*#__PURE__*/and( /*#__PURE__*/branch({ down: typeIsSuccess, + event: [TYPE$1, /*#__PURE__*/is$1(LOADEND)], + up: typeIsSuccess, xhr: [STATUS, isHttpSuccessU] })), 'hasSucceeded'); @@ -369,7 +367,7 @@ var getJson = /*#__PURE__*/setName( /*#__PURE__*/pipe2U(performJson, /*#__PURE__ } else if (isDone(xhr) && (hasErrored(xhr) || hasTimedOut(xhr))) { return constantError(xhr); } else { - return never$1; + return never; } })), 'getJson'); @@ -385,21 +383,21 @@ var mergeTypes = function mergeTypes(x, y) { var mergeEvents = function mergeEvents(x, y) { return { - type: mergeTypes(x[TYPE$1], y[TYPE$1]), event: { - total: (get(TOTAL, x[EVENT]) || 0) + (get(TOTAL, y[EVENT]) || 0), - loaded: (get(LOADED, x[EVENT]) || 0) + (get(LOADED, y[EVENT]) || 0) - } + loaded: (get(LOADED, x[EVENT]) || 0) + (get(LOADED, y[EVENT]) || 0), + total: (get(TOTAL, x[EVENT]) || 0) + (get(TOTAL, y[EVENT]) || 0) + }, + type: mergeTypes(x[TYPE$1], y[TYPE$1]) }; }; var mergeXHRs = function mergeXHRs(x, y) { return { + down: mergeEvents(x[DOWN], y[DOWN]), event: y[EVENT][TYPE$1] === LOADEND ? x[EVENT] : y[EVENT], - xhr: y[XHR], + map: y[MAP], up: mergeEvents(x[UP], y[UP]), - down: mergeEvents(x[DOWN], y[DOWN]), - map: y[MAP] + xhr: y[XHR] }; }; @@ -410,9 +408,10 @@ var getResponseHeader = /*#__PURE__*/always(null); var of = function of(response) { return { + down: typeLoad, event: typeLoadend, + map: id, up: typeInitial, - down: typeLoad, xhr: { getAllResponseHeaders: getAllResponseHeaders, getResponseHeader: getResponseHeader, @@ -420,8 +419,7 @@ var of = function of(response) { response: response, status: 200, statusText: 'OK' - }, - map: id + } }; }; @@ -461,11 +459,11 @@ var Parallel = /*#__PURE__*/Applicative(map, of, apParallel); var typeIsString = [TYPE$1, isString]; -var isXHR = /*#__PURE__*/setName( /*#__PURE__*/and$1( /*#__PURE__*/branch({ - xhr: [READY_STATE, isNumber], - up: typeIsString, +var isXHR = /*#__PURE__*/setName( /*#__PURE__*/and( /*#__PURE__*/branch({ down: typeIsString, - map: isFunction + map: isFunction, + up: typeIsString, + xhr: [READY_STATE, isNumber] })), 'isXHR'); var IdentitySucceeded = /*#__PURE__*/IdentityOrU(isXHR, Succeeded); @@ -485,4 +483,4 @@ var tap = /*#__PURE__*/curryN(2, function tap(action) { }); }); -export { perform, upHasStarted, upIsProgressing, upHasCompleted, upHasFailed, upHasErrored, upHasTimedOut, upHasEnded, upLoaded, upTotal, upError, downHasStarted, downIsProgressing, downHasCompleted, downHasFailed, downHasErrored, downHasTimedOut, downHasEnded, downLoaded, downTotal, downError, readyState, isStatusAvailable, isDone, isProgressing, hasErrored, hasTimedOut, loaded, total, errors, response, responseType, responseURL, responseText, responseXML, status, statusIsHttpSuccess, statusText, responseHeader, allResponseHeaders, timeout, withCredentials, isHttpSuccess, performWith, performJson, hasSucceeded, hasFailed, getJson, result, of, chain, map, ap, Succeeded, apParallel, Parallel, isXHR, IdentitySucceeded, IdentityParallel, template, apply, tap }; +export { IdentityParallel, IdentitySucceeded, Parallel, Succeeded, allResponseHeaders, ap, apParallel, apply, chain, downError, downHasCompleted, downHasEnded, downHasErrored, downHasFailed, downHasStarted, downHasTimedOut, downIsProgressing, downLoaded, downTotal, errors, getJson, hasErrored, hasFailed, hasSucceeded, hasTimedOut, isDone, isHttpSuccess, isProgressing, isStatusAvailable, isXHR, loaded, map, of, perform, performJson, performWith, readyState, response, responseHeader, responseText, responseType, responseURL, responseXML, result, status, statusIsHttpSuccess, statusText, tap, template, timeout, total, upError, upHasCompleted, upHasEnded, upHasErrored, upHasFailed, upHasStarted, upHasTimedOut, upIsProgressing, upLoaded, upTotal, withCredentials }; diff --git a/dist/karet.xhr.js b/dist/karet.xhr.js index f7f339d..358482b 100644 --- a/dist/karet.xhr.js +++ b/dist/karet.xhr.js @@ -105,7 +105,7 @@ // - var setName = I.defineNameU; + var setName = I.defineNameU; var ADD_EVENT_LISTENER = 'addEventListener'; var DOWN = 'down'; @@ -142,6 +142,8 @@ var eventTypes = [LOADSTART, PROGRESS, LOAD, TIMEOUT, ERROR]; + var simpleSettings = [OVERRIDE_MIME_TYPE, RESPONSE_TYPE, TIMEOUT, WITH_CREDENTIALS]; + var hasKeys = function hasKeys(x) { return I.isFunction(x.keys); }; @@ -157,38 +159,33 @@ return new Map(xs); })); - var performPlain = /*#__PURE__*/(V.validate(V.freeFn(V.tuple(V.props({ - url: string, - method: V.optional(string), - user: V.optional(string), - password: V.optional(string), + var performPlain = /*#__PURE__*/( V.validate(V.freeFn(V.tuple(V.props({ + body: V.optional(V.accept), headers: V.propsOr(headerValue, I.object0), + method: V.optional(string), overrideMimeType: V.optional(string), - body: V.optional(V.accept), + password: V.optional(string), responseType: V.optional(string), timeout: V.optional(number), + url: string, + user: V.optional(string), withCredentials: V.optional(boolean) })), V.accept)))(function perform(args) { return delayUnsub(K.stream(function (_ref) { var emit = _ref.emit, end = _ref.end; - var url = args.url; var method = args.method; var user = args.user; var password = args.password; var headers = args.headers; - var overrideMimeType = args[OVERRIDE_MIME_TYPE]; var body = args.body; - var responseType = args[RESPONSE_TYPE]; - var timeout = args[TIMEOUT]; - var withCredentials = args[WITH_CREDENTIALS]; var xhr = new XMLHttpRequest(); - var state = { xhr: xhr, up: typeInitial, down: typeInitial, map: I.id }; + var state = { down: typeInitial, map: I.id, up: typeInitial, xhr: xhr }; var update = function update(dir, type) { return function (event) { - if (type !== PROGRESS || state[dir].type !== LOAD) emit(state = L.set(dir, { type: type, event: event }, state)); + if (type !== PROGRESS || state[dir].type !== LOAD) emit(state = L.set(dir, { event: event, type: type }, state)); }; }; eventTypes.forEach(function (type) { @@ -201,17 +198,18 @@ xhr[ADD_EVENT_LISTENER](LOADEND, function (event) { end(emit(state = L.set(EVENT, event, state))); }); - xhr.open(isNil(method) ? 'GET' : method, url, true, isNil(user) ? null : user, isNil(password) ? null : password); - if (responseType) { - xhr[RESPONSE_TYPE] = responseType; - if (responseType === JSON_ && xhr[RESPONSE_TYPE] !== JSON_) state = L.set(MAP, tryParse, state); + xhr.open(isNil(method) ? 'GET' : method, args.url, true, isNil(user) ? null : user, isNil(password) ? null : password); + simpleSettings.forEach(function (key) { + var value = args[key]; + if (value) xhr[key] = value; + }); + if (args[RESPONSE_TYPE] === JSON_ && xhr[RESPONSE_TYPE] !== JSON_) { + // IE11 workaround + state = L.set(MAP, tryParse, state); } - if (timeout) xhr[TIMEOUT] = timeout; - if (withCredentials) xhr[WITH_CREDENTIALS] = withCredentials; for (var header in headers) { xhr.setRequestHeader(header, headers[header]); } - if (overrideMimeType) xhr[OVERRIDE_MIME_TYPE](overrideMimeType); xhr.send(isNil(body) ? null : body); return function () { if (!xhr[STATUS]) xhr.abort(); @@ -221,10 +219,10 @@ var toLowerKeyedObject = /*#__PURE__*/L.get([/*#__PURE__*/L.array( /*#__PURE__*/L.cross([/*#__PURE__*/L.reread(toLower), L.identity])), /*#__PURE__*/L.inverse(L.keyed)]); - var normalizeOptions = /*#__PURE__*/(V.validate(V.modifyAfter(V.freeFn(V.tuple(V.or(string, V.propsOr(V.accept, { + var normalizeOptions = /*#__PURE__*/( V.validate(V.modifyAfter(V.freeFn(V.tuple(V.or(string, V.propsOr(V.accept, { headers: V.optional(V.cases([isNil, V.accept], [I.isArray, headersArray], [hasKeys, headersMap], [V.propsOr(headerValue, I.object0)])) }))), V.accept), F.lift)))( /*#__PURE__*/L.transform( /*#__PURE__*/L.ifElse(I.isString, /*#__PURE__*/L.modifyOp(function (url) { - return { url: url, headers: I.object0 }; + return { headers: I.object0, url: url }; }), /*#__PURE__*/L.branch({ headers: /*#__PURE__*/L.cond([isNil, /*#__PURE__*/L.setOp(I.object0)], [I.isArray, /*#__PURE__*/L.modifyOp(toLowerKeyedObject)], [hasKeys, /*#__PURE__*/L.modifyOp( /*#__PURE__*/I.pipe2U(Array.from, toLowerKeyedObject))], [[L.keys, /*#__PURE__*/L.modifyOp(toLower)]]) })))); @@ -344,16 +342,16 @@ }); var performJson = /*#__PURE__*/setName( /*#__PURE__*/performWith({ - responseType: JSON_, - headers: { 'Content-Type': 'application/json' } + headers: { 'Content-Type': 'application/json' }, + responseType: JSON_ }), 'performJson'); var typeIsSuccess = [TYPE$1, /*#__PURE__*/isOneOf([INITIAL, LOAD])]; var hasSucceeded = /*#__PURE__*/setName( /*#__PURE__*/L.and( /*#__PURE__*/L.branch({ + down: typeIsSuccess, event: [TYPE$1, /*#__PURE__*/L.is(LOADEND)], up: typeIsSuccess, - down: typeIsSuccess, xhr: [STATUS, isHttpSuccessU] })), 'hasSucceeded'); @@ -383,21 +381,21 @@ var mergeEvents = function mergeEvents(x, y) { return { - type: mergeTypes(x[TYPE$1], y[TYPE$1]), event: { - total: (L.get(TOTAL, x[EVENT]) || 0) + (L.get(TOTAL, y[EVENT]) || 0), - loaded: (L.get(LOADED, x[EVENT]) || 0) + (L.get(LOADED, y[EVENT]) || 0) - } + loaded: (L.get(LOADED, x[EVENT]) || 0) + (L.get(LOADED, y[EVENT]) || 0), + total: (L.get(TOTAL, x[EVENT]) || 0) + (L.get(TOTAL, y[EVENT]) || 0) + }, + type: mergeTypes(x[TYPE$1], y[TYPE$1]) }; }; var mergeXHRs = function mergeXHRs(x, y) { return { + down: mergeEvents(x[DOWN], y[DOWN]), event: y[EVENT][TYPE$1] === LOADEND ? x[EVENT] : y[EVENT], - xhr: y[XHR], + map: y[MAP], up: mergeEvents(x[UP], y[UP]), - down: mergeEvents(x[DOWN], y[DOWN]), - map: y[MAP] + xhr: y[XHR] }; }; @@ -408,9 +406,10 @@ var of = function of(response) { return { + down: typeLoad, event: typeLoadend, + map: I.id, up: typeInitial, - down: typeLoad, xhr: { getAllResponseHeaders: getAllResponseHeaders, getResponseHeader: getResponseHeader, @@ -418,8 +417,7 @@ response: response, status: 200, statusText: 'OK' - }, - map: I.id + } }; }; @@ -460,10 +458,10 @@ var typeIsString = [TYPE$1, I.isString]; var isXHR = /*#__PURE__*/setName( /*#__PURE__*/L.and( /*#__PURE__*/L.branch({ - xhr: [READY_STATE, I.isNumber], - up: typeIsString, down: typeIsString, - map: I.isFunction + map: I.isFunction, + up: typeIsString, + xhr: [READY_STATE, I.isNumber] })), 'isXHR'); var IdentitySucceeded = /*#__PURE__*/I.IdentityOrU(isXHR, Succeeded); @@ -483,68 +481,68 @@ }); }); - exports.perform = perform; - exports.upHasStarted = upHasStarted; - exports.upIsProgressing = upIsProgressing; - exports.upHasCompleted = upHasCompleted; - exports.upHasFailed = upHasFailed; - exports.upHasErrored = upHasErrored; - exports.upHasTimedOut = upHasTimedOut; - exports.upHasEnded = upHasEnded; - exports.upLoaded = upLoaded; - exports.upTotal = upTotal; - exports.upError = upError; - exports.downHasStarted = downHasStarted; - exports.downIsProgressing = downIsProgressing; + exports.IdentityParallel = IdentityParallel; + exports.IdentitySucceeded = IdentitySucceeded; + exports.Parallel = Parallel; + exports.Succeeded = Succeeded; + exports.allResponseHeaders = allResponseHeaders; + exports.ap = ap; + exports.apParallel = apParallel; + exports.apply = apply; + exports.chain = chain; + exports.downError = downError; exports.downHasCompleted = downHasCompleted; - exports.downHasFailed = downHasFailed; + exports.downHasEnded = downHasEnded; exports.downHasErrored = downHasErrored; + exports.downHasFailed = downHasFailed; + exports.downHasStarted = downHasStarted; exports.downHasTimedOut = downHasTimedOut; - exports.downHasEnded = downHasEnded; + exports.downIsProgressing = downIsProgressing; exports.downLoaded = downLoaded; exports.downTotal = downTotal; - exports.downError = downError; - exports.readyState = readyState; - exports.isStatusAvailable = isStatusAvailable; - exports.isDone = isDone; - exports.isProgressing = isProgressing; + exports.errors = errors; + exports.getJson = getJson; exports.hasErrored = hasErrored; + exports.hasFailed = hasFailed; + exports.hasSucceeded = hasSucceeded; exports.hasTimedOut = hasTimedOut; + exports.isDone = isDone; + exports.isHttpSuccess = isHttpSuccess; + exports.isProgressing = isProgressing; + exports.isStatusAvailable = isStatusAvailable; + exports.isXHR = isXHR; exports.loaded = loaded; - exports.total = total; - exports.errors = errors; + exports.map = map; + exports.of = of; + exports.perform = perform; + exports.performJson = performJson; + exports.performWith = performWith; + exports.readyState = readyState; exports.response = response; + exports.responseHeader = responseHeader; + exports.responseText = responseText; exports.responseType = responseType; exports.responseURL = responseURL; - exports.responseText = responseText; exports.responseXML = responseXML; + exports.result = result; exports.status = status; exports.statusIsHttpSuccess = statusIsHttpSuccess; exports.statusText = statusText; - exports.responseHeader = responseHeader; - exports.allResponseHeaders = allResponseHeaders; + exports.tap = tap; + exports.template = template; exports.timeout = timeout; + exports.total = total; + exports.upError = upError; + exports.upHasCompleted = upHasCompleted; + exports.upHasEnded = upHasEnded; + exports.upHasErrored = upHasErrored; + exports.upHasFailed = upHasFailed; + exports.upHasStarted = upHasStarted; + exports.upHasTimedOut = upHasTimedOut; + exports.upIsProgressing = upIsProgressing; + exports.upLoaded = upLoaded; + exports.upTotal = upTotal; exports.withCredentials = withCredentials; - exports.isHttpSuccess = isHttpSuccess; - exports.performWith = performWith; - exports.performJson = performJson; - exports.hasSucceeded = hasSucceeded; - exports.hasFailed = hasFailed; - exports.getJson = getJson; - exports.result = result; - exports.of = of; - exports.chain = chain; - exports.map = map; - exports.ap = ap; - exports.Succeeded = Succeeded; - exports.apParallel = apParallel; - exports.Parallel = Parallel; - exports.isXHR = isXHR; - exports.IdentitySucceeded = IdentitySucceeded; - exports.IdentityParallel = IdentityParallel; - exports.template = template; - exports.apply = apply; - exports.tap = tap; Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/dist/karet.xhr.min.js b/dist/karet.xhr.min.js index ccd0e5b..5148070 100644 --- a/dist/karet.xhr.min.js +++ b/dist/karet.xhr.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("karet.lift"),require("infestines"),require("kefir"),require("kefir.partial.lenses")):"function"==typeof define&&define.amd?define(["exports","karet.lift","infestines","kefir","kefir.partial.lenses","partial.lenses.validation"],t):t(((e=e||self).karet=e.karet||{},e.karet.xhr={}),e.karet.lift,e.I,e.Kefir,e.Kefir.partial.lenses)}(this,function(e,t,v,r,w,n){"use strict";function H(e){try{return JSON.parse(e)}catch(e){return null}}var u="value",i=v.inherit(function(e){r.Property.call(this),this.s=e,this.h=this.t=0},r.Property,{_onActivation:function(){var r=this;r.t?(clearTimeout(r.t),r.t=0):r.s.onAny(r.h=function(e){var t=e.type;t===u?r._emitValue(e[u]):"end"===t?r._emitEnd():r._emitError(e[u])})},_onDeactivation:function(){var e=this;e.t=setTimeout(function(){e.s.offAny(e.h),e.h=e.t=0},0)}}),o=function(e){return e instanceof r.Observable},s=function(e){return o(e)?e:r.constant(e)},a=function(e){return e instanceof r.Stream?e.toProperty():e},c=r.never(),d=v.curry(function(e,t){return o(t)?t.skipDuplicates(e):t})(v.acyclicEqualsU),p=v.curry(function(e,t){if(o(t))return t.filter(e);if(e(t))return t;throw Error(e.name)}),f=v.curry(function(e,t){return o(t)?a(t.flatMapLatest(v.pipe2U(e,s))):a(e(t))}),l=v.curry(function(e,t){return o(t)?a(t.map(e)):e(t)}),y=function(e){return e.toLowerCase()},h=function(e){return e},x="addEventListener",T="down",m="error",E="event",g="initial",O="json",S="load",b="loaded",k="loadend",U="loadstart",L="overrideMimeType",P="progress",R="readyState",A="responseType",I="status",q="timeout",M="total",N="type",_="up",j="withCredentials",C="xhr",F=v.freeze({type:g}),X=v.freeze({type:k}),z=v.freeze({type:S}),D=[U,P,S,q,m],J=function(e){return null==e},K=(0,v.id)(function(g){return e=r.stream(function(e){var n=e.emit,t=e.end,r=g.url,u=g.method,i=g.user,o=g.password,s=g.headers,a=g[L],c=g.body,d=g[A],p=g[q],f=g[j],l=new XMLHttpRequest,y={xhr:l,up:F,down:F,map:v.id},h=function(t,r){return function(e){r===P&&y[t].type===S||n(y=w.set(t,{type:r,event:e},y))}};for(var m in D.forEach(function(e){l[x](e,h(T,e)),l.upload[x](e,h(_,e))}),l[x]("readystatechange",function(e){n(y=w.set(E,e,y))}),l[x](k,function(e){t(n(y=w.set(E,e,y)))}),l.open(J(u)?"GET":u,r,!0,J(i)?null:i,J(o)?null:o),d&&(l[A]=d)===O&&l[A]!==O&&(y=w.set("map",H,y)),p&&(l[q]=p),f&&(l[j]=f),s)l.setRequestHeader(m,s[m]);return a&&l[L](a),l.send(J(c)?null:c),function(){l[I]||l.abort()}}),new i(e);var e}),G=w.get([w.array(w.cross([w.reread(y),w.identity])),w.inverse(w.keyed)]),V=(0,v.id)(w.transform(w.ifElse(v.isString,w.modifyOp(function(e){return{url:e,headers:v.object0}}),w.branch({headers:w.cond([J,w.setOp(v.object0)],[v.isArray,w.modifyOp(G)],[function(e){return v.isFunction(e.keys)},w.modifyOp(v.pipe2U(Array.from,G))],[[w.keys,w.modifyOp(y)]])})))),W=h(v.pipe2U(V,f(K))),B=v.curry(function(e,t){return e.includes(t)}),Q=v.curry(function(e,t){return w.get([t,N,B(e)])}),Y=Q(D),Z=Q([P,U]),$=Q([S]),ee=Q([m]),te=Q([q]),re=Q([S,q,m]),ne=Q([m,q]),ue=v.curry(function(e,t,r){return t([r,E,e])}),ie=ue(b,w.sum),oe=ue(M,w.sum),se=ue(m),ae=function(e){return 200<=e&&e<300},ce=v.curryN(3,function(e,t){return v.pipe2U(p(e),t)}),de=w.branches(T,_),pe=h(Y(_)),fe=h(Z(_)),le=h($(_)),ye=h(ee(_)),he=h(ee(_)),me=h(te(_)),ge=h(re(_)),ve=h(ie(_)),we=h(oe(_)),He=h(se(w.get,_)),xe=h(Y(T)),Te=h(Z(T)),Ee=h($(T)),Oe=h(ne(T)),Se=h(ee(T)),be=h(te(T)),ke=h(re(T)),Ue=h(ie(T)),Le=h(oe(T)),Pe=h(se(w.get,T)),Re=h(w.get([C,R])),Ae=h(w.get([C,R,function(e){return 2<=e}])),Ie=h(Q([k],E)),qe=h(Z(de)),Me=h(ee(de)),Ne=h(te(de)),_e=h(ie(de)),je=h(oe(de)),Ce=h(v.pipe2U(se(w.collect,de),d)),Fe=h(ce(Ee,v.pipe2U(t.lift(function(e){return(0,e.map)(e.xhr.response)}),d))),Xe=h(w.get([C,A])),ze=h(ce(Ae,w.get([C,"responseURL"]))),De=h(w.get([C,w.when(w.get([A,B(["","text"])])),"responseText"])),Je=h(ce(Ee,w.get([C,w.when(w.get([A,B(["","document"])])),"responseXML"]))),Ke=h(ce(Ae,w.get([C,I]))),Ge=h(w.get([C,I,ae])),Ve=h(ce(Ae,w.get([C,"statusText"]))),We=v.curryN(2,function(t){return ce(Ae,w.get([C,w.reread(function(e){return e.getResponseHeader(t)})]))}),Be=h(ce(Ae,w.get([C,w.reread(function(e){return e.getAllResponseHeaders()})]))),Qe=h(w.get([C,q])),Ye=h(w.get([C,j])),Ze=t.lift(ae),$e=t.lift(function(e,t){var r=v.assign({},e.headers,t.headers);return v.assign({},e,t,{headers:r})}),et=v.curry(function(e,t){return W($e(V(e),V(t)))}),tt=h(et({responseType:O,headers:{"Content-Type":"application/json"}})),rt=[N,B([g,S])],nt=h(w.and(w.branch({event:[N,w.is(k)],up:rt,down:rt,xhr:[I,ae]}))),ut=t.lift(function(e){return Ae(e)&&!Ge(e)||Oe(e)||ye(e)}),it=h(v.pipe2U(tt,f(function(e){return nt(e)?Fe(e):Ie(e)&&(Me(e)||Ne(e))?r.constantError(e):c}))),ot=h(ce(nt,Fe)),st=function(e,t){return{type:(r=e[N],n=t[N],D.indexOf(r)