Skip to content

Commit

Permalink
chore: prettier [skip-ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderKoen committed Jan 29, 2024
1 parent 3bbe6b8 commit 763df6b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
title: ""
labels: bug
assignees: ''

assignees: ""
---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
Expand Down
5 changes: 2 additions & 3 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
title: ""
labels: enhancement
assignees: ''

assignees: ""
---

**Is your feature request related to a problem? Please describe.**
Expand Down
11 changes: 9 additions & 2 deletions src/AxiosHttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import debug from "debug";
import { HttpHandler } from "./HttpHandler";
import { PrintOneOptions } from "./PrintOne";

export class AxiosHTTPHandler extends HttpHandler<AxiosRequestConfig, AxiosResponse> {
export class AxiosHTTPHandler extends HttpHandler<
AxiosRequestConfig,
AxiosResponse
> {
private readonly client: Axios;

constructor(token: string, options: Required<PrintOneOptions>, debug: debug.Debugger) {
constructor(
token: string,
options: Required<PrintOneOptions>,
debug: debug.Debugger,
) {
super(token, options, debug);
this.client = axios.create({
baseURL: new URL(options.version, options.url).href + "/",
Expand Down
21 changes: 11 additions & 10 deletions src/HttpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { PrintOneOptions } from "./PrintOne";
export abstract class HttpHandler<RequestOptions, Response> {
protected readonly debug: debug.Debugger;

constructor(token: string, protected readonly options: Required<PrintOneOptions>, debug: debug.Debugger) {
constructor(
token: string,
protected readonly options: Required<PrintOneOptions>,
debug: debug.Debugger,
) {
// We require these, so each extended class has type-safe auto-fill
token;

Expand All @@ -16,10 +20,7 @@ export abstract class HttpHandler<RequestOptions, Response> {
* @param url The url to perform the request to
* @param options The options for the request
*/
public abstract GET<T>(
url: string,
options?: RequestOptions,
): Promise<T>;
public abstract GET<T>(url: string, options?: RequestOptions): Promise<T>;

/**
* Performs a GET request and returns the response as a ArrayBuffer.
Expand Down Expand Up @@ -47,13 +48,13 @@ export abstract class HttpHandler<RequestOptions, Response> {
* @param url The url to perform the request to
* @param options The options for the request
*/
public abstract DELETE<T>(
url: string,
options?: RequestOptions,
): Promise<T>;
public abstract DELETE<T>(url: string, options?: RequestOptions): Promise<T>;

protected handleErrors(response: Response) {
const res = response as { status: number; data: { statusCode?: number; message: string[] } }
const res = response as {
status: number;
data: { statusCode?: number; message: string[] };
};

if (res.status >= 400) {
throw new PrintOneError(
Expand Down
21 changes: 16 additions & 5 deletions src/PrintOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ import { FriendlyStatus } from "./enums/Status";
import { Format } from "./enums/Format";
import { AxiosHTTPHandler } from "./AxiosHttpHandler";

export type RequestHandler = new (token: string, options: Required<PrintOneOptions>, debug: debug.Debugger) => HttpHandler<unknown, unknown>;
export type RequestHandler = new (
token: string,
options: Required<PrintOneOptions>,
debug: debug.Debugger,
) => HttpHandler<unknown, unknown>;

export type PrintOneOptions = Partial<{
url: string;
version: "v2";

/** Overwrite the default client */
client?: RequestHandler;
}>;

const DEFAULT_OPTIONS: Required<PrintOneOptions> = {
url: "https://api.print.one/",
version: "v2",
client: AxiosHTTPHandler
client: AxiosHTTPHandler,
};

export type Protected = {
Expand Down Expand Up @@ -72,8 +76,15 @@ export class PrintOne {

// istanbul ignore next
constructor(token: string, options: PrintOneOptions = {}) {
this._protected.options = { ...DEFAULT_OPTIONS, ...options } as Required<PrintOneOptions>;
this._protected.client = new this._protected.options.client(token, this.options, this.debug);
this._protected.options = {
...DEFAULT_OPTIONS,
...options,
} as Required<PrintOneOptions>;
this._protected.client = new this._protected.options.client(
token,
this.options,
this.debug,
);

this.debug("Initialized");
}
Expand Down
50 changes: 29 additions & 21 deletions test/CustomHttpHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { HttpHandler } from "../src/HttpHandler";

class CustomHttpHandler extends HttpHandler<unknown, unknown> {
public override GET<T>(): Promise<T> {
return Promise.reject(new Error('Custom GET request handler'))
return Promise.reject(new Error("Custom GET request handler"));
}
public override GETBuffer(): Promise<Uint8Array> {
return Promise.reject(new Error('Custom GETBuffer request handler'))
return Promise.reject(new Error("Custom GETBuffer request handler"));
}
public override POST<T>(): Promise<T> {
return Promise.reject(new Error('Custom POST request handler'))
return Promise.reject(new Error("Custom POST request handler"));
}
public override DELETE<T>(): Promise<T> {
return Promise.reject(new Error('Custom DELETE request handler'))
return Promise.reject(new Error("Custom DELETE request handler"));
}
}

Expand All @@ -32,34 +32,42 @@ describe("custom HttpHandler is used when provided to PrintOne instance", () =>
});

it("custom GET method should be called", async () => {
await expect(printOne.getSelf()).rejects.toThrow('Custom GET request handler');
await expect(printOne.getSelf()).rejects.toThrow(
"Custom GET request handler",
);
});

it("custom GETBuffer method should be called", async () => {
const customFile = new Preview(printOne.protected, {
detailsUrl: '',
detailsUrl: "",
orderingKey: 1,
url: '',
})
await expect(customFile.download()).rejects.toThrow('Custom GETBuffer request handler');
url: "",
});
await expect(customFile.download()).rejects.toThrow(
"Custom GETBuffer request handler",
);
});

it("custom POST method should be called", async () => {
await expect(printOne.createTemplate({
format: 'GREETINGCARD_SQ14',
name: 'test',
pages: ['', ''],
})).rejects.toThrow('Custom POST request handler');
await expect(
printOne.createTemplate({
format: "GREETINGCARD_SQ14",
name: "test",
pages: ["", ""],
}),
).rejects.toThrow("Custom POST request handler");
});

it("custom DELETE method should be called", async () => {
const customFile = new CustomFile(printOne.protected, {
createdAt: new Date().toISOString(),
fileExtension: 'png',
fileName: 'test',
id: '',
size: 1000
})
await expect(customFile.delete()).rejects.toThrow('Custom DELETE request handler');
fileExtension: "png",
fileName: "test",
id: "",
size: 1000,
});
await expect(customFile.delete()).rejects.toThrow(
"Custom DELETE request handler",
);
});
});

0 comments on commit 763df6b

Please sign in to comment.