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 1 commit
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: 6 additions & 1 deletion lib/types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ export interface HandlerDecorationMethod {
}

/**
* The general case for decorators added via server.decorate.
* The general case for decorator values added via server.decorate.
*/
export type DecorationValue<T> = DecorationMethod<T> | any;

/**
* Decorator function.
*/
export type DecorationMethod<T> = (this: T, ...args: any[]) => any;

Expand Down
16 changes: 8 additions & 8 deletions lib/types/server/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ServerRegisterPluginObject,
ServerRegisterPluginObjectArray,
DecorateName,
DecorationMethod,
DecorationValue,
HandlerDecorationMethod,
PluginProperties
} from '../plugin';
Expand Down Expand Up @@ -310,13 +310,13 @@ export class Server<A = ServerApplicationState> {
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoratetype-property-method-options)
*/
decorate(type: 'handler', property: DecorateName, method: HandlerDecorationMethod, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
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: '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: '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: 'request', property: DecorateName, method: (existing: ((...args: any[]) => any)) => (request: Request) => DecorationValue<Request>, options: {apply: true, extend: true}): void;
decorate(type: 'request', property: DecorateName, method: (request: Request) => DecorationValue<Request>, options: {apply: true, extend?: boolean | undefined}): void;
decorate(type: 'request', property: DecorateName, method: DecorationValue<Request>, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'toolkit', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationValue<ResponseToolkit>, options: {apply?: boolean | undefined, extend: true}): void;
decorate(type: 'toolkit', property: DecorateName, method: DecorationValue<ResponseToolkit>, options?: {apply?: boolean | undefined, extend?: boolean | undefined}): void;
decorate(type: 'server', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationValue<Server>, options: {apply?: boolean | undefined, extend: true}): void;
decorate(type: 'server', property: DecorateName, method: DecorationValue<Server>, 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