Add linter support

* Added linter and fixed errors

* Updated error handling to only throw error objects

---------

Co-authored-by: Colin Williams <colin@colincwilliams.com>
This commit is contained in:
Colin Williams
2023-03-17 14:15:19 -07:00
committed by GitHub
parent fc13216313
commit ae7d6f98ac
7 changed files with 243 additions and 187 deletions
+36 -33
View File
@@ -1,63 +1,66 @@
interface D4HResponse<DataType> {
statusCode: number;
data: DataType;
error: string;
statusCode: number
data: DataType
}
interface D4HError {
statusCode: number;
error: string;
error: string
message: string
statusCode: number
}
export default class HttpUtils {
private readonly _fetchLimit: number;
private readonly _token: string;
private readonly _fetchLimit: number
private readonly _token: string
constructor(token: string, fetchLimit: number) {
if (!token) {
throw new Error("Token cannot be empty");
throw new Error('Token cannot be empty')
}
this._fetchLimit = fetchLimit;
this._token = token;
this._fetchLimit = fetchLimit
this._token = token
}
async get<DataType>(url: URL): Promise<DataType> {
let method = "GET";
let headers = {
"Authorization": `Bearer ${this._token}`,
};
console.log(url);
let rawResponse = await fetch(url.toString(), { method, headers });
let response = await rawResponse.json() as D4HResponse<DataType>;
if (response.statusCode !== 200) {
throw response as D4HError;
const method = 'GET'
const headers = {
'Authorization': `Bearer ${this._token}`,
}
return response.data as DataType;
console.log(url)
const rawResponse = await fetch(url.toString(), { method, headers })
const response = await rawResponse.json() as D4HResponse<DataType> & D4HError
if (response.statusCode !== 200) {
const d4hError = response as D4HError
throw new Error(`${d4hError.statusCode}: ${d4hError.error}: ${d4hError.message}`)
}
return response.data
}
async getMany<DataType>(url: URL): Promise<DataType[]> {
let results: DataType[] = [];
let results: DataType[] = []
let offset = 0;
let offset = 0
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
while (true) {
let urlWithOffset = new URL(url);
urlWithOffset.searchParams.append('offset', offset.toString());
urlWithOffset.searchParams.append('limit', this._fetchLimit.toString());
const urlWithOffset = new URL(url)
urlWithOffset.searchParams.append('offset', offset.toString())
urlWithOffset.searchParams.append('limit', this._fetchLimit.toString())
let newResults = await this.get<DataType[]>(urlWithOffset);
results = results.concat(newResults);
offset += this._fetchLimit;
const newResults = await this.get<DataType[]>(urlWithOffset)
results = results.concat(newResults)
offset += this._fetchLimit
if (newResults.length < this._fetchLimit) {
break;
break
}
}
return results;
return results
}
}