Skip to content

Commit

Permalink
[CM-1540] Add support for headers in callhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Oct 31, 2024
1 parent 5b66791 commit b4685ca
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"js-cookie": "^3.0.5",
"live-connect-common": "^4.0.0"
"live-connect-common": "^4.1.0"
},
"devDependencies": {
"@kineticcafe/rollup-plugin-delete": "^3.0.0",
Expand Down
49 changes: 33 additions & 16 deletions src/calls.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CallHandler, isFunction } from 'live-connect-common'
import { CallHandler, Headers, isFunction } from 'live-connect-common'

export class DefaultCallHandler implements CallHandler {
ajaxGet(url: string, responseHandler: (responseText: string, response: object) => void, onError?: (error: unknown) => void, timeout = 1000): void {
ajaxGet(url: string, responseHandler: (responseText: string, response: object) => void, onError?: (error: unknown) => void, timeout = 1000, headers?: Headers): void {
function errorCallback(message: string) {
if (isFunction(onError)) onError(new Error(message))
}

function xhrCall(): XMLHttpRequest {
function xhrCall(): void {
const xhr = new XMLHttpRequest()
const startTime = Date.now()
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
Expand All @@ -17,30 +18,46 @@ export class DefaultCallHandler implements CallHandler {
}
}
}
return xhr
xhr.ontimeout = () => {
const duration = Date.now() - startTime
errorCallback(`Timeout after ${duration} (${timeout}), url: ${url}`)
}
xhr.open('GET', url, true)
xhr.timeout = timeout
xhr.withCredentials = true
if (headers != null) {
for (const [name, value] of Object.entries(headers)) {
if (value != null) {
xhr.setRequestHeader(name, value)
}
}
}
xhr.send()
}

function xdrCall(): XDomainRequest {
function xdrCall(): void {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const xdr = new window.XDomainRequest!()
const startTime = Date.now()
xdr.onprogress = () => undefined
xdr.onerror = () => errorCallback(`Error during XDR call: ${xdr.responseText}, url: ${url}`)
xdr.onload = () => responseHandler(xdr.responseText, xdr)
return xdr
xdr.ontimeout = () => {
const duration = Date.now() - startTime
errorCallback(`Timeout after ${duration} (${timeout}), url: ${url}`)
}
xdr.open('GET', url, true)
xdr.timeout = timeout
xdr.withCredentials = true
xdr.send()
}

try {
const startTime = Date.now()
const request = (window && window.XDomainRequest) ? xdrCall() : xhrCall()

request.ontimeout = () => {
const duration = Date.now() - startTime
errorCallback(`Timeout after ${duration} (${timeout}), url: ${url}`)
if (window.XMLHttpRequest) {
xhrCall()
} else {
xdrCall()
}
request.open('GET', url, true)
request.timeout = timeout
request.withCredentials = true
request.send()
} catch (error) {
errorCallback(`Error while constructing ajax request, ${error}, url: ${url}`)
}
Expand Down
14 changes: 14 additions & 0 deletions test/calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,18 @@ describe('Calls', () => {
expect(obj.src).to.eq('http://localhost')
expect(obj.onload).to.be.undefined()
})

it('should send headers', function (done) {
// @ts-expect-error
const successCallback = (body, request) => {
expect(body).to.eql('{"comment": "Howdy"}')
expect(request).to.eql(requests[0])
expect(request.requestHeaders['X-Test']).to.eql('test')
done()
}
calls.ajaxGet('http://steve.liadm.com/idex/any/any', successCallback, undefined, undefined, { 'X-Test': 'test' })
const request = requests[0]
// @ts-expect-error
request.respond(200, { 'Content-Type': 'application/json' }, '{"comment": "Howdy"}')
})
})

0 comments on commit b4685ca

Please sign in to comment.