2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2019-12-02 13:21:55 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2019-12-02 13:21:55 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
IOAuth2Options,
|
|
|
|
} from 'n8n-workflow';
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
import { snakeCase } from 'change-case';
|
|
|
|
|
|
|
|
export async function shopifyApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-07-15 01:36:01 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'oAuth2') as string;
|
|
|
|
|
|
|
|
let credentials;
|
|
|
|
let credentialType = 'shopifyOAuth2Api';
|
|
|
|
|
|
|
|
if (authenticationMethod === 'apiKey') {
|
|
|
|
credentials = await this.getCredentials('shopifyApi');
|
|
|
|
credentialType = 'shopifyApi';
|
|
|
|
} else if (authenticationMethod === 'accessToken') {
|
|
|
|
credentials = await this.getCredentials('shopifyAccessTokenApi');
|
|
|
|
credentialType = 'shopifyAccessTokenApi';
|
|
|
|
} else {
|
|
|
|
credentials = await this.getCredentials('shopifyOAuth2Api');
|
|
|
|
}
|
2019-12-02 13:21:55 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-19 04:37: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
|
|
|
|
2022-07-15 01:36:01 -07:00
|
|
|
const oAuth2Options: IOAuth2Options = {
|
|
|
|
tokenType: 'Bearer',
|
|
|
|
keyToIncludeInAccessTokenHeader: 'X-Shopify-Access-Token',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (authenticationMethod === 'apiKey') {
|
2022-08-17 08:50:24 -07:00
|
|
|
Object.assign(options, {
|
|
|
|
auth: { username: credentials.apiKey, password: credentials.password },
|
|
|
|
});
|
2022-07-15 01:36:01 -07:00
|
|
|
}
|
|
|
|
|
2020-04-23 17:56:53 -07:00
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).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;
|
|
|
|
}
|
2022-07-15 01:36:01 -07:00
|
|
|
|
2023-08-21 02:47:38 -07:00
|
|
|
// Only limit and fields are allowed for page_info links
|
|
|
|
// https://shopify.dev/docs/api/usage/pagination-rest#limitations-and-considerations
|
|
|
|
if (uri && uri.includes('page_info')) {
|
|
|
|
options.qs = {};
|
|
|
|
|
|
|
|
if (query.limit) {
|
|
|
|
options.qs.limit = query.limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.fields) {
|
|
|
|
options.qs.fields = query.fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, credentialType, options, {
|
2022-11-11 02:32:43 -08:00
|
|
|
oauth2: oAuth2Options,
|
|
|
|
});
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
2019-12-02 13:21:55 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function shopifyApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-04-23 17:56:53 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
2022-09-02 07:47:03 -07:00
|
|
|
/*
|
|
|
|
When paginating some parameters
|
|
|
|
(e.g. product:getAll -> title ) cannot
|
|
|
|
be empty in the query string, so remove
|
|
|
|
all the empty ones before paginating.
|
|
|
|
*/
|
|
|
|
for (const field in query) {
|
|
|
|
if (query[field] === '') {
|
|
|
|
delete query[field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 17:56:53 -07:00
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(this, method, resource, body, query, uri, {
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
});
|
2020-04-23 17:56:53 -07:00
|
|
|
if (responseData.headers.link) {
|
2022-12-02 12:54:28 -08:00
|
|
|
uri = responseData.headers.link.split(';')[0].replace('<', '').replace('>', '');
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData.body[propertyName] as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.headers.link?.includes('rel="next"'));
|
2020-04-23 17:56:53 -07:00
|
|
|
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
|
|
|
}
|