2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-10-22 01:17:39 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2020-10-22 01:17:39 -07:00
|
|
|
ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
2020-10-22 01:17:39 -07:00
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-10-22 01:17:39 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function stravaApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: string,
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2020-10-22 01:17:39 -07:00
|
|
|
method,
|
|
|
|
form: body,
|
|
|
|
qs,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://www.strava.com/api/v3${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-10-22 01:17:39 -07:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
if (Object.keys(headers).length !== 0) {
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.getNode().type.includes('Trigger') && resource.includes('/push_subscriptions')) {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('stravaOAuth2Api');
|
2024-10-14 03:17:19 -07:00
|
|
|
if (method === 'GET' || method === 'DELETE') {
|
2020-10-22 01:17:39 -07:00
|
|
|
qs.client_id = credentials.clientId;
|
|
|
|
qs.client_secret = credentials.clientSecret;
|
|
|
|
} else {
|
|
|
|
body.client_id = credentials.clientId;
|
|
|
|
body.client_secret = credentials.clientSecret;
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
return await this.helpers?.request(options);
|
2020-10-22 01:17:39 -07:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'stravaOAuth2Api', options, {
|
|
|
|
includeCredentialsOnRefreshOnBody: true,
|
|
|
|
});
|
2020-10-22 01:17:39 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-10-22 01:17:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function stravaApiRequestAllItems(
|
|
|
|
this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
query: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2020-10-22 01:17:39 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.per_page = 30;
|
|
|
|
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await stravaApiRequest.call(this, method, resource, body, query);
|
|
|
|
query.page++;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.length !== 0);
|
2020-10-22 01:17:39 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|