3.0.1 • Published 1 year ago

@timetreeapp/web-api v3.0.1

Weekly downloads
63
License
MIT
Repository
github
Last release
1 year ago

TimeTree Web API

npm version npm.io

@timetreeapp/web-api is simple HTTP client for requesting to TimeTree's Web API.

Installation

$ npm install @timetreeapp/web-api
# if you prefer yarn
$ yarn add @timetreeapp/web-api

Usage

Accessing API endpoints as a OAuth App

https://codesandbox.io/s/nifty-firefly-93mwg?fontsize=14&hidenavigation=1&theme=dark

import { OAuthClient } from "@timetreeapp/web-api";

const client = new OAuthClient("<your-access-token>");

(async () => {
  const data = await client.getCalendars();
  console.log("calendars", data);
})();

Options

new OAuthClient(
  accessToken: string;
  {
  // you can overwrite for testing purposes
  baseURL?: string;

  // specifies the number of milliseconds before the request times out.
  // this api is using axios timeout
  // see: https://github.com/axios/axios
  timeout?: number; // default is `0` (no timeout)

  // specifies the number of times to retry the request
  retry?: number; // default is `0` (no retry)

  // specifies whether retry to request when using retry option.
  // parameters is axios's error response
  validateRetryable?: (error: AxiosError) => boolean;

  // callbacks before retry
  onRetry?: (e: Error, count: number) => boolean;
})

Scheme

TimeTree API conforms to JSONAPI. But this sdk can deserialize response data and request data.

// server response
{
  data: {
    id: "1234",
    type: "calendar",
    attributes: {
      name: "Some Calendar",
      description: "Calendar description",
      color: "#2ecc87",
      order: 0,
      image_url: "https://attachments.timetreeapp.com/path/to/image.png",
      created_at: "2019-04-01T12:34:56.000Z"
    },
    relationships: {
      labels: {
        data: [...]
      }
    }
  }
}
// #getCalendar result
{
  id: "1234",
  type: "calendar",
  name: "Some Calendar",
  description: "Calendar description",
  color: "#2ecc87",
  order: 0,
  imageUrl: "https://attachments.timetreeapp.com/path/to/image.png",
  createdAt: "2019-04-01T12:34:56.000Z",
  labels: [
    {
      id: "1234,1",
      type: "label",
      name: "label title(empty if default)",
      color: "#2ecc87"
    },
    { id: "1234,2", type: "label" },
  ]
}

Error Handling

this client throws axios's error object as it is. see https://github.com/axios/axios#handling-errors

try {
  const data = client.getUser();
} catch (e) {
  e.response?.status
}

Get OAuth Access Token

TimeTree API uses OAuth2, you can use some OAuth2 library like client-oauth2

Accessing API endpoints as a Calendar App

import { CalendarAppAuthenticator, CalendarAppClient } from "@timetreeapp/web-api";

// Generate and download the private key. Please refer to the link below.
// https://developers.timetreeapp.com/docs/api/calendar-app
const authenticator = new CalendarAppAuthenticator({
  applicationId: "<your-calendar-app-id>",
  privateKey: "-----BEGIN RSA PRIVATE KEY-----\n....-----END RSA PRIVATE KEY-----\n",
});

(async () => {
  // When the Calendar App is linked by the user, TimeTree will
  // notify installation_id in an HTTP POST request.
  const response = await authenticator.getAccessToken("<installation-id>");

  const client = new CalendarAppClient(response.accessToken);
  const data = await client.getCalendar();
  console.log("calendar", data);
})();

Licence

MIT

3.0.1

1 year ago

3.0.0

2 years ago

2.0.1

3 years ago

2.0.0

4 years ago

2.0.0-alpha.2

4 years ago

2.0.0-alpha.1

4 years ago

1.8.0-beta.1

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.8

4 years ago

1.6.7

4 years ago

1.6.6

4 years ago

1.6.5

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.2.2

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago