1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-06 15:46:57 +01:00
Files
openshift-docs/modules/serverless-typescript-context-object-reference.adoc
Maxim Svistunov 8eb88306c8 Add docs on developing TypeScript functions
Fix section ID

Move TypeScript context object reference to the proper guide

Fix function header

Co-authored-by: Lance Ball <lball@redhat.com>

Fix another function header

Co-authored-by: Lance Ball <lball@redhat.com>

Improve wording

Co-authored-by: Lance Ball <lball@redhat.com>

Fix yet another function header

Co-authored-by: Lance Ball <lball@redhat.com>

Fix broken list

Add the step of installing dependencies

Substitute curl commands with kn func emit commands

Improve kn func emit examples

Several stylistic improvements

Make paragraph more precise

Co-authored-by: jrangelramos <jrangelramos@gmail.com>

Add a comma

Use updated TypeScript code examples

Co-authored-by: Lance Ball <lball@redhat.com>

Improvements to TypeScript code

Improve TypeScript code

Co-authored-by: jrangelramos <jrangelramos@gmail.com>
2021-09-01 14:42:20 +02:00

150 lines
4.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
[id="serverless-typescript-context-object-reference_{context}"]
= TypeScript context object reference
The `context` object has several properties that can be accessed by the function developer.
[id="serverless-typescript-context-object-reference-log_{context}"]
== log
Provides a logging object that can be used to write output to the cluster logs. The log adheres to the link:https://getpino.io/#/docs/api[Pino logging API].
.Example log
[source,javascript]
----
export function handle(context: Context): string {
// log the incoming request body's 'hello' parameter
if (context.body) {
context.log.info((context.body as Record<string, string>).hello);
} else {
context.log.info('No data received');
}
return 'OK';
}
----
You can access the function by using the `kn func emit` command to invoke it:
.Example command
[source,terminal]
----
$ kn func emit --sink 'http://example.function.com'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
----
You can change the log level to one of `fatal`, `error`, `warn`, `info`, `debug`, `trace`, or `silent`. To do that, change the value of `logLevel` by assigning one of these values to the environment variable `FUNC_LOG_LEVEL` using the `config` command.
[id="serverless-typescript-context-object-reference-query_{context}"]
== query
Returns the query string for the request, if any, as key-value pairs. These attributes are also found on the context object itself.
.Example query
[source,javascript]
----
export function handle(context: Context): string {
// log the 'name' query parameter
if (context.query) {
context.log.info((context.query as Record<string, string>).name);
} else {
context.log.info('No data received');
}
return 'OK';
}
----
You can access the function by using the `kn func emit` command to invoke it:
.Example command
[source,terminal]
----
$ kn func emit --sink 'http://example.function.com' --data '{"name": "tiger"}'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
----
[id="serverless-typescript-context-object-reference-body_{context}"]
== body
Returns the request body, if any. If the request body contains JSON code, this will be parsed so that the attributes are directly available.
.Example body
[source,javascript]
----
export function handle(context: Context): string {
// log the incoming request body's 'hello' parameter
if (context.body) {
context.log.info((context.body as Record<string, string>).hello);
} else {
context.log.info('No data received');
}
return 'OK';
}
----
You can access the function by using the `kn func emit` command to invoke it:
.Example command
[source,terminal]
----
$ kn func emit --sink 'http://example.function.com' --data '{"hello": "world"}'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
----
[id="serverless-typescript-context-object-reference-headers_{context}"]
== headers
Returns the HTTP request headers as an object.
.Example header
[source,javascript]
----
export function handle(context: Context): string {
// log the incoming request body's 'hello' parameter
if (context.body) {
context.log.info((context.headers as Record<string, string>)['custom-header']);
} else {
context.log.info('No data received');
}
return 'OK';
}
----
You can access the function by using the `curl` command to invoke it:
.Example command
[source,terminal]
----
$ curl -H'x-custom-header: some-value' http://example.function.com
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
----
[id="serverless-typescript-context-object-reference-http-requests_{context}"]
== HTTP requests
method:: Returns the HTTP request method as a string.
httpVersion:: Returns the HTTP version as a string.
httpVersionMajor:: Returns the HTTP major version number as a string.
httpVersionMinor:: Returns the HTTP minor version number as a string.