chore: merge hoppscotch/staging into self-hosted/main

This commit is contained in:
Andrew Bastin
2023-04-04 02:17:29 +05:30
19 changed files with 383 additions and 186 deletions

View File

@@ -50,7 +50,6 @@
"buffer": "^6.0.3",
"esprima": "^4.0.1",
"events": "^3.3.0",
"firebase": "^9.8.4",
"fp-ts": "^2.12.1",
"fuse.js": "^6.6.2",
"globalthis": "^1.0.3",

View File

@@ -26,7 +26,7 @@
</template>
<script setup lang="ts">
import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"
import { platform } from "~/platform"
import { GQLConnection } from "~/helpers/GQLConnection"
import { getCurrentStrategyID } from "~/helpers/network"
import { useReadonlyStream, useStream } from "@composables/stream"
@@ -48,7 +48,7 @@ const onConnectClick = () => {
if (!connected.value) {
props.conn.connect(url.value, headers.value as any)
logHoppRequestRunToAnalytics({
platform.analytics?.logHoppRequestRunToAnalytics({
platform: "graphql-schema",
strategy: getCurrentStrategyID(),
})

View File

@@ -379,7 +379,7 @@ import {
import { commonHeaders } from "~/helpers/headers"
import { GQLConnection } from "~/helpers/GQLConnection"
import { makeGQLHistoryEntry, addGraphqlHistoryEntry } from "~/newstore/history"
import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"
import { platform } from "~/platform"
import { getCurrentStrategyID } from "~/helpers/network"
import { useCodemirror } from "@composables/codemirror"
import jsonLinter from "~/helpers/editor/linting/json"
@@ -748,7 +748,7 @@ const runQuery = async () => {
console.error(e)
}
logHoppRequestRunToAnalytics({
platform.analytics?.logHoppRequestRunToAnalytics({
platform: "graphql-query",
strategy: getCurrentStrategyID(),
})

View File

@@ -0,0 +1,21 @@
import { platform } from "~/platform"
let initialized = false
export function initializeApp() {
if (!initialized) {
try {
platform.auth.performAuthInit()
platform.sync.settings.initSettingsSync()
platform.sync.collections.initCollectionsSync()
platform.sync.history.initHistorySync()
platform.sync.environments.initEnvironmentsSync()
platform.analytics?.initAnalytics()
initialized = true
} catch (e) {
// initializeApp throws exception if we reinitialize
initialized = true
}
}
}

View File

@@ -1,36 +0,0 @@
import { initializeApp } from "firebase/app"
import { platform } from "~/platform"
import { initAnalytics } from "./analytics"
const firebaseConfig = {
apiKey: import.meta.env.VITE_API_KEY,
authDomain: import.meta.env.VITE_AUTH_DOMAIN,
databaseURL: import.meta.env.VITE_DATABASE_URL,
projectId: import.meta.env.VITE_PROJECT_ID,
storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
appId: import.meta.env.VITE_APP_ID,
measurementId: import.meta.env.VITE_MEASUREMENT_ID,
}
let initialized = false
export function initializeFirebase() {
if (!initialized) {
try {
initializeApp(firebaseConfig)
platform.auth.performAuthInit()
platform.sync.settings.initSettingsSync()
platform.sync.collections.initCollectionsSync()
platform.sync.history.initHistorySync()
platform.sync.environments.initEnvironmentsSync()
initAnalytics()
initialized = true
} catch (e) {
// initializeApp throws exception if we reinitialize
initialized = true
}
}
}

View File

@@ -1,99 +0,0 @@
import {
collection,
doc,
getFirestore,
onSnapshot,
setDoc,
} from "firebase/firestore"
import { platform } from "~/platform"
import { applySetting, settingsStore, SettingsDef } from "~/newstore/settings"
/**
* Used locally to prevent infinite loop when settings sync update
* is applied to the store which then fires the store sync listener.
* When you want to update settings and not want to fire the update listener,
* set this to true and then set it back to false once it is done
*/
let loadedSettings = false
/**
* Write Transform
*/
async function writeSettings(setting: string, value: any) {
const currentUser = platform.auth.getCurrentUser()
if (currentUser === null)
throw new Error("Cannot write setting, user not signed in")
const st = {
updatedOn: new Date(),
author: currentUser.uid,
author_name: currentUser.displayName,
author_image: currentUser.photoURL,
name: setting,
value,
}
try {
await setDoc(
doc(getFirestore(), "users", currentUser.uid, "settings", setting),
st
)
} catch (e) {
console.error("error updating", st, e)
throw e
}
}
export function initSettings() {
const currentUser$ = platform.auth.getCurrentUserStream()
settingsStore.dispatches$.subscribe((dispatch) => {
const currentUser = platform.auth.getCurrentUser()
if (currentUser && loadedSettings) {
if (dispatch.dispatcher === "bulkApplySettings") {
Object.keys(dispatch.payload).forEach((key) => {
writeSettings(key, dispatch.payload[key])
})
} else {
writeSettings(
dispatch.payload.settingKey,
settingsStore.value[dispatch.payload.settingKey as keyof SettingsDef]
)
}
}
})
let snapshotStop: (() => void) | null = null
// Subscribe and unsubscribe event listeners
currentUser$.subscribe((user) => {
if (!user && snapshotStop) {
// User logged out
snapshotStop()
snapshotStop = null
} else if (user) {
snapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "settings"),
(settingsRef) => {
const settings: any[] = []
settingsRef.forEach((doc) => {
const setting = doc.data()
setting.id = doc.id
settings.push(setting)
})
loadedSettings = false
settings.forEach((e) => {
if (e && e.name && e.value != null) {
applySetting(e.name, e.value)
}
})
loadedSettings = true
}
)
}
})
}

View File

@@ -1,6 +1,6 @@
import Paho, { ConnectionOptions } from "paho-mqtt"
import { BehaviorSubject, Subject } from "rxjs"
import { logHoppRequestRunToAnalytics } from "../fb/analytics"
import { platform } from "~/platform"
export type MQTTConnectionConfig = {
username?: string
@@ -105,7 +105,7 @@ export class MQTTConnection {
this.handleError(e)
}
logHoppRequestRunToAnalytics({
platform.analytics?.logHoppRequestRunToAnalytics({
platform: "mqtt",
})
}

View File

@@ -1,7 +1,7 @@
import { BehaviorSubject, Subject } from "rxjs"
import { logHoppRequestRunToAnalytics } from "../fb/analytics"
import { SIOClientV2, SIOClientV3, SIOClientV4, SIOClient } from "./SIOClients"
import { SIOClientVersion } from "~/newstore/SocketIOSession"
import { platform } from "~/platform"
export const SOCKET_CLIENTS = {
v2: SIOClientV2,
@@ -113,7 +113,7 @@ export class SIOConnection {
this.handleError(error, "CONNECTION")
}
logHoppRequestRunToAnalytics({
platform.analytics?.logHoppRequestRunToAnalytics({
platform: "socketio",
})
}

View File

@@ -1,5 +1,5 @@
import { BehaviorSubject, Subject } from "rxjs"
import { logHoppRequestRunToAnalytics } from "../fb/analytics"
import { platform } from "~/platform"
export type SSEEvent = { time: number } & (
| { type: "STARTING" }
@@ -63,7 +63,7 @@ export class SSEConnection {
})
}
logHoppRequestRunToAnalytics({
platform.analytics?.logHoppRequestRunToAnalytics({
platform: "sse",
})
}

View File

@@ -1,5 +1,5 @@
import { BehaviorSubject, Subject } from "rxjs"
import { logHoppRequestRunToAnalytics } from "../fb/analytics"
import { platform } from "~/platform"
export type WSErrorMessage = SyntaxError | Event
@@ -71,7 +71,7 @@ export class WSConnection {
this.handleError(error as SyntaxError)
}
logHoppRequestRunToAnalytics({
platform.analytics?.logHoppRequestRunToAnalytics({
platform: "wss",
})
}

View File

@@ -2,7 +2,7 @@ import { createApp } from "vue"
import { PlatformDef, setPlatformDef } from "./platform"
import { setupLocalPersistence } from "./newstore/localpersistence"
import { performMigrations } from "./helpers/migrations"
import { initializeFirebase } from "./helpers/fb"
import { initializeApp } from "./helpers/app"
import { initBackendGQLClient } from "./helpers/backend/GQLClient"
import { HOPP_MODULES } from "@modules/."
@@ -20,7 +20,7 @@ export function createHoppApp(el: string | Element, platformDef: PlatformDef) {
// Some basic work that needs to be done before module inits even
initBackendGQLClient()
initializeFirebase()
initializeApp()
setupLocalPersistence()
performMigrations()

View File

@@ -6,8 +6,8 @@ import {
} from "vue-router"
import { setupLayouts } from "virtual:generated-layouts"
import generatedRoutes from "virtual:generated-pages"
import { logPageView } from "~/helpers/fb/analytics"
import { readonly, ref } from "vue"
import { platform } from "~/platform"
const routes = setupLayouts(generatedRoutes)
@@ -59,7 +59,7 @@ export default <HoppModule>{
// module to expose a stream of router events that can be independently
// subbed to
router.afterEach((to) => {
logPageView(to.fullPath)
platform.analytics?.logPageView(to.fullPath)
_isLoadingInitialRoute.value = false

View File

@@ -9,7 +9,7 @@
<script lang="ts">
import { defineComponent } from "vue"
import { useI18n } from "@composables/i18n"
import { initializeFirebase } from "~/helpers/fb"
import { initializeApp } from "~/helpers/app"
import { platform } from "~/platform"
export default defineComponent({
@@ -25,7 +25,7 @@ export default defineComponent({
}
},
beforeMount() {
initializeFirebase()
initializeApp()
},
async mounted() {
this.signingInWithEmail = true

View File

@@ -155,7 +155,7 @@ import {
GetInviteDetailsQueryVariables,
} from "~/helpers/backend/graphql"
import { acceptTeamInvitation } from "~/helpers/backend/mutations/TeamInvitation"
import { initializeFirebase } from "~/helpers/fb"
import { initializeApp } from "~/helpers/app"
import { platform } from "~/platform"
import { onLoggedIn } from "@composables/auth"
import { useReadonlyStream } from "@composables/stream"
@@ -234,7 +234,7 @@ export default defineComponent({
}
},
beforeMount() {
initializeFirebase()
initializeApp()
},
mounted() {
if (typeof this.$route.query.id === "string") {

View File

@@ -0,0 +1,12 @@
export type HoppRequestEvent =
| {
platform: "rest" | "graphql-query" | "graphql-schema"
strategy: "normal" | "proxy" | "extension"
}
| { platform: "wss" | "sse" | "socketio" | "mqtt" }
export type AnalyticsPlatformDef = {
initAnalytics: () => void
logHoppRequestRunToAnalytics: (ev: HoppRequestEvent) => void
logPageView: (pagePath: string) => void
}

View File

@@ -5,10 +5,12 @@ import { CollectionsPlatformDef } from "./collections"
import { SettingsPlatformDef } from "./settings"
import { HistoryPlatformDef } from "./history"
import { TabStatePlatformDef } from "./tab"
import { AnalyticsPlatformDef } from "./analytics"
export type PlatformDef = {
ui?: UIPlatformDef
auth: AuthPlatformDef
analytics?: AnalyticsPlatformDef
sync: {
environments: EnvironmentsPlatformDef
collections: CollectionsPlatformDef

View File

@@ -1,3 +1,8 @@
import {
AnalyticsPlatformDef,
HoppRequestEvent,
} from "@hoppscotch/common/platform/analytics"
import {
Analytics,
getAnalytics,
@@ -6,13 +11,13 @@ import {
setUserId,
setUserProperties,
} from "firebase/analytics"
import { platform } from "~/platform"
import { def as platformAuth } from "./firebase/auth"
import {
HoppAccentColor,
HoppBgColor,
settings$,
settingsStore,
} from "~/newstore/settings"
} from "@hoppscotch/common/newstore/settings"
let analytics: Analytics | null = null
@@ -27,13 +32,6 @@ type SettingsCustomDimensions = {
usesTelemetry: boolean
}
type HoppRequestEvent =
| {
platform: "rest" | "graphql-query" | "graphql-schema"
strategy: "normal" | "proxy" | "extension"
}
| { platform: "wss" | "sse" | "socketio" | "mqtt" }
export function initAnalytics() {
analytics = getAnalytics()
@@ -42,7 +40,7 @@ export function initAnalytics() {
}
function initLoginListeners() {
const authEvents$ = platform.auth.getAuthEventsStream()
const authEvents$ = platformAuth.getAuthEventsStream()
authEvents$.subscribe((ev) => {
if (ev.event === "login") {
@@ -107,3 +105,9 @@ export function logPageView(pagePath: string) {
})
}
}
export const def: AnalyticsPlatformDef = {
initAnalytics,
logHoppRequestRunToAnalytics,
logPageView,
}

View File

@@ -5,9 +5,11 @@ import { def as collectionsDef } from "./collections"
import { def as settingsDef } from "./settings"
import { def as historyDef } from "./history"
import { def as tabStateDef } from "./tab"
import { def as analyticsDef } from "./analytics"
createHoppApp("#app", {
auth: authDef,
analytics: analyticsDef,
sync: {
environments: envDef,
collections: collectionsDef,