Skip to content

fix: add correct types to improve usage in TypeScript projects #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 121 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"prelint": "npm run build",
"lint": "standardx src examples",
"fix": "standardx --fix",
"pretest": "npm run lint",
"pretest": "npm run lint && npm run test:ts",
"test": "mocha test/**/*.js",
"test:ts": "mocha --require ts-node/register ./test-ts/**/*.ts",
"coverage": "nyc --reporter=lcov --reporter=text npm run test",
"coverage-publish": "wget -qO - https://coverage.codacy.com/get.sh | bash -s report -l JavaScript -r coverage/lcov.info",
"generate-docs": "typedoc --tsconfig ./tsconfig.json src",
"generate-docs": "typedoc --tsconfig ./tsconfig.json --plugin typedoc-plugin-markdown src",
"release": "standard-version",
"prepublish": "npm run build"
},
Expand Down Expand Up @@ -101,9 +102,10 @@
"uuid": "~8.0.0"
},
"devDependencies": {
"@paztis/typedoc": "^0.1.6",
"@types/ajv": "^1.0.0",
"@types/axios": "^0.14.0",
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@types/node": "^13.13.9",
"chai": "~4.2.0",
"eslint-config-standard": "^14.1.1",
Expand All @@ -114,6 +116,9 @@
"nyc": "~15.0.0",
"standard-version": "^7.1.0",
"standardx": "^5.0.0",
"ts-node": "^8.10.2",
"typedoc": "^0.17.7",
"typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.8.3"
},
"publishConfig": {
Expand Down
10 changes: 5 additions & 5 deletions src/lib/bindings/http/http_emitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CloudEvent } from "../../cloudevent.js";
import { CloudEvent } from "../../cloudevent";

const BinaryHTTPEmitter = require("./emitter_binary.js");
const StructuredEmitter = require("./emitter_structured.js");
Expand All @@ -20,7 +20,7 @@ const {
* @see https://github.com/cloudevents/spec/blob/v1.0/http-protocol-binding.md#13-content-modes
*/
export class HTTPEmitter {
url: URL | undefined;
url: URL | string;
binary: any;
structured: any;
static headers: Function;
Expand All @@ -34,7 +34,7 @@ export class HTTPEmitter {
* @param {string} [options.version] The HTTP binding specification version. Default: "1.0"
* @throws {TypeError} if no options.url is provided or an unknown specification version is provided.
*/
constructor({ url, version = SPEC_V1 } = { url: undefined }) {
constructor({ url = "", version = SPEC_V1 }) {
if (version !== SPEC_V03 && version !== SPEC_V1) {
throw new TypeError(
`Unknown CloudEvent specification version: ${version}`);
Expand Down Expand Up @@ -62,7 +62,7 @@ export class HTTPEmitter {
* Possible values are "binary" and "structured". Default: structured
* @returns {Promise} Promise with an eventual response from the receiver
*/
send(event: CloudEvent, { url, mode = "binary", ...httpOpts } = { url: undefined }) {
send(event: CloudEvent, { url = "", mode = "binary", ...httpOpts } = {}) {
// @ts-ignore Type 'URL | undefined' is not assignable to type 'undefined'. Type 'URL' is not assignable to type 'undefined'.ts(2322)
if (!url) { url = this.url; }
// @ts-ignore Property 'url' does not exist on type '{}'
Expand All @@ -85,7 +85,7 @@ export class HTTPEmitter {
* @param {string} [version] spec version number - default 1.0
* @returns {Object} the headers that will be sent for the event
*/
function headers(event: CloudEvent, version = SPEC_V1) {
function headers(event: CloudEvent, version: string = SPEC_V1) {
const headers = {};
let headerMap;
if (version === SPEC_V1) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bindings/http/http_receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class HTTPReceiver {
* @param {Object|JSON} body The body of the HTTP request
* @return {CloudEvent} A new {CloudEvent} instance
*/
accept(headers: {}, body: { specversion: string }) {
accept(headers: {}, body: { specversion?: string, [k:string]: any }) {
const mode: string = getMode(headers);
const version = getVersion(mode, headers, body);
switch (version) {
Expand All @@ -70,7 +70,7 @@ function getMode(headers: { [key: string]: string }) {
throw new ValidationError("no cloud event detected");
}

function getVersion(mode: string, headers: { [key: string]: string }, body: string | { specversion: string }) {
function getVersion(mode: string, headers: { [key: string]: string }, body: string | { specversion?: string }) {
if (mode === BINARY) {
// Check the headers for the version
const versionHeader = headers[DEFAULT_SPEC_VERSION_HEADER];
Expand Down
Loading