@pbvision/fastify-firestore-service 0.0.24 → 0.0.26
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/package.json +2 -2
- package/src/api/exception.js +41 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbvision/fastify-firestore-service",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.26",
|
|
4
4
|
"description": "Web Framework using Fastify and Firestore ORM",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@fastify/static": "^6.12",
|
|
43
43
|
"@fastify/swagger": "^8.1.0",
|
|
44
44
|
"@fastify/swagger-ui": "^2.0.1",
|
|
45
|
-
"@pbvision/firestore-orm": "^0.0.
|
|
45
|
+
"@pbvision/firestore-orm": "^0.0.11",
|
|
46
46
|
"@sentry/node": "^7.91.0",
|
|
47
47
|
"fastify": "^4.10.0",
|
|
48
48
|
"fastify-plugin": "^4.5.1",
|
package/src/api/exception.js
CHANGED
|
@@ -56,12 +56,13 @@ class __RequestDone extends Error {
|
|
|
56
56
|
* @param {Integer} code An optional code to override exception's default
|
|
57
57
|
* STATUS
|
|
58
58
|
*/
|
|
59
|
-
constructor (message, data =
|
|
59
|
+
constructor (message, data = undefined, code = undefined) {
|
|
60
60
|
super(message)
|
|
61
61
|
this.httpCode = code ?? this.constructor.STATUS
|
|
62
62
|
assert(this.httpCode !== undefined, 'Status must be defined')
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
if (this.constructor.SCHEMA !== undefined) {
|
|
64
|
+
this.constructor.schemaValidator?.(data)
|
|
65
|
+
}
|
|
65
66
|
this.data = data
|
|
66
67
|
}
|
|
67
68
|
}
|
|
@@ -73,7 +74,21 @@ class __RequestDone extends Error {
|
|
|
73
74
|
*/
|
|
74
75
|
class RequestDone extends __RequestDone {
|
|
75
76
|
static STATUS = 200
|
|
76
|
-
static SCHEMA = S.obj()
|
|
77
|
+
static SCHEMA = S.obj() // optional
|
|
78
|
+
|
|
79
|
+
static get schemaValidator () {
|
|
80
|
+
const orig = super.schemaValidator
|
|
81
|
+
// istanbul ignore else
|
|
82
|
+
if (this.SCHEMA === RequestDone.SCHEMA) {
|
|
83
|
+
return data => {
|
|
84
|
+
if (data !== undefined) {
|
|
85
|
+
orig(data)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// istanbul ignore next
|
|
90
|
+
return orig
|
|
91
|
+
}
|
|
77
92
|
|
|
78
93
|
/**
|
|
79
94
|
* Return data to return to caller.
|
|
@@ -87,7 +102,7 @@ class RequestDone extends __RequestDone {
|
|
|
87
102
|
* @param {Object} data Data to return to caller
|
|
88
103
|
* @param {Integer} code A status to override default status.
|
|
89
104
|
*/
|
|
90
|
-
constructor (data, code = undefined) {
|
|
105
|
+
constructor (data = undefined, code = undefined) {
|
|
91
106
|
super(undefined, data, code)
|
|
92
107
|
assert(this.httpCode < 300, 'Status code must be less than 300')
|
|
93
108
|
}
|
|
@@ -104,6 +119,11 @@ class RequestDone extends __RequestDone {
|
|
|
104
119
|
*/
|
|
105
120
|
class RequestOkay extends RequestDone {
|
|
106
121
|
static STATUS = 200
|
|
122
|
+
// request okay response will be verified like a regular response
|
|
123
|
+
static SCHEMA = undefined
|
|
124
|
+
constructor (data = undefined) {
|
|
125
|
+
super(data, 200)
|
|
126
|
+
}
|
|
107
127
|
}
|
|
108
128
|
|
|
109
129
|
/**
|
|
@@ -120,9 +140,22 @@ class RequestOkay extends RequestDone {
|
|
|
120
140
|
* @see RequestDone
|
|
121
141
|
*/
|
|
122
142
|
class RequestError extends __RequestDone {
|
|
123
|
-
static SCHEMA = S.obj()
|
|
143
|
+
static SCHEMA = S.obj() // optional
|
|
144
|
+
|
|
145
|
+
static get schemaValidator () {
|
|
146
|
+
const orig = super.schemaValidator
|
|
147
|
+
// istanbul ignore else
|
|
148
|
+
if (this.SCHEMA === RequestError.SCHEMA) {
|
|
149
|
+
return data => {
|
|
150
|
+
if (data !== undefined) {
|
|
151
|
+
orig(data)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return orig
|
|
156
|
+
}
|
|
124
157
|
|
|
125
|
-
constructor (message, data =
|
|
158
|
+
constructor (message, data = undefined, code = undefined) {
|
|
126
159
|
super(message, data, code)
|
|
127
160
|
assert(this.httpCode >= 300, 'Status code must be at least 300')
|
|
128
161
|
}
|
|
@@ -134,7 +167,7 @@ class RequestError extends __RequestDone {
|
|
|
134
167
|
return S.obj({
|
|
135
168
|
code: S.str,
|
|
136
169
|
message: S.str.optional(),
|
|
137
|
-
data: S.obj() // Data is validated in constructor, don't validate again.
|
|
170
|
+
data: S.obj().optional() // Data is validated in constructor, don't validate again.
|
|
138
171
|
})
|
|
139
172
|
}
|
|
140
173
|
|