feat: Add boilerplate for the file module (#6956)
This commit is contained in:
1 parent
eb93e8f386
commit
9ccd88d025
36 files changed
+887
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { deleteFilesWorkflow } from "@medusajs/core-flows"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "file",
|
||||
variables,
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [file] = await remoteQuery(queryObject)
|
||||
res.status(200).json({ file })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
|
||||
const { errors } = await deleteFilesWorkflow(req.scope).run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "file",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import multer from "multer"
|
||||
import { transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import { AdminGetProductsProductParams } from "../products/validators"
|
||||
|
||||
const upload = multer({ dest: "uploads/" })
|
||||
|
||||
export const adminUploadRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["ALL"],
|
||||
matcher: "/admin/uploads*",
|
||||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/uploads/:id",
|
||||
middlewares: [],
|
||||
},
|
||||
// TODO: There is a `/protected` route in v1 that might need a bit more thought when implementing
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/uploads",
|
||||
middlewares: [upload.array("files")],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/uploads/:id",
|
||||
middlewares: [],
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,6 @@
|
||||
export const defaultAdminUploadFields = ["id", "url"]
|
||||
|
||||
export const retrieveUploadConfig = {
|
||||
defaultFields: defaultAdminUploadFields,
|
||||
isList: false,
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { uploadFilesWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateProductDTO } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<CreateProductDTO>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const input = req.files as any[]
|
||||
const { result, errors } = await uploadFilesWorkflow(req.scope).run({
|
||||
input: {
|
||||
files: input?.map((f) => ({
|
||||
filename: "test",
|
||||
mimeType: "image/jpeg",
|
||||
content: f.buffer,
|
||||
})),
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
res.status(200).json({ files: result })
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import { adminStockLocationRoutesMiddlewares } from "./admin/stock-locations/mid
|
||||
import { adminStoreRoutesMiddlewares } from "./admin/stores/middlewares"
|
||||
import { adminTaxRateRoutesMiddlewares } from "./admin/tax-rates/middlewares"
|
||||
import { adminTaxRegionRoutesMiddlewares } from "./admin/tax-regions/middlewares"
|
||||
import { adminUploadRoutesMiddlewares } from "./admin/uploads/middlewares"
|
||||
import { adminUserRoutesMiddlewares } from "./admin/users/middlewares"
|
||||
import { adminWorkflowsExecutionsMiddlewares } from "./admin/workflows-executions/middlewares"
|
||||
import { authRoutesMiddlewares } from "./auth/middlewares"
|
||||
@@ -61,5 +62,6 @@ export const config: MiddlewaresConfig = {
|
||||
...adminSalesChannelRoutesMiddlewares,
|
||||
...adminStockLocationRoutesMiddlewares,
|
||||
...adminProductTypeRoutesMiddlewares,
|
||||
...adminUploadRoutesMiddlewares,
|
||||
],
|
||||
}
|
||||
Reference in new issue
Block a user