UNPKG

mt-mappersmith

Version:

It is a lightweight rest client for node.js and the browser

1 lines 7.82 kB
{"version":3,"sources":["../../../src/gateway/xhr.ts"],"sourcesContent":["import { Gateway } from './gateway'\nimport Response from '../response'\nimport type { Method } from './types'\nimport type { Headers } from '../types'\nimport { assign, parseResponseHeaders, btoa } from '../utils/index'\nimport { createTimeoutError } from './timeout-error'\n\nlet toBase64: (data: string) => string\ntry {\n toBase64 = window.btoa\n} catch {\n toBase64 = btoa\n}\n\nexport class XHR extends Gateway {\n private canceled = false\n private timer?: ReturnType<typeof setTimeout>\n\n get() {\n const xmlHttpRequest = this.createXHR()\n xmlHttpRequest.open('GET', this.request.url(), true)\n this.setHeaders(xmlHttpRequest, {})\n this.configureTimeout(xmlHttpRequest)\n this.configureBinary(xmlHttpRequest)\n xmlHttpRequest.send()\n }\n\n head() {\n const xmlHttpRequest = this.createXHR()\n xmlHttpRequest.open('HEAD', this.request.url(), true)\n this.setHeaders(xmlHttpRequest, {})\n this.configureTimeout(xmlHttpRequest)\n this.configureBinary(xmlHttpRequest)\n xmlHttpRequest.send()\n }\n\n post() {\n this.performRequest('post')\n }\n\n put() {\n this.performRequest('put')\n }\n\n patch() {\n this.performRequest('patch')\n }\n\n delete() {\n this.performRequest('delete')\n }\n\n configureBinary(xmlHttpRequest: XMLHttpRequest) {\n if (this.request.isBinary()) {\n xmlHttpRequest.responseType = 'blob'\n }\n }\n\n configureTimeout(xmlHttpRequest: XMLHttpRequest) {\n this.canceled = false\n this.timer = undefined\n\n const timeout = this.request.timeout()\n\n if (timeout) {\n xmlHttpRequest.timeout = timeout\n xmlHttpRequest.addEventListener('timeout', () => {\n this.canceled = true\n this.timer && clearTimeout(this.timer)\n const error = createTimeoutError(`Timeout (${timeout}ms)`)\n this.dispatchClientError(error.message, error)\n })\n\n // PhantomJS doesn't support timeout for XMLHttpRequest\n this.timer = setTimeout(() => {\n this.canceled = true\n const error = createTimeoutError(`Timeout (${timeout}ms)`)\n this.dispatchClientError(error.message, error)\n }, timeout + 1)\n }\n }\n\n configureCallbacks(xmlHttpRequest: XMLHttpRequest) {\n xmlHttpRequest.addEventListener('load', () => {\n if (this.canceled) {\n return\n }\n\n this.timer && clearTimeout(this.timer)\n this.dispatchResponse(this.createResponse(xmlHttpRequest))\n })\n\n xmlHttpRequest.addEventListener('error', (e) => {\n if (this.canceled) {\n return\n }\n\n this.timer && clearTimeout(this.timer)\n const guessedErrorCause = e\n ? // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (e as any).message || (e as any).name\n : xmlHttpRequest.responseText\n\n const errorMessage = 'Network error'\n const enhancedMessage = guessedErrorCause ? `: ${guessedErrorCause}` : ''\n const error = new Error(`${errorMessage}${enhancedMessage}`)\n this.dispatchClientError(errorMessage, error)\n })\n\n const xhrOptions = this.options().XHR\n\n if (xhrOptions.withCredentials) {\n xmlHttpRequest.withCredentials = true\n }\n\n if (xhrOptions.configure) {\n xhrOptions.configure(xmlHttpRequest)\n }\n }\n\n performRequest(method: Method) {\n const requestMethod = this.shouldEmulateHTTP() ? 'post' : method\n const xmlHttpRequest = this.createXHR()\n xmlHttpRequest.open(requestMethod.toUpperCase(), this.request.url(), true)\n\n const customHeaders: Headers = {}\n const body = this.prepareBody(method, customHeaders) as XMLHttpRequestBodyInit\n this.setHeaders(xmlHttpRequest, customHeaders)\n this.configureTimeout(xmlHttpRequest)\n this.configureBinary(xmlHttpRequest)\n\n xmlHttpRequest.send(body)\n }\n\n createResponse(xmlHttpRequest: XMLHttpRequest) {\n const status = xmlHttpRequest.status\n const data = this.request.isBinary() ? xmlHttpRequest.response : xmlHttpRequest.responseText\n const responseHeaders = parseResponseHeaders(xmlHttpRequest.getAllResponseHeaders())\n return new Response(this.request, status, data, responseHeaders)\n }\n\n setHeaders(xmlHttpRequest: XMLHttpRequest, customHeaders: Headers) {\n const auth = this.request.auth()\n const headers = assign(customHeaders, {\n ...this.request.headers(),\n ...(auth ? { authorization: `Basic ${toBase64(`${auth.username}:${auth.password}`)}` } : {}),\n })\n\n Object.keys(headers).forEach((headerName) => {\n xmlHttpRequest.setRequestHeader(headerName, `${headers[headerName]}`)\n })\n }\n\n createXHR() {\n const xmlHttpRequest = new XMLHttpRequest()\n this.configureCallbacks(xmlHttpRequest)\n return xmlHttpRequest\n }\n}\n\nexport default XHR\n"],"mappings":";AAAA,SAAS,eAAe;AACxB,OAAO,cAAc;AAGrB,SAAS,QAAQ,sBAAsB,YAAY;AACnD,SAAS,0BAA0B;AAEnC,IAAI;AACJ,IAAI;AACF,aAAW,OAAO;AACpB,QAAQ;AACN,aAAW;AACb;AAEO,IAAM,MAAN,cAAkB,QAAQ;AAAA,EACvB,WAAW;AAAA,EACX;AAAA,EAER,MAAM;AACJ,UAAM,iBAAiB,KAAK,UAAU;AACtC,mBAAe,KAAK,OAAO,KAAK,QAAQ,IAAI,GAAG,IAAI;AACnD,SAAK,WAAW,gBAAgB,CAAC,CAAC;AAClC,SAAK,iBAAiB,cAAc;AACpC,SAAK,gBAAgB,cAAc;AACnC,mBAAe,KAAK;AAAA,EACtB;AAAA,EAEA,OAAO;AACL,UAAM,iBAAiB,KAAK,UAAU;AACtC,mBAAe,KAAK,QAAQ,KAAK,QAAQ,IAAI,GAAG,IAAI;AACpD,SAAK,WAAW,gBAAgB,CAAC,CAAC;AAClC,SAAK,iBAAiB,cAAc;AACpC,SAAK,gBAAgB,cAAc;AACnC,mBAAe,KAAK;AAAA,EACtB;AAAA,EAEA,OAAO;AACL,SAAK,eAAe,MAAM;AAAA,EAC5B;AAAA,EAEA,MAAM;AACJ,SAAK,eAAe,KAAK;AAAA,EAC3B;AAAA,EAEA,QAAQ;AACN,SAAK,eAAe,OAAO;AAAA,EAC7B;AAAA,EAEA,SAAS;AACP,SAAK,eAAe,QAAQ;AAAA,EAC9B;AAAA,EAEA,gBAAgB,gBAAgC;AAC9C,QAAI,KAAK,QAAQ,SAAS,GAAG;AAC3B,qBAAe,eAAe;AAAA,IAChC;AAAA,EACF;AAAA,EAEA,iBAAiB,gBAAg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