myhours-api-wrapper
v0.1.1
Published
Unofficial Wrapper for the MyHours API
Downloads
2
Readme
MyHours API Wrapper
This is an unofficial wrapper for the MyHours API, which provides convenience methods for indirectly interacting with the documented v1.1 API (api2). This includes getting/editing/setting/deleting of projects, tasks, and time logs, team members, tags, etc.
Full Disclosure: This project is not affiliated with MyHours in any way. Use it at your own risk.
This SDK effectively mirrors what's available in the official MyHours API, providing convenience methods for each endpoint. For reference, please refer to the actual MyHours API v1.1 to see what kinds of actions are available.
Installation
npm install myhours-api-wrapper
Usage
Examples are provided in the examples
directory, which the following examples are based on.
General usage is as follows:
import { MyHoursAPIWrapper } from 'myhours-api-wrapper';
const mhWrapper = new MyHoursAPIWrapper();
// login
const session = await mhWrapper.login('my-username', 'my-password');
if (!session) {
throw new Error('Login failed');
} else {
console.log('Logged in');
// save to session to be used later...
}
// you can also set a previous session, rather than logging in repeatedly
mhWrapper.setSession(session);
// get active project & team members
const projects = await mhWrapper.getActiveProjects();
const teamMates = await mhWrapper.getAllTeamMembers();
// retrieve dashboard reports in a given time range
const dateFrom = new Date();
dateFrom.setDate(dateFrom.getDate() - 1);
const dashboardReports = await mhWrapper.getDashboardReports(dateFrom, new Date(), ReportGroup.GroupByClientProjects);
console.log('Dashboard reports:', dashboardReports);
// retrieve logs in a given time range
// get logs from the 1st to the 31st of the current month
const dateFrom = new Date();
dateFrom.setDate(1);
const dateTo = new Date();
dateTo.setDate(31);
const logs = await mhWrapper.getTimeLogs(dateFrom, dateTo);
console.log('Logs:', logs);
For restoring a session, you can use the setSession
method. This allows you to avoid repeatedly logging in:
/**
* An example of using an auth token to login instead
*/
import { MyHoursAPIWrapper, Session } from 'myhours-api-wrapper';
// create an instance of the MyHours API
const mhWrapper = new MyHoursAPIWrapper();
// load some session data, this could be from a file or a database
const session: Session = {
accessToken: 'my-auth-token',
expiresIn: 1800,
refreshToken: 'my-refresh-token',
expiresDate: new Date() // the exact time & date when the access token expires, this is something the SDK adds for convenience
};
// login using an auth token
mhWrapper.setSession(session);
// No need to login explicitly now that we have restored the session
// Logging in again, with a valid unexpired session, will just skip the login process
// do something like getting all active projects
const projects = await mhWrapper.getActiveProjects();
console.log('Projects:', projects);
Sessions can be refreshed as well:
// later on, refresh your token & session
const freshSession = await mhWrapper.refresh();
// this will give you a new session with a new access token, but it's your responsibility to store it
// the SDK is effectively stateless, so you need to manage the session yourself
// store the new session somewhere
// you can serialize it to JSON and store it where you like, taking proper security precautions
JSON.stringify(freshSession);
Some other things you can do as well:
// add tasks to projects
const task = await mhWrapper.addTaskToProject('my-project-id', 'list-name', 'task-name', 'task-description', 0, 0, 'xyz');
// add time logs
const timeLog = await mhWrapper.addTimeLog('my-project-id', 'my-task-id', 'Working on a new feature', new Date(), new Date(), new Date(), true, 0);
// get all tags
const tags = await mhWrapper.getAllTags();
Contributing
This SDK is a work in progress, and contributions are welcome! If you find a bug, or want to add a feature, please open an issue or a pull request.
Most of the API is covered here (based on the MyHours postman documentation), but your results may vary if the MyHours API changes in the future. If you find any issues, please report them!