Skip to content

Commit

Permalink
chore: implement feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderKoen committed Nov 2, 2023
1 parent ef046d8 commit 9f29c77
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 30 deletions.
9 changes: 5 additions & 4 deletions src/PrintOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,7 +104,7 @@ export class PrintOne {
*/
public async getCustomFiles(
options: PaginationOptions<
"createdAt" | "fileName" | "size" | "id" | "fileExtension" | string
"createdAt" | "fileName" | "size" | "id" | "fileExtension"
> = {},
): Promise<PaginatedResponse<CustomFile>> {
const data = await this.client.GET<IPaginatedResponse<ICustomFile>>(
Expand Down Expand Up @@ -209,7 +210,7 @@ export class PrintOne {
sender?: Address;
template: Template | string;
/**
* Defaults to GLOSSY
* @default GLOSSY
*/
finish?: Finish;
mergeVariables?: Record<string, unknown>;
Expand Down Expand Up @@ -259,8 +260,8 @@ export class PrintOne {
filter?: {
friendlyStatus?: InFilter<FriendlyStatus>;
billingId?: InFilter;
format?: InFilter;
finish?: InFilter;
format?: InFilter<Format>;
finish?: InFilter<Finish>;
isBillable?: boolean;
createdAt?: DateFilter;
anonymizedAt?: DateFilter | boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
18 changes: 13 additions & 5 deletions src/models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ArrayBuffer> {
public async download(
polling = true,
timeoutSeconds = 20,
): Promise<ArrayBuffer> {
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++;
}

Expand All @@ -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++;
}

Expand Down
40 changes: 28 additions & 12 deletions src/models/PaginatedResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { IPaginatedResponse } from "./_interfaces/IPaginatedResponse";
import { Protected } from "../PrintOne";

export type Meta = {
total: number;
filterOptions: Record<string, string[]>;
pages: number;
page: number;
pageSize: number;
};

export type Links = {
previousUrl: string | null;
currentUrl: string;
nextUrl: string | null;
};

export class PaginatedResponse<T = unknown> {
protected readonly _data: IPaginatedResponse<T>;

Expand Down Expand Up @@ -28,20 +42,22 @@ export class PaginatedResponse<T = unknown> {
};
}

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[] {
Expand Down
20 changes: 12 additions & 8 deletions src/models/Preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PreviewDetails> {
let time = 0;
do {
Expand All @@ -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");
}

Expand All @@ -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<ArrayBuffer> {
public async download(
polling = true,
timeoutSeconds = 20,
): Promise<ArrayBuffer> {
let time = 0;
do {
try {
Expand All @@ -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");
}

Expand Down
1 change: 1 addition & 0 deletions src/models/_interfaces/IPaginatedResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type IPaginatedResponse<T = unknown> = {
pages: number;
pageSize: number;
total: number;
filterOptions: Record<string, string[]>;
};
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export type SortDirection = "ASC" | "DESC";
export type SortBy<T> = {
order: SortDirection;
Expand Down

0 comments on commit 9f29c77

Please sign in to comment.