Skip to content

feat(TS): Changed tests to TypeScript and updated typings #136

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

Merged
merged 2 commits into from
Sep 11, 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
11 changes: 7 additions & 4 deletions source/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export interface SearchOperation {
export interface UpdateOperation {
action: "update";
entity_type: string;
entity_key: string[];
entity_key: string[] | string;
entity_data: any;
}

export interface DeleteOperation {
action: "delete";
entity_type: string;
entity_key: string[];
entity_key: string[] | string;
}

export interface QueryServerInformationOperation {
Expand Down Expand Up @@ -138,7 +138,7 @@ export function search({
*/
export function update(
type: string,
keys: string[],
keys: string[] | string,
data: any
): UpdateOperation {
return {
Expand All @@ -158,7 +158,10 @@ export function update(
* @param {Array} keys Identifying keys, typically [<entity id>]
* @return {Object} API operation
*/
function deleteOperation(type: string, keys: string[]): DeleteOperation {
function deleteOperation(
type: string,
keys: string[] | string
): DeleteOperation {
return {
action: "delete",
entity_type: type,
Expand Down
21 changes: 14 additions & 7 deletions source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
CreateResponse,
Data,
DeleteResponse,
Entity,
GetUploadMetadataResponse,
IsTuple,
MutationOptions,
Expand Down Expand Up @@ -669,7 +670,7 @@ export class Session {
entityType: string,
data: T,
identifyingKeys: Array<keyof T> = []
): Promise<T> {
): Promise<T & Entity> {
let keys = identifyingKeys as string[];

logger.info(
Expand Down Expand Up @@ -712,7 +713,7 @@ export class Session {

expression = `${expression} ${criteria.join(" and ")}`;

return this.query<T>(expression).then((response) => {
return this.query<T & Entity>(expression).then((response) => {
if (response.data.length === 0) {
return this.create<T>(entityType, data).then(({ data: responseData }) =>
Promise.resolve(responseData)
Expand All @@ -731,7 +732,7 @@ export class Session {

// Update entity if required.
let updated = false;
Object.keys(data).forEach((key: keyof T) => {
Object.keys(data).forEach((key: keyof (T & Entity)) => {
if (data[key] !== updateEntity[key]) {
updateEntity[key] = data[key];
updated = true;
Expand All @@ -748,7 +749,9 @@ export class Session {
}
return accumulator;
}, {} as T)
).then(({ data: responseData }) => Promise.resolve(responseData));
).then(({ data: responseData }) =>
Promise.resolve(responseData as T & Entity)
);
}

return Promise.resolve(response.data[0]);
Expand Down Expand Up @@ -858,7 +861,7 @@ export class Session {
) {
logger.debug("Create", entityType, data, options);

const responses = await this.call<[CreateResponse<T>]>(
const responses = await this.call<[CreateResponse<T & Entity>]>(
[operation.create(entityType, data)],
options
);
Expand All @@ -879,7 +882,7 @@ export class Session {
*/
async update<T extends Data = Data>(
type: string,
keys: string[],
keys: string[] | string,
data: T,
options: MutationOptions = {}
) {
Expand All @@ -903,7 +906,11 @@ export class Session {
* @param {object} options.decodeDatesAsIso - Decode dates as ISO strings instead of moment objects
* @return {Promise} Promise resolved with the response.
*/
async delete(type: string, keys: string[], options: MutationOptions = {}) {
async delete(
type: string,
keys: string[] | string,
options: MutationOptions = {}
) {
logger.debug("Delete", type, keys, options);

const responses = await this.call<[DeleteResponse]>(
Expand Down
9 changes: 7 additions & 2 deletions source/util/convert_to_iso_string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Moment } from "moment";
import type dayjs from "dayjs";

/**
* Checks if string is in ISO 6801 format.
*/
Expand All @@ -14,7 +17,9 @@ function isIsoDate(str: string) {
* @param data - string or date object
* @returns ISO 6801 compatible string, or null if invalid date
*/
export function convertToIsoString(data: string | Date) {
export function convertToIsoString(
data: string | Date | Moment | ReturnType<typeof dayjs>
) {
if (
data &&
// if this is a date object of type moment or dayjs, or regular date object (all of them has toISOString)
Expand All @@ -24,7 +29,7 @@ export function convertToIsoString(data: string | Date) {
) {
// wrap it new Date() to convert it to UTC based ISO string in case it is in another timezone
try {
return new Date(data).toISOString();
return new Date(data as any).toISOString();
} catch (err) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion test/back_off.test.js → test/back_off.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// :copyright: Copyright (c) 2023 ftrack
import { backOff } from "../source/util/back_off";
import { expect } from "vitest";
import { describe, it, expect } from "vitest";

describe("backOff", () => {
it("should return the result of the successful request", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { convertToIsoString } from "../source/util/convert_to_iso_string";
import moment from "moment";
import dayjs from "dayjs";
import { describe, it, expect } from "vitest";

describe("convertToIsoString", () => {
it("should convert date object to ISO", () => {
Expand Down Expand Up @@ -55,7 +56,7 @@ describe("convertToIsoString", () => {
new Date("hello world"),
NaN,
])("should return null for invalid ISO string: %s", (invalidDate) => {
const converted = convertToIsoString(invalidDate);
const converted = convertToIsoString(invalidDate as any); //casted to test for invalid type
expect(converted).toEqual(null);
});
});
2 changes: 1 addition & 1 deletion test/event_hub.test.js → test/event_hub.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventHub } from "../source/event_hub";
import { vi, describe, expect } from "vitest";
import { vi, describe, expect, beforeEach, afterEach, test } from "vitest";

describe("EventHub", () => {
let eventHub;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// :copyright: Copyright (c) 2023 ftrack
import { getChunkSize } from "../source/util/get_chunk_size";
import { describe, expect, it } from "vitest";

describe("getChunkSize", () => {
it("should return the correct chunk size for a given file size", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// :copyright: Copyright (c) 2019 ftrack
import normalizeString from "../source/util/normalize_string";
import { describe, it, expect } from "vitest";

describe("Normalize string", () => {
it("should normalize COMBINING DIAERESIS", () => {
Expand Down
4 changes: 3 additions & 1 deletion test/server.js → test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const handlers = [
);
}
const body = await Promise.all(
req.body.map(
(
await req.json()
).map(
async ({
action,
expression,
Expand Down
Loading