@valora/http-handler
v1.0.1
Published
Handle logging and errors in your Google Cloud Function HTTP endpoint implementations.
Downloads
4,495
Keywords
Readme
@valora/http-handler
Handle logging and errors in your Google Cloud Function HTTP endpoint implementations.
Development
Install dependencies:
yarnRun tests:
yarn testUsing
Add @valora/http-handler:
yarn add @valora/http-handlerDefine your HttpFunction:
import {
HttpFunction,
Request,
Response,
} from '@google-cloud/functions-framework/build/src/functions'
import { createLogger } from '@valora/logging'
import { asyncHandler, HttpError } from '@valora/http-handler'
// Any Bunyan logger
const logger = createLogger()
const requestHandler: HttpFunction = async (req: Request, res: Response) => {
if (!('greeting' in req.query)) {
// asyncHandler will handle HttpErrors for you.
throw new HttpError(400, 'Missing greeting', { code: 'NO_GREETING' })
}
res.status(200).send({ message: 'hello' })
}
const export helloCloudFunctionHandler = asyncHandler(requestHandler, logger)