UNPKG

16.2 kBTypeScriptView Raw
1// Type definitions for boom 4.3
2// Project: http://github.com/hapijs/boom
3// Definitions by: Igor Rogatty <http://github.com/rogatty>, AJP <http://github.com/AJamesPhillips>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.3
6
7export = Boom;
8
9declare namespace Boom {
10
11 /**
12 * boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom error response object (instance of Error) which includes the following properties:
13 * @see {@link https://github.com/hapijs/boom#boom}
14 */
15 export interface BoomError<Data = any> extends Error {
16 /** isBoom - if true, indicates this is a Boom object instance. */
17 isBoom: boolean;
18 /** isServer - convenience bool indicating status code >= 500. */
19 isServer: boolean;
20 /** message - the error message. */
21 message: string;
22 /** output - the formatted response. Can be directly manipulated after object construction to return a custom error response. Allowed root keys: */
23 output: Output;
24 /** reformat() - rebuilds error.output using the other object properties. */
25 reformat: () => string;
26 /** "If message is unset, the 'error' segment of the header will not be present and isMissing will be true on the error object." mentioned in @see {@link https://github.com/hapijs/boom#boomunauthorizedmessage-scheme-attributes} */
27 isMissing?: boolean;
28 /** https://github.com/hapijs/boom#createstatuscode-message-data and https://github.com/hapijs/boom/blob/v4.3.0/lib/index.js#L99 */
29 data: Data;
30 }
31
32 export interface Output {
33 /** statusCode - the HTTP status code (typically 4xx or 5xx). */
34 statusCode: number;
35 /** headers - an object containing any HTTP headers where each key is a header name and value is the header content. (Limited value type to string https://github.com/hapijs/boom/issues/151 ) */
36 headers: {[index: string]: string};
37 /** payload - the formatted object used as the response payload (stringified). Can be directly manipulated but any changes will be lost if reformat() is called. Any content allowed and by default includes the following content: */
38 payload: Payload;
39 }
40
41 export interface Payload {
42 /** statusCode - the HTTP status code, derived from error.output.statusCode. */
43 statusCode: number;
44 /** error - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from statusCode. */
45 error: string;
46 /** message - the error message derived from error.message. */
47 message: string;
48 /**
49 * "Every key/value pair will be included ... in the response payload under the attributes key."
50 * [see docs](https://github.com/hapijs/boom#boomunauthorizedmessage-scheme-attributes)
51 */
52 attributes?: any;
53 // Excluded this to aid typing of the other values. See tests for example casting to a custom interface to manipulate the payload
54 // [anyContent: string]: any;
55 }
56
57 /**
58 * Decorates an error with the boom properties
59 * @param error the error object to wrap. If error is already a boom object, returns back the same object.
60 * @param statusCode optional HTTP status code. Defaults to 500.
61 * @param message optional message string. If the error already has a message, it adds the message as a prefix. Defaults to no message.
62 * @see {@link https://github.com/hapijs/boom#wraperror-statuscode-message}
63 */
64 export function wrap(error: Error, statusCode?: number, message?: string): BoomError<null>;
65
66 /**
67 * Generates an Error object with the boom decorations
68 * @param statusCode an HTTP error code number. Must be greater or equal 400.
69 * @param message optional message string.
70 * @param data additional error data set to error.data property.
71 * @see {@link https://github.com/hapijs/boom#createstatuscode-message-data}
72 */
73 export function create<Data = null>(statusCode: number, message?: string, data?: Data): BoomError<Data>;
74
75 // 4xx
76 /**
77 * Returns a 400 Bad Request error
78 * @param message optional message.
79 * @param data optional additional error data.
80 * @see {@link https://github.com/hapijs/boom#boombadrequestmessage-data}
81 */
82 export function badRequest<Data = null>(message?: string, data?: Data): BoomError<Data>;
83
84 /**
85 * Returns a 401 Unauthorized error
86 * @param message optional message.
87 * @param scheme can be one of the following:
88 * * an authentication scheme name
89 * * an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.
90 * @param attributes an object of values to use while setting the 'WWW-Authenticate' header. This value is only used when scheme is a string, otherwise it is ignored. Every key/value pair will be included in the 'WWW-Authenticate' in the format of 'key="value"' as well as in the response payload under the attributes key. Alternatively value can be a string which is use to set the value of the scheme, for example setting the token value for negotiate header. If string is used message parameter must be null. null and undefined will be replaced with an empty string. If attributes is set, message will be used as the 'error' segment of the 'WWW-Authenticate' header. If message is unset, the 'error' segment of the header will not be present and isMissing will be true on the error object.
91 * @see {@link https://github.com/hapijs/boom#boomunauthorizedmessage-scheme-attributes}
92 */
93 export function unauthorized(message?: string, scheme?: string, attributes?: {[index: string]: string}): BoomError<null>;
94 export function unauthorized(message?: string, scheme?: string[]): BoomError<null>;
95 export function unauthorized(message?: null, scheme?: string, attributes?: {[index: string]: string} | string): BoomError<null>;
96 export function unauthorized(message?: null, scheme?: string[]): BoomError<null>;
97
98 /**
99 * Returns a 402 Payment Required error
100 * @param message optional message.
101 * @param data optional additional error data.
102 * @see {@link https://github.com/hapijs/boom#boompaymentrequiredmessage-data}
103 */
104 export function paymentRequired<Data = null>(message?: string, data?: Data): BoomError<Data>;
105
106 /**
107 * Returns a 403 Forbidden error
108 * @param message optional message.
109 * @param data optional additional error data.
110 * @see {@link https://github.com/hapijs/boom#boomforbiddenmessage-data}
111 */
112 export function forbidden<Data = null>(message?: string, data?: Data): BoomError<Data>;
113
114 /**
115 * Returns a 404 Not Found error
116 * @param message optional message.
117 * @param data optional additional error data.
118 * @see {@link https://github.com/hapijs/boom#boomnotfoundmessage-data}
119 */
120 export function notFound<Data = null>(message?: string, data?: Data): BoomError<Data>;
121
122 /**
123 * Returns a 405 Method Not Allowed error
124 * @param message optional message.
125 * @param data optional additional error data.
126 * @param allow optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header.
127 * @see {@link https://github.com/hapijs/boom#boommethodnotallowedmessage-data-allow}
128 */
129 export function methodNotAllowed<Data = null>(message?: string, data?: Data, allow?: string | string[]): BoomError<Data>;
130
131 /**
132 * Returns a 406 Not Acceptable error
133 * @param message optional message.
134 * @param data optional additional error data.
135 * @see {@link https://github.com/hapijs/boom#boomnotacceptablemessage-data}
136 */
137 export function notAcceptable<Data = null>(message?: string, data?: Data): BoomError<Data>;
138
139 /**
140 * Returns a 407 Proxy Authentication Required error
141 * @param message optional message.
142 * @param data optional additional error data.
143 * @see {@link https://github.com/hapijs/boom#boomproxyauthrequiredmessage-data}
144 */
145 export function proxyAuthRequired<Data = null>(message?: string, data?: Data): BoomError<Data>;
146
147 /**
148 * Returns a 408 Request Time-out error
149 * @param message optional message.
150 * @param data optional additional error data.
151 * @see {@link https://github.com/hapijs/boom#boomclienttimeoutmessage-data}
152 */
153 export function clientTimeout<Data = null>(message?: string, data?: Data): BoomError<Data>;
154
155 /**
156 * Returns a 409 Conflict error
157 * @param message optional message.
158 * @param data optional additional error data.
159 * @see {@link https://github.com/hapijs/boom#boomconflictmessage-data}
160 */
161 export function conflict<Data = null>(message?: string, data?: Data): BoomError<Data>;
162
163 /**
164 * Returns a 410 Gone error
165 * @param message optional message.
166 * @param data optional additional error data.
167 * @see {@link https://github.com/hapijs/boom#boomresourcegonemessage-data}
168 */
169 export function resourceGone<Data = null>(message?: string, data?: Data): BoomError<Data>;
170
171 /**
172 * Returns a 411 Length Required error
173 * @param message optional message.
174 * @param data optional additional error data.
175 * @see {@link https://github.com/hapijs/boom#boomlengthrequiredmessage-data}
176 */
177 export function lengthRequired<Data = null>(message?: string, data?: Data): BoomError<Data>;
178
179 /**
180 * Returns a 412 Precondition Failed error
181 * @param message optional message.
182 * @param data optional additional error data.
183 * @see {@link https://github.com/hapijs/boom#boompreconditionfailedmessage-data}
184 */
185 export function preconditionFailed<Data = null>(message?: string, data?: Data): BoomError<Data>;
186
187 /**
188 * Returns a 413 Request Entity Too Large error
189 * @param message optional message.
190 * @param data optional additional error data.
191 * @see {@link https://github.com/hapijs/boom#boomentitytoolargemessage-data}
192 */
193 export function entityTooLarge<Data = null>(message?: string, data?: Data): BoomError<Data>;
194
195 /**
196 * Returns a 414 Request-URI Too Large error
197 * @param message optional message.
198 * @param data optional additional error data.
199 * @see {@link https://github.com/hapijs/boom#boomuritoolongmessage-data}
200 */
201 export function uriTooLong<Data = null>(message?: string, data?: Data): BoomError<Data>;
202
203 /**
204 * Returns a 415 Unsupported Media Type error
205 * @param message optional message.
206 * @param data optional additional error data.
207 * @see {@link https://github.com/hapijs/boom#boomunsupportedmediatypemessage-data}
208 */
209 export function unsupportedMediaType<Data = null>(message?: string, data?: Data): BoomError<Data>;
210
211 /**
212 * Returns a 416 Requested Range Not Satisfiable error
213 * @param message optional message.
214 * @param data optional additional error data.
215 * @see {@link https://github.com/hapijs/boom#boomrangenotsatisfiablemessage-data}
216 */
217 export function rangeNotSatisfiable<Data = null>(message?: string, data?: Data): BoomError<Data>;
218
219 /**
220 * Returns a 417 Expectation Failed error
221 * @param message optional message.
222 * @param data optional additional error data.
223 * @see {@link https://github.com/hapijs/boom#boomexpectationfailedmessage-data}
224 */
225 export function expectationFailed<Data = null>(message?: string, data?: Data): BoomError<Data>;
226
227 /**
228 * Returns a 418 I'm a Teapot error
229 * @param message optional message.
230 * @param data optional additional error data.
231 * @see {@link https://github.com/hapijs/boom#boomteapotmessage-data}
232 */
233 export function teapot<Data = null>(message?: string, data?: Data): BoomError<Data>;
234
235 /**
236 * Returns a 422 Unprocessable Entity error
237 * @param message optional message.
238 * @param data optional additional error data.
239 * @see {@link https://github.com/hapijs/boom#boombaddatamessage-data}
240 */
241 export function badData<Data = null>(message?: string, data?: Data): BoomError<Data>;
242
243 /**
244 * Returns a 423 Locked error
245 * @param message optional message.
246 * @param data optional additional error data.
247 * @see {@link https://github.com/hapijs/boom#boomlockedmessage-data}
248 */
249 export function locked<Data = null>(message?: string, data?: Data): BoomError<Data>;
250
251 /**
252 * Returns a 428 Precondition Required error
253 * @param message optional message.
254 * @param data optional additional error data.
255 * @see {@link https://github.com/hapijs/boom#boompreconditionrequiredmessage-data}
256 */
257 export function preconditionRequired<Data = null>(message?: string, data?: Data): BoomError<Data>;
258
259 /**
260 * Returns a 429 Too Many Requests error
261 * @param message optional message.
262 * @param data optional additional error data.
263 * @see {@link https://github.com/hapijs/boom#boomtoomanyrequestsmessage-data}
264 */
265 export function tooManyRequests<Data = null>(message?: string, data?: Data): BoomError<Data>;
266
267 /**
268 * Returns a 451 Unavailable For Legal Reasons error
269 * @param message optional message.
270 * @param data optional additional error data.
271 * @see {@link https://github.com/hapijs/boom#boomillegalmessage-data}
272 */
273 export function illegal<Data = null>(message?: string, data?: Data): BoomError<Data>;
274
275 // 5xx
276 /**
277 * Returns a 500 Internal Server Error error
278 * Only 500 errors will hide your message from the end user. Your message is recorded in the server log.
279 * @param message optional message.
280 * @param data optional additional error data.
281 * @see {@link https://github.com/hapijs/boom#boombadimplementationmessage-data---alias-internal}
282 */
283 export function badImplementation<Data = null>(message?: string, data?: Data): BoomError<Data>;
284
285 /**
286 * Returns a 500 Internal Server Error error
287 * Only 500 errors will hide your message from the end user. Your message is recorded in the server log.
288 * @param message optional message.
289 * @param data optional additional error data.
290 * @see {@link https://github.com/hapijs/boom#boombadimplementationmessage-data---alias-internal}
291 */
292 export function internal<Data = null>(message?: string, data?: Data): BoomError<Data>;
293
294 /**
295 * Returns a 501 Not Implemented error with your error message to the user
296 * @param message optional message.
297 * @param data optional additional error data.
298 * @see {@link https://github.com/hapijs/boom#boomnotimplementedmessage-data}
299 */
300 export function notImplemented<Data = null>(message?: string, data?: Data): BoomError<Data>;
301
302 /**
303 * Returns a 502 Bad Gateway error with your error message to the user
304 * @param message optional message.
305 * @param data optional additional error data.
306 * @see {@link https://github.com/hapijs/boom#boombadgatewaymessage-data}
307 */
308 export function badGateway<Data = null>(message?: string, data?: Data): BoomError<Data>;
309
310 /**
311 * Returns a 503 Service Unavailable error with your error message to the user
312 * @param message optional message.
313 * @param data optional additional error data.
314 * @see {@link https://github.com/hapijs/boom#boomserverunavailablemessage-data}
315 */
316 export function serverUnavailable<Data = null>(message?: string, data?: Data): BoomError<Data>;
317
318 /**
319 * Returns a 504 Gateway Time-out error with your error message to the user
320 * @param message optional message.
321 * @param data optional additional error data.
322 * @see {@link https://github.com/hapijs/boom#boomgatewaytimeoutmessage-data}
323 */
324 export function gatewayTimeout<Data = null>(message?: string, data?: Data): BoomError<Data>;
325}