-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.d.ts
130 lines (130 loc) · 4.01 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
declare type AjaxRequestInit = RequestInit & {
/**
* By default a non 200-status code will raise an `APIError`. If this property
* is set to false, no API error will be thrown and the request will be treated
* as a success despite the server side error.
*/
allowNonSuccessStatusCode?: boolean;
};
/**
* Default options for every request.
*/
export declare const defaultInit: Partial<AjaxRequestInit> & {
headers: Record<string, string>;
url?: Partial<AjaxURLObject>;
};
declare type URLComponent = string | number | boolean | null | undefined;
declare type AjaxURLObject = {
/**
* The base URL of the request.
* Example: `https://www.sahnee.games/request?param1=true¶ms2=hello%20world¶m2=second#test`
*
* If our currently domain is `www.sahnee.games` the `url` in this example can be
* specified as `/request`. Otherwise it is `www.sahnee.games/request`.
*
* Can also be an array (e.g. `['api', 'user', 4]` for `'/api/user/4'`). `undefined` & `null` in
* the array are ignored.
*
* However prefer using `origin` for cross origin requests.
*/
url: string | URLComponent[];
/**
* The origin domain the `url` is relative to. By default the current domain.
*/
origin?: string;
/**
* The username for making the request.
*/
username?: string;
/**
* The password for making the request.
*/
password?: string;
/**
* The protocol to use.
*/
protocol?: string;
/**
* The port to use.
*/
port?: number;
/**
* The hash to use.
* Example: `https://www.sahnee.games/request?param1=true¶ms2=hello%20world¶m2=second#test`
* The `hash` in this case is `test`.
*/
hash?: string;
/**
* The search query parameters.
* Example: `https://www.sahnee.games/request?param1=true¶ms2=hello%20world¶m2=second#test`
* ```js
* search: {
* param1: true,
* param2: ['hello world', 'second']
* }
* ```
*/
search?: {
[key: string]: URLComponent | URLComponent[];
};
};
/**
* A valid URL for an ajax request. Can either directly be a string representing a raw URL or a object to safely build it (recommended).
*/
export declare type AjaxURL = string | AjaxURLObject;
/**
* Helper for inline building a URL.
* @param url The URL.
* @param query The search params.
*/
export declare const url: (p: AjaxURL) => string;
/**
* Makes an AJAX request.
* @param req The request.
* @param init Some details.
*/
export declare const ajax: (req: AjaxURL, init?: AjaxRequestInit) => Promise<Response>;
/**
* Loads the given request as JSON. You can also pass request JSON in the `json` prop.
* Automatically sets the headers `accept` and `content-type` to `application/json; charset=utf-8`.
* @param req The request.
* @param init Some details.
* @param init.json Pass the JSON payload here. The payload will be transformed to JSON using
* `JSON.stringify`. Should this not be sufficent this parameter can be omitted and the already
* seralized JSON may be passed as the body.
*/
export declare const json: <T>(req: AjaxURL, { json, ...init }?: RequestInit & {
/**
* By default a non 200-status code will raise an `APIError`. If this property
* is set to false, no API error will be thrown and the request will be treated
* as a success despite the server side error.
*/
allowNonSuccessStatusCode?: boolean | undefined;
} & {
json?: any;
}) => Promise<T>;
export interface IAPIError<T = {}> {
status: number;
code: string;
message: string;
details: T;
}
export default class APIError<T = {}> extends Error {
constructor(error: IAPIError<T>);
private $code;
private $status;
private $details;
/**
* The error code. Typically the name of the error class.
*/
get code(): string;
/**
* The HTTP status code of this error.
*/
get status(): number;
/**
* The error details.
*/
get details(): T;
}
export {};