diff --git a/src/mol-util/assets.ts b/src/mol-util/assets.ts index ceeac8a9713150288f47a8c5f6032127ddf52cef..6fde0487645dd37f591eda259cd140fc0ebc325a 100644 --- a/src/mol-util/assets.ts +++ b/src/mol-util/assets.ts @@ -16,10 +16,10 @@ type _File = File; type Asset = Asset.Url | Asset.File namespace Asset { - export type Url = { kind: 'url', id: UUID, url: string, title?: string, body?: string } + export type Url = { kind: 'url', id: UUID, url: string, title?: string, body?: string, headers?: [string, string][] } export type File = { kind: 'file', id: UUID, name: string, file?: _File } - export function Url(url: string, options?: { body?: string, title?: string }): Url { + export function Url(url: string, options?: { body?: string, title?: string, headers?: [string, string][] }): Url { return { kind: 'url', id: UUID.create22(), url, ...options }; } diff --git a/src/mol-util/data-source.ts b/src/mol-util/data-source.ts index 153d11f1792d42a3fd829535fce8755ff582b5ea..1ff444020ddfb54c500f4b8896e823adeb7bc8ba 100644 --- a/src/mol-util/data-source.ts +++ b/src/mol-util/data-source.ts @@ -42,6 +42,7 @@ export interface AjaxGetParams<T extends DataType = 'string'> { url: string, type?: T, title?: string, + headers?: [string, string][], body?: string } @@ -61,7 +62,7 @@ export function ajaxGet(url: string): Task<DataValue> export function ajaxGet<T extends DataType>(params: AjaxGetParams<T>): Task<DataResponse<T>> export function ajaxGet<T extends DataType>(params: AjaxGetParams<T> | string) { if (typeof params === 'string') return ajaxGetInternal(params, params, 'string'); - return ajaxGetInternal(params.title, params.url, params.type || 'string', params.body); + return ajaxGetInternal(params.title, params.url, params.type || 'string', params.body, params.headers); } export type AjaxTask = typeof ajaxGet @@ -256,12 +257,17 @@ function getRequestResponseType(type: DataType): XMLHttpRequestResponseType { } } -function ajaxGetInternal<T extends DataType>(title: string | undefined, url: string, type: T, body?: string): Task<DataResponse<T>> { +function ajaxGetInternal<T extends DataType>(title: string | undefined, url: string, type: T, body?: string, headers?: [string, string][]): Task<DataResponse<T>> { let xhttp: XMLHttpRequest | undefined = void 0; return Task.create(title ? title : 'Download', async ctx => { xhttp = RequestPool.get(); xhttp.open(body ? 'post' : 'get', url, true); + if (headers) { + for (const [name, value] of headers) { + xhttp.setRequestHeader(name, value); + } + } xhttp.responseType = getRequestResponseType(type); xhttp.send(body);