Code Monkey home page Code Monkey logo

strava's Introduction

Strava

npm npm bundle size NPM semantic-release

This library is a fully typed JavaScript wrapper of the Strava JSON API.

Installation

To install the package, run:

npm install strava

or

yarn add strava

Usage

The library can be initialized with a refresh token and optionally an access token. If these are not available, see below (Token exchange).

import { Strava } from 'strava'

const strava = new Strava({
  client_id: '123',
  client_secret: 'abc',
  refresh_token: 'def',
})

try {
  const activities = await strava.activities.getLoggedInAthleteActivities()
  console.log(activities)
} catch (error) {
  console.log(error)
}

Refreshing the access token

This library will automatically refresh the access token when needed. In order to store the token, you can use the on_token_refresh callback. This received an AccessToken object (consisting of access_token, expires_at, and refresh_token). Note that the refresh token as returned by this call can sometimes change, at which point the old token becomes invalid.

An AccessToken object can also be passed as a second argument to the Strava constructor. This can save an initial token refresh. As AccessToken contains a refresh token, the first argument does not need to contain a refresh token.

import { Strava } from 'strava'

const strava = new Strava(
  {
    client_id: '123',
    client_secret: 'abc',

    on_token_refresh: response => {
      cache.accessToken = response
    },
  },
  cache.accessToken,
)

Token exchange

When a user logs in for the first time, you will need to perform authorization with OAuth. This involves sending the user to https://www.strava.com/oauth/authorize, and receiving the auth code as a query parameter.

This can be used as follows:

import { Strava } from 'strava'

try {
  const strava = await Strava.createFromTokenExchange(
    {
      client_id: '123',
      client_secret: 'abc',
    },
    token,
  )

  const activities = await strava.activities.getLoggedInAthleteActivities()
  console.log(activities)
} catch (error) {
  console.log(error)
}

Getting athlete info

When a user logs in for the first time, the Strava API returns information about the newly logged-in user. This can be read using the on_token_refresh callback. Note that this will only ever be provided on the initial token exchange, before the promise returned from Strava.createFromTokenExchange returns. When the on_token_refresh callback is called again after the token expires, response.athlete will always be undefined.

import { Strava } from 'strava'

try {
  const strava = await Strava.createFromTokenExchange(
    {
      client_id: '123',
      client_secret: 'abc',
      on_token_refresh: response => {
        if (response.athlete) {
          console.log(response.athlete)
        }

        db.set('refresh_token', response.refresh_token)
      },
    },
    token,
  )

  const activities = await strava.activities.getLoggedInAthleteActivities()
  console.log(activities)
} catch (error) {
  console.log(error)
}

Contributing

Issues and pull requests are welcome.

License

MIT

strava's People

Contributors

andipaetzold avatar c-harding avatar chroth7 avatar dependabot[bot] avatar germain-gg avatar jpravetz avatar maksim77 avatar mosch avatar mschmiedel avatar rfoel avatar rhutchison avatar semantic-release-bot avatar thibaultleouay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

strava's Issues

getLoggedInAthleteActivites does not respect per_page parameter

The following request should return one activity but returns the default 30:

await strava.activities.getLoggedInAthleteActivities({ per_page: 1 });

Same when requesting LatLng key in Streams, only the default distance stream is returned:

await strava.streams.getActivityStreams({ id: 5937416221, keys: [StreamKeys.LatLng] });

This behavior works as intended in 1.3.2, so it broken recently. I believe it's related to this change:

https://github.com/rfoel/strava/blob/main/src/request.ts#L54

Specifically, the new query serialisation code results in urls of the following format /athlete/activities?query=%5Bobject+Object%5D

Current workaround is to downgrade to 1.3.2.

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Improve Oauth management

hey,

It would be super useful if the client could take an access_token parameter

Also it would be great if there was some methods to refresh the token

That would be my workflow
1- strava.oauth.refreshToken -> return the new Token
2 - strava.something({access_token})
3- strava.otherMethod({access_token})
4- store access token

I will provide a PR if it's ok

Missing field in DetailedActivity

Hi!
First of all, thank you so much for the library!

Unfortunately, there is an inaccuracy in the Strava documentation. The getActivityById method returns a json containing fields that are not present in the object description in the documentation. This can be easily detected by simply looking at the example that the authors themselves give on the right side of the documentation page.
In particular I am interested in two fields currently missing from your implementation:

  1. average_cadence
  2. suffer_score

Dependencies need updating and/or Change Request

I believe your dependencies could use an update, and I am requesting your input on this. I forked and tried to update myself, but you're using a few extra tools I haven't used before, like tsdx, and have a more complicated package.json that I am used to.
There are no notes in your README about building and testing, so maybe I am making mistakes.

Doing npm install as a first step resulted in lots of warnings, which is why I was going down the dependency update path. However all unit tests did pass, despite the warnings (but then they broke after my failed dependency update attempts).

My intent was to potentially make a few minor changes to your library for a pull request, which is why I was try to build locally.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.