Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
W.I.P.
Browse files Browse the repository at this point in the history
  • Loading branch information
pierce-h committed Sep 22, 2020
1 parent 4d28226 commit 5871fe9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"test/specs/core/api-client.spec.js",
"--timeout=60000",
"--retries=0"
],
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lodash": "^4.17.20",
"luxon": "^1.24.1",
"mocha": "^8.1.3",
"node-fetch": "^2.6.1",
"nodemon": "^2.0.4",
"nps": "^5.10.0",
"object-hash": "^2.0.3",
Expand Down Expand Up @@ -67,6 +68,7 @@
"@types/luxon": "^1.25.0",
"@types/mocha": "^8.0.3",
"@types/node": "^14.11.2",
"@types/node-fetch": "^2.5.7",
"@types/nodemon": "^1.19.0",
"@types/recursive-readdir": "^2.2.0",
"@types/update-notifier": "^4.1.1",
Expand Down
58 changes: 35 additions & 23 deletions src/core/api-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import axios, { Method, AxiosRequestConfig, AxiosError } from "axios";
// import axios, { Method, AxiosRequestConfig, AxiosError } from "axios";
import fetch, { RequestInit } from 'node-fetch';
import ono from '@jsdevtools/ono';
import Apps from "./resources/apps";
import Deployments from "./resources/deployments";
import Diagnostics from "./resources/diagnostics";
import Sellers from "./resources/sellers";
import Users from "./resources/users";

export type Method = 'GET' | 'DELETE' | 'POST' | 'PUT' | 'PATCH'

export interface ApiClientParams {
endpoint: string;
method: Method;
Expand Down Expand Up @@ -71,38 +74,47 @@ export default class APIClient {
"api-key": this.apiKey,
};

if (this.debug) {
axios.interceptors.request.use(request => {
console.log('Starting Request', request)
return request
})

axios.interceptors.response.use(response => {
console.log('Response:', response)
return response
})
}

const mergedHeaders = {
...defaultHeaders,
...headers,
};

const request: AxiosRequestConfig = {
headers: mergedHeaders,
method: method,
url: `${this._apiAuthority}/${endpoint}`,
...(isFileUpload && { maxContentLength: Infinity }),
};
const requestOptions: RequestInit = { method: method, headers: mergedHeaders }

if (body) requestOptions.body = JSON.stringify(body);


// if (this.debug) {
// axios.interceptors.request.use(request => {
// console.log('Starting Request', request)
// return request
// })

// axios.interceptors.response.use(response => {
// console.log('Response:', response)
// return response
// })
// }



// const request: AxiosRequestConfig = {
// headers: mergedHeaders,
// method: method,
// url: `${this._apiAuthority}/${endpoint}`,
// ...(isFileUpload && { maxContentLength: Infinity }),
// };


if (body) request.data = body;

try {
const response = await axios(request);
return response.data as T;
const response = await fetch(`${this._apiAuthority}/${endpoint}`, requestOptions);
const parsedResponse = await response.json();

return parsedResponse;
} catch (error) {
const err = error as AxiosError;

const err = error;

if (this.debug) {
console.log('Response:', err.response)
Expand Down

0 comments on commit 5871fe9

Please sign in to comment.