From 63fc408be6335e8f847b2f26887e9a9bb0c6e4bb Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Tue, 21 Apr 2020 23:36:56 +0200
Subject: [PATCH] ajaxGet: support custom http headers

---
 src/mol-util/assets.ts      |  4 ++--
 src/mol-util/data-source.ts | 10 ++++++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/mol-util/assets.ts b/src/mol-util/assets.ts
index ceeac8a97..6fde04876 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 153d11f17..1ff444020 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);
 
-- 
GitLab