get rid of more typescript errors

This commit is contained in:
Robert Schäfer 2025-05-30 23:30:22 +08:00
parent 0dd8f41808
commit 9c224b0e2e
5 changed files with 38 additions and 17 deletions

View File

@ -51,7 +51,7 @@ export interface Images {
relationshipType: 'HERO_IMAGE' | 'AVATAR_IMAGE',
imageInput: ImageInput | null | undefined,
opts?: MergeImageOpts,
) => Promise<Image>
) => Promise<Image | undefined>
}
export const images = (config: Context['config']) =>

View File

@ -16,7 +16,7 @@ import slug from 'slug'
import { v4 as uuid } from 'uuid'
import { sanitizeRelationshipType } from './sanitizeRelationshipTypes'
import { wrapTransaction } from './wrapTransaction'
import { wrapTransactionDeleteImage, wrapTransactionMergeImage } from './wrapTransaction'
import type { Images, FileDeleteCallback, FileUploadCallback } from './images'
import type { FileUpload } from 'graphql-upload'
@ -24,7 +24,8 @@ import type { FileUpload } from 'graphql-upload'
const deleteImage: Images['deleteImage'] = async (resource, relationshipType, opts = {}) => {
sanitizeRelationshipType(relationshipType)
const { transaction, deleteCallback } = opts
if (!transaction) return wrapTransaction(deleteImage, [resource, relationshipType], opts)
if (!transaction)
return wrapTransactionDeleteImage(deleteImage, [resource, relationshipType], opts)
const txResult = await transaction.run(
`
MATCH (resource {id: $resource.id})-[rel:${relationshipType}]->(image:Image)
@ -54,7 +55,7 @@ const mergeImage: Images['mergeImage'] = async (
sanitizeRelationshipType(relationshipType)
const { transaction, uploadCallback, deleteCallback } = opts
if (!transaction)
return wrapTransaction(mergeImage, [resource, relationshipType, imageInput], opts)
return wrapTransactionMergeImage(mergeImage, [resource, relationshipType, imageInput], opts)
let txResult
txResult = await transaction.run(

View File

@ -341,6 +341,9 @@ describe('mergeImage', () => {
expect(result).toMatchObject({
url: 'https://fsn1.your-objectstorage.com/ocelot-social-staging/original/f965ea15-1f6b-43aa-a535-927410e2585e-dsc02586.jpg',
})
if (!result?.url) {
throw new Error()
}
expect(new URL(result.url)).toBeDefined()
})
})

View File

@ -9,7 +9,7 @@ import { v4 as uuid } from 'uuid'
import { S3Configured } from '@config/index'
import { sanitizeRelationshipType } from './sanitizeRelationshipTypes'
import { wrapTransaction } from './wrapTransaction'
import { wrapTransactionDeleteImage, wrapTransactionMergeImage } from './wrapTransaction'
import type { Image, Images, FileDeleteCallback, FileUploadCallback } from './images'
import type { FileUpload } from 'graphql-upload'
@ -31,7 +31,8 @@ export const images = (config: S3Configured) => {
const deleteImage: Images['deleteImage'] = async (resource, relationshipType, opts = {}) => {
sanitizeRelationshipType(relationshipType)
const { transaction, deleteCallback = s3Delete } = opts
if (!transaction) return wrapTransaction(deleteImage, [resource, relationshipType], opts)
if (!transaction)
return wrapTransactionDeleteImage(deleteImage, [resource, relationshipType], opts)
const txResult = await transaction.run(
`
MATCH (resource {id: $resource.id})-[rel:${relationshipType}]->(image:Image)
@ -63,7 +64,7 @@ export const images = (config: S3Configured) => {
sanitizeRelationshipType(relationshipType)
const { transaction, uploadCallback, deleteCallback = s3Delete } = opts
if (!transaction)
return wrapTransaction(mergeImage, [resource, relationshipType, imageInput], opts)
return wrapTransactionMergeImage(mergeImage, [resource, relationshipType, imageInput], opts)
let txResult = await transaction.run(
`

View File

@ -1,21 +1,37 @@
import { getDriver } from '@db/neo4j'
import type { DeleteImageOpts, MergeImageOpts } from './images'
import type { DeleteImageOpts, MergeImageOpts, Images, ImageInput } from './images'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AsyncFunc = (...args: any[]) => Promise<any>
export const wrapTransaction = async <F extends AsyncFunc>(
wrappedCallback: F,
args: unknown[],
opts: DeleteImageOpts | MergeImageOpts,
) => {
export const wrapTransactionDeleteImage = async (
wrappedCallback: Images['deleteImage'],
args: [resource: { id: string }, relationshipType: 'HERO_IMAGE' | 'AVATAR_IMAGE'],
opts: DeleteImageOpts,
): ReturnType<Images['deleteImage']> => {
const session = getDriver().session()
try {
const result = await session.writeTransaction((transaction) => {
return wrappedCallback(...args, { ...opts, transaction })
})
return result
} finally {
await session.close()
}
}
export const wrapTransactionMergeImage = async (
wrappedCallback: Images['mergeImage'],
args: [
resource: { id: string },
relationshipType: 'HERO_IMAGE' | 'AVATAR_IMAGE',
imageInput: ImageInput | null | undefined,
],
opts: MergeImageOpts,
): ReturnType<Images['mergeImage']> => {
const session = getDriver().session()
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result = await session.writeTransaction((transaction) => {
return wrappedCallback(...args, { ...opts, transaction })
})
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result
} finally {
await session.close()