refactor: monorepo+pnpm (removed husky)

This commit is contained in:
Andrew Bastin
2021-09-10 00:28:28 +05:30
parent 917550ff4d
commit b28f82a881
445 changed files with 81301 additions and 63752 deletions

View File

@@ -0,0 +1,79 @@
import axios from "axios"
import { decodeB64StringToArrayBuffer } from "../utils/b64"
import { settingsStore } from "~/newstore/settings"
let cancelSource = axios.CancelToken.source()
export const cancelRunningAxiosRequest = () => {
cancelSource.cancel()
// Create a new cancel token
cancelSource = axios.CancelToken.source()
}
const axiosWithProxy = async (req) => {
try {
const { data } = await axios.post(
settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io",
{
...req,
wantsBinary: true,
},
{
cancelToken: cancelSource.token,
}
)
if (!data.success) {
throw new Error(data.data.message || "Proxy Error")
}
if (data.isBinary) {
data.data = decodeB64StringToArrayBuffer(data.data)
}
return data
} catch (e) {
// Check if the throw is due to a cancellation
if (axios.isCancel(e)) {
// eslint-disable-next-line no-throw-literal
throw "cancellation"
} else {
console.error(e)
throw e
}
}
}
const axiosWithoutProxy = async (req, _store) => {
try {
const res = await axios({
...req,
cancelToken: (cancelSource && cancelSource.token) || "",
responseType: "arraybuffer",
})
return res
} catch (e) {
if (axios.isCancel(e)) {
// eslint-disable-next-line no-throw-literal
throw "cancellation"
} else {
console.error(e)
throw e
}
}
}
const axiosStrategy = (req) => {
if (settingsStore.value.PROXY_ENABLED) {
return axiosWithProxy(req)
}
return axiosWithoutProxy(req)
}
export const testables = {
cancelSource,
}
export default axiosStrategy

View File

@@ -0,0 +1,90 @@
import { decodeB64StringToArrayBuffer } from "../utils/b64"
import { settingsStore } from "~/newstore/settings"
export const hasExtensionInstalled = () =>
typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined"
export const hasChromeExtensionInstalled = () =>
hasExtensionInstalled() &&
/Chrome/i.test(navigator.userAgent) &&
/Google/i.test(navigator.vendor)
export const hasFirefoxExtensionInstalled = () =>
hasExtensionInstalled() && /Firefox/i.test(navigator.userAgent)
export const cancelRunningExtensionRequest = () => {
if (
hasExtensionInstalled() &&
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest
) {
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest()
}
}
const extensionWithProxy = async (req) => {
const backupTimeDataStart = new Date().getTime()
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
method: "post",
url: settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io/",
data: {
...req,
wantsBinary: true,
},
})
const backupTimeDataEnd = new Date().getTime()
const parsedData = JSON.parse(res.data)
if (!parsedData.success) {
throw new Error(parsedData.data.message || "Proxy Error")
}
if (parsedData.isBinary) {
parsedData.data = decodeB64StringToArrayBuffer(parsedData.data)
}
if (!(res && res.config && res.config.timeData)) {
res.config = {
timeData: {
startTime: backupTimeDataStart,
endTime: backupTimeDataEnd,
},
}
}
parsedData.config = res.config
return parsedData
}
const extensionWithoutProxy = async (req) => {
const backupTimeDataStart = new Date().getTime()
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
...req,
wantsBinary: true,
})
const backupTimeDataEnd = new Date().getTime()
if (!(res && res.config && res.config.timeData)) {
res.config = {
timeData: {
startTime: backupTimeDataStart,
endTime: backupTimeDataEnd,
},
}
}
return res
}
const extensionStrategy = (req) => {
if (settingsStore.value.PROXY_ENABLED) {
return extensionWithProxy(req)
}
return extensionWithoutProxy(req)
}
export default extensionStrategy

View File

@@ -0,0 +1,59 @@
import axios from "axios"
import axiosStrategy from "../AxiosStrategy"
jest.mock("axios")
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
PROXY_ENABLED: false,
},
},
}
})
axios.CancelToken.source.mockReturnValue({ token: "test" })
axios.mockResolvedValue({})
describe("axiosStrategy", () => {
describe("No-Proxy Requests", () => {
test("sends request to the actual sender if proxy disabled", async () => {
await axiosStrategy({ url: "test" })
expect(axios).toBeCalledWith(
expect.objectContaining({
url: "test",
})
)
})
test("asks axios to return data as arraybuffer", async () => {
await axiosStrategy({ url: "test" })
expect(axios).toBeCalledWith(
expect.objectContaining({
responseType: "arraybuffer",
})
)
})
test("resolves successful requests", async () => {
await expect(axiosStrategy({})).resolves.toBeDefined()
})
test("rejects cancel errors with text 'cancellation'", () => {
axios.isCancel.mockReturnValueOnce(true)
axios.mockRejectedValue("err")
expect(axiosStrategy({})).rejects.toBe("cancellation")
})
test("rejects non-cancellation errors as-is", () => {
axios.isCancel.mockReturnValueOnce(false)
axios.mockRejectedValue("err")
expect(axiosStrategy({})).rejects.toBe("err")
})
})
})

View File

@@ -0,0 +1,156 @@
import axios from "axios"
import axiosStrategy, {
testables,
cancelRunningAxiosRequest,
} from "../AxiosStrategy"
jest.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
PROXY_ENABLED: true,
PROXY_URL: "test",
},
},
}
})
describe("cancelRunningAxiosRequest", () => {
test("cancels axios request and does that only 1 time", () => {
const cancelFunc = jest.spyOn(testables.cancelSource, "cancel")
cancelRunningAxiosRequest()
expect(cancelFunc).toHaveBeenCalledTimes(1)
})
})
describe("axiosStrategy", () => {
describe("Proxy Requests", () => {
test("sends POST request to proxy if proxy is enabled", async () => {
let passedURL
jest.spyOn(axios, "post").mockImplementation((url) => {
passedURL = url
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy({})
expect(passedURL).toEqual("test")
})
test("passes request fields to axios properly", async () => {
const reqFields = {
testA: "testA",
testB: "testB",
testC: "testC",
}
let passedFields
jest.spyOn(axios, "post").mockImplementation((_url, req) => {
passedFields = req
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy(reqFields)
expect(passedFields).toMatchObject(reqFields)
})
test("passes wantsBinary field", async () => {
let passedFields
jest.spyOn(axios, "post").mockImplementation((_url, req) => {
passedFields = req
return Promise.resolve({ data: { success: true, isBinary: false } })
})
await axiosStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
test("checks for proxy response success field and throws error message for non-success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: false,
data: {
message: "test message",
},
},
})
await expect(axiosStrategy({})).rejects.toThrow("test message")
})
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: false,
data: {},
},
})
await expect(axiosStrategy({})).rejects.toThrow("Proxy Error")
})
test("checks for proxy response success and doesn't throw for success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
data: {},
},
})
await expect(axiosStrategy({})).resolves.toBeDefined()
})
test("checks isBinary response field and resolve with the converted value if so", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
isBinary: true,
data: "testdata",
},
})
await expect(axiosStrategy({})).resolves.toMatchObject({
data: "testdata-converted",
})
})
test("checks isBinary response field and resolve with the actual value if not so", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
isBinary: false,
data: "testdata",
},
})
await expect(axiosStrategy({})).resolves.toMatchObject({
data: "testdata",
})
})
test("cancel errors are thrown with the string 'cancellation'", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
await expect(axiosStrategy({})).rejects.toBe("cancellation")
})
test("non-cancellation errors are thrown", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
await expect(axiosStrategy({})).rejects.toBe("errr")
})
})
})

View File

@@ -0,0 +1,218 @@
import extensionStrategy, {
hasExtensionInstalled,
hasChromeExtensionInstalled,
hasFirefoxExtensionInstalled,
cancelRunningExtensionRequest,
} from "../ExtensionStrategy"
jest.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
EXTENSIONS_ENABLED: true,
PROXY_ENABLED: false,
},
},
}
})
describe("hasExtensionInstalled", () => {
test("returns true if extension is present and hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
expect(hasExtensionInstalled()).toEqual(true)
})
test("returns false if extension not present or not hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
expect(hasExtensionInstalled()).toEqual(false)
})
})
describe("hasChromeExtensionInstalled", () => {
test("returns true if extension is hooked and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
})
describe("hasFirefoxExtensionInstalled", () => {
test("returns true if extension is hooked and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
})
describe("cancelRunningExtensionRequest", () => {
const cancelFunc = jest.fn()
beforeEach(() => {
cancelFunc.mockClear()
})
test("cancels request if extension installed and function present in hook", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
cancelRunningRequest: cancelFunc,
}
cancelRunningExtensionRequest()
expect(cancelFunc).toHaveBeenCalledTimes(1)
})
test("does not cancel request if extension not installed", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
cancelRunningExtensionRequest()
expect(cancelFunc).not.toHaveBeenCalled()
})
test("does not cancel request if extension installed but function not present", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
cancelRunningExtensionRequest()
expect(cancelFunc).not.toHaveBeenCalled()
})
})
describe("extensionStrategy", () => {
const sendReqFunc = jest.fn()
beforeEach(() => {
sendReqFunc.mockClear()
})
describe("Non-Proxy Requests", () => {
test("ask extension to send request", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await extensionStrategy({})
expect(sendReqFunc).toHaveBeenCalledTimes(1)
})
test("sends request to the actual sender if proxy disabled", async () => {
let passedUrl
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation(({ url }) => {
passedUrl = url
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({ url: "test" })
expect(passedUrl).toEqual("test")
})
test("asks extension to get binary data", async () => {
let passedFields
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation((fields) => {
passedFields = fields
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
test("resolves successful requests", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await expect(extensionStrategy({})).resolves.toBeDefined()
})
test("rejects errors as-is", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockRejectedValue("err")
await expect(extensionStrategy({})).rejects.toBe("err")
})
})
})

View File

@@ -0,0 +1,300 @@
import extensionStrategy, {
hasExtensionInstalled,
hasChromeExtensionInstalled,
hasFirefoxExtensionInstalled,
cancelRunningExtensionRequest,
} from "../ExtensionStrategy"
jest.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
value: {
EXTENSIONS_ENABLED: true,
PROXY_ENABLED: true,
PROXY_URL: "test",
},
},
}
})
describe("hasExtensionInstalled", () => {
test("returns true if extension is present and hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
expect(hasExtensionInstalled()).toEqual(true)
})
test("returns false if extension not present or not hooked", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
expect(hasExtensionInstalled()).toEqual(false)
})
})
describe("hasChromeExtensionInstalled", () => {
test("returns true if extension is hooked and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
})
describe("hasFirefoxExtensionInstalled", () => {
test("returns true if extension is hooked and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
})
describe("cancelRunningExtensionRequest", () => {
const cancelFunc = jest.fn()
beforeEach(() => {
cancelFunc.mockClear()
})
test("cancels request if extension installed and function present in hook", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
cancelRunningRequest: cancelFunc,
}
cancelRunningExtensionRequest()
expect(cancelFunc).toHaveBeenCalledTimes(1)
})
test("does not cancel request if extension not installed", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
cancelRunningExtensionRequest()
expect(cancelFunc).not.toHaveBeenCalled()
})
test("does not cancel request if extension installed but function not present", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
cancelRunningExtensionRequest()
expect(cancelFunc).not.toHaveBeenCalled()
})
})
describe("extensionStrategy", () => {
const sendReqFunc = jest.fn()
beforeEach(() => {
sendReqFunc.mockClear()
})
describe("Proxy Requests", () => {
test("asks extension to send request", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data":""}',
})
await extensionStrategy({})
expect(sendReqFunc).toHaveBeenCalledTimes(1)
})
test("sends POST request to proxy if proxy is enabled", async () => {
let passedUrl
let passedMethod
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation(({ method, url }) => {
passedUrl = url
passedMethod = method
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({})
expect(passedUrl).toEqual("test")
expect(passedMethod).toEqual("post")
})
test("passes request fields properly", async () => {
const reqFields = {
testA: "testA",
testB: "testB",
testC: "testC",
}
let passedFields
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation(({ data }) => {
passedFields = data
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy(reqFields)
expect(passedFields).toMatchObject(reqFields)
})
test("passes wantsBinary field", async () => {
let passedFields
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockImplementation(({ data }) => {
passedFields = data
return Promise.resolve({
data: '{"success":true,"data":""}',
})
})
await extensionStrategy({})
expect(passedFields).toHaveProperty("wantsBinary")
})
test("checks for proxy response success field and throws error message for non-success", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":false,"data": { "message": "testerr" } }',
})
await expect(extensionStrategy({})).rejects.toThrow("testerr")
})
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":false,"data": {} }',
})
await expect(extensionStrategy({})).rejects.toThrow("Proxy Error")
})
test("checks for proxy response success and doesn't throw for success", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success":true,"data": {} }',
})
await expect(extensionStrategy({})).resolves.toBeDefined()
})
test("checks isBinary response field and resolve with the converted value if so", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success": true, "isBinary": true, "data": "testdata" }',
})
await expect(extensionStrategy({})).resolves.toMatchObject({
data: "testdata-converted",
})
})
test("checks isBinary response field and resolve with the actual value if not so", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockResolvedValue({
data: '{"success": true, "isBinary": false, "data": "testdata" }',
})
await expect(extensionStrategy({})).resolves.toMatchObject({
data: "testdata",
})
})
test("failed request errors are thrown as-is", async () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
sendRequest: sendReqFunc,
}
sendReqFunc.mockRejectedValue("err")
await expect(extensionStrategy({})).rejects.toBe("err")
})
})
})