Skip to content

Commit 74cbea9

Browse files
committed
♻️ refactor(ofn/runtime): refine context proxy
Signed-off-by: Haili Zhang <haili.zhang@outlook.com>
1 parent da606dd commit 74cbea9

File tree

6 files changed

+221
-93
lines changed

6 files changed

+221
-93
lines changed

docs/generated/api.json

Lines changed: 161 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,15 @@
10221022
"kind": "Content",
10231023
"text": "> = "
10241024
},
1025+
{
1026+
"kind": "Reference",
1027+
"text": "OpenFunction",
1028+
"canonicalReference": "@openfunction/functions-framework!OpenFunction:interface"
1029+
},
1030+
{
1031+
"kind": "Content",
1032+
"text": " | "
1033+
},
10251034
{
10261035
"kind": "Reference",
10271036
"text": "HttpFunction",
@@ -1065,12 +1074,7 @@
10651074
},
10661075
{
10671076
"kind": "Content",
1068-
"text": "<T> | "
1069-
},
1070-
{
1071-
"kind": "Reference",
1072-
"text": "OpenFunction",
1073-
"canonicalReference": "@openfunction/functions-framework!OpenFunction:interface"
1077+
"text": "<T>"
10741078
},
10751079
{
10761080
"kind": "Content",
@@ -1094,7 +1098,7 @@
10941098
],
10951099
"typeTokenRange": {
10961100
"startIndex": 3,
1097-
"endIndex": 14
1101+
"endIndex": 15
10981102
}
10991103
},
11001104
{
@@ -1968,7 +1972,7 @@
19681972
"excerptTokens": [
19691973
{
19701974
"kind": "Content",
1971-
"text": "readonly context: "
1975+
"text": "protected readonly context: "
19721976
},
19731977
{
19741978
"kind": "Reference",
@@ -2084,85 +2088,104 @@
20842088
"name": "ProxyContext"
20852089
},
20862090
{
2087-
"kind": "Method",
2088-
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#response:member(1)",
2089-
"docComment": "/**\n * It returns an HTTP style response object with a `code`, `headers`, and `body` property\n *\n * @param body - The data you want to send back to the client.\n *\n * @param code - The HTTP status code to return.\n *\n * @param headers - An object containing the headers to be sent with the response.\n *\n * @returns A function that takes in data, code, and headers and returns an response object.\n */\n",
2091+
"kind": "Property",
2092+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#req:member",
2093+
"docComment": "/**\n * Getter returns the request object from the trigger.\n *\n * @returns The request object.\n */\n",
20902094
"excerptTokens": [
20912095
{
20922096
"kind": "Content",
2093-
"text": "response(body: "
2097+
"text": "get req(): "
20942098
},
20952099
{
2096-
"kind": "Content",
2097-
"text": "unknown"
2100+
"kind": "Reference",
2101+
"text": "Request",
2102+
"canonicalReference": "@types/express!~e.Request:interface"
20982103
},
20992104
{
21002105
"kind": "Content",
2101-
"text": ", code?: "
2106+
"text": "<import(\"express-serve-static-core\")."
21022107
},
21032108
{
2104-
"kind": "Content",
2105-
"text": "number"
2109+
"kind": "Reference",
2110+
"text": "ParamsDictionary",
2111+
"canonicalReference": "@types/express-serve-static-core!ParamsDictionary:interface"
21062112
},
21072113
{
21082114
"kind": "Content",
2109-
"text": ", headers?: "
2115+
"text": ", any, any, import(\"qs\")."
21102116
},
21112117
{
21122118
"kind": "Reference",
2113-
"text": "Record",
2114-
"canonicalReference": "!Record:type"
2119+
"text": "ParsedQs",
2120+
"canonicalReference": "@types/qs!~QueryString.ParsedQs:interface"
21152121
},
21162122
{
21172123
"kind": "Content",
2118-
"text": "<string, string>"
2124+
"text": ", "
21192125
},
21202126
{
2121-
"kind": "Content",
2122-
"text": "): "
2127+
"kind": "Reference",
2128+
"text": "Record",
2129+
"canonicalReference": "!Record:type"
21232130
},
21242131
{
2125-
"kind": "Reference",
2126-
"text": "HttpFunctionResponse",
2127-
"canonicalReference": "@openfunction/functions-framework!HttpFunctionResponse:interface"
2132+
"kind": "Content",
2133+
"text": "<string, any>> | undefined"
21282134
},
21292135
{
21302136
"kind": "Content",
21312137
"text": ";"
21322138
}
21332139
],
21342140
"isOptional": false,
2135-
"isStatic": false,
2136-
"returnTypeTokenRange": {
2137-
"startIndex": 8,
2141+
"releaseTag": "Public",
2142+
"name": "req",
2143+
"propertyTypeTokenRange": {
2144+
"startIndex": 1,
21382145
"endIndex": 9
21392146
},
2140-
"releaseTag": "Public",
2141-
"overloadIndex": 1,
2142-
"parameters": [
2147+
"isStatic": false
2148+
},
2149+
{
2150+
"kind": "Property",
2151+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#res:member",
2152+
"docComment": "/**\n * Getter returns the response object from the trigger.\n *\n * @returns The res property of the trigger object.\n */\n",
2153+
"excerptTokens": [
21432154
{
2144-
"parameterName": "body",
2145-
"parameterTypeTokenRange": {
2146-
"startIndex": 1,
2147-
"endIndex": 2
2148-
}
2155+
"kind": "Content",
2156+
"text": "get res(): "
21492157
},
21502158
{
2151-
"parameterName": "code",
2152-
"parameterTypeTokenRange": {
2153-
"startIndex": 3,
2154-
"endIndex": 4
2155-
}
2159+
"kind": "Reference",
2160+
"text": "Response",
2161+
"canonicalReference": "@types/express!~e.Response:interface"
21562162
},
21572163
{
2158-
"parameterName": "headers",
2159-
"parameterTypeTokenRange": {
2160-
"startIndex": 5,
2161-
"endIndex": 7
2162-
}
2164+
"kind": "Content",
2165+
"text": "<any, "
2166+
},
2167+
{
2168+
"kind": "Reference",
2169+
"text": "Record",
2170+
"canonicalReference": "!Record:type"
2171+
},
2172+
{
2173+
"kind": "Content",
2174+
"text": "<string, any>> | undefined"
2175+
},
2176+
{
2177+
"kind": "Content",
2178+
"text": ";"
21632179
}
21642180
],
2165-
"name": "response"
2181+
"isOptional": false,
2182+
"releaseTag": "Public",
2183+
"name": "res",
2184+
"propertyTypeTokenRange": {
2185+
"startIndex": 1,
2186+
"endIndex": 5
2187+
},
2188+
"isStatic": false
21662189
},
21672190
{
21682191
"kind": "Method",
@@ -2229,6 +2252,68 @@
22292252
],
22302253
"name": "send"
22312254
},
2255+
{
2256+
"kind": "Method",
2257+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#setTrigger:member(1)",
2258+
"docComment": "/**\n * It sets the trigger object to the request and response objects passed in\n *\n * @param req - The HTTP request object\n *\n * @param res - The HTTP response object\n */\n",
2259+
"excerptTokens": [
2260+
{
2261+
"kind": "Content",
2262+
"text": "setTrigger(req: "
2263+
},
2264+
{
2265+
"kind": "Reference",
2266+
"text": "Request",
2267+
"canonicalReference": "@types/express!~e.Request:interface"
2268+
},
2269+
{
2270+
"kind": "Content",
2271+
"text": ", res?: "
2272+
},
2273+
{
2274+
"kind": "Reference",
2275+
"text": "Response",
2276+
"canonicalReference": "@types/express!~e.Response:interface"
2277+
},
2278+
{
2279+
"kind": "Content",
2280+
"text": "): "
2281+
},
2282+
{
2283+
"kind": "Content",
2284+
"text": "void"
2285+
},
2286+
{
2287+
"kind": "Content",
2288+
"text": ";"
2289+
}
2290+
],
2291+
"isOptional": false,
2292+
"isStatic": false,
2293+
"returnTypeTokenRange": {
2294+
"startIndex": 5,
2295+
"endIndex": 6
2296+
},
2297+
"releaseTag": "Public",
2298+
"overloadIndex": 1,
2299+
"parameters": [
2300+
{
2301+
"parameterName": "req",
2302+
"parameterTypeTokenRange": {
2303+
"startIndex": 1,
2304+
"endIndex": 2
2305+
}
2306+
},
2307+
{
2308+
"parameterName": "res",
2309+
"parameterTypeTokenRange": {
2310+
"startIndex": 3,
2311+
"endIndex": 4
2312+
}
2313+
}
2314+
],
2315+
"name": "setTrigger"
2316+
},
22322317
{
22332318
"kind": "Property",
22342319
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#sidecarPort:member",
@@ -2255,6 +2340,34 @@
22552340
"endIndex": 2
22562341
},
22572342
"isStatic": false
2343+
},
2344+
{
2345+
"kind": "Property",
2346+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#trigger:member",
2347+
"docComment": "/**\n * The optional trigger of OpenFunction.\n */\n",
2348+
"excerptTokens": [
2349+
{
2350+
"kind": "Content",
2351+
"text": "protected trigger?: "
2352+
},
2353+
{
2354+
"kind": "Reference",
2355+
"text": "OpenFunctionTrigger",
2356+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionTrigger:type"
2357+
},
2358+
{
2359+
"kind": "Content",
2360+
"text": ";"
2361+
}
2362+
],
2363+
"isOptional": true,
2364+
"releaseTag": "Public",
2365+
"name": "trigger",
2366+
"propertyTypeTokenRange": {
2367+
"startIndex": 1,
2368+
"endIndex": 2
2369+
},
2370+
"isStatic": false
22582371
}
22592372
],
22602373
"implementsTokenRanges": []

docs/generated/api.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
```ts
66

77
/// <reference types="node" />
8+
/// <reference types="qs" />
89

910
import { CloudEventV1 as CloudEvent } from 'cloudevents';
11+
import { ParamsDictionary } from 'express-serve-static-core';
12+
import { ParsedQs } from 'qs';
1013
import { Request as Request_3 } from 'express';
1114
import { Response as Response_2 } from 'express';
1215

@@ -73,7 +76,7 @@ export interface EventFunctionWithCallback {
7376
}
7477

7578
// @public
76-
export type HandlerFunction<T = unknown> = HttpFunction | EventFunction | EventFunctionWithCallback | CloudEventFunction<T> | CloudEventFunctionWithCallback<T> | OpenFunction;
79+
export type HandlerFunction<T = unknown> = OpenFunction | HttpFunction | EventFunction | EventFunctionWithCallback | CloudEventFunction<T> | CloudEventFunctionWithCallback<T>;
7780

7881
// @public
7982
export const http: (functionName: string, handler: HttpFunction) => void;
@@ -137,15 +140,19 @@ export interface OpenFunctionContext {
137140
// @public
138141
export abstract class OpenFunctionRuntime {
139142
constructor(context: OpenFunctionContext);
140-
readonly context: OpenFunctionContext;
143+
protected readonly context: OpenFunctionContext;
141144
static Parse(context: OpenFunctionContext): OpenFunctionRuntime;
142145
static ProxyContext(context: OpenFunctionContext): OpenFunctionRuntime;
143-
response(body: unknown, code?: number, headers?: Record<string, string>): HttpFunctionResponse;
146+
get req(): Request_3<ParamsDictionary, any, any, ParsedQs, Record<string, any>> | undefined;
147+
get res(): Response_2<any, Record<string, any>> | undefined;
144148
abstract send(data: object, output?: string): Promise<object>;
149+
setTrigger(req: Request_3, res?: Response_2): void;
145150
get sidecarPort(): {
146151
HTTP: string;
147152
GRRC: string;
148153
};
154+
// Warning: (ae-forgotten-export) The symbol "OpenFunctionTrigger" needs to be exported by the entry point index.d.ts
155+
protected trigger?: OpenFunctionTrigger;
149156
}
150157

151158
// @public (undocumented)

src/function_wrappers.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
// eslint-disable-next-line node/no-deprecated-api
1616
import * as domain from 'domain';
1717

18-
import * as Debug from 'debug';
19-
import {get, isEmpty} from 'lodash';
20-
2118
import {Request, Response, RequestHandler} from 'express';
2219
import {sendCrashResponse} from './logger';
2320
import {sendResponse} from './invoker';
@@ -37,8 +34,6 @@ import {SignatureType} from './types';
3734
import {OpenFunctionContext} from './openfunction/function_context';
3835
import {OpenFunctionRuntime} from './openfunction/function_runtime';
3936

40-
const debug = Debug('common:wrapper');
41-
4237
/**
4338
* The handler function used to signal completion of event functions.
4439
*/
@@ -136,26 +131,14 @@ const wrapOpenFunction = (
136131
context: OpenFunctionContext
137132
): RequestHandler => {
138133
const ctx = OpenFunctionRuntime.ProxyContext(context);
134+
139135
const httpHandler = (req: Request, res: Response) => {
140136
const callback = getOnDoneCallback(res);
137+
ctx.setTrigger(req, res);
141138

142139
Promise.resolve()
143140
.then(() => userFunction(ctx, req.body))
144-
.then(result => {
145-
debug('ℹ️ User function returned: %j', result);
146-
147-
const data = get(result, 'body');
148-
const code = get(result, 'code', 200);
149-
const headers = get(result, 'headers');
150-
151-
!isEmpty(headers) && res.set(headers);
152-
153-
if (data !== undefined) {
154-
res.status(code).send(data);
155-
} else {
156-
res.status(code).end();
157-
}
158-
})
141+
.then(() => res.end())
159142
.catch(err => callback(err, undefined));
160143
};
161144

0 commit comments

Comments
 (0)