|
| 1 | +export as namespace yea; |
| 2 | + |
| 3 | +export interface YeaResponse { |
| 4 | + headers: { |
| 5 | + [key: string]: string; |
| 6 | + }; |
| 7 | + body: string; |
| 8 | + status: number; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Error thrown when sending a request fails. |
| 13 | + */ |
| 14 | +export interface YeaRequestError extends Error { |
| 15 | + response: YeaResponse; |
| 16 | +} |
| 17 | + |
| 18 | +export type ResponseTransformer = (response: YeaResponse) => YeaResponse; |
| 19 | + |
| 20 | +declare class YeaAjaxRequest implements PromiseLike<YeaResponse> { |
| 21 | + jsonResponseTransformer: ResponseTransformer; |
| 22 | + |
| 23 | + then<TResult1 = YeaResponse, TResult2 = never>( |
| 24 | + onfulfilled?: |
| 25 | + | ((value: YeaResponse) => TResult1 | PromiseLike<TResult1>) |
| 26 | + | undefined |
| 27 | + | null, |
| 28 | + onrejected?: |
| 29 | + | ((reason: any) => TResult2 | PromiseLike<TResult2>) |
| 30 | + | undefined |
| 31 | + | null |
| 32 | + ): Promise<TResult1 | TResult2>; |
| 33 | + |
| 34 | + /** |
| 35 | + * Sets the HTTP method. |
| 36 | + * @see get |
| 37 | + * @see post |
| 38 | + * @see put |
| 39 | + * @see delete |
| 40 | + */ |
| 41 | + method( |
| 42 | + method: |
| 43 | + | "GET" |
| 44 | + | "POST" |
| 45 | + | "PUT" |
| 46 | + | "DELETE" |
| 47 | + | "get" |
| 48 | + | "post" |
| 49 | + | "put" |
| 50 | + | "delete" |
| 51 | + ): this; |
| 52 | + |
| 53 | + /** |
| 54 | + * Initializes a GET request |
| 55 | + * @example `yea.get('http://example.com/data.json')` |
| 56 | + */ |
| 57 | + get(url: string): this; |
| 58 | + |
| 59 | + /** |
| 60 | + * Initializes a POST request |
| 61 | + * @example `yea.post('http://example.com/data')` |
| 62 | + */ |
| 63 | + post(url: string): this; |
| 64 | + |
| 65 | + /** |
| 66 | + * Initializes a PUT request |
| 67 | + */ |
| 68 | + put(url: string): this; |
| 69 | + |
| 70 | + /** |
| 71 | + * Initializes a DELETE request |
| 72 | + */ |
| 73 | + delete(url: string): this; |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets the full URL of the request. If a query-string is present, it will override any previously set query parameters. |
| 77 | + */ |
| 78 | + url(url: string): this; |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the base URL to which all subsequent request URLs will be appended. |
| 82 | + */ |
| 83 | + baseUrl(baseUrl: string): this; |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets query parameters from an object. Overwrites existing query. |
| 87 | + */ |
| 88 | + query(query: object | string): this; |
| 89 | + |
| 90 | + /** |
| 91 | + * Sets request headers from an object. Overwrites existing headers. |
| 92 | + */ |
| 93 | + headers(headers: object): this; |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets request headers from an object. Only overwrites headers present in the given object. |
| 97 | + */ |
| 98 | + amendHeaders(headers: object): this; |
| 99 | + |
| 100 | + /** |
| 101 | + * Adds or updates a header value. |
| 102 | + */ |
| 103 | + header(name: string, value: string): this; |
| 104 | + |
| 105 | + /** |
| 106 | + * Removes a header from the request. |
| 107 | + */ |
| 108 | + unsetHeader(name: string): this; |
| 109 | + |
| 110 | + /** |
| 111 | + * Set the raw body of the request. See also `json` and `urlencoded`. |
| 112 | + */ |
| 113 | + body(data: string | number): this; |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets a URL-encoded body and sets the `Content-Type` header to `'application/urlencoded'`. |
| 117 | + */ |
| 118 | + urlencoded(data: object): this; |
| 119 | + |
| 120 | + /** |
| 121 | + * Sets a JSON-encoded body and sets the `Content-Type` header to `'application/json'`. |
| 122 | + */ |
| 123 | + json(data: any): this; |
| 124 | + |
| 125 | + /** |
| 126 | + * Sets a timeout after which the request will be aborted and the Promise rejected. |
| 127 | + */ |
| 128 | + timeout(milliseconds): this; |
| 129 | + |
| 130 | + /** |
| 131 | + * Removes the timeout-value previously set with `timeout`. |
| 132 | + */ |
| 133 | + unsetTimeout(): this; |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets a path (similar to `lodash.get`) which will be resolved once the response is returned. |
| 137 | + * @example |
| 138 | +```js |
| 139 | +const account = await request.get('...').prop('data.accounts[0]'); |
| 140 | +const contentType = await request.get('...').prop(['headers', 'content-type']); |
| 141 | +``` |
| 142 | + */ |
| 143 | + prop(path: string): this; |
| 144 | + |
| 145 | + /** |
| 146 | + * Sets a list of response transformers which are called when a response is received. See the documentation for more information. |
| 147 | + */ |
| 148 | + setResponseTransformers(transformers: ResponseTransformer[]): this; |
| 149 | + |
| 150 | + /** |
| 151 | + * By default any 2XX status code resolves the Promise and other status codes will reject it. This can be customized using `setAllowedStatusCode`. |
| 152 | + */ |
| 153 | + setAllowedStatusCode( |
| 154 | + allowedStatusCode: number | RegExp | ((statusCode: number) => boolean) |
| 155 | + ): this; |
| 156 | + |
| 157 | + /** |
| 158 | + * Override global dependencies which are used internally (like Promise). See the documentation for more information. |
| 159 | + */ |
| 160 | + polyfills(polyfills: { Promise: typeof Promise }): this; |
| 161 | + |
| 162 | + /** |
| 163 | + * Returns the request configuration as an object. |
| 164 | + */ |
| 165 | + toObject(): object; |
| 166 | + config(): object; |
| 167 | + debug(): object; |
| 168 | + |
| 169 | + /** |
| 170 | + * Shorthand for `.urlencoded(data).send()`. |
| 171 | + */ |
| 172 | + sendUrlencoded(data: object): Promise<YeaResponse>; |
| 173 | + |
| 174 | + /** |
| 175 | + * Shorthand for `.json(data).send()`. |
| 176 | + */ |
| 177 | + sendJson(data: any): Promise<YeaResponse>; |
| 178 | + |
| 179 | + /** |
| 180 | + * Dispatches the request and returns a Promise. See also `sendJson` and `sendUrlencoded`. |
| 181 | + */ |
| 182 | + send(body?: string): Promise<YeaResponse>; |
| 183 | +} |
| 184 | + |
| 185 | +// An instance of a class is exported |
| 186 | +// i.e. `module.exports = new YeaAjaxRequest()` |
| 187 | +declare const _YeaAjaxRequest: YeaAjaxRequest; |
| 188 | +export default _YeaAjaxRequest; |
0 commit comments