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

@tokks/strava

v1.1.5

Published

Fast, light weighted, promise based, fully typed (Typescript) node client for Strava v3 API.

Downloads

14

Readme

@tokks/strava

npm package Build Status Coverage Status Dependencies Status DevDependencies Status

This is a fast, light weighted, promise based, fully typed (Typescript) node client for Strava v3 API.

Installation

npm install @tokks/strava mappersmith

or

yarn add @tokks/strava mappersmith

Getting started

Creating a Strava app

First of all you will need to create your own strava api app at Strava. Follow their steps, fill in all the necessary forms and you should be provided a Client ID and Client Secret. Those are important to set up your Node application, save them (but don't worry, you can check back at any time.)

A read access_token and a refresh_token will also be available for you, those are useful for testing and setting up your Node application but you will not need them in the final version. Save them as well (note that the access_token expires every 6 hours so you might need to reach back a few more times).

Setting up your Client ID and Client Secret

This package is going to read those secrets from the following environment variables STRAVA_CLIENT_ID and STRAVA_SECRET, make sure to store them correctly, one way to do it is:

export STRAVA_CLIENT_ID="Client ID"
export STRAVA_SECRET="Client Secret"

Oauth flow (getting user access token)

The first thing your node application needs to do is get user access token, as described in Strava API Auth flow.

Follow the steps described in the Oauth flow above

  • The user has to be redirected to:
https://www.strava.com/oauth/authorize?client_id=${YOUR_CLIENT_ID}&redirect_uri=${YOUR_DOMAIN_ROUTE}&response_type=code&scope=${YOUR_NEEDED_SCOPE}

YOUR_DOMAIN_ROUTE is the callback that will be called when the user authorizes your application in the strava interface. It should be able to read the provided code and scope from the query string.

YOUR_NEEDED_SCOPE are the scopes that your Node application will request from the user, you can read more about scopes here.

With that code at hand, it's time to call the Strava API for the first time to request a valid user access_token

const response = await stravaApi().Auth.authorize({ body: { code } }))

// >> response.data()
// {
//   access_token: 'a4b945687g...',
//   athlete: {...},
//   expires_at: 1568775134,
//   expires_in: 21600,
//   refresh_token: 'e5n567567...',
//   token_type: 'Bearer',
// }

// >> response.status()
// 200

You should probably save this access_token on your database or session cache or something, so you don't keep on asking your user to authorize your application against Strava all the time.

Using Signed Resources on the API

In order to use all the authenticated endpoints provided by Strava API, you are going to need that access_token you just saved when initializing a client.

const response = await stravaApi({ access_token }).Activities.getActivityById({ id })

// >> response.data()
// {
// achievement_count: 0,
// athlete: { id: 134815, resource_state: 1 },
// athlete_count: 1,
// average_cadence: 67.1,
// average_heartrate: 140.3,
// average_speed: 5.54,
// average_watts: 175.3,
// comment_count: 1,
// ...
// }

// >> response.status()
// 200

And it's all typed in Typescript

We do try to keep the naming as close as possible to the official strava documentation so you should have no problem relating to why the client calls .Activities.getActivityById instead of .UserActivities.fetchSingle when reading Strava official documentation.

Strava Official Docs

Strava Documentation

Author

Marcelo Tokarnia [email protected]