diff options
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, |