mirror of
https://github.com/kc4x4sar/d4h-typescript.git
synced 2026-06-03 09:23:36 -07:00
Added test project.
This commit is contained in:
@@ -0,0 +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", "true");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export interface Group {
|
||||
bundle: string;
|
||||
id: number;
|
||||
title: string;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
interface D4HResponse<DataType> {
|
||||
statusCode: number;
|
||||
data: DataType;
|
||||
error: string;
|
||||
}
|
||||
|
||||
interface D4HError {
|
||||
statusCode: number;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export default class HttpUtils {
|
||||
private readonly _fetchLimit: number;
|
||||
private readonly _token: string;
|
||||
|
||||
constructor(token: string, fetchLimit: number) {
|
||||
if (!token) {
|
||||
throw new Error("Token cannot be empty");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return response.data as DataType;
|
||||
}
|
||||
|
||||
async getMany<DataType>(url: URL): Promise<DataType[]> {
|
||||
let results: DataType[] = [];
|
||||
|
||||
let offset = 0;
|
||||
while (true) {
|
||||
let 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;
|
||||
|
||||
if (newResults.length < this._fetchLimit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
export enum CustomFieldType {
|
||||
Number = "number",
|
||||
Text = "text",
|
||||
Date = "date",
|
||||
}
|
||||
|
||||
export interface CustomField {
|
||||
id: number;
|
||||
type: CustomFieldType;
|
||||
label: string;
|
||||
value_string: string | undefined;
|
||||
value: string | undefined;
|
||||
}
|
||||
|
||||
export interface EmergencyContact {
|
||||
name: string;
|
||||
relation: string;
|
||||
phone: string;
|
||||
alt_phone: string;
|
||||
}
|
||||
|
||||
export interface MemberStatus {
|
||||
id: number;
|
||||
type: string;
|
||||
value: string;
|
||||
label: MemberStatusLabel | undefined;
|
||||
}
|
||||
|
||||
export interface MemberStatusLabel {
|
||||
id: number;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface Member {
|
||||
address: string;
|
||||
custom_fields: CustomField[];
|
||||
email: string;
|
||||
emergency_contacts: EmergencyContact;
|
||||
group_ids: number[];
|
||||
homephone: string;
|
||||
id: number;
|
||||
mobilephone: string;
|
||||
name: string;
|
||||
notes: string;
|
||||
position: string;
|
||||
ref: string;
|
||||
status: MemberStatus;
|
||||
workphone: string;
|
||||
}
|
||||
Reference in New Issue
Block a user