Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wabe): throw an error when try to upload/read/delete file but no file adapter defined #171

Merged
merged 3 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/wabe/src/files/hookDeleteFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { HookObject } from '../hooks/HookObject'

const deleteFile = async (hookObject: HookObject<any, any>) => {
if (!hookObject.context.wabe.controllers.file) return

const schema = hookObject.context.wabe.config.schema?.classes?.find(
(currentClass) => currentClass.name === hookObject.className,
)
Expand All @@ -16,6 +14,9 @@ const deleteFile = async (hookObject: HookObject<any, any>) => {

if (!fileName) return

if (!hookObject.context.wabe.controllers.file)
throw new Error('No file adapter found')

await hookObject.context.wabe.controllers.file?.deleteFile(fileName)
})
}
Expand Down
5 changes: 3 additions & 2 deletions packages/wabe/src/files/hookReadFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { HookObject } from '../hooks/HookObject'

const getFile = async (hookObject: HookObject<any, any>) => {
if (!hookObject.context.wabe.controllers.file) return

const schema = hookObject.context.wabe.config.schema?.classes?.find(
(currentClass) => currentClass.name === hookObject.className,
)
Expand Down Expand Up @@ -35,6 +33,9 @@ const getFile = async (hookObject: HookObject<any, any>) => {
)
return

if (!hookObject.context.wabe.controllers.file)
throw new Error('No file adapter found')

const fileUrlFromBucket =
await hookObject.context.wabe.controllers.file?.readFile(fileName)

Expand Down
11 changes: 7 additions & 4 deletions packages/wabe/src/files/hookUploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { HookObject } from '../hooks/HookObject'

const handleFile = async (hookObject: HookObject<any, any>) => {
if (!hookObject.context.wabe.controllers.file) return

const newData = hookObject.getNewData()

const schema = hookObject.context.wabe.config.schema?.classes?.find(
Expand All @@ -13,8 +11,10 @@ const handleFile = async (hookObject: HookObject<any, any>) => {

await Promise.all(
Object.keys(newData).map(async (keyName) => {
const file = newData[keyName].file as File
const url = newData[keyName].url as string
const file = newData[keyName]?.file as File
const url = newData[keyName]?.url as string

if (!file && !url) return

if (url) {
hookObject.upsertNewData(keyName, { url, isPresignedUrl: false })
Expand All @@ -24,6 +24,9 @@ const handleFile = async (hookObject: HookObject<any, any>) => {
if (schema.fields[keyName].type !== 'File' || !(file instanceof File))
return

if (!hookObject.context.wabe.controllers.file)
throw new Error('No file adapter found')

// We upload the file and set the name of the file in the newData
await hookObject.context.wabe.controllers.file?.uploadFile(file)

Expand Down
30 changes: 30 additions & 0 deletions packages/wabe/src/files/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ describe('File upload', () => {
})
})

it('should throw an error if no fil ter is provided', async () => {
const previousFileController = wabe.controllers.file
// @ts-expect-error
wabe.controllers.file = null

const formData = new FormData()

formData.append(
'operations',
JSON.stringify({
query:
'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file {name, isPresignedUrl}}}}',
variables: { file: null },
}),
)

formData.append('map', JSON.stringify({ 0: ['variables.file'] }))

formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))

const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
method: 'POST',
body: formData,
})

expect(await res.text()).toContain('No file adapter found')

wabe.controllers.file = previousFileController
})

it("should upload a file with the database controller's method", async () => {
await wabe.controllers.database.createObject({
// @ts-expect-error
Expand Down