Fix issues on Strava-Node

This commit is contained in:
Jan Oberhauser 2020-10-22 10:19:17 +02:00
parent 95b33662c9
commit 3e56d5fc08
4 changed files with 79 additions and 61 deletions

View file

@ -1,7 +1,7 @@
import { import {
INodeProperties, INodeProperties,
} from "n8n-workflow"; } from 'n8n-workflow';
export const activityOperations = [ export const activityOperations = [
{ {
@ -37,9 +37,9 @@ export const activityOperations = [
description: 'Get all activity comments', description: 'Get all activity comments',
}, },
{ {
name: 'Get Kudoers', name: 'Get Kudos',
value: 'getKudoers', value: 'getKudos',
description: 'Get all activity kudoers', description: 'Get all activity kudos',
}, },
{ {
name: 'Get Laps', name: 'Get Laps',
@ -64,9 +64,9 @@ export const activityOperations = [
export const activityFields = [ export const activityFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* activity:create */ /* activity:create */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'Name', displayName: 'Name',
name: 'name', name: 'name',
@ -78,7 +78,7 @@ export const activityFields = [
'activity', 'activity',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
@ -96,7 +96,7 @@ export const activityFields = [
'activity', 'activity',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
@ -124,14 +124,14 @@ export const activityFields = [
displayName: 'Elapsed Time (Seconds)', displayName: 'Elapsed Time (Seconds)',
name: 'elapsedTime', name: 'elapsedTime',
type: 'number', type: 'number',
required :true, required: true,
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'activity', 'activity',
], ],
operation: [ operation: [
'create' 'create',
] ]
}, },
}, },
@ -191,9 +191,10 @@ export const activityFields = [
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */
/* activity:update */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* activity:update */
/* -------------------------------------------------------------------------- */
{ {
displayName: 'Activity ID', displayName: 'Activity ID',
name: 'activityId', name: 'activityId',
@ -273,9 +274,10 @@ export const activityFields = [
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */
/* activity:get */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* activity:get */
/* -------------------------------------------------------------------------- */
{ {
displayName: 'Activity ID', displayName: 'Activity ID',
name: 'activityId', name: 'activityId',
@ -294,9 +296,10 @@ export const activityFields = [
default: '', default: '',
description: 'ID or email of activity', description: 'ID or email of activity',
}, },
/* -------------------------------------------------------------------------- */
/* activity */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* activity */
/* -------------------------------------------------------------------------- */
{ {
displayName: 'Activity ID', displayName: 'Activity ID',
name: 'activityId', name: 'activityId',
@ -308,10 +311,10 @@ export const activityFields = [
'activity', 'activity',
], ],
operation: [ operation: [
'comment', 'getComments',
'lap', 'getLaps',
'kudo', 'getKudos',
'zone', 'getZones',
], ],
}, },
}, },
@ -330,7 +333,7 @@ export const activityFields = [
operation: [ operation: [
'getComments', 'getComments',
'getLaps', 'getLaps',
'getKudoers', 'getKudos',
'getZones', 'getZones',
], ],
}, },
@ -350,7 +353,7 @@ export const activityFields = [
operation: [ operation: [
'getComments', 'getComments',
'getLaps', 'getLaps',
'getKudoers', 'getKudos',
'getZones', 'getZones',
], ],
returnAll: [ returnAll: [
@ -365,9 +368,10 @@ export const activityFields = [
default: 50, default: 50,
description: 'How many results to return.', description: 'How many results to return.',
}, },
/* -------------------------------------------------------------------------- */
/* activity:getAll */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* activity:getAll */
/* -------------------------------------------------------------------------- */
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',

View file

@ -1,6 +1,6 @@
import { import {
OptionsWithUri, OptionsWithUri,
} from 'request'; } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
@ -89,4 +89,3 @@ export async function stravaApiRequestAllItems(this: IHookFunctions | ILoadOptio
return returnData; return returnData;
} }

View file

@ -74,13 +74,13 @@ export class Strava implements INodeType {
if (resource === 'activity') { if (resource === 'activity') {
//https://developers.strava.com/docs/reference/#api-Activities-createActivity //https://developers.strava.com/docs/reference/#api-Activities-createActivity
if (operation === 'create') { if (operation === 'create') {
const name = this.getNodeParameter('name', i) as string; const name = this.getNodeParameter('name', i) as string;
const type = this.getNodeParameter('type', i) as string; const type = this.getNodeParameter('type', i) as string;
const startDate = this.getNodeParameter('startDate', i) as string; const startDate = this.getNodeParameter('startDate', i) as string;
const elapsedTime = this.getNodeParameter('elapsedTime', i) as number; const elapsedTime = this.getNodeParameter('elapsedTime', i) as number;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
@ -105,33 +105,33 @@ export class Strava implements INodeType {
} }
//https://developers.strava.com/docs/reference/#api-Activities-getActivityById //https://developers.strava.com/docs/reference/#api-Activities-getActivityById
if (operation === 'get') { if (operation === 'get') {
const activityId = this.getNodeParameter('activityId', i) as string; const activityId = this.getNodeParameter('activityId', i) as string;
responseData = await stravaApiRequest.call(this, 'GET', `/activities/${activityId}`); responseData = await stravaApiRequest.call(this, 'GET', `/activities/${activityId}`);
} }
if (['getLaps', 'getZones', 'getKudoers', 'getComments'].includes(operation)) { if (['getLaps', 'getZones', 'getKudos', 'getComments'].includes(operation)) {
const path: IDataObject = { const path: IDataObject = {
'getComments': 'comments', 'getComments': 'comments',
'getZones': 'zones', 'getZones': 'zones',
'getKudoers': 'kudoers', 'getKudos': 'kudos',
'getLaps': 'laps', 'getLaps': 'laps',
}; };
const activityId = this.getNodeParameter('activityId', i) as string; const activityId = this.getNodeParameter('activityId', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
responseData = await stravaApiRequest.call(this, 'GET', `/activities/${activityId}/${path[operation]}`); responseData = await stravaApiRequest.call(this, 'GET', `/activities/${activityId}/${path[operation]}`);
if (returnAll === false) { if (returnAll === false) {
const limit = this.getNodeParameter('limit', i) as number; const limit = this.getNodeParameter('limit', i) as number;
responseData = responseData.splice(0, limit); responseData = responseData.splice(0, limit);
} }
} }
//https://developers.mailerlite.com/reference#subscribers //https://developers.mailerlite.com/reference#subscribers
if (operation === 'getAll') { if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll) { if (returnAll) {
@ -144,7 +144,7 @@ export class Strava implements INodeType {
} }
//https://developers.strava.com/docs/reference/#api-Activities-updateActivityById //https://developers.strava.com/docs/reference/#api-Activities-updateActivityById
if (operation === 'update') { if (operation === 'update') {
const activityId = this.getNodeParameter('activityId', i) as string; const activityId = this.getNodeParameter('activityId', i) as string;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject; const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;

View file

@ -161,30 +161,45 @@ export class StravaTrigger implements INodeType {
try { try {
responseData = await stravaApiRequest.call(this, 'POST', endpoint, body); responseData = await stravaApiRequest.call(this, 'POST', endpoint, body);
} catch (error) { } catch (error) {
const errors = error.response.body.errors; if (error.response && error.response.body && error.response.body.errors) {
for (error of errors) { const errors = error.response.body.errors;
// if there is a subscription already created for (error of errors) {
if (error.resource === 'PushSubscription' && error.code === 'already exists') { // if there is a subscription already created
const options = this.getNodeParameter('options') as IDataObject; if (error.resource === 'PushSubscription' && error.code === 'already exists') {
//get the current subscription const options = this.getNodeParameter('options') as IDataObject;
const webhooks = await stravaApiRequest.call(this, 'GET', `/push_subscriptions`, {}); //get the current subscription
const webhooks = await stravaApiRequest.call(this, 'GET', `/push_subscriptions`, {});
if (options.deleteIfExist) { if (options.deleteIfExist) {
// delete the subscription // delete the subscription
await stravaApiRequest.call(this, 'DELETE', `/push_subscriptions/${webhooks[0].id}`); await stravaApiRequest.call(this, 'DELETE', `/push_subscriptions/${webhooks[0].id}`);
// now there is room create a subscription with the n8n data // now there is room create a subscription with the n8n data
const body = { const body = {
callback_url: webhookUrl, callback_url: webhookUrl,
verify_token: randomBytes(20).toString('hex') as string, verify_token: randomBytes(20).toString('hex') as string,
}; };
responseData = await stravaApiRequest.call(this, 'POST', `/push_subscriptions`, body); responseData = await stravaApiRequest.call(this, 'POST', `/push_subscriptions`, body);
} else { } else {
throw new Error(`A subscription already exist [${webhooks[0].callback_url}]. throw new Error(`A subscription already exist [${webhooks[0].callback_url}].
If you want to delete this subcription and create a new one with the current parameters please go to options and set delete if exist to true`); If you want to delete this subcription and create a new one with the current parameters please go to options and set delete if exist to true`);
}
} }
} }
} }
if (!responseData) {
let errorMessage = '';
if (error.response && error.response.body && error.response.body.message) {
errorMessage = error.response.body.message;
} else {
errorMessage = error.message;
}
throw new Error(
`Strava error response [${error.statusCode}]: ${errorMessage}`
);
}
} }
if (responseData.id === undefined) { if (responseData.id === undefined) {