Files
speckle-server/packages/frontend-2/middleware/requireValidFunction.ts
T
Kristaps Fabians Geikins 843606775c feat(fe2): parallel middlewares (#5314)
* parallel middlewares foundation + hydration mismatch

* moved to fully parallel middlewares

* a bit less hacky

* some more cleanup

* improved nuxt 4 error formatting

* make parallel middlewares toggleable
2025-08-27 12:38:04 +03:00

54 lines
1.3 KiB
TypeScript

import { functionAccessCheckQuery } from '~/lib/automate/graphql/queries'
import { useApolloClientFromNuxt } from '~/lib/common/composables/graphql'
import {
convertThrowIntoFetchResult,
getFirstErrorMessage
} from '~/lib/common/helpers/graphql'
export default defineParallelizedNuxtRouteMiddleware(async (to) => {
const functionId = to.params.fid as string
const isAutomateEnabled = useIsAutomateModuleEnabled()
if (!isAutomateEnabled.value) {
return abortNavigation(
createError({
statusCode: 404,
message: 'Page not found'
})
)
}
const client = useApolloClientFromNuxt()
const { data, errors } = await client
.query({
query: functionAccessCheckQuery,
variables: { id: functionId },
context: {
skipLoggingErrors: true
}
})
.catch(convertThrowIntoFetchResult)
if (data?.automateFunction?.id) return
const isNotFound = (errors || []).find(
(e) => e.extensions?.['code'] === 'FUNCTION_NOT_FOUND'
)
if (isNotFound) {
return abortNavigation(
createError({ statusCode: 404, message: 'Function not found' })
)
}
if (errors?.length) {
const errMsg = getFirstErrorMessage(errors)
return abortNavigation(
createError({
statusCode: 500,
message: errMsg
})
)
}
})