Skip to content

[VEG-2395] Remove axios from zcli-core #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/zcli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"dependencies": {
"@oclif/plugin-plugins": "=2.1.12",
"axios": "^0.27.2",
"chalk": "^4.1.2",
"fs-extra": "^10.1.0"
},
Expand Down
76 changes: 52 additions & 24 deletions packages/zcli-core/src/lib/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ describe('Auth', () => {
describe('loginInteractively', () => {
const auth = new Auth({ secureStore: new SecureStore() })
const promptStub = sinon.stub()
const sandbox = sinon.createSandbox()
let fetchStub: any

before(() => {
fetchStub = sandbox.stub(global, 'fetch')
})
after(() => {
sandbox.restore()
})

test
.do(() => {
Expand All @@ -88,16 +97,21 @@ describe('Auth', () => {
.stub(CliUx.ux, 'prompt', () => promptStub)
.stub(auth.secureStore, 'setSecret', () => Promise.resolve())
.stub(auth, 'setLoggedInProfile', () => Promise.resolve())
.nock('https://z3ntest.zendesk.com', api => {
api
.get('/api/v2/account/settings.json')
.reply(function () {
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
return [200]
})
.do(() => {
fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/account/settings.json',
sinon.match(function (params) {
if (params.headers.Authorization === 'Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') {
return true
}
return false
})).resolves({
status: 200,
json: () => Promise.resolve({})
} as any)
})
.it('should return true on login success', async () => {
expect(await auth.loginInteractively()).to.equal(true)
sandbox.reset()
})

test
Expand All @@ -110,16 +124,21 @@ describe('Auth', () => {
.stub(CliUx.ux, 'prompt', () => promptStub)
.stub(auth.secureStore, 'setSecret', () => Promise.resolve())
.stub(auth, 'setLoggedInProfile', () => Promise.resolve())
.nock('https://z3ntest.example.com', api => {
api
.get('/api/v2/account/settings.json')
.reply(function () {
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
return [200]
})
.do(() => {
fetchStub.withArgs('https://z3ntest.example.com/api/v2/account/settings.json',
sinon.match(function (params) {
if (params.headers.Authorization === 'Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') {
return true
}
return false
})).resolves({
status: 200,
json: () => Promise.resolve({})
} as any)
})
.it('should login successfully using the passed domain and the prompted subdomain', async () => {
expect(await auth.loginInteractively({ domain: 'example.com' } as Profile)).to.equal(true)
sandbox.reset()
})

test
Expand All @@ -131,16 +150,21 @@ describe('Auth', () => {
.stub(CliUx.ux, 'prompt', () => promptStub)
.stub(auth.secureStore, 'setSecret', () => Promise.resolve())
.stub(auth, 'setLoggedInProfile', () => Promise.resolve())
.nock('https://z3ntest.example.com', api => {
api
.get('/api/v2/account/settings.json')
.reply(function () {
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
return [200]
})
.do(() => {
fetchStub.withArgs('https://z3ntest.example.com/api/v2/account/settings.json',
sinon.match(function (params) {
if (params.headers.Authorization === 'Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') {
return true
}
return false
})).resolves({
status: 200,
json: () => Promise.resolve({})
} as any)
})
.it('should login successfully using the passed subdomain and domain', async () => {
expect(await auth.loginInteractively({ subdomain: 'z3ntest', domain: 'example.com' })).to.equal(true)
sandbox.reset()
})

test
Expand All @@ -151,11 +175,15 @@ describe('Auth', () => {
promptStub.onThirdCall().resolves('123456')
})
.stub(CliUx.ux, 'prompt', () => promptStub)
.nock('https://z3ntest.zendesk.com', api => api
.get('/api/v2/account/settings.json')
.reply(403))
.do(() => {
fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/account/settings.json', sinon.match.any).resolves({
status: 403,
json: () => Promise.resolve({})
} as any)
})
.it('should return false on login failure', async () => {
expect(await auth.loginInteractively()).to.equal(false)
sandbox.reset()
})
})

Expand Down
12 changes: 5 additions & 7 deletions packages/zcli-core/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CLIError } from '@oclif/core/lib/errors'
import * as chalk from 'chalk'
import { CliUx } from '@oclif/core'
import Config from './config'
import axios from 'axios'
import SecureStore from './secureStore'
import { Profile } from '../types'
import { getAccount, parseSubdomain } from './authUtils'
Expand Down Expand Up @@ -67,12 +66,11 @@ export default class Auth {
const email = await CliUx.ux.prompt('Email')
const token = await CliUx.ux.prompt('API Token', { type: 'hide' })
const authToken = this.createBasicAuthToken(email, token)
const testAuth = await axios.get(
`${baseUrl}/api/v2/account/settings.json`,
{
headers: { Authorization: authToken },
validateStatus: function (status) { return status < 500 }
})
const testAuth = await fetch(`${baseUrl}/api/v2/account/settings.json`, {
headers: {
Authorization: authToken
}
})

if (testAuth.status === 200 && this.secureStore) {
await this.secureStore.setSecret(account, authToken)
Expand Down
20 changes: 16 additions & 4 deletions packages/zcli-core/src/lib/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, test } from '@oclif/test'
import * as sinon from 'sinon'
import { createRequestConfig, requestAPI } from './request'
import * as requestUtils from './requestUtils'
import Auth from './auth'
Expand Down Expand Up @@ -90,6 +91,14 @@ describe('createRequestConfig', () => {
})

describe('requestAPI', () => {
const sandbox = sinon.createSandbox()
let fetchStub: any
before(() => {
fetchStub = sandbox.stub(global, 'fetch')
})
after(() => {
sandbox.restore()
})
test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
Expand All @@ -99,13 +108,16 @@ describe('requestAPI', () => {
})
.stub(requestUtils, 'getSubdomain', () => 'fake')
.stub(requestUtils, 'getDomain', () => 'fake.com')
.nock('https://z3ntest.zendesk.com', api => {
api
.get('/api/v2/me')
.reply(200)
.do(() => {
fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/me', sinon.match.any).resolves({
status: 200,
ok: true,
json: () => Promise.resolve({})
} as any)
})
.it('should call an http endpoint', async () => {
const response = await requestAPI('api/v2/me', { method: 'GET' })
expect(response.status).to.equal(200)
sandbox.reset()
})
})
18 changes: 15 additions & 3 deletions packages/zcli-core/src/lib/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios'
import SecureStore from './secureStore'
import Auth from './auth'
import { CLIError } from '@oclif/core/lib/errors'
Expand Down Expand Up @@ -47,8 +46,21 @@ export const createRequestConfig = async (url: string, options: any = {}) => {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const requestAPI = async (url: string, options: any = {}, json = false) => {
const requestConfig = await createRequestConfig(url, options)
return axios.request(requestConfig)
const response = await fetch(`${requestConfig.baseURL}/${requestConfig.url}`, {
method: requestConfig.method,
headers: requestConfig.headers,
body: requestConfig.data ? JSON.stringify(requestConfig.data) : undefined
})

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}

return {
config: requestConfig,
status: response.status,
data: await response.json()
}
}
Loading