mirror of
https://github.com/kc4x4sar/d4h-typescript.git
synced 2026-06-03 09:23:36 -07:00
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:
+95
-95
@@ -1,96 +1,96 @@
|
||||
import HttpUtils from "./httpUtils";
|
||||
import type { Group } from "./group";
|
||||
import type { Member } from "./member";
|
||||
|
||||
const D4H_FETCH_LIMIT = 250;
|
||||
const D4H_BASE_URL = "https://api.d4h.org/v2";
|
||||
|
||||
export interface GetMemberOptions {
|
||||
includeDetails?: boolean;
|
||||
}
|
||||
|
||||
export interface GetMembersOptions {
|
||||
groupId?: number;
|
||||
includeCustomFields?: boolean;
|
||||
includeDetails?: boolean;
|
||||
}
|
||||
|
||||
export interface GetGroupsOptions {
|
||||
memberId?: number;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export default class D4H {
|
||||
private readonly _httpUtils: HttpUtils;
|
||||
|
||||
constructor(token: string) {
|
||||
this._httpUtils = new HttpUtils(token, D4H_FETCH_LIMIT);
|
||||
}
|
||||
|
||||
/********************************************/
|
||||
/**************** MEMBERS *******************/
|
||||
/********************************************/
|
||||
|
||||
async getMember(id: number, options?: GetMemberOptions): Promise<Member> {
|
||||
let url = new URL(`${D4H_BASE_URL}/team/members/${id}`)
|
||||
|
||||
if (options !== undefined) {
|
||||
let optionsList = url.searchParams;
|
||||
|
||||
if (options.includeDetails !== undefined) {
|
||||
optionsList.append("include_details", "true");
|
||||
}
|
||||
}
|
||||
|
||||
return await this._httpUtils.get<Member>(url);
|
||||
}
|
||||
|
||||
async getMembers(options?: GetMembersOptions): Promise<Member[]> {
|
||||
let url = new URL(`${D4H_BASE_URL}/team/members`)
|
||||
|
||||
if (options !== undefined) {
|
||||
let optionsList = url.searchParams;
|
||||
|
||||
if (options.groupId !== undefined) {
|
||||
optionsList.append("group_id", options.groupId.toString());
|
||||
}
|
||||
|
||||
if (options.includeDetails !== undefined) {
|
||||
optionsList.append("include_details", "true");
|
||||
}
|
||||
|
||||
if (options.includeCustomFields !== undefined) {
|
||||
optionsList.append("include_custom_fields", "true");
|
||||
}
|
||||
}
|
||||
|
||||
return await this._httpUtils.getMany(url);
|
||||
}
|
||||
|
||||
/********************************************/
|
||||
/***************** GROUPS *******************/
|
||||
/********************************************/
|
||||
|
||||
async getGroup(id: number): Promise<Group> {
|
||||
let url = new URL(`${D4H_BASE_URL}/team/groups/${id}`)
|
||||
return await this._httpUtils.get<Group>(url);
|
||||
}
|
||||
|
||||
async getGroups(options?: GetGroupsOptions): Promise<Group[]> {
|
||||
let url = new URL(`${D4H_BASE_URL}/team/groups`)
|
||||
|
||||
if (options !== undefined) {
|
||||
let optionsList = url.searchParams;
|
||||
|
||||
if (options.memberId !== undefined) {
|
||||
optionsList.append("member_id", options.memberId.toString());
|
||||
}
|
||||
|
||||
if (options.title !== undefined) {
|
||||
optionsList.append("title", options.title);
|
||||
}
|
||||
}
|
||||
|
||||
return await this._httpUtils.getMany(url);
|
||||
}
|
||||
import HttpUtils from './httpUtils'
|
||||
import type { Group } from './group'
|
||||
import type { Member } from './member'
|
||||
|
||||
const D4H_FETCH_LIMIT = 250
|
||||
const D4H_BASE_URL = 'https://api.d4h.org/v2'
|
||||
|
||||
export interface GetMemberOptions {
|
||||
includeDetails?: boolean;
|
||||
}
|
||||
|
||||
export interface GetMembersOptions {
|
||||
groupId?: number;
|
||||
includeCustomFields?: boolean;
|
||||
includeDetails?: boolean;
|
||||
}
|
||||
|
||||
export interface GetGroupsOptions {
|
||||
memberId?: number;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export default class D4H {
|
||||
private readonly _httpUtils: HttpUtils
|
||||
|
||||
constructor(token: string) {
|
||||
this._httpUtils = new HttpUtils(token, D4H_FETCH_LIMIT)
|
||||
}
|
||||
|
||||
/********************************************/
|
||||
/**************** MEMBERS *******************/
|
||||
/********************************************/
|
||||
|
||||
async getMember(id: number, options?: GetMemberOptions): Promise<Member> {
|
||||
const url = new URL(`${D4H_BASE_URL}/team/members/${id}`)
|
||||
|
||||
if (options !== undefined) {
|
||||
const optionsList = url.searchParams
|
||||
|
||||
if (options.includeDetails !== undefined) {
|
||||
optionsList.append('include_details', 'true')
|
||||
}
|
||||
}
|
||||
|
||||
return await this._httpUtils.get<Member>(url)
|
||||
}
|
||||
|
||||
async getMembers(options?: GetMembersOptions): Promise<Member[]> {
|
||||
const url = new URL(`${D4H_BASE_URL}/team/members`)
|
||||
|
||||
if (options !== undefined) {
|
||||
const optionsList = url.searchParams
|
||||
|
||||
if (options.groupId !== undefined) {
|
||||
optionsList.append('group_id', options.groupId.toString())
|
||||
}
|
||||
|
||||
if (options.includeDetails !== undefined) {
|
||||
optionsList.append('include_details', 'true')
|
||||
}
|
||||
|
||||
if (options.includeCustomFields !== undefined) {
|
||||
optionsList.append('include_custom_fields', 'true')
|
||||
}
|
||||
}
|
||||
|
||||
return await this._httpUtils.getMany(url)
|
||||
}
|
||||
|
||||
/********************************************/
|
||||
/***************** GROUPS *******************/
|
||||
/********************************************/
|
||||
|
||||
async getGroup(id: number): Promise<Group> {
|
||||
const url = new URL(`${D4H_BASE_URL}/team/groups/${id}`)
|
||||
return await this._httpUtils.get<Group>(url)
|
||||
}
|
||||
|
||||
async getGroups(options?: GetGroupsOptions): Promise<Group[]> {
|
||||
const url = new URL(`${D4H_BASE_URL}/team/groups`)
|
||||
|
||||
if (options !== undefined) {
|
||||
const optionsList = url.searchParams
|
||||
|
||||
if (options.memberId !== undefined) {
|
||||
optionsList.append('member_id', options.memberId.toString())
|
||||
}
|
||||
|
||||
if (options.title !== undefined) {
|
||||
optionsList.append('title', options.title)
|
||||
}
|
||||
}
|
||||
|
||||
return await this._httpUtils.getMany(url)
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
export interface Group {
|
||||
bundle: string;
|
||||
id: number;
|
||||
title: string;
|
||||
bundle: string
|
||||
id: number
|
||||
title: string
|
||||
}
|
||||
+36
-33
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+48
-48
@@ -1,49 +1,49 @@
|
||||
export enum CustomFieldType {
|
||||
Number = "number",
|
||||
Text = "text",
|
||||
Date = "date",
|
||||
}
|
||||
|
||||
export interface CustomField {
|
||||
id: number;
|
||||
type: CustomFieldType;
|
||||
label: string;
|
||||
value_string: string | null;
|
||||
value: string | null;
|
||||
}
|
||||
|
||||
export interface EmergencyContact {
|
||||
name: string | null;
|
||||
relation: string | null;
|
||||
phone: string | null;
|
||||
alt_phone: string | null;
|
||||
}
|
||||
|
||||
export interface MemberStatus {
|
||||
id: number;
|
||||
type: string;
|
||||
value: string;
|
||||
label: MemberStatusLabel | null;
|
||||
}
|
||||
|
||||
export interface MemberStatusLabel {
|
||||
id: number;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface Member {
|
||||
address: string;
|
||||
custom_fields?: CustomField[];
|
||||
email: string | null;
|
||||
emergency_contacts: EmergencyContact[];
|
||||
group_ids: number[] | null;
|
||||
homephone: string;
|
||||
id: number;
|
||||
mobilephone: string;
|
||||
name: string;
|
||||
notes: string | null;
|
||||
position: string;
|
||||
ref: string;
|
||||
status: MemberStatus;
|
||||
workphone: string;
|
||||
export enum CustomFieldType {
|
||||
Number = 'number',
|
||||
Text = 'text',
|
||||
Date = 'date',
|
||||
}
|
||||
|
||||
export interface CustomField {
|
||||
id: number;
|
||||
type: CustomFieldType;
|
||||
label: string;
|
||||
value_string: string | null;
|
||||
value: string | null;
|
||||
}
|
||||
|
||||
export interface EmergencyContact {
|
||||
name: string | null;
|
||||
relation: string | null;
|
||||
phone: string | null;
|
||||
alt_phone: string | null;
|
||||
}
|
||||
|
||||
export interface MemberStatus {
|
||||
id: number;
|
||||
type: string;
|
||||
value: string;
|
||||
label: MemberStatusLabel | null;
|
||||
}
|
||||
|
||||
export interface MemberStatusLabel {
|
||||
id: number;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface Member {
|
||||
address: string;
|
||||
custom_fields?: CustomField[];
|
||||
email: string | null;
|
||||
emergency_contacts: EmergencyContact[];
|
||||
group_ids: number[] | null;
|
||||
homephone: string;
|
||||
id: number;
|
||||
mobilephone: string;
|
||||
name: string;
|
||||
notes: string | null;
|
||||
position: string;
|
||||
ref: string;
|
||||
status: MemberStatus;
|
||||
workphone: string;
|
||||
}
|
||||
Reference in New Issue
Block a user