From df84089069b792940e3020081780bc9782c1cea3 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Thu, 7 May 2020 16:31:26 -0700 Subject: [PATCH 1/4] refactor: introduce typescript Signed-off-by: Grant Timmerman --- .gitignore | 3 ++ examples/typescript-ex/src/index.js | 64 +++++++++++++++++++++++++++++ index.js | 6 +-- package-lock.json | 6 +++ package.json | 13 +++--- tsconfig.json | 13 ++++++ 6 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 examples/typescript-ex/src/index.js create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index d08c926e..988a06ee 100644 --- a/.gitignore +++ b/.gitignore @@ -81,3 +81,6 @@ typings/ # Package lock package-lock.json + +# Incremental TypeScript Support +index.ts \ No newline at end of file diff --git a/examples/typescript-ex/src/index.js b/examples/typescript-ex/src/index.js new file mode 100644 index 00000000..c61257d6 --- /dev/null +++ b/examples/typescript-ex/src/index.js @@ -0,0 +1,64 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const v1_1 = require("cloudevents-sdk/v1"); +function doSomeStuff() { + const myevent = v1_1.event() + .source('/source') + .type('type') + .dataContentType('text/plain') + .dataschema('http://d.schema.com/my.json') + .subject('cha.json') + .data('my-data') + .addExtension("my-ext", "0x600"); + console.log(myevent.toString()); + console.log(myevent.getExtensions()); + const config = { + method: "POST", + url: "https://enu90y24i64jp.x.pipedream.net/" + }; + // ------ emitter structured + const structured = new v1_1.StructuredHTTPEmitter(config); + structured.emit(myevent).then(res => { + // success + console.log("Structured Mode: Success!"); + }) + .catch(err => { + // error + console.error(err); + }); + // ------ emitter binary + const binary = new v1_1.BinaryHTTPEmitter(config); + binary.emit(myevent).then(res => { + console.log("Binary Mode: Success!"); + }) + .catch(err => { + console.error(err); + }); + // ------ receiver structured + const payload = myevent.toString(); + const headers = { + "Content-Type": "application/cloudevents+json" + }; + const receiverStructured = new v1_1.StructuredHTTPReceiver(); + console.log(receiverStructured.parse(payload, headers).toString()); + // ------ receiver binary + const extension1 = "mycuston-ext1"; + const data = { + "data": "dataString" + }; + const attributes = { + "ce-type": "type", + "ce-specversion": "1.0", + "ce-source": "source", + "ce-id": "id", + "ce-time": "2019-06-16T11:42:00Z", + "ce-dataschema": "http://schema.registry/v1", + "Content-Type": "application/json", + "ce-extension1": extension1 + }; + const receiverBinary = new v1_1.BinaryHTTPReceiver(); + console.log(receiverBinary.parse(data, attributes).toString()); + return true; +} +exports.doSomeStuff = doSomeStuff; +doSomeStuff(); diff --git a/index.js b/index.js index 22e87aab..114067c2 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ +"use strict"; const CloudEvent = require("./lib/cloudevent.js"); const HTTPReceiver = require("./lib/bindings/http/http_receiver.js"); - module.exports = { - CloudEvent, - HTTPReceiver + CloudEvent, + HTTPReceiver }; diff --git a/package-lock.json b/package-lock.json index c276e0f3..6fa106a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -324,6 +324,12 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, + "@types/node": { + "version": "13.13.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.5.tgz", + "integrity": "sha512-3ySmiBYJPqgjiHA7oEaIo2Rzz0HrOZ7yrNO5HWyaE5q0lQ3BppDZ3N53Miz8bw2I7gh1/zir2MGVZBvpb1zq9g==", + "dev": true + }, "JSONStream": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.5.tgz", diff --git a/package.json b/package.json index 5811aa47..5244cad9 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "lint": "standardx", "fix": "standardx --fix", "pretest": "npm run lint", + "build": "tsc --project tsconfig.json", + "watch": "tsc --project tsconfig.json --watch", "test": "mocha test/**/*.js", "coverage": "nyc --reporter=lcov --reporter=text npm run test", "precoverage-publish": "npm run coverage", @@ -92,15 +94,16 @@ "uuid": "~8.0.0" }, "devDependencies": { + "@types/node": "^13.13.5", "chai": "~4.2.0", - "mocha": "~7.1.1", - "nock": "~12.0.3", - "nyc": "~15.0.0", "eslint-config-standard": "^14.1.1", "eslint-plugin-import": "^2.20.2", "eslint-plugin-node": "^11.1.0", - "standardx": "^5.0.0", - "standard-version": "^7.1.0" + "mocha": "~7.1.1", + "nock": "~12.0.3", + "nyc": "~15.0.0", + "standard-version": "^7.1.0", + "standardx": "^5.0.0" }, "publishConfig": { "access": "public" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..ad06cf2c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compileOnSave": true, + "compilerOptions": { + "target": "es2016", + "lib": ["ESNext"], + "strict": true, + "moduleResolution": "node", + "module": "commonjs" + }, + "include": [ + "index.ts", + ] +} \ No newline at end of file From 74ce71fb14dc144709953d712a8904dd4a1369d2 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Thu, 7 May 2020 16:33:42 -0700 Subject: [PATCH 2/4] chore: remove examples compilation Signed-off-by: Grant Timmerman --- .gitignore | 2 +- examples/typescript-ex/src/index.js | 64 ----------------------------- index.ts | 7 ++++ 3 files changed, 8 insertions(+), 65 deletions(-) delete mode 100644 examples/typescript-ex/src/index.js create mode 100644 index.ts diff --git a/.gitignore b/.gitignore index 988a06ee..2e00463e 100644 --- a/.gitignore +++ b/.gitignore @@ -83,4 +83,4 @@ typings/ package-lock.json # Incremental TypeScript Support -index.ts \ No newline at end of file +index.js \ No newline at end of file diff --git a/examples/typescript-ex/src/index.js b/examples/typescript-ex/src/index.js deleted file mode 100644 index c61257d6..00000000 --- a/examples/typescript-ex/src/index.js +++ /dev/null @@ -1,64 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const v1_1 = require("cloudevents-sdk/v1"); -function doSomeStuff() { - const myevent = v1_1.event() - .source('/source') - .type('type') - .dataContentType('text/plain') - .dataschema('http://d.schema.com/my.json') - .subject('cha.json') - .data('my-data') - .addExtension("my-ext", "0x600"); - console.log(myevent.toString()); - console.log(myevent.getExtensions()); - const config = { - method: "POST", - url: "https://enu90y24i64jp.x.pipedream.net/" - }; - // ------ emitter structured - const structured = new v1_1.StructuredHTTPEmitter(config); - structured.emit(myevent).then(res => { - // success - console.log("Structured Mode: Success!"); - }) - .catch(err => { - // error - console.error(err); - }); - // ------ emitter binary - const binary = new v1_1.BinaryHTTPEmitter(config); - binary.emit(myevent).then(res => { - console.log("Binary Mode: Success!"); - }) - .catch(err => { - console.error(err); - }); - // ------ receiver structured - const payload = myevent.toString(); - const headers = { - "Content-Type": "application/cloudevents+json" - }; - const receiverStructured = new v1_1.StructuredHTTPReceiver(); - console.log(receiverStructured.parse(payload, headers).toString()); - // ------ receiver binary - const extension1 = "mycuston-ext1"; - const data = { - "data": "dataString" - }; - const attributes = { - "ce-type": "type", - "ce-specversion": "1.0", - "ce-source": "source", - "ce-id": "id", - "ce-time": "2019-06-16T11:42:00Z", - "ce-dataschema": "http://schema.registry/v1", - "Content-Type": "application/json", - "ce-extension1": extension1 - }; - const receiverBinary = new v1_1.BinaryHTTPReceiver(); - console.log(receiverBinary.parse(data, attributes).toString()); - return true; -} -exports.doSomeStuff = doSomeStuff; -doSomeStuff(); diff --git a/index.ts b/index.ts new file mode 100644 index 00000000..22e87aab --- /dev/null +++ b/index.ts @@ -0,0 +1,7 @@ +const CloudEvent = require("./lib/cloudevent.js"); +const HTTPReceiver = require("./lib/bindings/http/http_receiver.js"); + +module.exports = { + CloudEvent, + HTTPReceiver +}; From 52f6b111e761e0252fb4a32d3e23ead32283e715 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Thu, 7 May 2020 16:34:42 -0700 Subject: [PATCH 3/4] chore: git rm old js file Signed-off-by: Grant Timmerman --- index.js | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 index.js diff --git a/index.js b/index.js deleted file mode 100644 index 114067c2..00000000 --- a/index.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; -const CloudEvent = require("./lib/cloudevent.js"); -const HTTPReceiver = require("./lib/bindings/http/http_receiver.js"); -module.exports = { - CloudEvent, - HTTPReceiver -}; From f55c6de6c5ccf1b79bf3c6b9a5ea2c395c2d72bf Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Fri, 8 May 2020 09:45:31 -0700 Subject: [PATCH 4/4] fix: add build before npm test for travis Signed-off-by: Grant Timmerman --- .travis.yml | 2 ++ package-lock.json | 6 ++++++ package.json | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 37d1a085..48e95250 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,8 @@ jobs: env: global: secure: t0A5/Fk1Qj8WkAk0ZuQqpFjrpINV6gfL+d1fXgq03EOAG/7FEyyz+AKkYaLT6avp26VWBzLgEoWC3RNmWD0v1/Ruckb95YpQcb5e4JotAce1ZsHgGTgx0UO146tkyGvXBw6MmO2nX2O/sHzkmyR6rlKkBi9LEKokJ9OFd6fi9nNtksgUDxkqNlNTu100fIQAV2RcN47C6iUx+gJIa+H/8QX/Cz02MyheXLe8a763wMQapz5GHrk2KuVY6FpylcAlSi2so+PjEGfKq3MWaQnTPkevkRDLtGcecVtFAq/0VbvoKc97PHoVT0x+nf48k1gj2VnQtYj6EVB74yRvqM9KkNOsfAQ0z9zF5wXMRs11SZywSViLQsnD9Ue6eYbYmodOKn4DFlLtoRyLBUspzLuoL76lERGe2cHU+Ebz2Nb9jL88o+SjfGrSJQEJU7SLZLbzJ7T/3SspWcAlo+Fo9UTCOB4/yL22Yge/SWQkdd1orBhohpjpyrU9Z1+IRvVoU1EICr04zSFUwEwn6Yxbt0ArgTZBEf11bf+YpCo+rBXNAd+XbZ79PGOEppFb+Hxrt9i1S1RnesDJnwC0k7cswU55Rv5gcNCo9lC+N4ZwS5y+5r6DsBfB+12YdsFORFyNbClECro1ODv4STf3rBBnjL8+ziPFjP4d4OwiGY+vG8bvLXY= +before_script: + - npm run build script: - if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then npm run coverage-publish; else npm test; fi diff --git a/package-lock.json b/package-lock.json index 6fa106a5..78010982 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5042,6 +5042,12 @@ "is-typedarray": "^1.0.0" } }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + }, "uglify-js": { "version": "3.9.1", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.9.1.tgz", diff --git a/package.json b/package.json index 5244cad9..6d1daca1 100644 --- a/package.json +++ b/package.json @@ -103,7 +103,8 @@ "nock": "~12.0.3", "nyc": "~15.0.0", "standard-version": "^7.1.0", - "standardx": "^5.0.0" + "standardx": "^5.0.0", + "typescript": "^3.8.3" }, "publishConfig": { "access": "public"