Skip to content

Commit

Permalink
Merge pull request #4 from Print-one/next
Browse files Browse the repository at this point in the history
docs: issue templates
  • Loading branch information
UnderKoen authored Jan 23, 2024
2 parents ade71f6 + 29573b2 commit cc75ab8
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 95 deletions.
24 changes: 24 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
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 '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: enhancement
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ on:
push:
branches:
- main
paths-ignore:
- "docs/**"
- "**/*.md"
jobs:
release:
name: Release
Expand Down
5 changes: 1 addition & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# 1.0.0 (2023-11-30)


### Bug Fixes

* building error ([1fa3a2b](https://github.com/Print-one/print-one-js/commit/1fa3a2b9b43ee92c2ff2a277a8502a19a3bd82f4))
Initial release
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[build-img]:https://github.com/Print-one/print-one-js/actions/workflows/release.yml/badge.svg
[build-url]:https://github.com/Print-one/print-one-js/actions/workflows/release.yml

[issues-img]:https://img.shields.io/github/issues/Print-one/print-one-js
[issues-img]:https://img.shields.io/github/issues/Print-one/print-one-js/bug
[issues-url]:https://github.com/Print-one/print-one-js/issues

[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
Expand Down
110 changes: 110 additions & 0 deletions src/AxiosHttpHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import axios, { Axios, AxiosRequestConfig, AxiosResponse } from "axios";
import debug from "debug";
import { HttpHandler } from "./HttpHandler";
import { PrintOneOptions } from "./PrintOne";

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

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 + "/",
responseType: "json",
validateStatus: () => true,
headers: {
"x-api-key": token,
},
});
}
/**
* Performs a GET request.
* @param url The url to perform the request to
* @param options The options for the request
*/
public async GET<T>(
url: string,
options: AxiosRequestConfig = {},
): Promise<T> {
const response = await this.client.request<T>({
method: "GET",
url: url,
...options,
});

this.debug(`GET ${response.request.path}`);

this.handleErrors(response);

return response.data;
}

/**
* Performs a GET request and returns the response as a ArrayBuffer.
* @param url The url to perform the request to
* @param options The options for the request
*/
public async GETBuffer(
url: string,
options: AxiosRequestConfig = {},
): Promise<Uint8Array> {
const response = await this.client.request<number[]>({
method: "GET",
url: url,
responseType: "arraybuffer",
...options,
});

this.debug(`GET ${response.request.path}`);

this.handleErrors(response);

return Uint8Array.from(response.data);
}

/**
* Performs a POST request.
* @param data The data to send with the request
* @param options The options for the request
*/
public async POST<T>(
url: string,
data: unknown,
options: AxiosRequestConfig = {},
): Promise<T> {
const response = await this.client.request<T>({
method: "POST",
url: url,
data: data,
...options,
});

this.debug(`POST ${response.request.path}`);

this.handleErrors(response);

return response.data;
}

/**
* Performs a DELETE request.
* @param url The url to perform the request to
* @param options The options for the request
*/
public async DELETE<T>(
url: string,
options: AxiosRequestConfig = {},
): Promise<T> {
const response = await this.client.request<T>({
method: "DELETE",
url: url,
...options,
});

this.debug(`DELETE ${response.request.path}`);

this.handleErrors(response);

return response.data;
}
}
95 changes: 24 additions & 71 deletions src/HttpHandler.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,64 @@
import { Axios, AxiosRequestConfig, AxiosResponse } from "axios";
import debug from "debug";
import { PrintOneError } from "./errors/PrintOneError";
import { PrintOneOptions } from "./PrintOne";

export class AxiosHTTP {
private readonly client: Axios;
private readonly debug: debug.Debugger;
export abstract class HttpHandler<RequestOptions, Response> {
protected readonly 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;

constructor(client: Axios, debug: debug.Debugger) {
this.client = client;
this.debug = debug;
}
/**
* Performs a GET request.
* @param url The url to perform the request to
* @param options The options for the request
*/
public async GET<T>(
public abstract GET<T>(
url: string,
options: AxiosRequestConfig = {},
): Promise<T> {
const response = await this.client.request<T>({
method: "GET",
url: url,
...options,
});

this.debug(`GET ${response.request.path}`);

this.handleErrors(response);

return response.data;
}
options?: RequestOptions,
): Promise<T>;

/**
* Performs a GET request and returns the response as a ArrayBuffer.
* @param url The url to perform the request to
* @param options The options for the request
*/
public async GETBuffer(
public abstract GETBuffer(
url: string,
options: AxiosRequestConfig = {},
): Promise<Uint8Array> {
const response = await this.client.request<number[]>({
method: "GET",
url: url,
responseType: "arraybuffer",
...options,
});

this.debug(`GET ${response.request.path}`);

this.handleErrors(response);

return Uint8Array.from(response.data);
}
options?: RequestOptions,
): Promise<Uint8Array>;

/**
* Performs a POST request.
* @param data The data to send with the request
* @param options The options for the request
*/
public async POST<T>(
public abstract POST<T>(
url: string,
data: unknown,
options: AxiosRequestConfig = {},
): Promise<T> {
const response = await this.client.request<T>({
method: "POST",
url: url,
data: data,
...options,
});

this.debug(`POST ${response.request.path}`);

this.handleErrors(response);

return response.data;
}
options?: RequestOptions,
): Promise<T>;

/**
* Performs a DELETE request.
* @param url The url to perform the request to
* @param options The options for the request
*/
public async DELETE<T>(
public abstract DELETE<T>(
url: string,
options: AxiosRequestConfig = {},
): Promise<T> {
const response = await this.client.request<T>({
method: "DELETE",
url: url,
...options,
});

this.debug(`DELETE ${response.request.path}`);
options?: RequestOptions,
): Promise<T>;

this.handleErrors(response);

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

private handleErrors(response: AxiosResponse) {
if (response.status >= 400) {
if (res.status >= 400) {
throw new PrintOneError(
response.data.statusCode ?? response.status,
response.data.message,
res.data.statusCode ?? res.status,
res.data.message,
);
}
}
Expand Down
Loading

0 comments on commit cc75ab8

Please sign in to comment.