2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-11-03 14:33:10 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
import { IRelation } from './Interfaces';
|
|
|
|
|
|
|
|
export async function orbitApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-11-03 14:33:10 -08:00
|
|
|
try {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('orbitApi');
|
2020-11-03 14:33:10 -08:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${credentials.accessToken}`,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
uri: uri || `https://app.orbit.love/api/v1${resource}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-11-03 14:33:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to paginated flow endpoint
|
|
|
|
* and return all results
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function orbitApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-11-03 14:33:10 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await orbitApiRequest.call(this, method, resource, body, query);
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
|
|
|
|
if (query.resolveIdentities === true) {
|
|
|
|
resolveIdentities(responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.resolveMember === true) {
|
|
|
|
resolveMember(responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
query.page++;
|
2022-08-17 08:50:24 -07:00
|
|
|
if (query.limit && returnData.length >= query.limit) {
|
2020-11-03 14:33:10 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.data.length !== 0);
|
2020-11-03 14:33:10 -08:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveIdentities(responseData: IRelation) {
|
|
|
|
const identities: IDataObject = {};
|
|
|
|
for (const data of responseData.included) {
|
|
|
|
identities[data.id as string] = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(responseData.data)) {
|
|
|
|
responseData.data = [responseData.data];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < responseData.data.length; i++) {
|
|
|
|
for (let y = 0; y < responseData.data[i].relationships.identities.data.length; y++) {
|
|
|
|
//@ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData.data[i].relationships.identities.data[y] =
|
|
|
|
identities[responseData.data[i].relationships.identities.data[y].id];
|
2020-11-03 14:33:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveMember(responseData: IRelation) {
|
|
|
|
const members: IDataObject = {};
|
|
|
|
for (const data of responseData.included) {
|
|
|
|
members[data.id as string] = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(responseData.data)) {
|
|
|
|
responseData.data = [responseData.data];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < responseData.data.length; i++) {
|
|
|
|
//@ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData.data[i].relationships.member.data =
|
|
|
|
//@ts-ignore
|
|
|
|
members[responseData.data[i].relationships.member.data.id];
|
2020-11-03 14:33:10 -08:00
|
|
|
}
|
|
|
|
}
|