Skip to content

Commit 3c657b9

Browse files
committed
build: add tsc type checking in the ci/test pipeline
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 <[email protected]>
1 parent cf36a15 commit 3c657b9

24 files changed

+494
-3
lines changed

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const CloudEvent: typeof import("./lib/cloudevent.js");
2+
export const HTTPReceiver: typeof import("./lib/bindings/http/http_receiver.js");

lib/bindings/http/commons.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export function sanityAndClone(headers: any): {};
2+
export function sanityContentType(contentType: any): any;

lib/bindings/http/constants.d.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
declare const _exports: {
2+
readonly HEADERS: string;
3+
readonly CHARSET_DEFAULT: string;
4+
readonly SPEC_V03: string;
5+
readonly SPEC_V1: string;
6+
readonly DEFAULT_SPEC_VERSION_HEADER: string;
7+
readonly ENCODING_BASE64: string;
8+
readonly DATA_ATTRIBUTE: string;
9+
readonly MIME_JSON: string;
10+
readonly MIME_OCTET_STREAM: string;
11+
readonly MIME_CE: string;
12+
readonly MIME_CE_JSON: string;
13+
readonly HEADER_CONTENT_TYPE: string;
14+
readonly DEFAULT_CONTENT_TYPE: string;
15+
readonly DEFAULT_CE_CONTENT_TYPE: string;
16+
readonly BINARY_HEADERS_03: {
17+
TYPE: string;
18+
SPEC_VERSION: string;
19+
SOURCE: string;
20+
ID: string;
21+
TIME: string;
22+
SCHEMA_URL: string;
23+
CONTENT_ENCONDING: string;
24+
SUBJECT: string;
25+
EXTENSIONS_PREFIX: string;
26+
};
27+
readonly STRUCTURED_ATTRS_03: {
28+
TYPE: string;
29+
SPEC_VERSION: string;
30+
SOURCE: string;
31+
ID: string;
32+
TIME: string;
33+
SCHEMA_URL: string;
34+
CONTENT_ENCONDING: string;
35+
CONTENT_TYPE: string;
36+
SUBJECT: string;
37+
DATA: string;
38+
};
39+
readonly BINARY_HEADERS_1: {
40+
TYPE: string;
41+
SPEC_VERSION: string;
42+
SOURCE: string;
43+
ID: string;
44+
TIME: string;
45+
DATA_SCHEMA: string;
46+
SUBJECT: string;
47+
EXTENSIONS_PREFIX: string;
48+
};
49+
readonly STRUCTURED_ATTRS_1: {
50+
TYPE: string;
51+
SPEC_VERSION: string;
52+
SOURCE: string;
53+
ID: string;
54+
TIME: string;
55+
DATA_SCHEMA: string;
56+
CONTENT_TYPE: string;
57+
SUBJECT: string;
58+
DATA: string;
59+
DATA_BASE64: string;
60+
};
61+
};
62+
export = _exports;

lib/bindings/http/http_receiver.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export = HTTPReceiver;
2+
/**
3+
* A class to receive a CloudEvent from an HTTP POST request.
4+
*/
5+
declare class HTTPReceiver {
6+
receivers: {
7+
v1: {
8+
structured: import("./receiver_structured_1.js");
9+
binary: import("./receiver_binary_1.js");
10+
};
11+
v03: {
12+
structured: import("./receiver_structured_0_3.js");
13+
binary: import("./receiver_binary_0_3.js");
14+
};
15+
};
16+
/**
17+
* Acceptor for an incoming HTTP CloudEvent POST. Can process
18+
* binary and structured incoming CloudEvents.
19+
*
20+
* @param {Object} headers HTTP headers keyed by header name ("Content-Type")
21+
* @param {Object|JSON} body The body of the HTTP request
22+
// @ts-ignore tsc can't find CloudEvent - is that because it's not imported?
23+
* @return {CloudEvent} A new {CloudEvent} instance
24+
*/
25+
accept(headers: any, body: any): any;
26+
}

lib/bindings/http/http_receiver.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class HTTPReceiver {
3838
*
3939
* @param {Object} headers HTTP headers keyed by header name ("Content-Type")
4040
* @param {Object|JSON} body The body of the HTTP request
41+
// @ts-ignore tsc can't find CloudEvent - is that because it's not imported?
4142
* @return {CloudEvent} A new {CloudEvent} instance
4243
*/
4344
accept(headers, body) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export = BinaryHTTPReceiver;
2+
declare function BinaryHTTPReceiver(parsersByEncoding: any, setterByHeader: any, allowedContentTypes: any, requiredHeaders: any, Spec: any, specversion: any, extensionsPrefix: any, checkDecorator: any): void;
3+
declare class BinaryHTTPReceiver {
4+
constructor(parsersByEncoding: any, setterByHeader: any, allowedContentTypes: any, requiredHeaders: any, Spec: any, specversion: any, extensionsPrefix: any, checkDecorator: any);
5+
parsersByEncoding: any;
6+
setterByHeader: any;
7+
allowedContentTypes: any;
8+
requiredHeaders: any;
9+
Spec: any;
10+
spec: any;
11+
specversion: any;
12+
extensionsPrefix: any;
13+
checkDecorator: any;
14+
check(payload: any, headers: any): void;
15+
parse(payload: any, headers: any): import("../../cloudevent.js");
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export = Receiver;
2+
declare function Receiver(configuration: any): void;
3+
declare class Receiver {
4+
constructor(configuration: any);
5+
receiver: import("./receiver_binary.js");
6+
check(payload: any, headers: any): void;
7+
parse(payload: any, headers: any): import("../../cloudevent.js");
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export = Receiver;
2+
declare function Receiver(configuration: any): void;
3+
declare class Receiver {
4+
constructor(configuration: any);
5+
receiver: import("./receiver_binary.js");
6+
check(payload: any, headers: any): void;
7+
parse(payload: any, headers: any): import("../../cloudevent.js");
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export = StructuredHTTPReceiver;
2+
declare function StructuredHTTPReceiver(parserByMime: any, parserMap: any, allowedContentTypes: any, Spec: any): void;
3+
declare class StructuredHTTPReceiver {
4+
constructor(parserByMime: any, parserMap: any, allowedContentTypes: any, Spec: any);
5+
parserByMime: any;
6+
parserMap: any;
7+
allowedContentTypes: any;
8+
Spec: any;
9+
spec: any;
10+
check(payload: any, headers: any): void;
11+
parse(payload: any, headers: any): import("../../cloudevent.js");
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export = Receiver;
2+
declare function Receiver(configuration: any): void;
3+
declare class Receiver {
4+
constructor(configuration: any);
5+
receiver: import("./receiver_structured.js");
6+
check(payload: any, headers: any): void;
7+
parse(payload: any, headers: any): import("../../cloudevent.js");
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export = Receiver;
2+
declare function Receiver(configuration: any): void;
3+
declare class Receiver {
4+
constructor(configuration: any);
5+
receiver: import("./receiver_structured.js");
6+
check(payload: any, headers: any): void;
7+
parse(payload: any, headers: any): import("../../cloudevent.js");
8+
}

lib/cloudevent.d.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
export = CloudEvent;
2+
/**
3+
* An instance of a CloudEvent.
4+
*/
5+
declare class CloudEvent {
6+
/**
7+
* Creates a new CloudEvent instance
8+
* @param {Spec} [userSpec] A CloudEvent version specification
9+
* @param {Formatter} [userFormatter] Converts the event into a readable string
10+
*/
11+
constructor(userSpec?: import("./specs/spec_1.js"), userFormatter?: import("./formats/json/formatter.js"));
12+
spec: any;
13+
formatter: any;
14+
extensions: {};
15+
/**
16+
* Get the formatters available to this CloudEvent
17+
* @returns {Object} a JSON formatter
18+
*/
19+
getFormats(): any;
20+
/**
21+
* Formats the CloudEvent as JSON. Validates the event according
22+
* to the CloudEvent specification and throws an exception if
23+
* it's invalid.
24+
* @returns {JSON} the CloudEvent in JSON form
25+
*/
26+
format(): JSON;
27+
/**
28+
* Formats the CLoudEvent as JSON. No specification validation
29+
* is performed.
30+
* @returns {JSON} the CloudEvent in JSON form
31+
*/
32+
toString(): JSON;
33+
/**
34+
* Sets the event type
35+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
36+
* @param {string} type the type of event related to the originating source
37+
* @returns {CloudEvent} this CloudEvent
38+
*/
39+
type(type: string): CloudEvent;
40+
/**
41+
* Gets the event type
42+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
43+
* @returns {String} the type of event related to the originating source
44+
*/
45+
getType(): string;
46+
specversion(version: any): any;
47+
/**
48+
* Gets the CloudEvent specification version
49+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#specversion
50+
* @returns {string} The CloudEvent version that this event adheres to
51+
*/
52+
getSpecversion(): string;
53+
/**
54+
* Sets the origination source of this event.
55+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
56+
// @ts-ignore tsc can't find URI type
57+
* @param {URI} source the context in which the event happened
58+
* @returns {CloudEvent} this CloudEvent instance
59+
*/
60+
source(source: any): CloudEvent;
61+
/**
62+
* Gets the origination source of this event.
63+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
64+
* @returns {string} the event source
65+
*/
66+
getSource(): string;
67+
/**
68+
* Sets the event id. Source + id must be unique for each distinct event.
69+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#id
70+
* @param {string} id source+id must be unique for each distinct event
71+
* @returns {CloudEvent} this CloudEvent instance
72+
*/
73+
id(id: string): CloudEvent;
74+
/**
75+
* Gets the event id.
76+
* @returns {string} the event id
77+
*/
78+
getId(): string;
79+
/**
80+
* Sets the timestamp for this event
81+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
82+
* @param {Date} time timestamp when the event occurred
83+
* @returns {CloudEvent} this CloudEvent instance
84+
*/
85+
time(time: Date): CloudEvent;
86+
/**
87+
* Gets the timestamp for this event
88+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
89+
* @returns {Date} the timestamp for this event
90+
*/
91+
getTime(): Date;
92+
schemaurl(schemaurl: any): CloudEvent;
93+
getSchemaurl(): any;
94+
/**
95+
* Sets the content type of the data value for this event
96+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
97+
* @param {string} contenttype per https://tools.ietf.org/html/rfc2046
98+
* @returns {CloudEvent} this CloudEvent instance
99+
*/
100+
dataContenttype(contenttype: string): CloudEvent;
101+
/**
102+
* Gets the content type of the data value for this event
103+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
104+
* @returns {string} the content type for the data in this event
105+
*/
106+
getDataContenttype(): string;
107+
/**
108+
* Sets the data for this event
109+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
110+
* @param {*} data any data associated with this event
111+
* @returns {CloudEvent} this CloudEvent instance
112+
*/
113+
data(data: any): CloudEvent;
114+
/**
115+
* Gets any data that has been set for this event
116+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
117+
* @returns {*} any data set for this event
118+
*/
119+
getData(): any;
120+
/**
121+
* Adds an extension attribute to this CloudEvent
122+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
123+
* @param {*} key the name of the extension attribute
124+
* @param {*} value the value of the extension attribute
125+
* @returns {CloudEvent} this CloudEvent instance
126+
*/
127+
addExtension(key: any, value: any): CloudEvent;
128+
/**
129+
* Gets the extension attributes, if any, associated with this event
130+
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
131+
* @returns {Object} the extensions attributes - if none exist will will be {}
132+
* // TODO - this should return null or undefined if no extensions
133+
*/
134+
getExtensions(): any;
135+
}

lib/cloudevent.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class CloudEvent {
1111
* @param {Formatter} [userFormatter] Converts the event into a readable string
1212
*/
1313
constructor(userSpec, userFormatter) {
14-
this.spec = userSpec ? new userSpec(CloudEvent) : new Spec(CloudEvent);
15-
this.formatter = userFormatter ? new userFormatter() : new Formatter();
14+
// @ts-ignore
15+
this.spec = (userSpec) ? new userSpec(CloudEvent) : new Spec(CloudEvent);
16+
// @ts-ignore
17+
this.formatter = (userFormatter) ? new userFormatter() : new Formatter();
1618

1719
// The map of extensions
1820
this.extensions = {};
@@ -93,6 +95,7 @@ class CloudEvent {
9395
/**
9496
* Sets the origination source of this event.
9597
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
98+
// @ts-ignore tsc can't find URI type
9699
* @param {URI} source the context in which the event happened
97100
* @returns {CloudEvent} this CloudEvent instance
98101
*/

lib/formats/base64.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export = Base64Parser;
2+
declare class Base64Parser {
3+
constructor(decorator: any);
4+
decorator: any;
5+
parse(payload: any): string;
6+
}

lib/formats/json/formatter.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export = JSONFormatter;
2+
declare class JSONFormatter {
3+
format(payload: any): any;
4+
toString(payload: any): string;
5+
}

lib/formats/json/parser.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export = JSONParser;
2+
declare class JSONParser {
3+
constructor(decorator: any);
4+
decorator: any;
5+
/**
6+
* Parses the payload with an optional decorator
7+
* @param {object|string} payload the JSON payload
8+
* @return {object} the parsed JSON payload.
9+
*/
10+
parse(payload: any): any;
11+
}

lib/specs/spec_0_3.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export = Spec03;
2+
declare function Spec03(_caller: any): void;
3+
declare class Spec03 {
4+
constructor(_caller: any);
5+
payload: {
6+
specversion: string;
7+
id: any;
8+
};
9+
caller: any;
10+
check(ce: any): void;
11+
id(_id: any): Spec03;
12+
getId(): any;
13+
source(_source: any): Spec03;
14+
getSource(): any;
15+
specversion(): Spec03;
16+
getSpecversion(): string;
17+
type(_type: any): Spec03;
18+
getType(): any;
19+
dataContentEncoding(encoding: any): Spec03;
20+
getDataContentEncoding(): any;
21+
dataContentType(_contenttype: any): Spec03;
22+
getDataContentType(): any;
23+
schemaurl(_schemaurl: any): Spec03;
24+
getSchemaurl(): any;
25+
subject(_subject: any): Spec03;
26+
getSubject(): any;
27+
time(_time: any): Spec03;
28+
getTime(): any;
29+
data(_data: any): Spec03;
30+
getData(): any;
31+
addExtension(key: any, value: any): Spec03;
32+
}

0 commit comments

Comments
 (0)