Skip to content

Commit 1105cb2

Browse files
committed
squash: typescript example updated
1 parent eeeabff commit 1105cb2

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

examples/typescript-ex/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@ Then, start
1515
```bash
1616
npm start
1717
```
18-
19-
See your event payload [here, at requestbin](https://requestbin.com/r/enu90y24i64jp)

examples/typescript-ex/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"license": "Apache-2.0",
1212
"keywords": [],
1313
"scripts": {
14-
"start": "node build/src/index.js",
14+
"start": "node build/index.js",
1515
"test": "echo \"Error: no test specified\" && exit 1",
1616
"check": "gts check",
1717
"clean": "gts clean",
@@ -22,9 +22,9 @@
2222
"posttest": "npm run check"
2323
},
2424
"devDependencies": {
25-
"gts": "^1.1.0",
26-
"typescript": "~3.5.0",
2725
"@types/node": "^8.9.0",
28-
"cloudevents-sdk": "1.0.0"
26+
"cloudevents-sdk": "~2.0.1",
27+
"gts": "^1.1.0",
28+
"typescript": "~3.9.5"
2929
}
3030
}

examples/typescript-ex/src/index.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
import { CloudEvent, HTTPREceiver } from '../../';
1+
import { CloudEvent, HTTPReceiver } from '../../..';
2+
import { CloudEventV1 } from '../../../lib/v1';
23

34
export function doSomeStuff() {
4-
const receiver = new HTTPREceiver();
5+
const receiver = new HTTPReceiver();
56

6-
const myevent: CloudEvent = new CloudEvent()
7-
.source('/source')
8-
.type('type')
9-
.dataContentType('text/plain')
10-
.dataschema('http://d.schema.com/my.json')
11-
.subject('cha.json')
12-
.data('my-data')
13-
.addExtension("my-ext", "0x600");
7+
const myevent: CloudEventV1 = new CloudEvent({
8+
source: '/source',
9+
type: 'type',
10+
dataContentType: 'text/plain',
11+
dataSchema: 'https://d.schema.com/my.json',
12+
subject: 'cha.json',
13+
data: 'my-data'
14+
});
15+
myevent.addExtension('extension-1', 'some extension data');
1416

15-
console.log(myevent.toString());
16-
console.log(myevent.getExtensions());
17+
console.log("My structured event:", myevent.toString());
18+
console.log("My structured event extensions:", myevent.getExtensions());
1719

1820
// ------ receiver structured
19-
const payload = myevent.toString();
2021
const headers = {
21-
"Content-Type":"application/cloudevents+json"
22+
"content-type": "application/cloudevents+json"
2223
};
2324

24-
console.log(receiver.accept(headers, payload).toString());
25+
// Typically used with an incoming HTTP request where myevent.format() is the actual
26+
// body of the HTTP
27+
console.log('Received structured event:', receiver.accept(headers, myevent.format()).toString());
2528

2629
// ------ receiver binary
27-
const extension1 = "mycuston-ext1";
2830
const data = {
29-
"data" : "dataString"
31+
"data": "dataString"
3032
};
3133
const attributes = {
32-
"ce-type" : "type",
33-
"ce-specversion" : "1.0",
34-
"ce-source" : "source",
35-
"ce-id" : "id",
36-
"ce-time" : "2019-06-16T11:42:00Z",
37-
"ce-dataschema" : "http://schema.registry/v1",
38-
"Content-Type" : "application/json",
39-
"ce-extension1" : extension1
34+
"ce-type": "type",
35+
"ce-specversion": "1.0",
36+
"ce-source": "source",
37+
"ce-id": "id",
38+
"ce-time": "2019-06-16T11:42:00Z",
39+
"ce-dataschema": "http://schema.registry/v1",
40+
"Content-Type": "application/json",
41+
"ce-extension1": "extension1"
4042
};
4143

42-
console.log(receiver.accept(attributes, data).toString());
44+
console.log("My binary event:", receiver.accept(attributes, data).toString());
45+
console.log("My binary event extensions:", receiver.accept(attributes, data).toString());
4346

4447
return true;
4548
}

examples/typescript-ex/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"extends": "./node_modules/gts/tsconfig-google.json",
33
"compilerOptions": {
4-
"rootDir": ".",
5-
"outDir": "build",
4+
"rootDir": "./src",
5+
"outDir": "./build/",
66
"lib": [
77
"es6",
88
"dom"

0 commit comments

Comments
 (0)