Files
speckle-server/packages/frontend-2/lib/core/composables/server.ts
T
Kristaps Fabians Geikins 83d8035dc2 chore: upgrade to eslint 9 (#2348)
* root + server

* frontend

* frontend-2

* dui3

* dui3

* tailwind theme

* ui-components

* preview service

* viewer

* viewer-sandbox

* fileimport-service

* webhook service

* objectloader

* shared

* ui-components-nuxt

* WIP full config

* WIP full linter

* eslint projectwide util

* minor fix

* removing redundant ci

* clean up test errors

* fixed prettier formatting

* CI improvements

* TSC lint fix

* 'buildBatch' needs to be async since some batch types (like Text) require it. Removed a disabled liniting rule from ObjLoader

* removed unnecessary void

---------

Co-authored-by: AlexandruPopovici <alexandrupopoviciioan@gmail.com>
2024-06-12 14:38:02 +03:00

93 lines
2.7 KiB
TypeScript

import { useQuery } from '@vue/apollo-composable'
import { nanoid } from 'nanoid'
import { graphql } from '~~/lib/common/generated/gql'
import type { H3Event } from 'h3'
import type { Optional } from '@speckle/shared'
export const mainServerInfoDataQuery = graphql(`
query MainServerInfoData {
serverInfo {
adminContact
blobSizeLimitBytes
canonicalUrl
company
description
guestModeEnabled
inviteOnly
name
termsOfService
version
automateUrl
}
}
`)
export function useServerInfo() {
const { result } = useQuery(mainServerInfoDataQuery)
const serverInfo = computed(() => result.value?.serverInfo)
const isGuestMode = computed(() => !!serverInfo.value?.guestModeEnabled)
return { serverInfo, isGuestMode }
}
/**
* Get the req.id that is/was used in the initial SSR request
*
* Note: In SSR, this can only be used after the 001-logging middleware, cause that's when the ID is set
*/
export function useServerRequestId() {
const nuxt = useNuxtApp()
const state = useState('server_request_id', () => {
// The client side should not need to resolve this info, as it should come from the serialized SSR state
if (import.meta.client || !nuxt.ssrContext) return undefined
return nuxt.ssrContext.event.node.req.id as string
})
return computed(() => state.value)
}
/**
* Get the value that should be used as the request correlation ID
*
* Note: In SSR, this can only be used after the 001-logging middleware, cause that's when the ID is set, otherwise
* we fallback to a new nanoid
*/
export function useRequestId(params?: {
/**
* Specify, if invoking composable from within server-side event handler, cause we don't have access to useNuxtApp()
* or anything of the like there
*/
event?: H3Event
}) {
let id = nanoid()
if (import.meta.server) {
id = (params?.event || useNuxtApp().ssrContext?.event)?.node.req.id as string
if (!id) {
throw new Error("Couldn't determine request ID")
}
return id
} else {
// We retain it in the state so that all reqs going out from the same client-side app session
// have the same id
const state = useState('app_request_correlation_id', () => id)
return state.value
}
}
/**
* Resolve the user's geolocation, if possible (only supported wherever we host behind Cloudflare)
*/
export function useUserCountry() {
const nuxt = useNuxtApp()
const state = useState('active_user_country', () => {
// The client side should not need to resolve this info, as it should come from the serialized SSR state
if (import.meta.client || !nuxt.ssrContext) return undefined
return nuxt.ssrContext.event.node.req.headers['cf-ipcountry'] as Optional<string>
})
return computed(() => state.value)
}