forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunction.js
147 lines (130 loc) · 3.46 KB
/
function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable node/no-missing-require */
const fs = require('fs');
const debug = require('debug')('test:conformance');
const functions = require('@openfunction/functions-framework');
const fileName = 'function_output.json';
functions.http('writeHttpDeclarative', (req, res) => {
writeJson(req.body);
res.sendStatus(200);
});
functions.cloudEvent('writeCloudEventDeclarative', cloudEvent => {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
});
function writeHttp(req, res) {
writeJson(req.body);
res.sendStatus(200);
}
function writeCloudEvent(cloudEvent) {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
}
function writeLegacyEvent(data, context) {
const content = {
data: data,
context: {
eventId: context.eventId,
timestamp: context.timestamp,
eventType: context.eventType,
resource: context.resource,
},
};
writeJson(content);
}
function writeJson(content) {
const json = JSON.stringify(content);
fs.writeFileSync(fileName, json);
}
async function tryKnativeAsyncBindingAndPubSub(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.send(data);
ctx.res.send(data);
}
async function tryKnativeAsyncStateSave(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.state
.save(data)
.then(res => {
debug('✅ Success save: %o', res);
})
.catch(err => {
debug('❌ Failure occurred: %o', err);
});
ctx.res.send(data);
}
async function tryKnativeAsyncStateGet(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.state
.get(data)
.then(res => {
debug('✅ Success get: %o', res);
})
.catch(err => {
debug('❌ Failure occurred: %o', err);
});
ctx.res.send(data);
}
async function tryKnativeAsyncStateGetBulk(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.state
.getBulk(data)
.then(res => {
debug('✅ Success getBulk: %o', res);
})
.catch(err => {
debug('❌ Failure occurred: %o', err);
});
ctx.res.send(data);
}
async function tryKnativeAsyncStateDelete(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.state
.delete(data)
.then(res => {
debug('✅ Success delete: %o', res);
})
.catch(err => {
debug('❌ Failure occurred: %o', err);
});
ctx.res.send(data);
}
async function tryKnativeAsyncStateTransaction(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.state
.transaction(data)
.then(res => {
debug('✅ Success transaction: %o', res);
})
.catch(err => {
debug('❌ Failure occurred: %o', err);
});
ctx.res.send(data);
}
async function tryKnativeAsyncStateQuery(ctx, data) {
debug('✅ Function should receive request: %o', data);
await ctx.state
.query(data)
.then(res => {
debug('✅ Success query: %o', res);
})
.catch(err => {
debug('❌ Failure occurred: %o', err);
});
ctx.res.send(data);
}
function tryAsync(ctx, data) {
debug('✅ Function should receive from "%o": %o', ctx.inputs, data);
}
module.exports = {
writeHttp,
writeCloudEvent,
writeLegacyEvent,
tryKnativeAsyncBindingAndPubSub,
tryKnativeAsyncStateSave,
tryKnativeAsyncStateGet,
tryKnativeAsyncStateGetBulk,
tryKnativeAsyncStateDelete,
tryKnativeAsyncStateTransaction,
tryKnativeAsyncStateQuery,
tryAsync,
};