diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-09 19:59:08 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-09 19:59:08 +0100 |
commit | 6415564b9259a4a6a2f6ec9cb934eab3d56a1677 (patch) | |
tree | 577372a635b5503c1ad81b2af8523a839e790bb7 /src/util | |
parent | 99bccae9fe1588f711e7606dc3c6d7dd4a25675a (diff) | |
download | wallet-core-6415564b9259a4a6a2f6ec9cb934eab3d56a1677.tar.xz |
tos
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/http.ts | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/util/http.ts b/src/util/http.ts index ab253b232..79039f516 100644 --- a/src/util/http.ts +++ b/src/util/http.ts @@ -24,7 +24,7 @@ */ export interface HttpResponse { status: number; - headers: { [name: string]: string }; + headers: Headers; json(): Promise<any>; text(): Promise<string>; } @@ -34,6 +34,31 @@ export interface HttpRequestOptions { } /** + * Headers, roughly modeled after the fetch API's headers object. + */ +export class Headers { + private headerMap = new Map<string, string>(); + + get(name: string): string | null { + const r = this.headerMap.get(name.toLowerCase()); + if (r) { + return r; + } + return null; + } + + set(name: string, value: string): void { + const normalizedName = name.toLowerCase(); + const existing = this.headerMap.get(normalizedName); + if (existing !== undefined) { + this.headerMap.set(normalizedName, existing + "," + value); + } else { + this.headerMap.set(normalizedName, value); + } + } +} + +/** * The request library is bundled into an interface to m responseJson: object & any;ake mocking easy. */ export interface HttpRequestLibrary { @@ -103,12 +128,12 @@ export class BrowserHttpLib implements HttpRequestLibrary { const arr = headers.trim().split(/[\r\n]+/); // Create a map of header names to values - const headerMap: { [name: string]: string } = {}; + const headerMap = new Headers(); arr.forEach(function(line) { const parts = line.split(": "); const header = parts.shift(); const value = parts.join(": "); - headerMap[header!] = value; + headerMap.set(header!, value); }); const resp: HttpResponse = { status: myRequest.status, |