Skip to content

Commit

Permalink
feat: add decorator type property (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthyk authored Jan 26, 2024
1 parent 140b0ee commit caab0a1
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 26 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface OverviewStructureSource {

interface OverviewStructureDecorator {
name: string;
type: 'undefined' | 'object' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol' | 'function' | 'array'
source?: OverviewStructureSource,
}

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function wrapFastify (instance, pluginOpts) {
function wrapDecorator (instance, type, { addSource }) {
const originalDecorate = instance[type]
instance[type] = function wrapDecorate (name, value) {
const decoratorNode = getDecoratorNode(name)
const decoratorNode = getDecoratorNode(name, value)
if (addSource) {
decoratorNode.source = getSource()[0]
}
Expand Down
5 changes: 3 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ function getHookNode (hookFunction) {
}
}

function getDecoratorNode (decoratorName) {
function getDecoratorNode (decoratorName, decoratedValue) {
return {
name: decoratorName
name: decoratorName,
type: Array.isArray(decoratedValue) ? 'array' : typeof decoratedValue
}
}

Expand Down
25 changes: 16 additions & 9 deletions test/decorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ test('decorator', async t => {
app.decorateRequest('root-req-two', function () {})
app.decorateReply('root-reply', function () {})

app.decorate('root-symbol', Symbol('testSymbol'))
app.decorateReply('root-reply-array', [])
app.decorateRequest('root-req-boolean', true)

app.register(function register1 (instance, opts, next) {
instance.decorateRequest('child-1', 42)
instance.decorate('child-1-bigint', BigInt(1))
instance.decorate('child-1-undefined')
instance.register(function register2 (sub, opts, next) {
sub.decorateReply('sub', 42)
next()
})
instance.register(function register3 (sub, opts, next) {
sub.decorate('sub-instance', 50)
sub.decorateReply('sub', 50)
sub.decorateRequest('sub-object', {})
next()
})
next()
Expand All @@ -36,23 +43,23 @@ test('decorator', async t => {
t.equal(root.children.length, 2)
t.equal(root.children[0].name, 'register1')
t.equal(root.children[1].name, 'sibling')
t.same(root.decorators.decorate, [{ name: 'root-func' }])
t.same(root.decorators.decorateRequest, [{ name: 'root-req' }, { name: 'root-req-two' }])
t.same(root.decorators.decorateReply, [{ name: 'root-reply' }])
t.same(root.decorators.decorate, [{ name: 'root-func', type: 'function' }, { name: 'root-symbol', type: 'symbol' }])
t.same(root.decorators.decorateRequest, [{ name: 'root-req', type: 'function' }, { name: 'root-req-two', type: 'function' }, { name: 'root-req-boolean', type: 'boolean' }])
t.same(root.decorators.decorateReply, [{ name: 'root-reply', type: 'function' }, { name: 'root-reply-array', type: 'array' }])

const reg1 = root.children[0]
t.same(reg1.decorators.decorate, [])
t.same(reg1.decorators.decorateRequest, [{ name: 'child-1' }])
t.same(reg1.decorators.decorate, [{ name: 'child-1-bigint', type: 'bigint' }, { name: 'child-1-undefined', type: 'undefined' }])
t.same(reg1.decorators.decorateRequest, [{ name: 'child-1', type: 'number' }])
t.same(reg1.decorators.decorateReply, [])
t.equal(reg1.children.length, 2)

const reg2 = reg1.children[0]
t.same(reg2.decorators.decorate, [])
t.same(reg2.decorators.decorateRequest, [])
t.same(reg2.decorators.decorateReply, [{ name: 'sub' }])
t.same(reg2.decorators.decorateReply, [{ name: 'sub', type: 'number' }])

const reg3 = reg1.children[1]
t.same(reg3.decorators.decorate, [{ name: 'sub-instance' }])
t.same(reg3.decorators.decorateRequest, [])
t.same(reg3.decorators.decorateReply, [{ name: 'sub' }])
t.same(reg3.decorators.decorate, [{ name: 'sub-instance', type: 'number' }])
t.same(reg3.decorators.decorateRequest, [{ name: 'sub-object', type: 'object' }])
t.same(reg3.decorators.decorateReply, [{ name: 'sub', type: 'number' }])
})
11 changes: 7 additions & 4 deletions test/fixture/app-no-source.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,20 @@
"decorators": {
"decorate": [
{
"name": "decorApp"
"name": "decorApp",
"type": "string"
}
],
"decorateRequest": [
{
"name": "decorApp"
"name": "decorApp",
"type": "string"
}
],
"decorateReply": [
{
"name": "decorApp"
"name": "decorApp",
"type": "string"
}
]
},
Expand All @@ -325,4 +328,4 @@
"onRoute": [],
"onRegister": []
}
}
}
5 changes: 4 additions & 1 deletion test/fixture/app-source.json
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@
"decorate": [
{
"name": "decorApp",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/app.js",
Expand All @@ -474,6 +475,7 @@
"decorateRequest": [
{
"name": "decorApp",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/app.js",
Expand All @@ -489,6 +491,7 @@
"decorateReply": [
{
"name": "decorApp",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/app.js",
Expand Down Expand Up @@ -535,4 +538,4 @@
"onRoute": [],
"onRegister": []
}
}
}
11 changes: 10 additions & 1 deletion test/fixture/autoload.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"decorate": [
{
"name": "fileTwo",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/file-two.js",
Expand All @@ -88,6 +89,7 @@
"decorateRequest": [
{
"name": "fileTwo",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/file-two.js",
Expand All @@ -103,6 +105,7 @@
"decorateReply": [
{
"name": "fileTwo",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/file-two.js",
Expand Down Expand Up @@ -224,6 +227,7 @@
"decorate": [
{
"name": "fileOne",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/autoload/file-one.js",
Expand All @@ -239,6 +243,7 @@
"decorateRequest": [
{
"name": "fileOne",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/autoload/file-one.js",
Expand All @@ -254,6 +259,7 @@
"decorateReply": [
{
"name": "fileOne",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/autoload/file-one.js",
Expand Down Expand Up @@ -969,6 +975,7 @@
"decorate": [
{
"name": "decorApp",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/autoload.js",
Expand All @@ -984,6 +991,7 @@
"decorateRequest": [
{
"name": "decorApp",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/autoload.js",
Expand All @@ -999,6 +1007,7 @@
"decorateReply": [
{
"name": "decorApp",
"type": "string",
"source": {
"stackIndex": 0,
"fileName": "test/sources/autoload.js",
Expand Down Expand Up @@ -1045,4 +1054,4 @@
"onRoute": [],
"onRegister": []
}
}
}
16 changes: 8 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ test('basic test', async t => {
t.type(structure.id, 'number')
t.equal(structure.children.length, 3)
t.same(structure.decorators.decorate, [
{ name: 'test' },
{ name: 'testObject' },
{ name: 'testArray' }
{ name: 'test', type: 'function' },
{ name: 'testObject', type: 'object' },
{ name: 'testArray', type: 'array' }
])
t.same(structure.hooks, require('./fixture/index.00.json'))
})
Expand Down Expand Up @@ -76,7 +76,7 @@ test('register', async t => {

t.equal(root.children.length, 1)
t.equal(root.children[0].name, 'register1')
t.same(root.decorators.decorate, [{ name: 'foo-bar' }])
t.same(root.decorators.decorate, [{ name: 'foo-bar', type: 'function' }])
t.equal(root.hooks.onRequest.length, 0)
t.equal(root.hooks.preParsing.length, 0)
t.equal(root.hooks.preValidation.length, 0)
Expand Down Expand Up @@ -121,11 +121,11 @@ test('hide empty', async t => {

t.strictSame(structure.decorators, {
decorate: [
{ name: 'emptyObject' },
{ name: 'emptyArray' }
{ name: 'emptyObject', type: 'object' },
{ name: 'emptyArray', type: 'array' }
],
decorateRequest: [
{ name: 'oneReqDecor' }
{ name: 'oneReqDecor', type: 'array' }
]
})

Expand All @@ -145,7 +145,7 @@ test('hide empty', async t => {

t.strictSame(structure.children[1].children[0].decorators, {
decorateReply: [
{ name: 'oneRep' }
{ name: 'oneRep', type: 'object' }
]
})
})
Expand Down

0 comments on commit caab0a1

Please sign in to comment.