Skip to content

Commit ba2c0d3

Browse files
committed
🎨 refactor: add ofn register with tests and docs
Signed-off-by: Haili Zhang <haili.zhang@outlook.com>
1 parent b056ba9 commit ba2c0d3

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed

docs/generated/api.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,36 @@
15001500
],
15011501
"extendsTokenRanges": []
15021502
},
1503+
{
1504+
"kind": "Variable",
1505+
"canonicalReference": "@openfunction/functions-framework!openfunction:var",
1506+
"docComment": "/**\n * Register a function that responds to OpenFunction.\n *\n * @param functionName - the name of the function\n *\n * @param handler - the function to invoke when handling OpenFunction\n *\n * @public\n */\n",
1507+
"excerptTokens": [
1508+
{
1509+
"kind": "Content",
1510+
"text": "openfunction: "
1511+
},
1512+
{
1513+
"kind": "Content",
1514+
"text": "(functionName: string, handler: "
1515+
},
1516+
{
1517+
"kind": "Reference",
1518+
"text": "OpenFunction",
1519+
"canonicalReference": "@openfunction/functions-framework!OpenFunction:interface"
1520+
},
1521+
{
1522+
"kind": "Content",
1523+
"text": ") => void"
1524+
}
1525+
],
1526+
"releaseTag": "Public",
1527+
"name": "openfunction",
1528+
"variableTypeTokenRange": {
1529+
"startIndex": 1,
1530+
"endIndex": 4
1531+
}
1532+
},
15031533
{
15041534
"kind": "Interface",
15051535
"canonicalReference": "@openfunction/functions-framework!OpenFunctionBinding:interface",

docs/generated/api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export interface OpenFunction {
113113
(ctx: OpenFunctionRuntime, data: {}): any;
114114
}
115115

116+
// @public
117+
export const openfunction: (functionName: string, handler: OpenFunction) => void;
118+
116119
// @public
117120
export interface OpenFunctionBinding {
118121
[key: string]: OpenFunctionComponent;

src/function_registry.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {HttpFunction, CloudEventFunction, HandlerFunction} from './functions';
15+
import {
16+
HttpFunction,
17+
CloudEventFunction,
18+
HandlerFunction,
19+
OpenFunction,
20+
} from './functions';
1621
import {SignatureType} from './types';
1722

1823
interface RegisteredFunction<T> {
@@ -94,3 +99,16 @@ export const cloudEvent = <T = unknown>(
9499
): void => {
95100
register(functionName, 'cloudevent', handler);
96101
};
102+
103+
/**
104+
* Register a function that responds to OpenFunction.
105+
* @param functionName - the name of the function
106+
* @param handler - the function to invoke when handling OpenFunction
107+
* @public
108+
*/
109+
export const openfunction = (
110+
functionName: string,
111+
handler: OpenFunction
112+
): void => {
113+
register(functionName, 'openfunction', handler);
114+
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export * from './functions';
2020
/**
2121
* @public
2222
*/
23-
export {http, cloudEvent} from './function_registry';
23+
export {http, cloudEvent, openfunction} from './function_registry';
2424

2525
/**
2626
* @public

test/function_registry.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ describe('function_registry', () => {
3131
assert.deepStrictEqual((userFunction as () => string)(), 'CE_PASS');
3232
});
3333

34+
it('can register OpenFunction functions', () => {
35+
FunctionRegistry.openfunction('OpenFunction', () => 'OFN_PASS');
36+
const {userFunction, signatureType} =
37+
FunctionRegistry.getRegisteredFunction('OpenFunction')!;
38+
assert.deepStrictEqual('openfunction', signatureType);
39+
assert.deepStrictEqual((userFunction as () => string)(), 'OFN_PASS');
40+
});
41+
3442
it('throws an error if you try to register a function with an invalid URL', () => {
3543
// Valid function names
3644
const validFunctions = ['httpFunction', 'ceFunction', 'test-func'];

test/function_wrappers.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as assert from 'assert';
22
import * as sinon from 'sinon';
33
import {Request, Response} from 'express';
4-
import {Context, CloudEvent} from '../src/functions';
4+
5+
import {OpenFunctionContext} from '../src/openfunction/function_context';
6+
7+
import {Context, CloudEvent, OpenFunctionRuntime} from '../src/functions';
58
import {wrapUserFunction} from '../src/function_wrappers';
69

710
describe('wrapUserFunction', () => {
@@ -17,6 +20,12 @@ describe('wrapUserFunction', () => {
1720
},
1821
};
1922

23+
const OPENFUNCTION_CONTEXT: OpenFunctionContext = {
24+
name: 'test-context',
25+
version: '1.0.0',
26+
runtime: 'Knative',
27+
};
28+
2029
const createRequest = (body: object) =>
2130
({
2231
body,
@@ -105,4 +114,21 @@ describe('wrapUserFunction', () => {
105114
);
106115
func(request, response, () => {});
107116
});
117+
118+
it('correctly wraps an OpenFunctionn function', done => {
119+
const request = createRequest(CLOUD_EVENT);
120+
const response = createResponse();
121+
const func = wrapUserFunction(
122+
async (context: OpenFunctionRuntime, data: {}) => {
123+
assert.deepStrictEqual(data, CLOUD_EVENT);
124+
assert.deepStrictEqual(context.req?.body, CLOUD_EVENT);
125+
// await to make sure wrapper handles async code
126+
await new Promise(resolve => setTimeout(resolve, 20));
127+
done();
128+
},
129+
'openfunction',
130+
OPENFUNCTION_CONTEXT
131+
);
132+
func(request, Object.assign(response, {end: () => {}}), () => {});
133+
});
108134
});

0 commit comments

Comments
 (0)