Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update decorator types to be correct #4525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/types/server/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,17 @@ export class Server<A = ServerApplicationState> {
decorate(type: 'request', property: DecorateName, method: (existing: ((...args: any[]) => any)) => (request: Request) => DecorationMethod<Request>, options: {apply: true, extend: true}): void;
decorate(type: 'request', property: DecorateName, method: (request: Request) => DecorationMethod<Request>, options: {apply: true, extend?: boolean | undefined}): void;
decorate(type: 'request', property: DecorateName, method: DecorationMethod<Request>, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'request', property: DecorateName, value: (existing: ((...args: any[]) => any)) => (request: Request) => any, options: {apply: true, extend: true}): void;
decorate(type: 'request', property: DecorateName, value: (request: Request) => any, options: {apply: true, extend?: boolean | undefined}): void;
decorate(type: 'request', property: DecorateName, value: any, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'toolkit', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationMethod<ResponseToolkit>, options: {apply?: boolean | undefined, extend: true}): void;
decorate(type: 'toolkit', property: DecorateName, method: DecorationMethod<ResponseToolkit>, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'toolkit', property: DecorateName, value: (existing: ((...args: any[]) => any)) => any, options: {apply?: boolean | undefined, extend: true}): void;
decorate(type: 'toolkit', property: DecorateName, value: any, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'server', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationMethod<Server>, options: {apply?: boolean | undefined, extend: true}): void;
decorate(type: 'server', property: DecorateName, method: DecorationMethod<Server>, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'server', property: DecorateName, value: (existing: ((...args: any[]) => any)) => any, options: {apply?: boolean | undefined, extend: true}): void;
decorate(type: 'server', property: DecorateName, value: any, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;

/**
* Used within a plugin to declare a required dependency on other plugins where:
Expand Down
84 changes: 83 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('Server', () => {

describe('decorate()', () => {

it('decorates request', async () => {
it('decorates request with function', async () => {

const server = Hapi.server();

Expand All @@ -316,6 +316,25 @@ describe('Server', () => {
expect(res.result).to.match(/^.*\:.*\:.*\:.*\:.*$/);
});

it('decorates request with object', async () => {

const server = Hapi.server();

const customData = { id: '123' };

server.decorate('request', 'customData', customData);

server.route({
method: 'GET',
path: '/',
handler: (request) => request.customData
});

const res = await server.inject('/');
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal({ id: '123' });
});

it('decorates request (apply)', async () => {

const server = Hapi.server();
Expand Down Expand Up @@ -366,6 +385,26 @@ describe('Server', () => {
expect(res.result).to.match(/^.*\:.*\:.*\:.*\:.*!$/);
});

it('decorates request (extend) with an array', async () => {

const server = Hapi.server();

const items = ['one', 'two', 'three'];

server.decorate('request', 'items', items);
server.decorate('request', 'items', (existing) => [...existing, 'four'], { extend: true });

server.route({
method: 'GET',
path: '/',
handler: (request) => request.items
});

const res = await server.inject('/');
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal([...items, 'four']);
});

it('decorates request (apply + extend)', async () => {

const server = Hapi.server();
Expand Down Expand Up @@ -444,6 +483,25 @@ describe('Server', () => {
expect(res.result.status).to.equal('ok');
});

it('decorates toolkit with boolean', async () => {

const server = Hapi.server();

const isOk = true;

server.decorate('toolkit', 'isOk', isOk);

server.route({
method: 'GET',
path: '/',
handler: (request, h) => h.isOk
});

const res = await server.inject('/');
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal(true);
});

it('add new handler', async () => {

const test = {
Expand Down Expand Up @@ -559,6 +617,30 @@ describe('Server', () => {
expect(res.result).to.equal('ok');
});

it('decorates server with Map', async () => {

const server = Hapi.server();

const itemsMap = new Map();
itemsMap.set('one', 'One');
itemsMap.set('two', 'Two');
itemsMap.set('three', 'Three');

server.decorate('server', 'itemsMap', itemsMap);

server.route({
method: 'GET',
path: '/',
handler: (request) => request.server.itemsMap
});

const res = await server.inject('/');
expect(res.statusCode).to.equal(200);
expect(res.result.get('one')).to.equal('One');
expect(res.result.get('two')).to.equal('Two');
expect(res.result.get('three')).to.equal('Three');
});

it('throws on double server decoration', () => {

const server = Hapi.server();
Expand Down