2022-08-01 13:47:55 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-04-03 02:07:21 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-04-03 02:07:21 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IDataObject, IHookFunctions, IWebhookFunctions, NodeApiError } from 'n8n-workflow';
|
2021-04-03 02:07:21 -07:00
|
|
|
|
|
|
|
export async function erpNextApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const credentials = (await this.getCredentials('erpNextApi')) as ERPNextApiCredentials;
|
2021-04-30 11:37:46 -07:00
|
|
|
const baseUrl = getBaseUrl(credentials);
|
2021-04-03 02:07:21 -07:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
Accept: 'application/json',
|
2021-04-03 02:07:21 -07:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
2021-04-30 11:37:46 -07:00
|
|
|
uri: uri || `${baseUrl}${resource}`,
|
2021-04-03 02:07:21 -07:00
|
|
|
json: true,
|
2022-07-20 04:50:16 -07:00
|
|
|
rejectUnauthorized: !credentials.allowUnauthorizedCerts as boolean,
|
2021-04-03 02:07:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
|
|
|
if (!Object.keys(options.body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(options.qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
try {
|
2022-08-01 13:47:55 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'erpNextApi', options);
|
2021-04-03 02:07:21 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 403) {
|
2021-04-30 11:37:46 -07:00
|
|
|
throw new NodeApiError(this.getNode(), { message: 'DocType unavailable.' });
|
2021-04-03 02:07:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error.statusCode === 307) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeApiError(this.getNode(), {
|
|
|
|
message: 'Please ensure the subdomain is correct.',
|
|
|
|
});
|
2021-04-03 02:07:21 -07:00
|
|
|
}
|
2022-11-11 02:32:43 -08:00
|
|
|
throw error;
|
2021-04-03 02:07:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function erpNextApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const returnData: any[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query!.limit_start = 0;
|
|
|
|
query!.limit_page_length = 1000;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await erpNextApiRequest.call(this, method, resource, body, query);
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
query!.limit_start += query!.limit_page_length - 1;
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData.data && responseData.data.length > 0);
|
2021-04-03 02:07:21 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
2021-04-30 11:37:46 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the base API URL based on the user's environment.
|
|
|
|
*/
|
|
|
|
const getBaseUrl = ({ environment, domain, subdomain }: ERPNextApiCredentials) =>
|
2022-08-01 13:47:55 -07:00
|
|
|
environment === 'cloudHosted' ? `https://${subdomain}.erpnext.com` : domain;
|
2021-04-30 11:37:46 -07:00
|
|
|
|
|
|
|
type ERPNextApiCredentials = {
|
|
|
|
apiKey: string;
|
|
|
|
apiSecret: string;
|
|
|
|
environment: 'cloudHosted' | 'selfHosted';
|
|
|
|
subdomain?: string;
|
|
|
|
domain?: string;
|
2022-07-20 04:50:16 -07:00
|
|
|
allowUnauthorizedCerts?: boolean;
|
2021-04-30 11:37:46 -07:00
|
|
|
};
|