diff --git a/lib/src/d4h.ts b/lib/src/d4h.ts index f878d6d..a2546c4 100644 --- a/lib/src/d4h.ts +++ b/lib/src/d4h.ts @@ -1,6 +1,6 @@ import HttpUtils from './httpUtils' import type { Group } from './group' -import type { Member } from './member' +import type { Member, MemberUpdate } from './member' const D4H_FETCH_LIMIT = 250 const D4H_BASE_URL = 'https://api.d4h.org/v2' @@ -31,7 +31,7 @@ export default class D4H { /**************** MEMBERS *******************/ /********************************************/ - async getMember(id: number, options?: GetMemberOptions): Promise { + getMemberAsync(id: number, options?: GetMemberOptions): Promise { const url = new URL(`${D4H_BASE_URL}/team/members/${id}`) if (options !== undefined) { @@ -42,10 +42,10 @@ export default class D4H { } } - return await this._httpUtils.get(url) + return this._httpUtils.getAsync(url) } - async getMembers(options?: GetMembersOptions): Promise { + getMembersAsync(options?: GetMembersOptions): Promise { const url = new URL(`${D4H_BASE_URL}/team/members`) if (options !== undefined) { @@ -64,19 +64,24 @@ export default class D4H { } } - return await this._httpUtils.getMany(url) + return this._httpUtils.getManyAsync(url) + } + + updateMemberAsync(id: number, updates: MemberUpdate): Promise { + const url = new URL(`${D4H_BASE_URL}/team/members/${id}`) + return this._httpUtils.putAsync(url, updates) } /********************************************/ /***************** GROUPS *******************/ /********************************************/ - async getGroup(id: number): Promise { + getGroupAsync(id: number): Promise { const url = new URL(`${D4H_BASE_URL}/team/groups/${id}`) - return await this._httpUtils.get(url) + return this._httpUtils.getAsync(url) } - async getGroups(options?: GetGroupsOptions): Promise { + getGroupsAsync(options?: GetGroupsOptions): Promise { const url = new URL(`${D4H_BASE_URL}/team/groups`) if (options !== undefined) { @@ -91,6 +96,6 @@ export default class D4H { } } - return await this._httpUtils.getMany(url) + return this._httpUtils.getManyAsync(url) } } \ No newline at end of file diff --git a/lib/src/httpUtils.ts b/lib/src/httpUtils.ts index eac3c82..ff6ed6a 100644 --- a/lib/src/httpUtils.ts +++ b/lib/src/httpUtils.ts @@ -9,6 +9,11 @@ interface D4HError { statusCode: number } +enum HttpMethod { + Get = 'GET', + Put = 'PUT', +} + export default class HttpUtils { private readonly _fetchLimit: number private readonly _token: string @@ -22,16 +27,25 @@ export default class HttpUtils { this._token = token } - async get(url: URL): Promise { - const method = 'GET' + async requestAsync(url: URL, method: HttpMethod, body?: TBody): Promise { const headers = { 'Authorization': `Bearer ${this._token}`, + 'Content-Type': 'application/json' } console.log(url) + + const options: RequestInit = { + method, + headers, + } + + if (body) { + options.body = JSON.stringify(body) + } - const rawResponse = await fetch(url.toString(), { method, headers }) - const response = await rawResponse.json() as D4HResponse & D4HError + const rawResponse = await fetch(url.toString(), options) + const response = await rawResponse.json() as D4HResponse & D4HError if (response.statusCode !== 200) { const d4hError = response as D4HError @@ -40,8 +54,12 @@ export default class HttpUtils { return response.data } + + async getAsync(url: URL): Promise { + return this.requestAsync(url, HttpMethod.Get) + } - async getMany(url: URL): Promise { + async getManyAsync(url: URL): Promise { let results: DataType[] = [] let offset = 0 @@ -52,7 +70,7 @@ export default class HttpUtils { urlWithOffset.searchParams.append('offset', offset.toString()) urlWithOffset.searchParams.append('limit', this._fetchLimit.toString()) - const newResults = await this.get(urlWithOffset) + const newResults = await this.getAsync(urlWithOffset) results = results.concat(newResults) offset += this._fetchLimit @@ -63,4 +81,8 @@ export default class HttpUtils { return results } + + async putAsync(url: URL, body: TBody): Promise { + return this.requestAsync(url, HttpMethod.Put, body) + } } diff --git a/lib/src/member.ts b/lib/src/member.ts index 2eb1b7b..b08652a 100644 --- a/lib/src/member.ts +++ b/lib/src/member.ts @@ -46,4 +46,36 @@ export interface Member { ref: string; status: MemberStatus; workphone: string; +} + +export interface MemberUpdate { + name?: string | null + ref?: string | null + id_tag?: string | null + status_id?: number + status_custom_id?: number + retired_reason_id?: number + date_leave?: Date + date_join?: Date + position?: string | null + role_id?: number + cost_per_hour?: number + cost_per_use?: number + address_street?: string | null + address_city?: string | null + address_region?: string | null + address_postcode?: string | null + address_country?: string | null + lat?: number + lng?: number + gridref?: string | null + location_bookmark_id?: number + email?: string | null + phone_mobile?: string | null + phone_home?: string | null + phone_work?: string | null + pager?: string | null + pager_email?: string | null + address?: string | null + notes?: string | null } \ No newline at end of file diff --git a/test/index.ts b/test/index.ts index b00c52a..c7a0df1 100644 --- a/test/index.ts +++ b/test/index.ts @@ -15,8 +15,20 @@ let getMembersOptions: GetMembersOptions = { }; let main = async function() { + // Only execute update code on an instance where that's OK. + // Double-check your API key. Double-check what you're modifying. + /*try { + const memberId = 0000 // Set this to a real ID + await api.updateMemberAsync(memberId, { + notes: null, + }) + } catch (err) { + console.log(JSON.stringify(err)); + return; + }*/ + try { - let members = await api.getMembers(getMembersOptions); + const members = await api.getMembersAsync(getMembersOptions); console.log(`Retrieved ${members.length} members.`); } catch (err) { console.log(JSON.stringify(err)); @@ -24,13 +36,12 @@ let main = async function() { } try { - let groups = await api.getGroups(); + const groups = await api.getGroupsAsync(); console.log(`Retrieved ${groups.length} groups.`); } catch (err) { console.log(JSON.stringify(err)); return; } - } main().then(() => console.log("Execution complete")); \ No newline at end of file