2020-04-23 17:56:53 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
2020-08-10 05:44:18 -07:00
|
|
|
} from 'request';
|
2019-12-02 13:21:55 -08:00
|
|
|
|
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
BINARY_ENCODING,
|
2019-12-02 13:21:55 -08:00
|
|
|
IExecuteFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
IExecuteSingleFunctions,
|
2019-12-02 13:21:55 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2019-12-02 13:21:55 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-04-23 17:56:53 -07:00
|
|
|
import {
|
|
|
|
snakeCase,
|
|
|
|
} from 'change-case';
|
|
|
|
|
|
|
|
export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('shopifyApi');
|
2019-12-02 13:21:55 -08:00
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2019-12-02 13:21:55 -08:00
|
|
|
}
|
|
|
|
const headerWithAuthentication = Object.assign({},
|
2020-11-07 10:44:13 -08:00
|
|
|
{ Authorization: `Basic ${Buffer.from(`${credentials.apiKey}:${credentials.password}`).toString(BINARY_ENCODING)}` });
|
2019-12-02 13:21:55 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method,
|
|
|
|
qs: query,
|
2019-12-06 14:47:19 -08:00
|
|
|
uri: uri || `https://${credentials.shopSubdomain}.myshopify.com/admin/api/2019-10${resource}`,
|
2019-12-02 13:21:55 -08:00
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-12-02 13:21:55 -08:00
|
|
|
};
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2020-04-23 17:56:53 -07:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
2020-08-10 05:44:18 -07:00
|
|
|
if (Object.keys(body).length === 0) {
|
2020-04-23 17:56:53 -07:00
|
|
|
delete options.body;
|
|
|
|
}
|
2020-08-10 05:44:18 -07:00
|
|
|
if (Object.keys(query).length === 0) {
|
2020-04-23 17:56:53 -07:00
|
|
|
delete options.qs;
|
|
|
|
}
|
2019-12-02 13:21:55 -08:00
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 13:21:55 -08:00
|
|
|
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2020-08-10 05:44:18 -07:00
|
|
|
export async function shopifyApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-04-23 17:56:53 -07:00
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await shopifyApiRequest.call(this, method, resource, body, query, uri, { resolveWithFullResponse: true });
|
|
|
|
if (responseData.headers.link) {
|
2020-08-10 05:44:18 -07:00
|
|
|
uri = responseData.headers['link'].split(';')[0].replace('<', '').replace('>', '');
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData.body[propertyName]);
|
|
|
|
} while (
|
|
|
|
responseData.headers['link'] !== undefined &&
|
|
|
|
responseData.headers['link'].includes('rel="next"')
|
|
|
|
);
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2020-08-10 05:44:18 -07:00
|
|
|
export function keysToSnakeCase(elements: IDataObject[] | IDataObject): IDataObject[] {
|
2020-04-23 17:56:53 -07:00
|
|
|
if (elements === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (!Array.isArray(elements)) {
|
|
|
|
elements = [elements];
|
|
|
|
}
|
|
|
|
for (const element of elements) {
|
|
|
|
for (const key of Object.keys(element)) {
|
|
|
|
if (key !== snakeCase(key)) {
|
|
|
|
element[snakeCase(key)] = element[key];
|
|
|
|
delete element[key];
|
|
|
|
}
|
2019-12-02 13:21:55 -08:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 17:56:53 -07:00
|
|
|
return elements;
|
2019-12-02 13:21:55 -08:00
|
|
|
}
|