Skip to content

Commit

Permalink
build: add tsc type checking in the ci/test pipeline
Browse files Browse the repository at this point in the history
This commit introduces TypeScript checks and generates type declarations
for the existing JavaScript codebase using `tsc` prior to running the linter task.

Ref: cloudevents#9
Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance committed May 13, 2020
1 parent 09b0c76 commit 704278b
Show file tree
Hide file tree
Showing 24 changed files with 495 additions and 5 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CloudEvent: typeof import("./lib/cloudevent.js");
export const HTTPReceiver: typeof import("./lib/bindings/http/http_receiver.js");
2 changes: 2 additions & 0 deletions lib/bindings/http/commons.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function sanityAndClone(headers: any): {};
export function sanityContentType(contentType: any): any;
62 changes: 62 additions & 0 deletions lib/bindings/http/constants.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
declare const _exports: {
readonly HEADERS: string;
readonly CHARSET_DEFAULT: string;
readonly SPEC_V03: string;
readonly SPEC_V1: string;
readonly DEFAULT_SPEC_VERSION_HEADER: string;
readonly ENCODING_BASE64: string;
readonly DATA_ATTRIBUTE: string;
readonly MIME_JSON: string;
readonly MIME_OCTET_STREAM: string;
readonly MIME_CE: string;
readonly MIME_CE_JSON: string;
readonly HEADER_CONTENT_TYPE: string;
readonly DEFAULT_CONTENT_TYPE: string;
readonly DEFAULT_CE_CONTENT_TYPE: string;
readonly BINARY_HEADERS_03: {
TYPE: string;
SPEC_VERSION: string;
SOURCE: string;
ID: string;
TIME: string;
SCHEMA_URL: string;
CONTENT_ENCONDING: string;
SUBJECT: string;
EXTENSIONS_PREFIX: string;
};
readonly STRUCTURED_ATTRS_03: {
TYPE: string;
SPEC_VERSION: string;
SOURCE: string;
ID: string;
TIME: string;
SCHEMA_URL: string;
CONTENT_ENCONDING: string;
CONTENT_TYPE: string;
SUBJECT: string;
DATA: string;
};
readonly BINARY_HEADERS_1: {
TYPE: string;
SPEC_VERSION: string;
SOURCE: string;
ID: string;
TIME: string;
DATA_SCHEMA: string;
SUBJECT: string;
EXTENSIONS_PREFIX: string;
};
readonly STRUCTURED_ATTRS_1: {
TYPE: string;
SPEC_VERSION: string;
SOURCE: string;
ID: string;
TIME: string;
DATA_SCHEMA: string;
CONTENT_TYPE: string;
SUBJECT: string;
DATA: string;
DATA_BASE64: string;
};
};
export = _exports;
26 changes: 26 additions & 0 deletions lib/bindings/http/http_receiver.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export = HTTPReceiver;
/**
* A class to receive a CloudEvent from an HTTP POST request.
*/
declare class HTTPReceiver {
receivers: {
v1: {
structured: import("./receiver_structured_1.js");
binary: import("./receiver_binary_1.js");
};
v03: {
structured: import("./receiver_structured_0_3.js");
binary: import("./receiver_binary_0_3.js");
};
};
/**
* Acceptor for an incoming HTTP CloudEvent POST. Can process
* binary and structured incoming CloudEvents.
*
* @param {Object} headers HTTP headers keyed by header name ("Content-Type")
* @param {Object|JSON} body The body of the HTTP request
// @ts-ignore tsc can't find CloudEvent - is that because it's not imported?
* @return {CloudEvent} A new {CloudEvent} instance
*/
accept(headers: any, body: any): any;
}
4 changes: 2 additions & 2 deletions lib/bindings/http/http_receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class HTTPReceiver {
*
* @param {Object} headers HTTP headers keyed by header name ("Content-Type")
* @param {Object|JSON} body The body of the HTTP request
// @ts-ignore tsc can't find CloudEvent - is that because it's not imported?
* @return {CloudEvent} A new {CloudEvent} instance
*/
accept(headers, body) {
Expand Down Expand Up @@ -76,8 +77,7 @@ function getVersion(mode, headers, body) {
if (versionHeader) { version = versionHeader; }
} else {
// structured mode - the version is in the body
version = body instanceof String
? JSON.parse(body).specversion : body.specversion;
version = (typeof body == "string") ? JSON.parse(body).specversion : body.specversion;
}
return version;
}
Expand Down
16 changes: 16 additions & 0 deletions lib/bindings/http/receiver_binary.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export = BinaryHTTPReceiver;
declare function BinaryHTTPReceiver(parsersByEncoding: any, setterByHeader: any, allowedContentTypes: any, requiredHeaders: any, Spec: any, specversion: any, extensionsPrefix: any, checkDecorator: any): void;
declare class BinaryHTTPReceiver {
constructor(parsersByEncoding: any, setterByHeader: any, allowedContentTypes: any, requiredHeaders: any, Spec: any, specversion: any, extensionsPrefix: any, checkDecorator: any);
parsersByEncoding: any;
setterByHeader: any;
allowedContentTypes: any;
requiredHeaders: any;
Spec: any;
spec: any;
specversion: any;
extensionsPrefix: any;
checkDecorator: any;
check(payload: any, headers: any): void;
parse(payload: any, headers: any): import("../../cloudevent.js");
}
8 changes: 8 additions & 0 deletions lib/bindings/http/receiver_binary_0_3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = Receiver;
declare function Receiver(configuration: any): void;
declare class Receiver {
constructor(configuration: any);
receiver: import("./receiver_binary.js");
check(payload: any, headers: any): void;
parse(payload: any, headers: any): import("../../cloudevent.js");
}
8 changes: 8 additions & 0 deletions lib/bindings/http/receiver_binary_1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = Receiver;
declare function Receiver(configuration: any): void;
declare class Receiver {
constructor(configuration: any);
receiver: import("./receiver_binary.js");
check(payload: any, headers: any): void;
parse(payload: any, headers: any): import("../../cloudevent.js");
}
12 changes: 12 additions & 0 deletions lib/bindings/http/receiver_structured.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export = StructuredHTTPReceiver;
declare function StructuredHTTPReceiver(parserByMime: any, parserMap: any, allowedContentTypes: any, Spec: any): void;
declare class StructuredHTTPReceiver {
constructor(parserByMime: any, parserMap: any, allowedContentTypes: any, Spec: any);
parserByMime: any;
parserMap: any;
allowedContentTypes: any;
Spec: any;
spec: any;
check(payload: any, headers: any): void;
parse(payload: any, headers: any): import("../../cloudevent.js");
}
8 changes: 8 additions & 0 deletions lib/bindings/http/receiver_structured_0_3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = Receiver;
declare function Receiver(configuration: any): void;
declare class Receiver {
constructor(configuration: any);
receiver: import("./receiver_structured.js");
check(payload: any, headers: any): void;
parse(payload: any, headers: any): import("../../cloudevent.js");
}
8 changes: 8 additions & 0 deletions lib/bindings/http/receiver_structured_1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = Receiver;
declare function Receiver(configuration: any): void;
declare class Receiver {
constructor(configuration: any);
receiver: import("./receiver_structured.js");
check(payload: any, headers: any): void;
parse(payload: any, headers: any): import("../../cloudevent.js");
}
135 changes: 135 additions & 0 deletions lib/cloudevent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
export = CloudEvent;
/**
* An instance of a CloudEvent.
*/
declare class CloudEvent {
/**
* Creates a new CloudEvent instance
* @param {Spec} [userSpec] A CloudEvent version specification
* @param {Formatter} [userFormatter] Converts the event into a readable string
*/
constructor(userSpec?: import("./specs/spec_1.js"), userFormatter?: import("./formats/json/formatter.js"));
spec: any;
formatter: any;
extensions: {};
/**
* Get the formatters available to this CloudEvent
* @returns {Object} a JSON formatter
*/
getFormats(): any;
/**
* Formats the CloudEvent as JSON. Validates the event according
* to the CloudEvent specification and throws an exception if
* it's invalid.
* @returns {JSON} the CloudEvent in JSON form
*/
format(): JSON;
/**
* Formats the CLoudEvent as JSON. No specification validation
* is performed.
* @returns {JSON} the CloudEvent in JSON form
*/
toString(): JSON;
/**
* Sets the event type
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
* @param {string} type the type of event related to the originating source
* @returns {CloudEvent} this CloudEvent
*/
type(type: string): CloudEvent;
/**
* Gets the event type
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
* @returns {String} the type of event related to the originating source
*/
getType(): string;
specversion(version: any): any;
/**
* Gets the CloudEvent specification version
* @see https://github.com/cloudevents/spec/blob/master/spec.md#specversion
* @returns {string} The CloudEvent version that this event adheres to
*/
getSpecversion(): string;
/**
* Sets the origination source of this event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
// @ts-ignore tsc can't find URI type
* @param {URI} source the context in which the event happened
* @returns {CloudEvent} this CloudEvent instance
*/
source(source: any): CloudEvent;
/**
* Gets the origination source of this event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
* @returns {string} the event source
*/
getSource(): string;
/**
* Sets the event id. Source + id must be unique for each distinct event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#id
* @param {string} id source+id must be unique for each distinct event
* @returns {CloudEvent} this CloudEvent instance
*/
id(id: string): CloudEvent;
/**
* Gets the event id.
* @returns {string} the event id
*/
getId(): string;
/**
* Sets the timestamp for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
* @param {Date} time timestamp when the event occurred
* @returns {CloudEvent} this CloudEvent instance
*/
time(time: Date): CloudEvent;
/**
* Gets the timestamp for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
* @returns {Date} the timestamp for this event
*/
getTime(): Date;
schemaurl(schemaurl: any): CloudEvent;
getSchemaurl(): any;
/**
* Sets the content type of the data value for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
* @param {string} contenttype per https://tools.ietf.org/html/rfc2046
* @returns {CloudEvent} this CloudEvent instance
*/
dataContenttype(contenttype: string): CloudEvent;
/**
* Gets the content type of the data value for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
* @returns {string} the content type for the data in this event
*/
getDataContenttype(): string;
/**
* Sets the data for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
* @param {*} data any data associated with this event
* @returns {CloudEvent} this CloudEvent instance
*/
data(data: any): CloudEvent;
/**
* Gets any data that has been set for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
* @returns {*} any data set for this event
*/
getData(): any;
/**
* Adds an extension attribute to this CloudEvent
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
* @param {*} key the name of the extension attribute
* @param {*} value the value of the extension attribute
* @returns {CloudEvent} this CloudEvent instance
*/
addExtension(key: any, value: any): CloudEvent;
/**
* Gets the extension attributes, if any, associated with this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
* @returns {Object} the extensions attributes - if none exist will will be {}
* // TODO - this should return null or undefined if no extensions
*/
getExtensions(): any;
}
7 changes: 5 additions & 2 deletions lib/cloudevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class CloudEvent {
* @param {Formatter} [userFormatter] Converts the event into a readable string
*/
constructor(userSpec, userFormatter) {
this.spec = userSpec ? new userSpec(CloudEvent) : new Spec(CloudEvent);
this.formatter = userFormatter ? new userFormatter() : new Formatter();
// @ts-ignore
this.spec = (userSpec) ? new userSpec(CloudEvent) : new Spec(CloudEvent);
// @ts-ignore
this.formatter = (userFormatter) ? new userFormatter() : new Formatter();

// The map of extensions
this.extensions = {};
Expand Down Expand Up @@ -93,6 +95,7 @@ class CloudEvent {
/**
* Sets the origination source of this event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
// @ts-ignore tsc can't find URI type
* @param {URI} source the context in which the event happened
* @returns {CloudEvent} this CloudEvent instance
*/
Expand Down
6 changes: 6 additions & 0 deletions lib/formats/base64.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export = Base64Parser;
declare class Base64Parser {
constructor(decorator: any);
decorator: any;
parse(payload: any): string;
}
5 changes: 5 additions & 0 deletions lib/formats/json/formatter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export = JSONFormatter;
declare class JSONFormatter {
format(payload: any): any;
toString(payload: any): string;
}
11 changes: 11 additions & 0 deletions lib/formats/json/parser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export = JSONParser;
declare class JSONParser {
constructor(decorator: any);
decorator: any;
/**
* Parses the payload with an optional decorator
* @param {object|string} payload the JSON payload
* @return {object} the parsed JSON payload.
*/
parse(payload: any): any;
}
32 changes: 32 additions & 0 deletions lib/specs/spec_0_3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export = Spec03;
declare function Spec03(_caller: any): void;
declare class Spec03 {
constructor(_caller: any);
payload: {
specversion: string;
id: any;
};
caller: any;
check(ce: any): void;
id(_id: any): Spec03;
getId(): any;
source(_source: any): Spec03;
getSource(): any;
specversion(): Spec03;
getSpecversion(): string;
type(_type: any): Spec03;
getType(): any;
dataContentEncoding(encoding: any): Spec03;
getDataContentEncoding(): any;
dataContentType(_contenttype: any): Spec03;
getDataContentType(): any;
schemaurl(_schemaurl: any): Spec03;
getSchemaurl(): any;
subject(_subject: any): Spec03;
getSubject(): any;
time(_time: any): Spec03;
getTime(): any;
data(_data: any): Spec03;
getData(): any;
addExtension(key: any, value: any): Spec03;
}
Loading

0 comments on commit 704278b

Please sign in to comment.