func/templates/typescript/http
Lance Ball bfa5746442
chore: bump to buildpacks v0.8.3 for all versions (#402)
* chore: bump to buildpacks v0.8.2 for all versions

This is causing me to rethink using versions in these templates, and our
overall buildpack version/release strategy. But for now, we should land
this before 0.16.0

* adds trust for any quay.io/boson builder

Signed-off-by: Lance Ball <lball@redhat.com>
2021-06-23 12:44:42 -04:00
..
src feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
test feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
.builders.yaml chore: bump to buildpacks v0.8.3 for all versions (#402) 2021-06-23 12:44:42 -04:00
.eslintrc feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
.prettierrc feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
README.md feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
package-lock.json feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
package.json feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00
tsconfig.json feat: add typescript templates (#355) 2021-05-26 11:23:42 -04:00

README.md

TypeScript HTTP Function

Welcome to your new TypeScript function project! The boilerplate function code can be found in index.ts. This function will respond to incoming HTTP GET and POST requests.

Local execution

To run locally

npm install
npm run build
npm run local

The runtime will expose three endpoints.

  • / The endpoint for your function.
  • /health/readiness The endpoint for a readiness health check
  • /health/liveness The endpoint for a liveness health check

The parameter provided to the function endpoint at invocation is a Context object containing HTTP request information.

function handleRequest(context) {
  const log = context.log;
  log.info(context.httpVersion);
  log.info(context.method); // the HTTP request method (only GET or POST supported)
  log.info(context.query); // if query parameters are provided in a GET request
  log.info(context.body); // contains the request body for a POST request
  log.info(context.headers); // all HTTP headers sent with the event
}

The health checks can be accessed in your browser at http://localhost:8080/health/readiness and http://localhost:8080/health/liveness. You can use curl to POST an event to the function endpoint:

curl -X POST -d '{"hello": "world"}' \
  -H'Content-type: application/json' \
  http://localhost:8080

The readiness and liveness endpoints use overload-protection and will respond with HTTP 503 Service Unavailable with a Client-Retry header if your function is determined to be overloaded, based on the memory usage and event loop delay.

Testing

This function project includes a unit test and an integration test. Modify these, or add additional tests for your business logic.

npm test