From 9c224b0e2e5950a1e69e3537d69184c7e8143be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Fri, 30 May 2025 23:30:22 +0800 Subject: [PATCH] get rid of more typescript errors --- .../src/graphql/resolvers/images/images.ts | 2 +- .../graphql/resolvers/images/imagesLocal.ts | 7 ++-- .../graphql/resolvers/images/imagesS3.spec.ts | 3 ++ .../src/graphql/resolvers/images/imagesS3.ts | 7 ++-- .../resolvers/images/wrapTransaction.ts | 36 +++++++++++++------ 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/backend/src/graphql/resolvers/images/images.ts b/backend/src/graphql/resolvers/images/images.ts index fd420e1a3..e8ed256f5 100644 --- a/backend/src/graphql/resolvers/images/images.ts +++ b/backend/src/graphql/resolvers/images/images.ts @@ -51,7 +51,7 @@ export interface Images { relationshipType: 'HERO_IMAGE' | 'AVATAR_IMAGE', imageInput: ImageInput | null | undefined, opts?: MergeImageOpts, - ) => Promise + ) => Promise } export const images = (config: Context['config']) => diff --git a/backend/src/graphql/resolvers/images/imagesLocal.ts b/backend/src/graphql/resolvers/images/imagesLocal.ts index c9f575777..d1a53d1b0 100644 --- a/backend/src/graphql/resolvers/images/imagesLocal.ts +++ b/backend/src/graphql/resolvers/images/imagesLocal.ts @@ -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( diff --git a/backend/src/graphql/resolvers/images/imagesS3.spec.ts b/backend/src/graphql/resolvers/images/imagesS3.spec.ts index 3dc5316c5..37672b47d 100644 --- a/backend/src/graphql/resolvers/images/imagesS3.spec.ts +++ b/backend/src/graphql/resolvers/images/imagesS3.spec.ts @@ -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() }) }) diff --git a/backend/src/graphql/resolvers/images/imagesS3.ts b/backend/src/graphql/resolvers/images/imagesS3.ts index 247cdc9c7..f608e660a 100644 --- a/backend/src/graphql/resolvers/images/imagesS3.ts +++ b/backend/src/graphql/resolvers/images/imagesS3.ts @@ -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( ` diff --git a/backend/src/graphql/resolvers/images/wrapTransaction.ts b/backend/src/graphql/resolvers/images/wrapTransaction.ts index bcc17877d..a91a3f4c0 100644 --- a/backend/src/graphql/resolvers/images/wrapTransaction.ts +++ b/backend/src/graphql/resolvers/images/wrapTransaction.ts @@ -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 -export const wrapTransaction = async ( - 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 => { + 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 => { 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()