UNPKG

7.8 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
4import { AbortSignal } from "./AbortController";
5
6/** Represents an HTTP request. */
7export interface HttpRequest {
8 /** The HTTP method to use for the request. */
9 method?: string;
10
11 /** The URL for the request. */
12 url?: string;
13
14 /** The body content for the request. May be a string or an ArrayBuffer (for binary data). */
15 content?: string | ArrayBuffer;
16
17 /** An object describing headers to apply to the request. */
18 headers?: { [key: string]: string };
19
20 /** The XMLHttpRequestResponseType to apply to the request. */
21 responseType?: XMLHttpRequestResponseType;
22
23 /** An AbortSignal that can be monitored for cancellation. */
24 abortSignal?: AbortSignal;
25
26 /** The time to wait for the request to complete before throwing a TimeoutError. Measured in milliseconds. */
27 timeout?: number;
28}
29
30/** Represents an HTTP response. */
31export class HttpResponse {
32 /** Constructs a new instance of {@link @microsoft/signalr.HttpResponse} with the specified status code.
33 *
34 * @param {number} statusCode The status code of the response.
35 */
36 constructor(statusCode: number);
37
38 /** Constructs a new instance of {@link @microsoft/signalr.HttpResponse} with the specified status code and message.
39 *
40 * @param {number} statusCode The status code of the response.
41 * @param {string} statusText The status message of the response.
42 */
43 constructor(statusCode: number, statusText: string);
44
45 /** Constructs a new instance of {@link @microsoft/signalr.HttpResponse} with the specified status code, message and string content.
46 *
47 * @param {number} statusCode The status code of the response.
48 * @param {string} statusText The status message of the response.
49 * @param {string} content The content of the response.
50 */
51 constructor(statusCode: number, statusText: string, content: string);
52
53 /** Constructs a new instance of {@link @microsoft/signalr.HttpResponse} with the specified status code, message and binary content.
54 *
55 * @param {number} statusCode The status code of the response.
56 * @param {string} statusText The status message of the response.
57 * @param {ArrayBuffer} content The content of the response.
58 */
59 constructor(statusCode: number, statusText: string, content: ArrayBuffer);
60 constructor(
61 public readonly statusCode: number,
62 public readonly statusText?: string,
63 public readonly content?: string | ArrayBuffer) {
64 }
65}
66
67/** Abstraction over an HTTP client.
68 *
69 * This class provides an abstraction over an HTTP client so that a different implementation can be provided on different platforms.
70 */
71export abstract class HttpClient {
72 /** Issues an HTTP GET request to the specified URL, returning a Promise that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
73 *
74 * @param {string} url The URL for the request.
75 * @returns {Promise<HttpResponse>} A Promise that resolves with an {@link @microsoft/signalr.HttpResponse} describing the response, or rejects with an Error indicating a failure.
76 */
77 public get(url: string): Promise<HttpResponse>;
78
79 /** Issues an HTTP GET request to the specified URL, returning a Promise that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
80 *
81 * @param {string} url The URL for the request.
82 * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.
83 * @returns {Promise<HttpResponse>} A Promise that resolves with an {@link @microsoft/signalr.HttpResponse} describing the response, or rejects with an Error indicating a failure.
84 */
85 public get(url: string, options: HttpRequest): Promise<HttpResponse>;
86 public get(url: string, options?: HttpRequest): Promise<HttpResponse> {
87 return this.send({
88 ...options,
89 method: "GET",
90 url,
91 });
92 }
93
94 /** Issues an HTTP POST request to the specified URL, returning a Promise that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
95 *
96 * @param {string} url The URL for the request.
97 * @returns {Promise<HttpResponse>} A Promise that resolves with an {@link @microsoft/signalr.HttpResponse} describing the response, or rejects with an Error indicating a failure.
98 */
99 public post(url: string): Promise<HttpResponse>;
100
101 /** Issues an HTTP POST request to the specified URL, returning a Promise that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
102 *
103 * @param {string} url The URL for the request.
104 * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.
105 * @returns {Promise<HttpResponse>} A Promise that resolves with an {@link @microsoft/signalr.HttpResponse} describing the response, or rejects with an Error indicating a failure.
106 */
107 public post(url: string, options: HttpRequest): Promise<HttpResponse>;
108 public post(url: string, options?: HttpRequest): Promise<HttpResponse> {
109 return this.send({
110 ...options,
111 method: "POST",
112 url,
113 });
114 }
115
116 /** Issues an HTTP DELETE request to the specified URL, returning a Promise that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
117 *
118 * @param {string} url The URL for the request.
119 * @returns {Promise<HttpResponse>} A Promise that resolves with an {@link @microsoft/signalr.HttpResponse} describing the response, or rejects with an Error indicating a failure.
120 */
121 public delete(url: string): Promise<HttpResponse>;
122
123 /** Issues an HTTP DELETE request to the specified URL, returning a Promise that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
124 *
125 * @param {string} url The URL for the request.
126 * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.
127 * @returns {Promise<HttpResponse>} A Promise that resolves with an {@link @microsoft/signalr.HttpResponse} describing the response, or rejects with an Error indicating a failure.
128 */
129 public delete(url: string, options: HttpRequest): Promise<HttpResponse>;
130 public delete(url: string, options?: HttpRequest): Promise<HttpResponse> {
131 return this.send({
132 ...options,
133 method: "DELETE",
134 url,
135 });
136 }
137
138 /** Issues an HTTP request to the specified URL, returning a {@link Promise} that resolves with an {@link @microsoft/signalr.HttpResponse} representing the result.
139 *
140 * @param {HttpRequest} request An {@link @microsoft/signalr.HttpRequest} describing the request to send.
141 * @returns {Promise<HttpResponse>} A Promise that resolves with an HttpResponse describing the response, or rejects with an Error indicating a failure.
142 */
143 public abstract send(request: HttpRequest): Promise<HttpResponse>;
144
145 /** Gets all cookies that apply to the specified URL.
146 *
147 * @param url The URL that the cookies are valid for.
148 * @returns {string} A string containing all the key-value cookie pairs for the specified URL.
149 */
150 // @ts-ignore
151 public getCookieString(url: string): string {
152 return "";
153 }
154}