UNPKG

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