@pbvision/fastify-firestore-service 0.0.25 → 0.0.27
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 +1 -1
- package/src/api/api.js +13 -11
- package/src/api/exception.js +41 -8
package/package.json
CHANGED
package/src/api/api.js
CHANGED
|
@@ -341,7 +341,7 @@ class API {
|
|
|
341
341
|
* @protected
|
|
342
342
|
*/
|
|
343
343
|
async callAPI ({
|
|
344
|
-
method = 'POST', headers = {}, url, body, qsParams
|
|
344
|
+
method = 'POST', headers = {}, url, body, qsParams, ignoreRespBody = false
|
|
345
345
|
}) {
|
|
346
346
|
headers = {
|
|
347
347
|
...headers,
|
|
@@ -366,17 +366,19 @@ class API {
|
|
|
366
366
|
code: resp.status,
|
|
367
367
|
isOk: resp.status >= 200 && resp.status <= 299
|
|
368
368
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
369
|
+
if (!ignoreRespBody) {
|
|
370
|
+
const respBody = await resp.text()
|
|
371
|
+
if (respBody) {
|
|
372
|
+
if (resp.headers.get('content-type')?.startsWith('application/json')) {
|
|
373
|
+
try {
|
|
374
|
+
ret.data = JSON.parse(respBody)
|
|
375
|
+
} catch (e) {
|
|
376
|
+
// istanbul ignore next
|
|
377
|
+
throw new Error(`JSON.parse failed on ${respBody} with reason ${e}.`)
|
|
378
|
+
}
|
|
379
|
+
} else {
|
|
380
|
+
ret.data = respBody
|
|
377
381
|
}
|
|
378
|
-
} else {
|
|
379
|
-
ret.data = respBody
|
|
380
382
|
}
|
|
381
383
|
}
|
|
382
384
|
return ret
|
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
|
|