-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
511 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
ReclaimEndpoints, | ||
ReclaimHabit, | ||
ReclaimHabitCreate, | ||
} from "../types"; | ||
import { ReclaimClient } from "../client"; | ||
import { filterArray } from "../utils"; | ||
|
||
/** | ||
* A class for interacting with Reclaim habits. | ||
*/ | ||
export class ReclaimHabits { | ||
private path = ReclaimEndpoints.Habits; | ||
private client: ReclaimClient; | ||
|
||
constructor(client: ReclaimClient) { | ||
this.client = client; | ||
} | ||
|
||
/** | ||
* @description Create a new habit. | ||
* @param habit The habit to create. | ||
* @returns {ReclaimHabit} | ||
*/ | ||
async create(habit: ReclaimHabitCreate): Promise<ReclaimHabit> { | ||
const request = await this.client._fetcher(this.path, { | ||
method: "POST", | ||
body: JSON.stringify(habit), | ||
}); | ||
|
||
const createdHabit = request.find((h: ReclaimHabit) => h.title === habit.title); | ||
|
||
return createdHabit; | ||
} | ||
|
||
async search(filters?: Partial<ReclaimHabit>): Promise<ReclaimHabit[]> { | ||
const response = (await this.client._fetcher(this.path, { | ||
method: "GET", | ||
})) as ReclaimHabit[]; | ||
|
||
if (!filters) return response; | ||
|
||
// Filter the response based on the filter object | ||
return filterArray(response, filters); | ||
} | ||
|
||
/** | ||
* @description Get a single habit by ID. | ||
* @param id The ID of the habit to get. | ||
* @returns {ReclaimHabit | null} | ||
*/ | ||
async get(id: number): Promise<ReclaimHabit | null> { | ||
const habits = await this.search(); | ||
|
||
return habits.find((habit) => habit.id === id) || null; | ||
} | ||
|
||
/** | ||
* @description Update a habit. | ||
* @param habit The habit to update. | ||
* @returns {ReclaimHabit} | ||
*/ | ||
async update(id: number, habit: Partial<ReclaimHabitCreate>): Promise<ReclaimHabit> { | ||
return await this.client._fetcher(`${this.path}/${id}`, { | ||
method: "PATCH", | ||
body: JSON.stringify(habit), | ||
}); | ||
} | ||
|
||
/** | ||
* @description Delete a habit. | ||
* @param id The ID of the habit to delete. | ||
* @returns {null} | ||
*/ | ||
async delete(id: number): Promise<{ taskOrHabit: ReclaimHabit, events: unknown[] }> { | ||
return await this.client._fetcher(`planner/policy/habit/${id}`, { | ||
method: "DELETE", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.