UNPKG

@soleil-se/run

Version:

Run various utility scripts for creating apps, configs or migration tasks.

47 lines (40 loc) 1.49 kB
// eslint-disable-next-line import/no-unresolved import got from 'got'; import config from '@soleil-se/build-config'; const { name, host, auth, acceptUnauthorizedCert } = config.env; const [username, password] = Buffer.from(auth || '', 'base64').toString().split(/:(.*)/s); const options = { prefixUrl: `${host}/rest-api/1/0/${encodeURIComponent(name)}/`, username, password, responseType: 'json', resolveBodyOnly: true, hooks: { beforeRedirect: [ () => { // Addon upload won't work on redirect, throw error to stop the request throw new Error('REDIRECT'); }, ], beforeError: [ (error) => { const { response } = error; if (response) { const { body } = response; let message = body && body.message ? body.message : body.toString(); if (!message && response.statusCode === 401) { message = 'Unauthorized, correct username and password?'; } else if (response.statusCode >= 300 && response.statusCode < 400) { message = `Request to '${response?.requestUrl?.origin}' was redirected to '${response?.redirectUrls[0]?.origin}', check if the site primary address is correct in config.`; } return new Error(`${response.statusCode} - ${message}`); } return new Error(error.message); }, ], }, }; if (acceptUnauthorizedCert) { options.https = { rejectUnauthorized: false }; } export default got.extend(options);