Skip to content
Snippets Groups Projects
Commit 63fc408b authored by David Sehnal's avatar David Sehnal
Browse files

ajaxGet: support custom http headers

parent 69dedd8c
No related branches found
No related tags found
No related merge requests found
...@@ -16,10 +16,10 @@ type _File = File; ...@@ -16,10 +16,10 @@ type _File = File;
type Asset = Asset.Url | Asset.File type Asset = Asset.Url | Asset.File
namespace Asset { 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 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 }; return { kind: 'url', id: UUID.create22(), url, ...options };
} }
......
...@@ -42,6 +42,7 @@ export interface AjaxGetParams<T extends DataType = 'string'> { ...@@ -42,6 +42,7 @@ export interface AjaxGetParams<T extends DataType = 'string'> {
url: string, url: string,
type?: T, type?: T,
title?: string, title?: string,
headers?: [string, string][],
body?: string body?: string
} }
...@@ -61,7 +62,7 @@ export function ajaxGet(url: string): Task<DataValue> ...@@ -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>): Task<DataResponse<T>>
export function ajaxGet<T extends DataType>(params: AjaxGetParams<T> | string) { export function ajaxGet<T extends DataType>(params: AjaxGetParams<T> | string) {
if (typeof params === 'string') return ajaxGetInternal(params, params, '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 export type AjaxTask = typeof ajaxGet
...@@ -256,12 +257,17 @@ function getRequestResponseType(type: DataType): XMLHttpRequestResponseType { ...@@ -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; let xhttp: XMLHttpRequest | undefined = void 0;
return Task.create(title ? title : 'Download', async ctx => { return Task.create(title ? title : 'Download', async ctx => {
xhttp = RequestPool.get(); xhttp = RequestPool.get();
xhttp.open(body ? 'post' : 'get', url, true); xhttp.open(body ? 'post' : 'get', url, true);
if (headers) {
for (const [name, value] of headers) {
xhttp.setRequestHeader(name, value);
}
}
xhttp.responseType = getRequestResponseType(type); xhttp.responseType = getRequestResponseType(type);
xhttp.send(body); xhttp.send(body);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment