Browse Source

ajaxGet: support custom http headers

David Sehnal 5 years ago
parent
commit
63fc408be6
2 changed files with 10 additions and 4 deletions
  1. 2 2
      src/mol-util/assets.ts
  2. 8 2
      src/mol-util/data-source.ts

+ 2 - 2
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 };
     }
 

+ 8 - 2
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);