Minor WooCommerceTrigger-Node improvements

This commit is contained in:
Jan Oberhauser 2020-01-29 20:06:47 -08:00
parent cf1978aafa
commit fe9170e62f
2 changed files with 17 additions and 6 deletions

View file

@ -14,10 +14,10 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
const base64credentials = Buffer.from(`${credentials.consumerKey}:${credentials.consumerSecret}`).toString('base64');
let options: OptionsWithUri = { let options: OptionsWithUri = {
headers: { auth: {
Authorization: `Basic ${base64credentials}`, user: credentials.consumerKey as string,
password: credentials.consumerSecret as string,
}, },
method, method,
qs, qs,
@ -32,7 +32,18 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
throw new Error('WooCommerce Error: ' + error); if (error.statusCode === 401) {
// Return a clear error
throw new Error('The WooCommerce credentials are not valid!');
}
if (error.response.body && error.response.body.message) {
// Try to return the error prettier
throw new Error(`WooCommerce Error [${error.statusCode}]: ${error.response.body.message}`);
}
// If that data does not exist for some reason return the actual error
throw new Error('WooCommerce Error: ' + error.message);
} }
} }
@ -45,5 +56,5 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct
*/ */
export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) { export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) {
const data = `${credentials.consumerKey},${credentials.consumerSecret}`; const data = `${credentials.consumerKey},${credentials.consumerSecret}`;
return createHash('md5').update(data).digest("hex"); return createHash('md5').update(data).digest('hex');
} }

View file

@ -176,6 +176,6 @@ export class WooCommerceTrigger implements INodeType {
workflowData: [ workflowData: [
this.helpers.returnJsonArray(req.body), this.helpers.returnJsonArray(req.body),
], ],
} };
} }
} }