From 9f29c775ae70de6c477768ab7d100da193c00c11 Mon Sep 17 00:00:00 2001 From: UnderKoen Date: Thu, 2 Nov 2023 13:58:50 +0100 Subject: [PATCH] chore: implement feedback --- src/PrintOne.ts | 9 +++-- src/index.ts | 2 +- src/models/Order.ts | 18 ++++++--- src/models/PaginatedResponse.ts | 40 ++++++++++++++------ src/models/Preview.ts | 20 ++++++---- src/models/_interfaces/IPaginatedResponse.ts | 1 + src/utils.ts | 4 ++ 7 files changed, 64 insertions(+), 30 deletions(-) diff --git a/src/PrintOne.ts b/src/PrintOne.ts index a0fd102..1213827 100644 --- a/src/PrintOne.ts +++ b/src/PrintOne.ts @@ -25,6 +25,7 @@ import { Finish } from "./enums/Finish"; import { Order } from "./models/Order"; import { IOrder } from "./models/_interfaces/IOrder"; import { FriendlyStatus } from "./enums/Status"; +import { Format } from "./enums/Format"; export type PrintOneOptions = Partial<{ url: string; @@ -103,7 +104,7 @@ export class PrintOne { */ public async getCustomFiles( options: PaginationOptions< - "createdAt" | "fileName" | "size" | "id" | "fileExtension" | string + "createdAt" | "fileName" | "size" | "id" | "fileExtension" > = {}, ): Promise> { const data = await this.client.GET>( @@ -209,7 +210,7 @@ export class PrintOne { sender?: Address; template: Template | string; /** - * Defaults to GLOSSY + * @default GLOSSY */ finish?: Finish; mergeVariables?: Record; @@ -259,8 +260,8 @@ export class PrintOne { filter?: { friendlyStatus?: InFilter; billingId?: InFilter; - format?: InFilter; - finish?: InFilter; + format?: InFilter; + finish?: InFilter; isBillable?: boolean; createdAt?: DateFilter; anonymizedAt?: DateFilter | boolean; diff --git a/src/index.ts b/src/index.ts index 352c97d..37d2064 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export { Address } from "./models/Address"; export { Company } from "./models/Company"; export { CustomFile } from "./models/CustomFile"; export { Order } from "./models/Order"; -export { PaginatedResponse } from "./models/PaginatedResponse"; +export { PaginatedResponse, Meta, Links } from "./models/PaginatedResponse"; export { Preview } from "./models/Preview"; export { PreviewDetails } from "./models/PreviewDetails"; export { Template } from "./models/Template"; diff --git a/src/models/Order.ts b/src/models/Order.ts index d8c1061..b4ddb43 100644 --- a/src/models/Order.ts +++ b/src/models/Order.ts @@ -5,6 +5,7 @@ import { Format } from "../enums/Format"; import { Address } from "../models/Address"; import { Template } from "./Template"; import { Status } from "../enums/Status"; +import { sleep } from "../utils"; export class Order { private _data: IOrder; @@ -116,14 +117,21 @@ export class Order { /** * Download the order preview * @param polling If true, the order will be polled until it has finished processing. - * @param timeout How long it should poll until it gives up. + * @param timeoutSeconds How long it should poll until it gives up. * @throws { PrintOneError } If the order could not be downloaded. */ - public async download(polling = true, timeout = 20): Promise { + public async download( + polling = true, + timeoutSeconds = 20, + ): Promise { let time = 0; - while (polling && this.status === Status.order_created && time < timeout) { + while ( + polling && + this.status === Status.order_created && + time < timeoutSeconds + ) { await this.refresh(); - await new Promise((resolve) => setTimeout(resolve, 1000)); + await sleep(1000); time++; } @@ -145,7 +153,7 @@ export class Order { let time = 0; while (polling && this.status === Status.order_created && time < timeout) { await this.refresh(); - await new Promise((resolve) => setTimeout(resolve, 1000)); + await sleep(1000); time++; } diff --git a/src/models/PaginatedResponse.ts b/src/models/PaginatedResponse.ts index ffa4ae8..e9bf229 100644 --- a/src/models/PaginatedResponse.ts +++ b/src/models/PaginatedResponse.ts @@ -1,6 +1,20 @@ import { IPaginatedResponse } from "./_interfaces/IPaginatedResponse"; import { Protected } from "../PrintOne"; +export type Meta = { + total: number; + filterOptions: Record; + pages: number; + page: number; + pageSize: number; +}; + +export type Links = { + previousUrl: string | null; + currentUrl: string; + nextUrl: string | null; +}; + export class PaginatedResponse { protected readonly _data: IPaginatedResponse; @@ -28,20 +42,22 @@ export class PaginatedResponse { }; } - public get page(): number { - return this._data.page; - } - - public get pages(): number { - return this._data.pages; - } - - public get pageSize(): number { - return this._data.pageSize; + public get meta(): Meta { + return { + total: this._data.total, + filterOptions: this._data.filterOptions, + pages: this._data.pages, + page: this._data.page, + pageSize: this._data.pageSize, + }; } - public get total(): number { - return this._data.total; + public get links(): Links { + return { + previousUrl: this._data.previousUrl, + currentUrl: this._data.currentUrl, + nextUrl: this._data.nextUrl, + }; } public get data(): T[] { diff --git a/src/models/Preview.ts b/src/models/Preview.ts index bca3935..275abed 100644 --- a/src/models/Preview.ts +++ b/src/models/Preview.ts @@ -4,6 +4,7 @@ import { PrintOneError } from "../errors/PrintOneError"; import { PreviewDetails } from "./PreviewDetails"; import { IPreviewDetails } from "./_interfaces/IPreviewDetails"; import { TimeoutError } from "../errors/TimeoutError"; +import { sleep } from "../utils"; export class Preview { private _data: IPreview; @@ -30,13 +31,13 @@ export class Preview { /** * Get the details of the preview. * @param polling Whether to poll for the details until they are available. - * @param timeout How long it should poll until it gives up. + * @param timeoutSeconds How long it should poll until it gives up. * @throws { TimeoutError } If the timeout is reached. * @throws { PrintOneError } If the details could not be retrieved. */ public async getDetails( polling = true, - timeout = 20, + timeoutSeconds = 20, ): Promise { let time = 0; do { @@ -51,9 +52,9 @@ export class Preview { const error = e as PrintOneError; if (error.statusCode === 404) { - await new Promise((resolve) => setTimeout(resolve, 1000)); + await sleep(1000); - if (time++ >= timeout) { + if (time++ >= timeoutSeconds) { throw new TimeoutError("Timeout reached"); } @@ -71,11 +72,14 @@ export class Preview { /** * Download the preview. * @param polling Whether to poll for the image until they are available. - * @param timeout How long it should poll until it gives up. + * @param timeoutSeconds How long it should poll until it gives up. * @throws { TimeoutError } If the timeout is reached. * @throws { PrintOneError } If the details could not be retrieved. */ - public async download(polling = true, timeout = 20): Promise { + public async download( + polling = true, + timeoutSeconds = 20, + ): Promise { let time = 0; do { try { @@ -87,9 +91,9 @@ export class Preview { const error = e as PrintOneError; if (error.statusCode === 404) { - await new Promise((resolve) => setTimeout(resolve, 1000)); + await sleep(1000); - if (time++ >= timeout) { + if (time++ >= timeoutSeconds) { throw new TimeoutError("Timeout reached"); } diff --git a/src/models/_interfaces/IPaginatedResponse.ts b/src/models/_interfaces/IPaginatedResponse.ts index 7e6430a..07d5398 100644 --- a/src/models/_interfaces/IPaginatedResponse.ts +++ b/src/models/_interfaces/IPaginatedResponse.ts @@ -7,4 +7,5 @@ export type IPaginatedResponse = { pages: number; pageSize: number; total: number; + filterOptions: Record; }; diff --git a/src/utils.ts b/src/utils.ts index 64bd8a7..34699fc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,7 @@ +export async function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + export type SortDirection = "ASC" | "DESC"; export type SortBy = { order: SortDirection;