@platformatic/sql-events 0.44.0 → 0.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/errors.js ADDED
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+
3
+ const createError = require('@fastify/error')
4
+
5
+ const ERROR_PREFIX = 'PLT_SQL_EVENTS'
6
+
7
+ module.exports = {
8
+ ObjectRequiredUnderTheDataProperty: createError(`${ERROR_PREFIX}_OBJECT_IS_REQUIRED_UNDER_THE_DATA_PROPERTY`, 'The object that will be published is required under the data property'),
9
+ PrimaryKeyIsNecessaryInsideData: createError(`${ERROR_PREFIX}_PRIMARY_KEY_IS_NECESSARY_INSIDE_DATA`, 'The primaryKey is necessary inside data'),
10
+ NoSuchActionError: createError(`${ERROR_PREFIX}_NO_SUCH_ACTION`, 'No such action %s')
11
+
12
+ }
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { FastifyInstance } from 'fastify'
2
2
  import { Readable } from 'stream'
3
3
  import { SQLMapperPluginInterface, Entities } from '@platformatic/sql-mapper'
4
+ import { FastifyError } from '@fastify/error'
4
5
 
5
6
  export interface SQLEventsPluginInterface {
6
7
  subscribe: (topic: string | string[]) => Promise<Readable>
@@ -20,3 +21,12 @@ export interface SQLEventsPluginOptions<T extends Entities> {
20
21
  export default function plugin<T extends Entities>(app: FastifyInstance, options: SQLEventsPluginOptions<T>): Promise<SQLEventsPluginInterface>
21
22
 
22
23
  export function setupEmitter<T extends Entities>(options: SQLEventsPluginOptions<T>): void
24
+
25
+ /**
26
+ * All the errors thrown by the plugin.
27
+ */
28
+ export module errors {
29
+ export const ObjectRequiredUnderTheDataProperty: () => FastifyError
30
+ export const PrimaryKeyIsNecessaryInsideData: () => FastifyError
31
+ export const NoSuchActionError: (actions: string) => FastifyError
32
+ }
package/index.js CHANGED
@@ -6,6 +6,7 @@ const camelcase = require('camelcase')
6
6
  const { PassThrough } = require('stream')
7
7
  const MQEmitterRedis = require('mqemitter-redis')
8
8
  const { promisify } = require('util')
9
+ const errors = require('./errors')
9
10
 
10
11
  async function fastifySqlEvents (app, opts) {
11
12
  setupEmitter({ ...opts, mapper: app.platformatic, log: app.log })
@@ -83,11 +84,11 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
83
84
  // getPublishTopic is async because it could be overridden to be asynchronous
84
85
  entity.getPublishTopic = async function ({ action, data }) {
85
86
  if (!data) {
86
- throw new Error('The object that will be published is required under the data property')
87
+ throw new errors.ObjectRequiredUnderTheDataProperty()
87
88
  }
88
89
 
89
90
  if (!data[primaryKey]) {
90
- throw new Error('The primaryKey is necessary inside data')
91
+ throw new errors.PrimaryKeyIsNecessaryInsideData()
91
92
  }
92
93
 
93
94
  switch (action) {
@@ -108,7 +109,7 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
108
109
  case 'delete':
109
110
  return `/entity/${entityName}/delete/+`
110
111
  default:
111
- throw new Error(`no such action ${action}`)
112
+ throw new errors.NoSuchActionError(action)
112
113
  }
113
114
  }
114
115
  }
@@ -135,3 +136,4 @@ function noop () {}
135
136
 
136
137
  module.exports = fp(fastifySqlEvents)
137
138
  module.exports.setupEmitter = setupEmitter
139
+ module.exports.errors = errors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-events",
3
- "version": "0.44.0",
3
+ "version": "0.45.0",
4
4
  "description": "Emit events via MQEmitter",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -20,9 +20,10 @@
20
20
  "standard": "^17.1.0",
21
21
  "tap": "^16.3.6",
22
22
  "tsd": "^0.29.0",
23
- "@platformatic/sql-mapper": "0.44.0"
23
+ "@platformatic/sql-mapper": "0.45.0"
24
24
  },
25
25
  "dependencies": {
26
+ "@fastify/error": "^3.2.1",
26
27
  "camelcase": "^6.3.0",
27
28
  "fastify-plugin": "^4.5.0",
28
29
  "mqemitter": "^5.0.0",
@@ -1,9 +1,10 @@
1
1
  import { expectType } from 'tsd'
2
2
  import { fastify, FastifyInstance } from 'fastify'
3
- import plugin, { setupEmitter } from '../../index'
3
+ import plugin, { setupEmitter, errors } from '../../index'
4
4
  import { Readable } from 'stream'
5
5
  import { SQLMapperPluginInterface, Entities } from '@platformatic/sql-mapper'
6
6
  import { SQLEventsPluginInterface } from '../../index'
7
+ import { FastifyError } from '@fastify/error'
7
8
 
8
9
  declare module 'fastify' {
9
10
  interface FastifyInstance {
@@ -22,3 +23,11 @@ instance.register(async (instance) => {
22
23
  setupEmitter({ mapper: {} as SQLMapperPluginInterface<Entities> })
23
24
  setupEmitter({ mapper: {} as SQLMapperPluginInterface<Entities>, connectionString: 'redis://localhost:6379' })
24
25
  setupEmitter({ mapper: {} as SQLMapperPluginInterface<Entities>, mq: {} })
26
+
27
+ // Errors
28
+ type ErrorWithNoParams = () => FastifyError
29
+ type ErrorWithOneParam = (param: string) => FastifyError
30
+
31
+ expectType<ErrorWithOneParam>(errors.NoSuchActionError)
32
+ expectType<ErrorWithNoParams>(errors.ObjectRequiredUnderTheDataProperty)
33
+ expectType<ErrorWithNoParams>(errors.PrimaryKeyIsNecessaryInsideData)