-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from BaseAdresseNationale/antoineludeau/clean…
…-api-response Improved api response format and homogeneity
- Loading branch information
Showing
9 changed files
with
224 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,26 @@ | ||
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import | ||
import express from 'express' | ||
import analyticsMiddleware from '../../middleware/analytics.js' | ||
|
||
import {handleAPIResponse} from '../helper.js' | ||
import {getUuids, uncollidUuids} from './helpers.js' | ||
|
||
const app = new express.Router() | ||
|
||
app.get('/', analyticsMiddleware, async (req, res) => { | ||
let response | ||
try { | ||
const length = Number(req.query.quantity) || 1 | ||
if (length > 100_000) { | ||
res.status(400).send({ | ||
date: new Date(), | ||
status: 'error', | ||
message: 'Quantity must be less than 100 000', | ||
response: {}, | ||
}) | ||
handleAPIResponse(res, 400, 'Quantity must be less than 100 000', {}) | ||
return | ||
} | ||
|
||
const ids = await uncollidUuids(getUuids(length)) | ||
|
||
response = { | ||
date: new Date(), | ||
status: 'success', | ||
response: ids, | ||
} | ||
handleAPIResponse(res, 200, 'Successfully generated UUIDs', ids) | ||
} catch (error) { | ||
const {message} = error | ||
response = { | ||
date: new Date(), | ||
status: 'error', | ||
message, | ||
response: {}, | ||
} | ||
console.error(error) | ||
handleAPIResponse(res, 500, 'Internal server error', {}) | ||
} | ||
|
||
res.send(response) | ||
}) | ||
|
||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.