UNPKG

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