Skip to content

Commit

Permalink
Some more typ fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Oct 20, 2023
1 parent 125bb3d commit c5aff4b
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions apps/cms-ui/components/UpsertEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const UpsertEntry: React.FC<{

const [, createEntry] = useMutation(useMemo(() => buildUpsertEntryMutation(entity), [entity]))

// @ts-expect-error fix later
const defaults = useMemo(() => fields.reduce((acc, field) => {
// eslint-disable-next-line no-nested-ternary, functional/immutable-data, unicorn/no-nested-ternary
// @ts-expect-error fix later
Expand Down
2 changes: 1 addition & 1 deletion apps/supplement-stack/graphql/Mutation/addSupplement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const addSupplement: MutationResolvers['addSupplement'] = async (parent, {
const supplement: DocumentForInsert<typeof SupplementIntakeDbType[0], typeof SupplementIntakeDbType[1]> = {
amountInGrams,
foodId: new ObjectId(foodId),
userId: new ObjectId(decodedToken.userId as string),
userId: new ObjectId(decodedToken!.userId),
intakeTime,
}

Expand Down
2 changes: 1 addition & 1 deletion apps/supplement-stack/graphql/Query/mySupplementIntakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { QueryResolvers } from '../schema.generated'

const mySupplementIntakes: QueryResolvers['mySupplementIntakes'] = async (_, __, { decodedToken }) => {
const results = await Supplements.find({
userId: new ObjectId(decodedToken.userId),
userId: new ObjectId(decodedToken!.userId),
})

return results
Expand Down
6 changes: 2 additions & 4 deletions packages/auth-anonymous/graphql/Query/me.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { QueryResolvers } from '../schema.generated'
import type { TokenContents } from '@zemble/core'

const me: QueryResolvers['me'] = (_, __, { decodedToken, token }) => {
console.log({ decodedToken, token })
return decodedToken!
}
const me: QueryResolvers['me'] = (_, __, { decodedToken }) => decodedToken as TokenContents

export default me
9 changes: 1 addition & 8 deletions packages/auth-api-token/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "node",
"module": "CommonJS",
"noImplicitAny": false,
"types": [ "bun-types" ]
}
"extends": "../../tsconfig.json"
}
6 changes: 3 additions & 3 deletions packages/cms-users/graphql/Mutation/updatePermissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Mutation.updatePermissions', () => {
{ silenceErrors: true },
)

expect(errors?.[0].message).toEqual(`Accessing 'Mutation.updatePermissions' requires authentication.`)
expect(errors?.[0]?.message).toEqual(`Accessing 'Mutation.updatePermissions' requires authentication.`)
})

it('Should succeed', async () => {
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('Mutation.updatePermissions', () => {
silenceErrors: true,
})

expect(errors?.[0].message).toEqual('User not found')
expect(errors?.[0]?.message).toEqual('User not found')
})

it('Should fail if removing user-admin permission from self', async () => {
Expand All @@ -116,6 +116,6 @@ describe('Mutation.updatePermissions', () => {
silenceErrors: true,
})

expect(errors?.[0].message).toEqual('You cannot remove your own user-admin permission')
expect(errors?.[0]?.message).toEqual('You cannot remove your own user-admin permission')
})
})
8 changes: 8 additions & 0 deletions packages/cms/dynamicSchema/createDynamicSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ const fieldResolver = (parent: EntityEntryType, field: AnyField, displayNameFiel
return parent._id.toHexString()
}
if (field.name === 'displayName') {
// @ts-expect-error sdfgsdfg
return displayNameField && parent[displayNameField]
// @ts-expect-error sdfgsdfg
? parent[displayNameField].toString()
: parent._id.toHexString()
}
// @ts-expect-error sdfgsdfg
if (parent[field.name] !== undefined && parent[field.name] !== null) {
// @ts-expect-error sdfgsdfg
return parent[field.name]
}
if (field.__typename === 'ArrayField') {
Expand Down Expand Up @@ -98,7 +102,9 @@ export default async () => {
resolve: async (externalId: string) => {
const resolved = await getById.load(externalId)

// @ts-expect-error sdfgsdfg
return entity.displayNameField && resolved?.[entity.displayNameField]
// @ts-expect-error sdfgsdfg
? resolved[entity.displayNameField].toString()
: resolved?._id.toHexString()
},
Expand Down Expand Up @@ -133,7 +139,9 @@ export default async () => {
}, {
displayName: {
type: GraphQLString,
// @ts-expect-error sdfgsdfg
resolve: (parent: EntityEntryType) => (entity.displayNameField && parent[entity.displayNameField]
// @ts-expect-error sdfgsdfg
? parent[entity.displayNameField].toString()
: parent._id.toHexString()),
},
Expand Down
12 changes: 8 additions & 4 deletions packages/cms/dynamicSchema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ export const createTraverser = (entity: EntitySchemaType) => {
const mapArrayFields = (
fieldName: string,
data: Record<string, unknown> | readonly Record<string, unknown>[],
) => (Array.isArray(data) ? data : [data]).map((el: Record<string, unknown>): Record<string, unknown> => ({
__typename: (capitalize(entity.nameSingular) + capitalize(fieldName) + capitalize(Object.keys(el)[0])),
...traverseData(el),
}))
) => (Array.isArray(data) ? data : [data]).map((el: Record<string, unknown>): Record<string, unknown> => {
const ending = capitalize(Object.keys(el)[0]!)

return ({
__typename: (capitalize(entity.nameSingular) + capitalize(fieldName) + ending),
...traverseData(el),
})
})

return traverseData
}
5 changes: 4 additions & 1 deletion packages/cms/graphql/Mutation/addFieldsToEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ const addFieldsToEntity: MutationResolvers['addFieldsToEntity'] = async (_, { na
],
}), {}))

const fieldsFailingValidation = fieldsRequiringValidation.filter((fieldName) => failingDocs.some((doc) => !doc[fieldName] && doc[fieldName] !== false))
const fieldsFailingValidation = fieldsRequiringValidation.filter((
fieldName,
// @ts-expect-error fix sometime
) => failingDocs.some((doc) => !doc[fieldName] && doc[fieldName] !== false))

if (failingDocs.length > 0) {
throw new GraphQLError(`Cannot require ${fieldsFailingValidation.join(', ')} on entity ${namePlural}. Either provide a default value or add it without requiring these fields.`)
Expand Down
7 changes: 4 additions & 3 deletions packages/routes/utils/initializePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ const initializeRoutes = async (
const routesAndFilenames = await readRoutes(routePath)

const routePromises = Object.keys(routesAndFilenames).map(async (route) => {
const relativePath = path.join(config.rootUrl ?? '', routesAndFilenames[route].relativePath.toLowerCase())
const filename = routesAndFilenames[route].filename.toLowerCase()
const val = routesAndFilenames[route]!
const relativePath = path.join(config.rootUrl ?? '', val.relativePath.toLowerCase())
const filename = val.filename.toLowerCase()

const is404 = filename.startsWith('404')

Expand Down Expand Up @@ -145,7 +146,7 @@ const initializeRoutes = async (
})
}, {
headers: {
'Content-Type': conentType,
'Content-Type': conentType!,
'X-Content-Type-Options': 'nosniff',
'Content-Length': fileStream.readableLength.toString(),
'Transfer-Encoding': 'chunked',
Expand Down
2 changes: 1 addition & 1 deletion packages/routes/utils/readRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'node:fs'
import * as path from 'node:path'

type PathsWithMetadata = Record<string, {readonly filename: string, readonly relativePath: string}>
export type PathsWithMetadata = Record<string, {readonly filename: string, readonly relativePath: string}>

export const readRoutes = async (rootDir: string, prefix = ''): Promise<PathsWithMetadata> => fs.readdirSync(path.join(rootDir, prefix)).reduce(async (prev, filename) => {
const route = path.join(rootDir, prefix, filename)
Expand Down
2 changes: 1 addition & 1 deletion packages/todo/graphql/Mutation/createTodo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MutationResolvers } from '../schema.generated'

const createTodo: MutationResolvers['createTodo'] = async (_, { title }, { pubsub, decodedToken, kv }) => {
const id = Math.random().toString(36).substring(7)
const { userId } = decodedToken
const { userId } = decodedToken!
const todo = { title, id, completed: false }
await kv(userId).set(id, { title, id, completed: false })

Expand Down
2 changes: 1 addition & 1 deletion packages/todo/graphql/Mutation/updateTodoStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MutationResolvers, Todo } from '../schema.generated'
const updateTodoStatus: MutationResolvers['updateTodoStatus'] = async (_, {
id, completed,
}, { pubsub, decodedToken, kv }) => {
const { userId } = decodedToken
const { userId } = decodedToken!
const todoIdWithUser = `${userId}_${id}`
const previous = await kv<Todo>(userId).get(todoIdWithUser)

Expand Down

0 comments on commit c5aff4b

Please sign in to comment.