mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Add generic functions
This commit is contained in:
parent
8ca3d2876c
commit
33d13f231c
|
@ -0,0 +1,58 @@
|
||||||
|
import {
|
||||||
|
OptionsWithUri,
|
||||||
|
} from 'request';
|
||||||
|
|
||||||
|
import {
|
||||||
|
IExecuteFunctions,
|
||||||
|
IHookFunctions,
|
||||||
|
} from 'n8n-core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
IDataObject,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
|
export async function instagramBasicDisplayApiRequest(
|
||||||
|
this: IHookFunctions | IExecuteFunctions,
|
||||||
|
method: string,
|
||||||
|
endpoint: string,
|
||||||
|
body: IDataObject = {},
|
||||||
|
qs: IDataObject = {},
|
||||||
|
) {
|
||||||
|
|
||||||
|
const options: OptionsWithUri = {
|
||||||
|
method,
|
||||||
|
headers: {
|
||||||
|
'User-Agent': 'n8n',
|
||||||
|
'Content-Type': 'text/plain',
|
||||||
|
'Accept': 'application/json',
|
||||||
|
},
|
||||||
|
body,
|
||||||
|
qs,
|
||||||
|
uri: `https://graph.instagram.com${endpoint}`,
|
||||||
|
json: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Object.keys(qs).length === 0) {
|
||||||
|
delete qs.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(body).length === 0) {
|
||||||
|
delete options.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
//@ts-ignore
|
||||||
|
return await this.helpers.requestOAuth2.call(this, 'instagramBasicDisplayOAuth2Api', options, 'Bearer');
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
if (error.statusCode === 401) {
|
||||||
|
throw new Error('Invalid Instagram credentials!');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error?.error?.error?.message) {
|
||||||
|
throw new Error(`Instagram error response [${error.statusCode}]: ${error?.error?.error?.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue