Skip to content
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

feat: Typescript 5/4 #72

Merged
merged 6 commits into from
Feb 9, 2023
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
14 changes: 0 additions & 14 deletions source/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,3 @@ export const NotUniqueError = errorFactory("NotUniqueError");
export const CreateComponentError = errorFactory("CreateComponentError");

export const AbortError = errorFactory("AbortError");

const exports = {
ServerError,
ServerPermissionDeniedError,
ServerValidationError,
EventServerReplyTimeoutError,
EventServerConnectionTimeoutError,
EventServerPublishError,
NotUniqueError,
CreateComponentError,
AbortError,
};

export default exports;
2 changes: 0 additions & 2 deletions source/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,3 @@ export class Event {
this._data.source = source;
}
}

export default Event;
29 changes: 16 additions & 13 deletions source/event_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { v4 as uuidV4 } from "uuid";
import loglevel from "loglevel";
import io, { SocketIO } from "./socket.io-websocket-only.cjs";
import Event from "./event";
import { Event } from "./event";
import {
EventServerConnectionTimeoutError,
EventServerReplyTimeoutError,
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface EventEntity {
}

export interface SubscriberMetadata {
id?: string;
id: string;
[key: string]: any;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ export class EventHub {
}

/** Connect to the event server. */
connect() {
connect(): void {
this._socketIo = io.connect(this._serverUrl, {
"max reconnection attempts": Infinity,
"reconnection limit": 10000,
Expand Down Expand Up @@ -283,7 +283,10 @@ export class EventHub {
* @param {Number} [options.timeout] Timeout in seconds [30]
* @return {Promise}
*/
publishAndWaitForReply(event: Event, { timeout = 30 }: { timeout: number }) {
publishAndWaitForReply(
event: Event,
{ timeout = 30 }: { timeout: number }
): Promise<unknown> {
const eventId = event.getData().id;
const response = new Promise((resolve, reject) => {
const onReply: EventCallback = (replyEvent) => {
Expand Down Expand Up @@ -346,8 +349,8 @@ export class EventHub {
subscribe(
subscription: string,
callback: EventCallback,
metadata: SubscriberMetadata = {}
) {
metadata?: SubscriberMetadata
): string {
const subscriber = this._addSubscriber(subscription, callback, metadata);
this._notifyServerAboutSubscriber(subscriber);
return subscriber.metadata.id;
Expand All @@ -359,7 +362,7 @@ export class EventHub {
* @param {String} identifier Subscriber ID returned from subscribe method.
* @return {Boolean} True if a subscriber was removed, false otherwise
*/
unsubscribe(identifier: string) {
unsubscribe(identifier: string): boolean {
let hasFoundSubscriberToRemove = false;
this._subscribers = this._subscribers.filter((subscriber) => {
if (subscriber.metadata.id === identifier) {
Expand Down Expand Up @@ -408,7 +411,9 @@ export class EventHub {
_addSubscriber(
subscription: string,
callback: EventCallback,
metadata: SubscriberMetadata = {}
metadata: SubscriberMetadata = {
id: uuidV4(),
}
) {
// Ensure subscription is on supported format.
// TODO: Remove once subscription parsing is supported.
Expand Down Expand Up @@ -461,9 +466,9 @@ export class EventHub {
* Return null if no subscriber with *identifier* found.
*
* @param {String} identifier
* @return {String|null}
* @return {Subscriber|null}
*/
getSubscriberByIdentifier(identifier: string) {
getSubscriberByIdentifier(identifier: string): Subscriber | null {
for (const subscriber of this._subscribers.slice()) {
if (subscriber.metadata.id === identifier) {
return subscriber;
Expand Down Expand Up @@ -550,7 +555,7 @@ export class EventHub {
sourceEventPayload: EventPayload,
data: Data,
source: Data | null = null
) {
): Promise<string> {
const replyEvent = new Event("ftrack.meta.reply", {
...data,
target: `id=${sourceEventPayload.source.id}`,
Expand All @@ -561,5 +566,3 @@ export class EventHub {
return this.publish(replyEvent);
}
}

export default EventHub;
12 changes: 6 additions & 6 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// :copyright: Copyright (c) 2016 ftrack

export { Session } from "./session";
export * from "./session";

export { default as Event } from "./event";
export { default as EventHub } from "./event_hub";
export * from "./event";
export * from "./event_hub";

export { default as error } from "./error";
export { default as operation } from "./operation";
export { default as projectSchema } from "./project_schema";
export * as error from "./error";
export * as operation from "./operation";
export * as projectSchema from "./project_schema";

export { SERVER_LOCATION_ID } from "./constant";
20 changes: 6 additions & 14 deletions source/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export type Operation =
* @param {Object} data Entity data to use for creation
* @return {Object} API operation
*/
export function createOperation(type: string, data: any): CreateOperation {
export function create(type: string, data: any): CreateOperation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though they probably are not ment to be used outside this project, must we not wait with changing function names until it's time to make a major version release?

Copy link
Contributor Author

@jimmycallin jimmycallin Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they were not exposed publicly from index.ts, so only way to have used them would be if someone imported them directly from @ftrack/api/dist/operation, which I don't think is supported behavior. The function names reflect their names in the now removed default object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm just overly cautious.

return {
action: "create",
entity_type: type,
Expand All @@ -97,7 +97,7 @@ export function createOperation(type: string, data: any): CreateOperation {
* @param {string} expression API query expression
* @return {Object} API operation
*/
export function queryOperation(expression: string): QueryOperation {
export function query(expression: string): QueryOperation {
return { action: "query", expression };
}

Expand All @@ -109,7 +109,7 @@ export function queryOperation(expression: string): QueryOperation {
* @param {string} expression API query expression
* @return {Object} API operation
*/
export function searchOperation({
export function search({
expression,
entityType,
terms,
Expand All @@ -136,7 +136,7 @@ export function searchOperation({
* @param {Object} data values to update
* @return {Object} API operation
*/
export function updateOperation(
export function update(
type: string,
keys: string[],
data: any
Expand All @@ -158,20 +158,12 @@ export function updateOperation(
* @param {Array} keys Identifying keys, typically [<entity id>]
* @return {Object} API operation
*/
export function deleteOperation(type: string, keys: string[]): DeleteOperation {
function deleteOperation(type: string, keys: string[]): DeleteOperation {
return {
action: "delete",
entity_type: type,
entity_key: keys,
};
}

const exports = {
query: queryOperation,
create: createOperation,
update: updateOperation,
delete: deleteOperation,
search: searchOperation,
};

export default exports;
export { deleteOperation as delete };
10 changes: 2 additions & 8 deletions source/project_schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// :copyright: Copyright (c) 2016 ftrack

import operation from "./operation";
import Session from "./session";
import * as operation from "./operation";
import { Session } from "./session";

/**
* Project schema namespace
Expand Down Expand Up @@ -117,9 +117,3 @@ export function getStatuses(

return response;
}

const exports = {
getStatuses,
};

export default exports;
Loading