Skip to content

Commit

Permalink
Improve boomify typings
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Oct 26, 2024
1 parent 9084371 commit d8637d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,9 @@ export function isBoom(obj: unknown, statusCode?: number): obj is Boom;
*
* @returns A boom object
*/
export function boomify<Data, Decoration>(err: Boom<Data>, options?: BoomifyOptions<Data> & { decorate: Decoration }): Boom<Data> & Decoration;
export function boomify<Data, Decoration>(err: unknown, options?: BoomifyOptions<Data> & { decorate: Decoration }): Boom<Data> & Decoration;
export function boomify<Data extends object>(err: Boom<Data>, options?: BoomifyOptions<Data>): Boom<Data>;
export function boomify<Data>(err: unknown, options?: BoomifyOptions<Data>): Boom<Data>;
export function boomify<Terr, Data, Decoration>(err: Terr, options: BoomifyOptions<Data> & { decorate: Decoration, data: Data }): (Terr extends Boom ? Terr : Boom<Data>) & Omit<Decoration, 'data'>;
export function boomify<Terr, Data, Decoration>(err: Terr, options: BoomifyOptions<Data> & { decorate: Decoration }): (Terr extends Boom ? Terr : Boom<Data>) & Decoration;
export function boomify<Terr, Data = any>(err: Terr, options?: BoomifyOptions<Data>): Terr extends Boom ? Terr : Boom<Data>;

// 4xx Errors

Expand Down
5 changes: 5 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ expect.type<Boom.Output['headers']>(boom.output.headers);
// boomify()

const error = new Error('Unexpected input');
class BadaBoom extends Boom.Boom {}

expect.type<Boom.Boom>(Boom.boomify(error, { statusCode: 400 }));
expect.type<Boom.Boom>(Boom.boomify(error, { statusCode: 400, message: 'Unexpected Input', override: false }));
expect.type<number>(Boom.boomify(error, { decorate }).x);
expect.type<Boom.Boom>(Boom.boomify('error'));
expect.type<Boom.Boom<{ foo: 'bar' }>>(Boom.boomify(new Boom.Boom<{ foo: 'bar' }>()));
expect.type<number>(Boom.boomify(error, { decorate: { data: 1 } }).data);
expect.type<string>(Boom.boomify(error, { decorate: { data: 1 }, data: 'bla' }).data);
expect.type<string>(Boom.boomify(error, { data: 'bla' }).data);
expect.type<BadaBoom>(Boom.boomify(new BadaBoom(), { statusCode: 400 }));

expect.error(Boom.boomify(error, { statusCode: '400' }));
expect.error(Boom.boomify(error, { statusCode: 400, message: true }));
Expand Down

0 comments on commit d8637d6

Please sign in to comment.