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
// idea 1 - use JOI (or similar) at the router level | |
let params = { | |
query: { | |
limit: Joi.number().integer().min(1).max(100).default(10) | |
} | |
} | |
server.get("/horse", enforceParams(params), controller.myRoute) | |
// idea 2 - use JOI (or similar) at the controller level | |
async function myRoute(req, res) { | |
let params = { | |
query: { | |
accountId: Joi.string(), | |
limit: Joi.number().integer().min(1).max(100).default(10) | |
} | |
} | |
enforceParams(req, params) | |
/* ... other stuff ... */ | |
} | |
// idea 3 - manually using a custom Error object | |
async function myRoute(req, res) { | |
if (!req.params.accountId) { | |
throw new MissingParamError("Missing required accountId") | |
} | |
if (typeof req.query.limit !== 'number') { | |
throw new MissingParamError("Expected limit to be a number") | |
} | |
/* ... other stuff ... */ | |
} | |
// idea 4 - decorators | |
@params({ | |
query: { | |
accountId: Joi.string(), | |
limit: Joi.number().integer().min(1).max(100).default(10) | |
} | |
}) | |
function myRoute(req, res) {} | |
// idea 5 - use flow, but then transpile with a plugin to convert to somethign like 3? | |
async function myRoute(req, res) { | |
let params: { limit?: number, accountId: string } = req.params | |
} | |
// converts with a babel plugin to | |
async function myRoute(req, res) { | |
let params = req.params | |
if (typeof params.accountId !== 'string') { | |
throw new MissingParamError("accountId must be a string") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment