Skip to content

Commit 202a6b0

Browse files
authored
feat: upgrade to body-parser 2.x (#572)
1 parent 82c1453 commit 202a6b0

File tree

9 files changed

+185
-66
lines changed

9 files changed

+185
-66
lines changed

lib/controllers/v1/computervision_controller.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const ComputervisionController = class ComputervisionController {
9696
if ( !req.userSession && !req.applicationSession ) {
9797
throw util.httpError( 401, "Unauthorized" );
9898
}
99-
const photoURL = req.query.image_url || req.body.image_url;
99+
const photoURL = req.query.image_url || req.body?.image_url;
100100
if ( !photoURL ) {
101101
throw util.httpError( 422, "No scorable photo" );
102102
}
@@ -164,7 +164,7 @@ const ComputervisionController = class ComputervisionController {
164164
if ( !req.userSession && !req.applicationSession ) {
165165
throw util.httpError( 401, "Unauthorized" );
166166
}
167-
if ( req.userSession?.isAdmin && req.body.image_url ) {
167+
if ( req.userSession?.isAdmin && req.body?.image_url ) {
168168
return ComputervisionController.scoreImageURL( req );
169169
}
170170
req.file = req.file || (
@@ -192,33 +192,33 @@ const ComputervisionController = class ComputervisionController {
192192
} );
193193
formData.append( "geomodel", "true" );
194194
formData.append( "format", "object" );
195-
if ( req.body.taxon_id ) {
195+
if ( req.body?.taxon_id ) {
196196
formData.append( "taxon_id", req.body.taxon_id );
197197
}
198-
if ( req.body.aggregated || req.query.aggregated ) {
198+
if ( req.body?.aggregated || req.query.aggregated ) {
199199
formData.append( "aggregated", "true" );
200200
}
201-
const testFeature = req.body.test_feature || req.query.test_feature;
201+
const testFeature = req.body?.test_feature || req.query.test_feature;
202202
if ( testFeature === "ancestor_unrestricted" ) {
203203
formData.append( "common_ancestor_rank_type", "unrestricted" );
204204
} else if ( testFeature === "ancestor_major_ranks" ) {
205205
formData.append( "common_ancestor_rank_type", "major" );
206206
}
207-
if ( !req.body.skip_frequencies && req.body.lat && req.body.lng ) {
207+
if ( !req.body?.skip_frequencies && req.body?.lat && req.body?.lng ) {
208208
formData.append( "lat", req.body.lat );
209209
formData.append( "lng", req.body.lng );
210210
}
211211

212-
if ( req.body.include_representative_photos
212+
if ( req.body?.include_representative_photos
213213
|| req.query.include_representative_photos
214214
) {
215215
formData.append( "return_embedding", "true" );
216216
}
217217

218-
if ( req.body.human_exclusion
218+
if ( req.body?.human_exclusion
219219
|| req.query.human_exclusion
220220
) {
221-
formData.append( "human_exclusion", req.body.human_exclusion || req.query.human_exclusion );
221+
formData.append( "human_exclusion", req.body?.human_exclusion || req.query.human_exclusion );
222222
}
223223

224224
const requestAbortController = new AbortController( );
@@ -354,7 +354,7 @@ const ComputervisionController = class ComputervisionController {
354354
s.frequency_score = s.geo_score >= s.geo_threshold ? 1 : 0;
355355
s.taxon_id = s.id;
356356
delete s.id;
357-
if ( !( req.body.aggregated || req.query.aggregated ) ) {
357+
if ( !( req.body?.aggregated || req.query.aggregated ) ) {
358358
s.original_geo_score = s.geo_score;
359359
s.original_combined_score = s.combined_score;
360360
delete s.geo_threshold;
@@ -366,13 +366,13 @@ const ComputervisionController = class ComputervisionController {
366366
await ESModel.fetchBelongsTo( withTaxa, Taxon, taxonOpts );
367367
await Taxon.preloadIntoTaxonPhotos( _.map( _.filter( withTaxa, "taxon" ), "taxon" ), { localeOpts } );
368368

369-
if ( !( req.body.aggregated || req.query.aggregated ) ) {
369+
if ( !( req.body?.aggregated || req.query.aggregated ) ) {
370370
_.each( withTaxa, s => {
371371
delete s.taxon_id;
372372
} );
373373
}
374374

375-
if ( req.body.include_representative_photos
375+
if ( req.body?.include_representative_photos
376376
|| req.query.include_representative_photos
377377
) {
378378
await ComputervisionController.addRepresentativePhotos(

lib/controllers/v1/taxa_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ TaxaController.suggestifyRequest = async req => {
917917
req.suggestParams = _.cloneDeep( req.query );
918918
// For a POST request, all the relevant params may be in the POST body instead
919919
// of the query
920-
if ( _.isEmpty( req.suggestParams ) ) {
920+
if ( _.isEmpty( req.suggestParams ) && !_.isEmpty( req.body ) ) {
921921
req.suggestParams = _.cloneDeep( req.body );
922922
delete req.suggestParams.image;
923923
}

lib/controllers/v2/identifications_controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const destroy = async req => {
2929
delete req.params.uuid;
3030
// Override the crazy thing in the Rails API that just withdraws when you
3131
// try to delete. DELETE should really delete.
32+
req.body ||= { };
3233
req.body.delete = true;
3334
return ctrlv1.delete( req );
3435
};

lib/controllers/v2/log_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const create = async req => {
2-
if ( req.body.level === "error" ) {
2+
if ( req.body?.level === "error" ) {
33
req._logClientError = true;
44
req.body.error_message = req.body.message;
55
} else {

lib/controllers/v2/observations_controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ const taxonSummary = async req => {
7878
}
7979
delete req.params.uuid;
8080
delete req.query.fields;
81-
delete req.body.fields;
81+
if ( !_.isEmpty( req.body ) ) {
82+
delete req.body.fields;
83+
}
8284
const r = await InaturalistAPI.iNatJSWrap( inatjs.observations.taxonSummary, req );
8385
// Prune the Rails response to match the API schema
8486
// TODO make a re-usable method that coerces an object something that fits a Joi schema

lib/inaturalist_api_v2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ const InaturalistAPIV2 = class InaturalistAPIV2 {
390390

391391
// Extract the requested response fields
392392
static requestFields( req ) {
393-
const fields = req.body.fields || req.query.fields;
393+
const fields = req.body?.fields || req.query.fields;
394394
// fields may already be an object, if so return
395395
if ( _.isObject( fields ) ) {
396396
return fields;

lib/logstasher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const Logstasher = class Logstasher {
154154
return;
155155
}
156156

157-
_.each( req.body.extra, ( v, k ) => {
157+
_.each( req.body?.extra, ( v, k ) => {
158158
if ( !_.isObject( v ) ) {
159159
payload.extra ||= { };
160160
payload.extra[k] = v;

0 commit comments

Comments
 (0)