11.5.3. TypeScript 함수 호출 정보

Knative(kn) CLI를 사용하여 함수 프로젝트를 생성할 때 CloudEvents 또는 간단한 HTTP 요청에 응답하는 프로젝트를 생성할 수 있습니다. Knative의 CloudEvents는 HTTP를 통해 POST 요청으로 전송되므로 함수 유형 모두 수신되는 HTTP 이벤트를 수신하고 응답합니다.

간단한 HTTP 요청을 사용하여 TypeScript 함수를 호출할 수 있습니다. 들어오는 요청이 수신되면 context 오브젝트를 첫 번째 매개 변수로 사용하여 함수가 호출됩니다.

11.5.3.1. TypeScript 컨텍스트 오브젝트

함수를 호출하려면 context 오브젝트를 첫 번째 매개 변수로 제공합니다. 컨텍스트 오브젝트의 속성에 액세스하면 들어오는 HTTP 요청에 대한 정보를 제공할 수 있습니다.

컨텍스트 오브젝트의 예

function handle(context:Context): string

이 정보에는 HTTP 요청 메서드, 요청 문자열 또는 요청과 함께 전송된 쿼리 문자열 또는 헤더, 요청 본문이 포함됩니다. CloudEvent 가 포함된 들어오는 요청은 context.cloudevent 를 사용하여 액세스할 수 있도록 CloudEvent의 들어오는 인스턴스를 컨텍스트 오브젝트에 연결합니다.

11.5.3.1.1. 컨텍스트 오브젝트 메서드

context 오브젝트에는 데이터 값을 수락하고 CloudEvent를 반환하는 단일 메서드 cloudEventResponse()가 있습니다.

Knative 시스템에서 서비스로 배포된 함수가 CloudEvent를 보내는 이벤트 브로커에 의해 호출되는 경우 브로커는 응답을 확인합니다. 응답이 CloudEvent인 경우 브로커가 이 이벤트를 처리합니다.

컨텍스트 오브젝트 메서드 예

// Expects to receive a CloudEvent with customer data
export function handle(context: Context, cloudevent?: CloudEvent): CloudEvent {
  // process the customer
  const customer = cloudevent.data;
  const processed = processCustomer(customer);
  return context.cloudEventResponse(customer)
    .source('/customer/process')
    .type('customer.processed')
    .response();
}

11.5.3.1.2. 컨텍스트 유형

TypeScript 유형 정의 파일은 함수에 사용하기 위해 다음 유형을 내보냅니다.

내보낸 유형 정의

// Invokable is the expeted Function signature for user functions
export interface Invokable {
    (context: Context, cloudevent?: CloudEvent): any
}

// Logger can be used for structural logging to the console
export interface Logger {
  debug: (msg: any) => void,
  info:  (msg: any) => void,
  warn:  (msg: any) => void,
  error: (msg: any) => void,
  fatal: (msg: any) => void,
  trace: (msg: any) => void,
}

// Context represents the function invocation context, and provides
// access to the event itself as well as raw HTTP objects.
export interface Context {
    log: Logger;
    req: IncomingMessage;
    query?: Record<string, any>;
    body?: Record<string, any>|string;
    method: string;
    headers: IncomingHttpHeaders;
    httpVersion: string;
    httpVersionMajor: number;
    httpVersionMinor: number;
    cloudevent: CloudEvent;
    cloudEventResponse(data: string|object): CloudEventResponse;
}

// CloudEventResponse is a convenience class used to create
// CloudEvents on function returns
export interface CloudEventResponse {
    id(id: string): CloudEventResponse;
    source(source: string): CloudEventResponse;
    type(type: string): CloudEventResponse;
    version(version: string): CloudEventResponse;
    response(): CloudEvent;
}

11.5.3.1.3. CloudEvent 데이터

들어오는 요청이 CloudEvent인 경우 CloudEvent와 관련된 모든 데이터가 이벤트에서 추출되며 두 번째 매개변수로 제공됩니다. 예를 들어 데이터 속성에 다음과 유사한 JSON 문자열이 포함된 CloudEvent가 수신되는 경우 다음과 같이 됩니다.

{
  "customerId": "0123456",
  "productId": "6543210"
}

호출될 때 context 오브젝트 다음에 함수에 대한 두 번째 매개 변수는 customerIdproductId 속성이 있는 JavaScript 오브젝트가 됩니다.

서명 예

function handle(context: Context, cloudevent?: CloudEvent): CloudEvent

이 예제의 cloudevent 매개 변수는 customerIdproductId 속성이 포함된 JavaScript 오브젝트입니다.