Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow access request in params #179

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Subscriptions,
Uploads,
} from './resources'
import { Oauth } from './resources/oauth'
import { RefreshTokenRequest } from './types'

export * from './types'
Expand All @@ -24,6 +25,7 @@ export class Strava {
athletes: Athletes
clubs: Clubs
gears: Gears
oauth: Oauth
routes: Routes
runningRaces: RunningRaces
segmentEfforts: SegmentEfforts
Expand All @@ -38,6 +40,7 @@ export class Strava {
this.athletes = new Athletes(this.request)
this.clubs = new Clubs(this.request)
this.gears = new Gears(this.request)
this.oauth = new Oauth()
this.routes = new Routes(this.request)
this.runningRaces = new RunningRaces(this.request)
this.segmentEfforts = new SegmentEfforts(this.request)
Expand Down
9 changes: 7 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type RequestParams = {
query?: Record<string, any>
body?: Record<string, any> | any
headers?: Record<string, any>
access_token?: string
}

export class Request {
Expand Down Expand Up @@ -49,7 +50,7 @@ export class Request {
uri: string,
params?: RequestParams,
): Promise<Response> {
await this.getAccessToken()
if (!params?.access_token) await this.getAccessToken()
const query: string =
params?.query && Object.keys(params?.query).length
? `?${new URLSearchParams(
Expand All @@ -60,7 +61,11 @@ export class Request {
).toString()}`
: ''
const headers = {
Authorization: `Bearer ${this.response.access_token}`,
Authorization: `Bearer ${
params?.access_token
? params?.access_token
: this.response.access_token
}`,
'content-type': 'application/json',
...(params?.headers ? params.headers : {}),
}
Expand Down
21 changes: 14 additions & 7 deletions src/resources/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,44 +79,48 @@ export class Activities {

async createActivity(
params: CreateActivityRequest,
access_token?: string,
): Promise<DetailedActivity> {
return await this.request.makeApiRequest<DetailedActivity>(
'post',
'/activities',
{ body: params },
{ body: params, access_token },
)
}

async getActivityById(
params: GetActivityByIdRequest,
access_token?: string,
): Promise<DetailedActivity> {
const { id, ...query } = params
return await this.request.makeApiRequest<DetailedActivity>(
'get',
`/activities/${id}`,
{ query },
{ query, access_token },
)
}

async getCommentsByActivityId(
params: GetCommentsByActivityIdRequest,
access_token?: string,
): Promise<Comment[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<Comment[]>(
'get',
`/activities/${id}/comments`,
{ query },
{ query, access_token },
)
}

async getKudoersByActivityId(
params: GetKudoersByActivityIdRequest,
access_token?: string,
): Promise<SummaryAthlete[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<SummaryAthlete[]>(
'get',
`/activities/${id}/kudos`,
{ query },
{ query, access_token },
)
}

Expand All @@ -133,12 +137,13 @@ export class Activities {

async getPhotosByActivityId(
params: GetPhotosByActivityIdRequest,
access_token?: string,
): Promise<Lap[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<Lap[]>(
'get',
`/activities/${id}/photos`,
{ query },
{ query, access_token },
)
}

Expand All @@ -154,23 +159,25 @@ export class Activities {

async getZonesByActivityId(
params: GetZonesByActivityIdRequest,
access_token?: string,
): Promise<ActivityZone[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<ActivityZone[]>(
'get',
`/activities/${id}/zones`,
{ query },
{ query, access_token },
)
}

async updateActivityById(
params: UpdateActivityByIdRequest,
access_token?: string,
): Promise<DetailedActivity> {
const { id, ...body } = params
return await this.request.makeApiRequest<DetailedActivity>(
'put',
`/activities/${id}`,
{ body },
{ body, access_token },
)
}
}
25 changes: 18 additions & 7 deletions src/resources/athletes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,40 @@ export class Athletes {
this.request = request
}

async getLoggedInAthlete(): Promise<DetailedAthlete> {
return await this.request.makeApiRequest<DetailedAthlete>('get', '/athlete')
async getLoggedInAthlete(access_token?: string): Promise<DetailedAthlete> {
return await this.request.makeApiRequest<DetailedAthlete>(
'get',
'/athlete',
{ access_token },
)
}

async getLoggedInAthleteZones(): Promise<Zones> {
return await this.request.makeApiRequest<Zones>('get', '/athlete/zones')
async getLoggedInAthleteZones(access_token?: string): Promise<Zones> {
return await this.request.makeApiRequest<Zones>('get', '/athlete/zones', {
access_token,
})
}

async getStats(params: getStatsRequest): Promise<ActivityStats> {
async getStats(
params: getStatsRequest,
access_token?: string,
): Promise<ActivityStats> {
const { id } = params
return await this.request.makeApiRequest<ActivityStats>(
'get',
`/athletes/${id}/stats`,
`/athletes/${id}/s tats`,
{ access_token },
)
}

async updateLoggedInAthlete(
params: UpdateLoggedInAthleteRequest,
access_token?: string,
): Promise<DetailedAthlete> {
return await this.request.makeApiRequest<DetailedAthlete>(
'put',
`/athlete`,
{ body: params },
{ body: params, access_token },
)
}
}
11 changes: 8 additions & 3 deletions src/resources/clubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,25 @@ export class Clubs {

async getClubActivitiesById(
params: GetClubActivitiesByIdRequest,
access_token?: string,
): Promise<SummaryActivity[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<SummaryActivity[]>(
'get',
`/clubs/${id}/activities`,
{ query },
{ query, access_token },
)
}

async getClubAdminsById(
params: GetClubAdminsByIdRequest,
access_token?: string,
): Promise<SummaryAthlete[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<SummaryAthlete[]>(
'get',
`/clubs/${id}/admins`,
{ query },
{ query, access_token },
)
}

Expand All @@ -72,23 +74,26 @@ export class Clubs {

async getClubMembersById(
params: GetClubMembersByIdRequest,
access_token?: string,
): Promise<SummaryAthlete[]> {
const { id, ...query } = params
return await this.request.makeApiRequest<SummaryAthlete[]>(
'get',
`/clubs/${id}/members`,
{ query },
{ query, access_token },
)
}

async getLoggedInAthleteClubs(
params?: getLoggedInAthleteClubsRequest,
access_token?: string,
): Promise<SummaryClub[]> {
return await this.request.makeApiRequest<SummaryClub[]>(
'get',
'/athlete/clubs',
{
query: params,
access_token,
},
)
}
Expand Down
11 changes: 9 additions & 2 deletions src/resources/gears.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ export class Gears {
this.request = request
}

async getGearById(params: getGearByIdRequest): Promise<DetailedGear> {
async getGearById(
params: getGearByIdRequest,
access_token?: string,
): Promise<DetailedGear> {
const { id } = params
return await this.request.makeApiRequest<DetailedGear>('get', `/gear/${id}`)
return await this.request.makeApiRequest<DetailedGear>(
'get',
`/gear/${id}`,
{ access_token },
)
}
}
32 changes: 32 additions & 0 deletions src/resources/oauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fetch from 'node-fetch'

import { RefreshTokenRequest, RefreshTokenResponse } from '../types'

export class Oauth {
constructor() {}

async refreshTokens(
token: RefreshTokenRequest,
): Promise<RefreshTokenResponse> {
if (!token) {
throw new Error('No token provided')
}
const query: string = new URLSearchParams({
client_id: token.client_id,
client_secret: token.client_secret,
refresh_token: token.refresh_token,
grant_type: 'refresh_token',
}).toString()

const response = await fetch(
`https://www.strava.com/oauth/token?${query}`,
{
method: 'post',
},
)
if (!response.ok) {
throw response
}
return (await response.json()) as RefreshTokenResponse
}
}
12 changes: 10 additions & 2 deletions src/resources/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ export class Routes {
this.request = request
}

async getRouteAsGPX(params: GetRouteAsGPXRequest): Promise<any> {
async getRouteAsGPX(
params: GetRouteAsGPXRequest,
access_token?: string,
): Promise<any> {
const { id } = params
return await this.request.makeApiRequest<any>(
'get',
`/routes/${id}/export_gpx`,
{ access_token },
)
}

async getRouteAsTCX(params: GetRouteAsTCXRequest): Promise<any> {
async getRouteAsTCX(
params: GetRouteAsTCXRequest,
access_token?: string,
): Promise<any> {
const { id } = params
return await this.request.makeApiRequest<any>(
'get',
`/routes/${id}/export_tcx`,
{ access_token },
)
}

Expand Down
5 changes: 4 additions & 1 deletion src/resources/runningRaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ export class RunningRaces {

async getRunningRaceById(
params: GetRunningRaceByIdRequest,
access_token?: string,
): Promise<RunningRace> {
const { id } = params
return await this.request.makeApiRequest<RunningRace>(
'get',
`/running_races/${id}`,
{ access_token },
)
}

async getRunningRaces(
params?: GetRunningRacesRequest,
access_token?: string,
): Promise<RunningRace[]> {
return await this.request.makeApiRequest<RunningRace[]>(
'get',
'/running_races',
{ query: params },
{ query: params, access_token },
)
}
}
5 changes: 4 additions & 1 deletion src/resources/segmentEfforts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ export class SegmentEfforts {

async getEffortsBySegmentId(
params: GetEffortsBySegmentIdRequest,
access_token?: string,
): Promise<DetailedSegmentEffort[]> {
return await this.request.makeApiRequest<DetailedSegmentEffort[]>(
'get',
'/segment_efforts',
{ query: params },
{ query: params, access_token },
)
}

async getSegmentEffortById(
params: GetSegmentEffortByIdRequest,
access_token?: string,
): Promise<DetailedSegmentEffort> {
const { id } = params
return await this.request.makeApiRequest<DetailedSegmentEffort>(
'get',
`/segment_efforts/${id}`,
{ access_token },
)
}
}
Loading