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: Added response types for different actions #83

Merged
merged 8 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions source/project_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

import { Data, QueryResponse } from "./types";
/**
* Project schema namespace
* @namespace project_schema
Expand Down Expand Up @@ -71,7 +71,7 @@ export function getStatuses(
)
);

response = session.call(operations);
response = session.call<QueryResponse<Data>>(operations);
response = response.then((results) => {
// Since the operations where performed in one batched call,
// the result will be merged into a single entity.
Expand Down
144 changes: 54 additions & 90 deletions source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,25 @@ import {
import { SERVER_LOCATION_ID } from "./constant";

import normalizeString from "./util/normalize_string";
import { Data } from "./types";
import type {
ActionResponse,
CallOptions,
CreateComponentOptions,
CreateResponse,
Data,
DeleteResponse,
GetUploadMetadataResponse,
IsTuple,
MutationOptions,
QueryOptions,
QueryResponse,
QueryServerInformationResponse,
ResponseError,
SearchOptions,
SearchResponse,
SessionOptions,
UpdateResponse,
} from "./types";
import { convertToIsoString } from "./util/convert_to_iso_string";

const logger = loglevel.getLogger("ftrack_api");
Expand All @@ -42,75 +60,6 @@ function splitFileExtension(fileName: string) {
return [basename, extension];
}

export interface EventHubOptions {
applicationId?: string;
}

export interface SessionOptions {
autoConnectEventHub?: boolean;
serverInformationValues?: string[];
eventHubOptions?: EventHubOptions;
clientToken?: string;
apiEndpoint?: string;
additionalHeaders?: Data;
strictApi?: boolean;
}

export interface CreateComponentOptions {
name?: string;
data?: Data;
onProgress?: (progress: number) => unknown;
xhr?: XMLHttpRequest;
onAborted?: () => unknown;
}

export interface Entity {
id: string;
__entity_type__: string;
}

export interface SearchOptions {
expression: string;
entityType: string;
terms?: string[];
contextId?: string;
objectTypeIds?: string[];
}

export interface Response<T> {
url?: any;
headers?: any;
action: string;
metadata: {
next: {
offset: number | null;
};
};
data: T[];
}

export interface ResponseError {
exception: string;
content: string;
error_code?: string;
error?: Data;
}

export interface MutationOptions {
pushToken?: string;
additionalHeaders?: Data;
decodeDatesAsIso?: boolean;
}

export interface QueryOptions {
abortController?: AbortController;
signal?: AbortSignal;
additionalHeaders?: Data;
decodeDatesAsIso?: boolean;
}

export interface CallOptions extends MutationOptions, QueryOptions {}

/**
* ftrack API session
* @class Session
Expand Down Expand Up @@ -264,7 +213,9 @@ export class Session {
* @instance
* @type {Promise}
*/
this.initializing = this.call(operations).then((responses) => {
this.initializing = this.call<
[QueryServerInformationResponse, QueryResponse]
>(operations).then((responses) => {
this.serverInformation = responses[0];
this.schemas = responses[1];
this.serverVersion = this.serverInformation.version;
Expand Down Expand Up @@ -558,6 +509,7 @@ export class Session {
* ServerError
* Generic server errors or network issues
*
* @typeParam T - either an array of responseTypes to get return type Tuple or a single response to get return type T[]. Default is ActionResponse
gismya marked this conversation as resolved.
Show resolved Hide resolved
* @param {Array} operations - API operations.
* @param {Object} options
* @param {AbortController} options.abortController - Abort controller, deprecated in favor of options.signal
Expand All @@ -567,7 +519,7 @@ export class Session {
* @param {string} options.decodeDatesAsIso - Return dates as ISO strings instead of moment objects
*
*/
call(
call<T = ActionResponse>(
operations: operation.Operation[],
{
abortController,
Expand All @@ -576,7 +528,7 @@ export class Session {
additionalHeaders = {},
decodeDatesAsIso = false,
}: CallOptions = {}
): Promise<Response<Data>[]> {
): Promise<IsTuple<T> extends true ? T : T[]> {
gismya marked this conversation as resolved.
Show resolved Hide resolved
const url = `${this.serverUrl}${this.apiEndpoint}`;

// Delay call until session is initialized if initialization is in
Expand Down Expand Up @@ -794,7 +746,10 @@ export class Session {
query(expression: string, options: QueryOptions = {}) {
logger.debug("Query", expression);
const queryOperation = operation.query(expression);
let request = this.call([queryOperation], options).then((responses) => {
let request = this.call<[QueryResponse<Data>]>(
[queryOperation],
options
).then((responses) => {
const response = responses[0];
return response;
});
Expand Down Expand Up @@ -844,7 +799,10 @@ export class Session {
contextId,
objectTypeIds,
});
let request = this.call([searchOperation], options).then((responses) => {
let request = this.call<[SearchResponse<Data>]>(
[searchOperation],
options
).then((responses) => {
const response = responses[0];
return response;
});
Expand All @@ -866,12 +824,13 @@ export class Session {
create(entityType: string, data: Data, options: MutationOptions = {}) {
logger.debug("Create", entityType, data, options);

let request = this.call([operation.create(entityType, data)], options).then(
(responses) => {
const response = responses[0];
return response;
}
);
let request = this.call<[CreateResponse<Data>]>(
[operation.create(entityType, data)],
options
).then((responses) => {
const response = responses[0];
return response;
});

return request;
}
Expand All @@ -896,7 +855,7 @@ export class Session {
) {
logger.debug("Update", type, keys, data, options);

const request = this.call(
const request = this.call<[UpdateResponse<Data>]>(
[operation.update(type, keys, data)],
options
).then((responses) => {
Expand All @@ -921,12 +880,13 @@ export class Session {
delete(type: string, keys: string[], options: MutationOptions = {}) {
logger.debug("Delete", type, keys, options);

let request = this.call([operation.delete(type, keys)], options).then(
(responses) => {
const response = responses[0];
return response;
}
);
let request = this.call<[DeleteResponse]>(
[operation.delete(type, keys)],
options
).then((responses) => {
const response = responses[0];
return response;
});

return request;
}
Expand Down Expand Up @@ -997,7 +957,9 @@ export class Session {
createComponent(
file: Blob,
options: CreateComponentOptions = {}
): Promise<Response<Data>[]> {
): Promise<
[CreateResponse<Data>, CreateResponse<Data>, GetUploadMetadataResponse]
> {
const componentName = options.name ?? (file as File).name;

let normalizedFileName;
Expand Down Expand Up @@ -1052,7 +1014,9 @@ export class Session {
location_id: SERVER_LOCATION_ID,
};

const componentAndLocationPromise = this.call([
const componentAndLocationPromise = this.call<
[CreateResponse, CreateResponse, GetUploadMetadataResponse]
>([
operation.create("FileComponent", component),
operation.create("ComponentLocation", componentLocation),
{
Expand Down
Loading