diff --git a/packages/zcli-apps/tests/functional/create.test.ts b/packages/zcli-apps/tests/functional/create.test.ts index c600ec6a..233b04f3 100644 --- a/packages/zcli-apps/tests/functional/create.test.ts +++ b/packages/zcli-apps/tests/functional/create.test.ts @@ -26,7 +26,8 @@ describe('apps', function () { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) .stub(createAppUtils, 'getManifestAppName', () => 'importantAppName') - .stub(requestUtils, 'getSubdomain', () => Promise.resolve('z3ntest')) + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .stub(appConfig, 'setConfig', () => Promise.resolve()) .env(env) .do(() => { @@ -64,6 +65,8 @@ describe('apps', function () { describe('with single app', () => { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .env(env) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') diff --git a/packages/zcli-apps/tests/functional/package.test.ts b/packages/zcli-apps/tests/functional/package.test.ts index d97f86ba..d09825e6 100644 --- a/packages/zcli-apps/tests/functional/package.test.ts +++ b/packages/zcli-apps/tests/functional/package.test.ts @@ -4,10 +4,13 @@ import * as fs from 'fs' import * as readline from 'readline' import * as AdmZip from 'adm-zip' import env from './env' +import * as requestUtils from '../../../zcli-core/src/lib/requestUtils' describe('package', function () { const appPath = path.join(__dirname, 'mocks/single_product_app') test + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .env(env) .nock('https://z3ntest.zendesk.com', api => { api @@ -22,6 +25,8 @@ describe('package', function () { }) test + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .env(env) .nock('https://z3ntest.zendesk.com', api => { api @@ -62,6 +67,8 @@ describe('zcliignore', function () { }) test + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .env(env) .nock('https://z3ntest.zendesk.com', api => { api diff --git a/packages/zcli-apps/tests/functional/validate.test.ts b/packages/zcli-apps/tests/functional/validate.test.ts index f50ae177..72f698f0 100644 --- a/packages/zcli-apps/tests/functional/validate.test.ts +++ b/packages/zcli-apps/tests/functional/validate.test.ts @@ -1,9 +1,12 @@ import { expect, test } from '@oclif/test' import * as path from 'path' import env from './env' +import * as requestUtils from '../../../zcli-core/src/lib/requestUtils' describe('validate', function () { test + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .env(env) .nock('https://z3ntest.zendesk.com', api => { api @@ -17,6 +20,8 @@ describe('validate', function () { }) test + .stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined)) + .stub(requestUtils, 'getDomain', () => Promise.resolve(undefined)) .env(env) .nock('https://z3ntest.zendesk.com', api => { api diff --git a/packages/zcli-core/src/lib/request.ts b/packages/zcli-core/src/lib/request.ts index 35724be6..8daa0fb0 100644 --- a/packages/zcli-core/src/lib/request.ts +++ b/packages/zcli-core/src/lib/request.ts @@ -4,15 +4,19 @@ import Auth from './auth' import { CLIError } from '@oclif/core/lib/errors' import * as chalk from 'chalk' import { EnvVars, varExists } from './env' -import { getBaseUrl, getDomain, getSubdomain } from './requestUtils' +import { getBaseUrl, getDomain as getProfileDomain, getSubdomain as getProfileSubdomain } from './requestUtils' + +const MSG_ENV_OR_LOGIN = 'Set the following environment variables: ZENDESK_SUBDOMAIN, ZENDESK_EMAIL, ZENDESK_API_TOKEN. Or try logging in via `zcli login -i`' +const ERR_NO_AUTH_TOKEN = `No authorization token found. ${MSG_ENV_OR_LOGIN}` +const ERR_AUTH_FAILED = `Authorization failed. ${MSG_ENV_OR_LOGIN}` +const ERR_ENV_SUBDOMAIN_NOT_FOUND = `No subdomain found. ${MSG_ENV_OR_LOGIN}` // eslint-disable-next-line @typescript-eslint/no-explicit-any export const requestAPI = async (url: string, options: any = {}, json = false) => { let auth if ( - varExists(EnvVars.SUBDOMAIN, EnvVars.EMAIL, EnvVars.PASSWORD) || - varExists(EnvVars.SUBDOMAIN, EnvVars.EMAIL, EnvVars.API_TOKEN) || - varExists(EnvVars.SUBDOMAIN, EnvVars.OAUTH_TOKEN) + varExists(EnvVars.SUBDOMAIN, EnvVars.OAUTH_TOKEN) || + varExists(EnvVars.SUBDOMAIN, EnvVars.EMAIL, EnvVars.API_TOKEN) ) { auth = new Auth() } else { @@ -22,23 +26,30 @@ export const requestAPI = async (url: string, options: any = {}, json = false) = } const authToken = await auth.getAuthorizationToken() - const subdomain = process.env[EnvVars.SUBDOMAIN] || (await getSubdomain(auth)) - const domain = process.env[EnvVars.SUBDOMAIN] ? process.env[EnvVars.DOMAIN] : await getDomain(auth) + if (!authToken) throw new CLIError(chalk.red(ERR_NO_AUTH_TOKEN)) + + const profileSubdomain = await getProfileSubdomain(auth) + const subdomain = process.env[EnvVars.SUBDOMAIN] || profileSubdomain + if (!subdomain) throw new CLIError(chalk.red(ERR_ENV_SUBDOMAIN_NOT_FOUND)) + + const profileDomain = await getProfileDomain(auth) + // If subdomain is set, use domain env even if not set. Otherwise, use profile domain. + const domain = process.env[EnvVars.SUBDOMAIN] ? process.env[EnvVars.DOMAIN] : profileDomain if (options.headers) { options.headers = { Authorization: authToken, ...options.headers } } else { options.headers = { Authorization: authToken } } - if (authToken && subdomain) { + const baseURL = getBaseUrl(subdomain, domain) return axios.request({ - baseURL: getBaseUrl(subdomain, domain), + baseURL, url, validateStatus: function (status) { return status < 500 }, ...options }) } - throw new CLIError(chalk.red('Authorization Failed, try logging in via `zcli login -i`!')) + throw new CLIError(chalk.red(ERR_AUTH_FAILED)) }