Feat(medusa-js, medusa-react): Upload endpoints in medusa js and react (#1716)
* export types from admin uploads * add delete and download to medusa-js * add upload endpoints to hooks * remove upload from js and react * pr feedback * Apply suggestions from code review Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> * rename types for admin uploads Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oliver Windall Juhl
parent
bf47d1aecd
commit
9018d270be
@@ -1258,6 +1258,9 @@
|
||||
"updated_at": "2021-03-16T21:24:00.389Z",
|
||||
"created_at": "2021-03-16T21:24:00.389Z",
|
||||
"deleted_at": null
|
||||
},
|
||||
"upload": {
|
||||
"url": "test-url"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { rest } from "msw"
|
||||
import { body } from "msw/lib/types/context"
|
||||
import { fixtures } from "../data"
|
||||
|
||||
export const adminHandlers = [
|
||||
@@ -1651,4 +1652,24 @@ export const adminHandlers = [
|
||||
rest.delete("/admin/auth", (req, res, ctx) => {
|
||||
return res(ctx.status(200))
|
||||
}),
|
||||
|
||||
rest.delete("/admin/uploads", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
id: (req.body as any).file_key,
|
||||
object: "file",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/uploads/download-url", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
download_url: fixtures.get("upload").url,
|
||||
})
|
||||
)
|
||||
}),
|
||||
]
|
||||
|
||||
@@ -25,3 +25,4 @@ export * from "./swaps"
|
||||
export * from "./tax-rates"
|
||||
export * from "./users"
|
||||
export * from "./variants"
|
||||
export * from "./uploads"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
AdminDeleteUploadsReq,
|
||||
IAdminPostUploadsFileReq,
|
||||
AdminDeleteUploadsRes,
|
||||
AdminPostUploadsDownloadUrlReq,
|
||||
AdminUploadsDownloadUrlRes,
|
||||
AdminUploadsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
|
||||
export const useAdminUploadFile = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminUploadsRes>,
|
||||
Error,
|
||||
IAdminPostUploadsFileReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation((payload: IAdminPostUploadsFileReq) => {
|
||||
return client.admin.uploads.create(payload)
|
||||
}, buildOptions(queryClient, [], options))
|
||||
}
|
||||
|
||||
export const useAdminCreatePresignedDownloadUrl = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminUploadsDownloadUrlRes>,
|
||||
Error,
|
||||
AdminPostUploadsDownloadUrlReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostUploadsDownloadUrlReq) =>
|
||||
client.admin.uploads.getPresignedDownloadUrl(payload),
|
||||
buildOptions(queryClient, [], options)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminDeleteFile = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminDeleteUploadsRes>,
|
||||
Error,
|
||||
AdminDeleteUploadsReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminDeleteUploadsReq) => client.admin.uploads.delete(payload),
|
||||
buildOptions(queryClient, [], options)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import {
|
||||
useAdminDeleteFile,
|
||||
useAdminCreatePresignedDownloadUrl,
|
||||
} from "../../../../src"
|
||||
import { createWrapper } from "../../../utils"
|
||||
|
||||
describe("useAdminDeleteFile hook", () => {
|
||||
test("Removes file with key and returns deleteresult", async () => {
|
||||
const file_key = "test"
|
||||
|
||||
const { result, waitFor } = renderHook(() => useAdminDeleteFile(), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
|
||||
result.current.mutate({ file_key })
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data).toEqual(
|
||||
expect.objectContaining({ id: file_key, object: "file", deleted: true })
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminCreatePresignedDownloadUrl hook", () => {
|
||||
test("", async () => {
|
||||
const file_key = "test"
|
||||
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminCreatePresignedDownloadUrl(),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({ file_key })
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.download_url).toEqual(fixtures.get("upload").url)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user