diff --git a/examples/typescript-ex/README.md b/examples/typescript-ex/README.md new file mode 100644 index 0000000..f26f08a --- /dev/null +++ b/examples/typescript-ex/README.md @@ -0,0 +1,19 @@ +# Typescript Example + +The types are available since v1.0.0 + +## How To Start + +Install and transpile: + +```bash +npm install +``` + +Then, start + +```bash +npm start +``` + +See your event payload [here](https://enu90y24i64jp.x.pipedream.net/) diff --git a/examples/typescript-ex/package.json b/examples/typescript-ex/package.json new file mode 100644 index 0000000..653f5b8 --- /dev/null +++ b/examples/typescript-ex/package.json @@ -0,0 +1,30 @@ +{ + "name": "typescript-ex", + "version": "1.0.0", + "description": "Simple typescript example using CloudEvents types", + "repository": "https://github.com/cloudevents/sdk-javascript.git", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "files": [ + "build/src" + ], + "license": "Apache-2.0", + "keywords": [], + "scripts": { + "start": "node build/src/index.js", + "test": "echo \"Error: no test specified\" && exit 1", + "check": "gts check", + "clean": "gts clean", + "compile": "tsc -p .", + "fix": "gts fix", + "prepare": "npm run compile", + "pretest": "npm run compile", + "posttest": "npm run check" + }, + "devDependencies": { + "gts": "^1.1.0", + "typescript": "~3.5.0", + "@types/node": "^8.9.0", + "cloudevents-sdk": "cloudevents/sdk-javascript#v1" + } +} diff --git a/examples/typescript-ex/prettier.config.js b/examples/typescript-ex/prettier.config.js new file mode 100644 index 0000000..a425d3f --- /dev/null +++ b/examples/typescript-ex/prettier.config.js @@ -0,0 +1,4 @@ +module.exports = { + singleQuote: true, + trailingComma: 'es5', +}; diff --git a/examples/typescript-ex/src/index.ts b/examples/typescript-ex/src/index.ts new file mode 100644 index 0000000..f27e935 --- /dev/null +++ b/examples/typescript-ex/src/index.ts @@ -0,0 +1,33 @@ +import Cloudevent, { event, StructuredHTTPEmitter } from 'cloudevents-sdk/v1'; + +export function doSomeStuff() { + + const myevent: Cloudevent = event() + .source('/source') + .type('type') + .dataContentType('text/plain') + .dataschema('http://d.schema.com/my.json') + .subject('cha.json') + .data('my-data'); + + console.log(myevent.format()); + + let config = { + method: "POST", + url : "https://enu90y24i64jp.x.pipedream.net/" + }; + + let emitter = new StructuredHTTPEmitter(config); + emitter.emit(myevent).then(res => { + // success + console.log("Success!") + }) + .catch(err => { + // error + console.error(err); + }) + + return true; +} + +doSomeStuff(); diff --git a/examples/typescript-ex/tsconfig.json b/examples/typescript-ex/tsconfig.json new file mode 100644 index 0000000..85710e8 --- /dev/null +++ b/examples/typescript-ex/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "./node_modules/gts/tsconfig-google.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "build", + "lib": [ + "es6", + "dom" + ] + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ], + "allowJs": true +} diff --git a/examples/typescript-ex/tslint.json b/examples/typescript-ex/tslint.json new file mode 100644 index 0000000..27872a1 --- /dev/null +++ b/examples/typescript-ex/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "gts/tslint.json", + "linterOptions": { + "exclude": [ + "**/*.json" + ] + } +}