2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-02-05 00:25:41 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2021-02-05 00:25:41 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
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';
|
2021-02-05 00:25:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function tapfiliateApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: 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
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string | undefined,
|
|
|
|
option: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('tapfiliateApi');
|
2021-02-05 00:25:41 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Api-Key': credentials.apiKey,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.tapfiliate.com/1.6${endpoint}`,
|
2021-02-05 00:25:41 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2021-02-05 00:25:41 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-02-05 00:25:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function tapfiliateApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: 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
|
|
|
) {
|
2021-02-05 00:25:41 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await tapfiliateApiRequest.call(this, method, endpoint, body, query, '', {
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
});
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData.body as IDataObject[]);
|
2021-02-05 00:25:41 -08:00
|
|
|
query.page++;
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.headers.link.includes('next'));
|
2021-02-05 00:25:41 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|