Skip to content

Commit

Permalink
feat(wabe): throw error when hook priority <= 0 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl authored Sep 1, 2024
1 parent 5befc33 commit 5d3d952
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/wabe/src/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getPort from 'get-port'
import { Wabe } from '.'
import { DatabaseEnum } from '../database'
import { Schema } from '../schema'
import { OperationType } from '../hooks'

describe('Server', () => {
it('should run server', async () => {
Expand Down Expand Up @@ -37,6 +38,68 @@ describe('Server', () => {
await wabe.close()
})

it('should throw an error if hook has negative value', async () => {
const databaseId = uuid()

const port = await getPort()
expect(
() =>
new Wabe({
rootKey:
'eIUbb9abFa8PJGRfRwgiGSCU0fGnLErph2QYjigDRjLsbyNA3fZJ8Npd0FJNzxAc',
database: {
type: DatabaseEnum.Mongo,
url: 'mongodb://127.0.0.1:27045',
name: databaseId,
},
port,
hooks: [
{
operationType: OperationType.BeforeCreate,
callback: () => {},
priority: -1,
},
],
}),
).toThrow('Hook priority <= 0 is reserved for internal uses')

expect(
() =>
new Wabe({
rootKey:
'eIUbb9abFa8PJGRfRwgiGSCU0fGnLErph2QYjigDRjLsbyNA3fZJ8Npd0FJNzxAc',
database: {
type: DatabaseEnum.Mongo,
url: 'mongodb://127.0.0.1:27045',
name: databaseId,
},
port,
hooks: [],
}),
).not.toThrow()

expect(
() =>
new Wabe({
rootKey:
'eIUbb9abFa8PJGRfRwgiGSCU0fGnLErph2QYjigDRjLsbyNA3fZJ8Npd0FJNzxAc',
database: {
type: DatabaseEnum.Mongo,
url: 'mongodb://127.0.0.1:27045',
name: databaseId,
},
port,
hooks: [
{
operationType: OperationType.BeforeCreate,
callback: () => {},
priority: 1,
},
],
}),
).not.toThrow()
})

it('should run server without schema object', async () => {
const databaseId = uuid()

Expand Down
3 changes: 3 additions & 0 deletions packages/wabe/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class Wabe<T extends WabeTypes> {
}

loadHooks() {
if (this.config.hooks?.find((hook) => hook.priority <= 0))
throw new Error('Hook priority <= 0 is reserved for internal uses')

this.config.hooks = [...getDefaultHooks(), ...(this.config.hooks || [])]
}

Expand Down

0 comments on commit 5d3d952

Please sign in to comment.