Skip to content

Commit

Permalink
linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabis94 committed Jan 10, 2025
1 parent 079f835 commit 873d54c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 152 deletions.
26 changes: 16 additions & 10 deletions packages/frontend-2/lib/common/helpers/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import type {
TypedDocumentNode,
ServerError,
ServerParseError,
ApolloCache
ApolloCache,
Unmasked
} from '@apollo/client/core'
import { GraphQLError } from 'graphql'
import type { DocumentNode } from 'graphql'
import type { DocumentNode, GraphQLFormattedError } from 'graphql'
import {
flatten,
has,
Expand All @@ -27,7 +28,7 @@ import {
} from 'lodash-es'
import type { Modifier, ModifierDetails, Reference } from '@apollo/client/cache'
import type { Get, PartialDeep, Paths, ReadonlyDeep, Tagged } from 'type-fest'
import type { GraphQLErrors, NetworkError } from '@apollo/client/errors'
import type { NetworkError } from '@apollo/client/errors'
import { nanoid } from 'nanoid'
import { StackTrace } from '~~/lib/common/helpers/debugging'
import dayjs from 'dayjs'
Expand Down Expand Up @@ -117,7 +118,7 @@ export function isInvalidAuth(error: ApolloError | NetworkError) {
export function convertThrowIntoFetchResult(
err: unknown
): FetchResult<undefined> & { apolloError?: ApolloError; isInvalidAuth: boolean } {
let gqlErrors: readonly GraphQLError[]
let gqlErrors: readonly GraphQLFormattedError[]
let apolloError: Optional<ApolloError> = undefined
if (err instanceof ApolloError) {
gqlErrors = err.graphQLErrors
Expand Down Expand Up @@ -154,7 +155,7 @@ export function convertThrowIntoFetchResult(
* Get first error message from a GQL errors array
*/
export function getFirstErrorMessage(
errs: readonly GraphQLError[] | undefined | null,
errs: readonly GraphQLError[] | readonly GraphQLFormattedError[] | undefined | null,
fallbackMessage = 'An unexpected issue occurred'
): string {
return errs?.[0]?.message || fallbackMessage
Expand All @@ -176,7 +177,7 @@ export function updateCacheByFilter<TData, TVariables = unknown>(
* mutate anything being passed into this function! E.g. if you want to mutate arrays,
* create new arrays through slice()/filter() instead
*/
updater: (data: TData) => TData | undefined,
updater: (data: Unmasked<TData>) => Unmasked<TData> | undefined,
options: Partial<{
/**
* Whether to suppress errors that occur when the fragment being queried
Expand All @@ -203,7 +204,7 @@ export function updateCacheByFilter<TData, TVariables = unknown>(
)
}

const readData = (): TData | null => {
const readData = (): Unmasked<TData> | null => {
if (fragment) {
return cache.readFragment(fragment)
} else if (query) {
Expand All @@ -213,7 +214,7 @@ export function updateCacheByFilter<TData, TVariables = unknown>(
}
}

const writeData = (data: TData): boolean => {
const writeData = (data: Unmasked<TData>): boolean => {
if (fragment) {
cache.writeFragment({ ...fragment, data, overwrite })
return true
Expand Down Expand Up @@ -486,7 +487,9 @@ export function evictObjectFields<
)
}

export const resolveGenericStatusCode = (errors: GraphQLErrors) => {
export const resolveGenericStatusCode = (
errors: readonly GraphQLError[] | readonly GraphQLFormattedError[]
) => {
if (errors.some((e) => e.extensions?.code === 'FORBIDDEN')) return 403
if (
errors.some((e) =>
Expand All @@ -508,7 +511,10 @@ export const resolveGenericStatusCode = (errors: GraphQLErrors) => {
return 500
}

export const errorFailedAtPathSegment = (error: GraphQLError, segment: string) => {
export const errorFailedAtPathSegment = (
error: GraphQLError | GraphQLFormattedError,
segment: string
) => {
const path = error.path || []
return path[path.length - 1] === segment
}
Expand Down
42 changes: 14 additions & 28 deletions packages/frontend-2/lib/viewer/composables/setup/postSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from '@speckle/viewer'
import { useAuthCookie } from '~~/lib/auth/composables/auth'
import type {
Comment,
Project,
ProjectCommentThreadsArgs,
ViewerResourceItem
Expand All @@ -40,10 +39,8 @@ import { useViewerCommentUpdateTracking } from '~~/lib/viewer/composables/commen
import {
getCacheId,
getObjectReference,
isReference,
modifyObjectFields
} from '~~/lib/common/helpers/graphql'
import type { ModifyFnCacheData } from '~~/lib/common/helpers/graphql'
import {
useViewerOpenedThreadUpdateEmitter,
useViewerThreadTracking
Expand All @@ -60,8 +57,6 @@ import {
} from '~~/lib/viewer/composables/ui'
import { onKeyStroke, watchTriggerable } from '@vueuse/core'
import { setupDebugMode } from '~~/lib/viewer/composables/setup/dev'
import type { Reference } from '@apollo/client'
import type { Modifier } from '@apollo/client/cache'
import { useEmbed } from '~/lib/viewer/composables/setup/embed'
import { useMixpanel } from '~~/lib/core/composables/mp'

Expand Down Expand Up @@ -224,7 +219,7 @@ function useViewerSubscriptionEventTracker() {
(event, cache) => {
const isArchived = event.type === ProjectCommentsUpdatedMessageType.Archived
const isNew = event.type === ProjectCommentsUpdatedMessageType.Created
const model = event.comment
const comment = event.comment

if (isArchived) {
// Mark as archived
Expand Down Expand Up @@ -253,30 +248,21 @@ function useViewerSubscriptionEventTracker() {
}
}
)
} else if (isNew && model) {
const parentId = model.parent?.id
} else if (isNew && comment) {
const parentId = comment.parent?.id

// Add reply to parent
if (parentId) {
cache.modify({
id: getCacheId('Comment', parentId),
fields: {
replies: ((
oldValue: ModifyFnCacheData<Comment['replies']> | Reference
) => {
if (isReference(oldValue)) return oldValue

const newValue: typeof oldValue = {
totalCount: (oldValue?.totalCount || 0) + 1,
items: [
getObjectReference('Comment', model.id),
...(oldValue?.items || [])
]
}
return newValue
}) as Modifier<ModifyFnCacheData<Comment['replies']> | Reference>
}
})
modifyObjectField(
cache,
getCacheId('Comment', parentId),
'replies',
({ helpers: { createUpdatedValue, ref } }) =>
createUpdatedValue(({ update }) => {
update('totalCount', (totalCount) => totalCount + 1)
update('items', (items) => [ref('Comment', comment.id), ...items])
})
)
} else {
// Add comment thread
modifyObjectFields<ProjectCommentThreadsArgs, Project['commentThreads']>(
Expand All @@ -286,7 +272,7 @@ function useViewerSubscriptionEventTracker() {
if (fieldName !== 'commentThreads') return

const newItems = [
getObjectReference('Comment', model.id),
getObjectReference('Comment', comment.id),
...(data.items || [])
]
return {
Expand Down
11 changes: 8 additions & 3 deletions packages/frontend-2/lib/workspaces/composables/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
workspaceUpdateRoleMutation
} from '~/lib/workspaces/graphql/mutations'
import { isFunction } from 'lodash-es'
import type { GraphQLError } from 'graphql'
import type { GraphQLError, GraphQLFormattedError } from 'graphql'
import { onWorkspaceUpdatedSubscription } from '~/lib/workspaces/graphql/subscriptions'
import { useLock } from '~/lib/common/composables/singleton'
import type { Get } from 'type-fest'
Expand Down Expand Up @@ -131,7 +131,10 @@ export const useProcessWorkspaceInvite = () => {
callback: () => MaybeAsync<void>
preventErrorToasts?:
| boolean
| ((errors: GraphQLError[], errMsg: string) => boolean)
| ((
errors: GraphQLError[] | GraphQLFormattedError[],
errMsg: string
) => boolean)
}>
) => {
if (!isWorkspacesEnabled.value) return
Expand Down Expand Up @@ -253,7 +256,9 @@ export const useWorkspaceInviteManager = <
*/
preventRedirect: boolean
route: RouteLocationNormalized
preventErrorToasts: boolean | ((errors: GraphQLError[], errMsg: string) => boolean)
preventErrorToasts:
| boolean
| ((errors: GraphQLError[] | GraphQLFormattedError[], errMsg: string) => boolean)
}>
) => {
const isWorkspacesEnabled = useIsWorkspacesEnabled()
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend-2/middleware/requireValidAutomation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export default defineNuxtRouteMiddleware(async (to) => {

if (data?.project?.automations) return

const isForbidden = (errors || []).find((e) => e.extensions['code'] === 'FORBIDDEN')
const isForbidden = (errors || []).find((e) => e.extensions?.['code'] === 'FORBIDDEN')
const isNotFoundProject = (errors || []).find(
(e) => e.extensions['code'] === 'STREAM_NOT_FOUND'
(e) => e.extensions?.['code'] === 'STREAM_NOT_FOUND'
)
const isNotFoundAutomation = (errors || []).find(
(e) => e.extensions['code'] === 'AUTOMATION_NOT_FOUND'
(e) => e.extensions?.['code'] === 'AUTOMATION_NOT_FOUND'
)
if (isForbidden) {
return abortNavigation(
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-2/middleware/requireValidFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
if (data?.automateFunction?.id) return

const isNotFound = (errors || []).find(
(e) => e.extensions['code'] === 'FUNCTION_NOT_FOUND'
(e) => e.extensions?.['code'] === 'FUNCTION_NOT_FOUND'
)
if (isNotFound) {
return abortNavigation(
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend-2/middleware/requireValidModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export default defineNuxtRouteMiddleware(async (to) => {
// If project succesfully resolved, move on
if (data?.project?.model?.id) return

const isForbidden = (errors || []).find((e) => e.extensions['code'] === 'FORBIDDEN')
const isForbidden = (errors || []).find((e) => e.extensions?.['code'] === 'FORBIDDEN')
const isProjectNotFound = (errors || []).find(
(e) => e.extensions['code'] === 'STREAM_NOT_FOUND'
(e) => e.extensions?.['code'] === 'STREAM_NOT_FOUND'
)
const isModelNotFound = (errors || []).find(
(e) => e.extensions['code'] === 'BRANCH_NOT_FOUND'
(e) => e.extensions?.['code'] === 'BRANCH_NOT_FOUND'
)

// Check if project exists and model is valid
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend-2/middleware/requireValidWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default defineNuxtRouteMiddleware(async (to) => {

if (data?.workspaceBySlug.id) return

const isForbidden = (errors || []).find((e) => e.extensions['code'] === 'FORBIDDEN')
const isForbidden = (errors || []).find((e) => e.extensions?.['code'] === 'FORBIDDEN')
const isNotFound = (errors || []).find(
(e) => e.extensions['code'] === 'WORKSPACE_NOT_FOUND_ERROR'
(e) => e.extensions?.['code'] === 'WORKSPACE_NOT_FOUND_ERROR'
)
if (isForbidden) {
return abortNavigation(
Expand Down Expand Up @@ -58,7 +58,7 @@ export default defineNuxtRouteMiddleware(async (to) => {

const firstErrorWithCode = errors.find((e) => e.extensions?.['code'])
if (firstErrorWithCode) {
const errorCode = firstErrorWithCode.extensions['code']
const errorCode = firstErrorWithCode.extensions?.['code']
return abortNavigation(
createError({
statusCode: 401,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"type-fest": "^2.13.1",
"typescript": "^5.0.4",
"unplugin-vue-macros": "^2.7.0",
"vee-validate": "^4.15.0",
"vee-validate": "4.7.0",
"vite": "^4.5.2",
"vite-plugin-dts": "^3.6.3",
"vue": "^3.3.8",
Expand Down
Loading

0 comments on commit 873d54c

Please sign in to comment.