2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
IHookFunctions,
|
|
|
|
IWebhookFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-04-03 02:07:21 -07:00
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
/**
|
|
|
|
* Return the base API URL based on the user's environment.
|
|
|
|
*/
|
|
|
|
const getBaseUrl = ({ environment, domain, subdomain }: ERPNextApiCredentials) =>
|
2023-05-22 03:11:08 -07:00
|
|
|
environment === 'cloudHosted' ? `https://${subdomain}.${domain}` : domain;
|
2023-01-13 09:11:56 -08:00
|
|
|
|
2021-04-03 02:07:21 -07:00
|
|
|
export async function erpNextApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-04-03 02:07:21 -07:00
|
|
|
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
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
let options: IRequestOptions = {
|
2021-04-03 02:07:21 -07:00
|
|
|
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,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${baseUrl}${resource}`,
|
2021-04-03 02:07:21 -07:00
|
|
|
json: true,
|
2022-12-02 12:54:28 -08:00
|
|
|
rejectUnauthorized: !credentials.allowUnauthorizedCerts,
|
2021-04-03 02:07:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
if (!Object.keys(options.body as IDataObject).length) {
|
2021-04-03 02:07:21 -07:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
if (!Object.keys(options.qs as IDataObject).length) {
|
2021-04-03 02:07:21 -07:00
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
try {
|
2022-12-23 10:09:52 -08: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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-04-03 02:07:21 -07:00
|
|
|
resource: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const returnData: any[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
2022-12-02 12:54:28 -08:00
|
|
|
query.limit_start = 0;
|
|
|
|
query.limit_page_length = 1000;
|
2021-04-03 02:07:21 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await erpNextApiRequest.call(this, method, resource, body, query);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
};
|