npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@giveback007/fitbit-api

v0.1.0

Published

A fitbit web-api utility that is meant to be comprehensive and type-safe

Downloads

41

Readme

This library supports both browser and nodejs.

Install

Install npm or yarn library:

npm install @giveback007/fitbit-api
<or>
yarn add @giveback007/fitbit-api

Polyfills

NodeJs requires fetch polyfills (not required if running in browser).

// Using ES6 modules
import 'cross-fetch/polyfill';

// Using CommonJS modules
require('cross-fetch/polyfill');

global.FormData = require('form-data');

Usage

If a fitbit-user-id isn't passed it will default to "-" for the current logged in user.

import { FitbitApi } from '@giveback007/fitbit-api';

const api = new FitbitApi("<access-token>", "<fitbit-user-id>" || "-");

api.user.getProfile().then(profile => console.log(profile));

Automatic Token Refresh

Pass in a function as a third argument, it will automatically be called on "expired_token" or "invalid_token" errors.

If the refresh function fails -> returns an error object with "expired_token" or "invalid_token".

If new "<access-token>" is successfully retrieved the api will retry the call it first failed on.

new FitbitApi("<access-token>", "<fitbit-user-id>", async () => {
  const newToken = await /* some code retrieving new access-token */;
  return newToken;
});

List Of Supported Endpoints

https://dev.fitbit.com/build/reference/web-api

Typescript & Intellisense

All data is typed and api endpoint with more complex interface arguments are boiled down to be easier to understand with the use intellisense.

Typescript and intellisense

Error Handling

Successful responses are wrapped in a success object:

{
    "type": "SUCCESS",
    "isSuccess": true,
    "code": 200,
    "data": {...},
    "response": Response,
    "headers": {
        "content-type": "application/json; charset=utf-8"
    }
}

And an error response will return an error object:

{
    "type": "ERROR",
    "isSuccess": false,
    "code": 401,
    "error": { "errors": [{...}], "success": false },
    "response": Response,
    "headers": {
        "content-length": "135",
        "content-type": "application/json"
    }
}

Headers

Certain fitbit response headers (such as rate limiting) are unsupported by the browser and therefore won't show up when the api is called.

Some of these headers are:

  • fitbit-rate-limit-limit
  • fitbit-rate-limit-remaining
  • fitbit-rate-limit-reset

Subscriptions

This can't be accessed in the browser since it requires passing in headers that the browser doesn't support.

Make sure to set up a subscriber endpoint with fitbit were you manage fitbit api app credentials https://dev.fitbit.com/apps. To add this to an existing application use the [Edit Application Settings] button.

For more information: https://dev.fitbit.com/build/reference/web-api/developer-guide/using-subscriptions/

Developer Discord

This project is by the MyAlyce team. If you have any questions join us in our discord:

Invitation Link, use #fitbit_integration channel for fitbit api specific things.

TODOs

QUICK TODOS:

  • setup a publish script
  • set this up: https://hackernoon.com/these-6-essential-tools-will-maintain-your-npm-modules-for-you-4cbbee88e0cb
  • publish to npm
  • chart based api data

TODO:

  • additional header handling (localization etc.)
  • a way to handle rate limiting
    • rate limiting headers only exist in node (possibly make rate limiting utils, look up how rate limiting translates to the end user)
  • tests
  • set up subscription endpoint on alyce
  • research some form of swagger api change detection
  • turn everything from fitbit into package. (eg. auth on nodejs & browser side)
    • separate out parts that don't have node dependencies &import the non-node dependent parts with node parts.
  • add a simple way to add-in/expose fitbit api endpoints.
  • implement 'Get Activity TCX' https://dev.fitbit.com/build/reference/web-api/activity/get-activity-tcx/