Skip to content

Commit b0f85aa

Browse files
authored
feat: Typescript 5/4 (#72)
* typescript enhancements * fix tests * readd types to operation type * fix: metadata.id cannot be null, and added return types to all public methods * fix: make data array * add abort signal instead of abortController, split up query and mutation options
1 parent afbede9 commit b0f85aa

File tree

8 files changed

+79
-110
lines changed

8 files changed

+79
-110
lines changed

source/error.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,3 @@ export const NotUniqueError = errorFactory("NotUniqueError");
8585
export const CreateComponentError = errorFactory("CreateComponentError");
8686

8787
export const AbortError = errorFactory("AbortError");
88-
89-
const exports = {
90-
ServerError,
91-
ServerPermissionDeniedError,
92-
ServerValidationError,
93-
EventServerReplyTimeoutError,
94-
EventServerConnectionTimeoutError,
95-
EventServerPublishError,
96-
NotUniqueError,
97-
CreateComponentError,
98-
AbortError,
99-
};
100-
101-
export default exports;

source/event.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,3 @@ export class Event {
4646
this._data.source = source;
4747
}
4848
}
49-
50-
export default Event;

source/event_hub.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { v4 as uuidV4 } from "uuid";
33
import loglevel from "loglevel";
44
import io, { SocketIO } from "./socket.io-websocket-only.cjs";
5-
import Event from "./event";
5+
import { Event } from "./event";
66
import {
77
EventServerConnectionTimeoutError,
88
EventServerReplyTimeoutError,
@@ -59,7 +59,7 @@ export interface EventEntity {
5959
}
6060

6161
export interface SubscriberMetadata {
62-
id?: string;
62+
id: string;
6363
[key: string]: any;
6464
}
6565

@@ -136,7 +136,7 @@ export class EventHub {
136136
}
137137

138138
/** Connect to the event server. */
139-
connect() {
139+
connect(): void {
140140
this._socketIo = io.connect(this._serverUrl, {
141141
"max reconnection attempts": Infinity,
142142
"reconnection limit": 10000,
@@ -283,7 +283,10 @@ export class EventHub {
283283
* @param {Number} [options.timeout] Timeout in seconds [30]
284284
* @return {Promise}
285285
*/
286-
publishAndWaitForReply(event: Event, { timeout = 30 }: { timeout: number }) {
286+
publishAndWaitForReply(
287+
event: Event,
288+
{ timeout = 30 }: { timeout: number }
289+
): Promise<unknown> {
287290
const eventId = event.getData().id;
288291
const response = new Promise((resolve, reject) => {
289292
const onReply: EventCallback = (replyEvent) => {
@@ -346,8 +349,8 @@ export class EventHub {
346349
subscribe(
347350
subscription: string,
348351
callback: EventCallback,
349-
metadata: SubscriberMetadata = {}
350-
) {
352+
metadata?: SubscriberMetadata
353+
): string {
351354
const subscriber = this._addSubscriber(subscription, callback, metadata);
352355
this._notifyServerAboutSubscriber(subscriber);
353356
return subscriber.metadata.id;
@@ -359,7 +362,7 @@ export class EventHub {
359362
* @param {String} identifier Subscriber ID returned from subscribe method.
360363
* @return {Boolean} True if a subscriber was removed, false otherwise
361364
*/
362-
unsubscribe(identifier: string) {
365+
unsubscribe(identifier: string): boolean {
363366
let hasFoundSubscriberToRemove = false;
364367
this._subscribers = this._subscribers.filter((subscriber) => {
365368
if (subscriber.metadata.id === identifier) {
@@ -408,7 +411,9 @@ export class EventHub {
408411
_addSubscriber(
409412
subscription: string,
410413
callback: EventCallback,
411-
metadata: SubscriberMetadata = {}
414+
metadata: SubscriberMetadata = {
415+
id: uuidV4(),
416+
}
412417
) {
413418
// Ensure subscription is on supported format.
414419
// TODO: Remove once subscription parsing is supported.
@@ -461,9 +466,9 @@ export class EventHub {
461466
* Return null if no subscriber with *identifier* found.
462467
*
463468
* @param {String} identifier
464-
* @return {String|null}
469+
* @return {Subscriber|null}
465470
*/
466-
getSubscriberByIdentifier(identifier: string) {
471+
getSubscriberByIdentifier(identifier: string): Subscriber | null {
467472
for (const subscriber of this._subscribers.slice()) {
468473
if (subscriber.metadata.id === identifier) {
469474
return subscriber;
@@ -550,7 +555,7 @@ export class EventHub {
550555
sourceEventPayload: EventPayload,
551556
data: Data,
552557
source: Data | null = null
553-
) {
558+
): Promise<string> {
554559
const replyEvent = new Event("ftrack.meta.reply", {
555560
...data,
556561
target: `id=${sourceEventPayload.source.id}`,
@@ -561,5 +566,3 @@ export class EventHub {
561566
return this.publish(replyEvent);
562567
}
563568
}
564-
565-
export default EventHub;

source/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// :copyright: Copyright (c) 2016 ftrack
22

3-
export { Session } from "./session";
3+
export * from "./session";
44

5-
export { default as Event } from "./event";
6-
export { default as EventHub } from "./event_hub";
5+
export * from "./event";
6+
export * from "./event_hub";
77

8-
export { default as error } from "./error";
9-
export { default as operation } from "./operation";
10-
export { default as projectSchema } from "./project_schema";
8+
export * as error from "./error";
9+
export * as operation from "./operation";
10+
export * as projectSchema from "./project_schema";
1111

1212
export { SERVER_LOCATION_ID } from "./constant";

source/operation.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export type Operation =
8181
* @param {Object} data Entity data to use for creation
8282
* @return {Object} API operation
8383
*/
84-
export function createOperation(type: string, data: any): CreateOperation {
84+
export function create(type: string, data: any): CreateOperation {
8585
return {
8686
action: "create",
8787
entity_type: type,
@@ -97,7 +97,7 @@ export function createOperation(type: string, data: any): CreateOperation {
9797
* @param {string} expression API query expression
9898
* @return {Object} API operation
9999
*/
100-
export function queryOperation(expression: string): QueryOperation {
100+
export function query(expression: string): QueryOperation {
101101
return { action: "query", expression };
102102
}
103103

@@ -109,7 +109,7 @@ export function queryOperation(expression: string): QueryOperation {
109109
* @param {string} expression API query expression
110110
* @return {Object} API operation
111111
*/
112-
export function searchOperation({
112+
export function search({
113113
expression,
114114
entityType,
115115
terms,
@@ -136,7 +136,7 @@ export function searchOperation({
136136
* @param {Object} data values to update
137137
* @return {Object} API operation
138138
*/
139-
export function updateOperation(
139+
export function update(
140140
type: string,
141141
keys: string[],
142142
data: any
@@ -158,20 +158,12 @@ export function updateOperation(
158158
* @param {Array} keys Identifying keys, typically [<entity id>]
159159
* @return {Object} API operation
160160
*/
161-
export function deleteOperation(type: string, keys: string[]): DeleteOperation {
161+
function deleteOperation(type: string, keys: string[]): DeleteOperation {
162162
return {
163163
action: "delete",
164164
entity_type: type,
165165
entity_key: keys,
166166
};
167167
}
168168

169-
const exports = {
170-
query: queryOperation,
171-
create: createOperation,
172-
update: updateOperation,
173-
delete: deleteOperation,
174-
search: searchOperation,
175-
};
176-
177-
export default exports;
169+
export { deleteOperation as delete };

source/project_schema.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// :copyright: Copyright (c) 2016 ftrack
22

3-
import operation from "./operation";
4-
import Session from "./session";
3+
import * as operation from "./operation";
4+
import { Session } from "./session";
55

66
/**
77
* Project schema namespace
@@ -117,9 +117,3 @@ export function getStatuses(
117117

118118
return response;
119119
}
120-
121-
const exports = {
122-
getStatuses,
123-
};
124-
125-
export default exports;

0 commit comments

Comments
 (0)