mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-28 14:09:43 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
import { OptionsWithUri } from 'request';
|
|
|
|
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
|
|
|
export async function googleApiRequest(
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
qs: IDataObject = {},
|
|
uri?: string,
|
|
option: IDataObject = {},
|
|
): Promise<any> {
|
|
let options: OptionsWithUri = {
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method,
|
|
body,
|
|
qs,
|
|
uri: uri || `https://analyticsreporting.googleapis.com${endpoint}`,
|
|
json: true,
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
try {
|
|
if (Object.keys(body).length === 0) {
|
|
delete options.body;
|
|
}
|
|
if (Object.keys(qs).length === 0) {
|
|
delete options.qs;
|
|
}
|
|
//@ts-ignore
|
|
return await this.helpers.requestOAuth2.call(this, 'googleAnalyticsOAuth2', options);
|
|
} catch (error) {
|
|
throw new NodeApiError(this.getNode(), error);
|
|
}
|
|
}
|
|
|
|
export async function googleApiRequestAllItems(
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
propertyName: string,
|
|
method: string,
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
query: IDataObject = {},
|
|
uri?: string,
|
|
): Promise<any> {
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
do {
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
|
|
if (body.reportRequests && Array.isArray(body.reportRequests)) {
|
|
body.reportRequests[0].pageToken = responseData[propertyName][0].nextPageToken;
|
|
} else {
|
|
body.pageToken = responseData.nextPageToken;
|
|
}
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
} while (
|
|
(responseData.nextPageToken !== undefined && responseData.nextPageToken !== '') ||
|
|
responseData[propertyName]?.[0].nextPageToken !== undefined
|
|
);
|
|
|
|
return returnData;
|
|
}
|
|
|
|
export function simplify(responseData: any | [any]) {
|
|
const response = [];
|
|
for (const {
|
|
columnHeader: { dimensions },
|
|
data: { rows },
|
|
} of responseData) {
|
|
if (rows === undefined) {
|
|
// Do not error if there is no data
|
|
continue;
|
|
}
|
|
for (const row of rows) {
|
|
const data: IDataObject = {};
|
|
if (dimensions) {
|
|
for (let i = 0; i < dimensions.length; i++) {
|
|
data[dimensions[i]] = row.dimensions[i];
|
|
data.total = row.metrics[0].values.join(',');
|
|
}
|
|
} else {
|
|
data.total = row.metrics[0].values.join(',');
|
|
}
|
|
response.push(data);
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
|
|
export function merge(responseData: [any]) {
|
|
const response: { columnHeader: IDataObject; data: { rows: [] } } = {
|
|
columnHeader: responseData[0].columnHeader,
|
|
data: responseData[0].data,
|
|
};
|
|
const allRows = [];
|
|
for (const {
|
|
data: { rows },
|
|
} of responseData) {
|
|
allRows.push(...rows);
|
|
}
|
|
response.data.rows = allRows as [];
|
|
return [response];
|
|
}
|