|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -module.exports = async function (context) { |
| 3 | +/** |
| 4 | + * An example function that responds to incoming CloudEvents over HTTP. For example, |
| 5 | + * from the Knative event Broker. Try invoking with a request such as this. |
| 6 | + * |
| 7 | + * curl -X POST -d '{"name": "Tiger", "customerId": "0123456789"}' \ |
| 8 | + * -H'Content-type: application/json' \ |
| 9 | + * -H'Ce-id: 1' \ |
| 10 | + * -H'Ce-source: cloud-event-example' \ |
| 11 | + * -H'Ce-type: dev.knative.example' \ |
| 12 | + * -H'Ce-specversion: 1.0' \ |
| 13 | + * http://localhost:8080 |
| 14 | + * |
| 15 | + * The event data is extracted from the incoming event and provided as the first |
| 16 | + * parameter to the function. The CloudEvent object itself may be accessed via the |
| 17 | + * context parameter, For example: |
| 18 | + * |
| 19 | + * const incomingEvent = context.cloudevent; |
| 20 | + * |
| 21 | + * @param {Object} customer the CloudEvent data. If the data content type is application/json |
| 22 | + * this will be converted to an Object via JSON.parse() |
| 23 | + * @param {Context} context the invocation context |
| 24 | + */ |
| 25 | +function processCustomer(customer, context) { |
| 26 | + console.log(customer, context) |
4 | 27 | if (!context.cloudevent) {
|
5 |
| - return Promise.reject(new Error('No cloud event received')); |
| 28 | + return 'No cloud event received'; |
6 | 29 | }
|
7 |
| - context.log.info(`Cloud event received: ${JSON.stringify(context.cloudevent)}`); |
8 |
| - return new Promise((resolve, reject) => { |
9 |
| - setTimeout(_ => resolve({ data: context.cloudevent.data }), 500); |
10 |
| - }); |
| 30 | + context.log.info('Processing customer', customer); |
| 31 | + context.log.info(`CloudEvent received: ${context.cloudevent.toString()}`); |
| 32 | + return { customer }; |
11 | 33 | };
|
| 34 | +module.exports = processCustomer; |
0 commit comments