n8n/packages/nodes-base/nodes/LinkedIn/GenericFunctions.ts
कारतोफ्फेलस्क्रिप्ट™ 7a4e9ef5fa
refactor: Remove n8n-core dependency in nodes-base (no-changelog) (#5649)
2023-03-09 18:13:15 +01:00

60 lines
1.2 KiB
TypeScript

import type { OptionsWithUrl } from 'request';
import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
export async function linkedInApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: any = {},
binary?: boolean,
_headers?: object,
): Promise<any> {
const options: OptionsWithUrl = {
headers: {
Accept: 'application/json',
'X-Restli-Protocol-Version': '2.0.0',
},
method,
body,
url: binary ? endpoint : `https://api.linkedin.com/v2${endpoint}`,
json: true,
};
// If uploading binary data
if (binary) {
delete options.json;
options.encoding = null;
}
if (Object.keys(body as IDataObject).length === 0) {
delete options.body;
}
try {
return await this.helpers.requestOAuth2.call(this, 'linkedInOAuth2Api', options, {
tokenType: 'Bearer',
});
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
export function validateJSON(json: string | undefined): any {
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = '';
}
return result;
}