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

feature: introduce additional_data to the product endpoints #8405

Merged
merged 17 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
10 changes: 4 additions & 6 deletions packages/framework/framework/src/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,12 @@ export interface MedusaRequest<Body = unknown>
* A generic context object that can be used across the request lifecycle
*/
context?: Record<string, any>

/**
* Custom validators for the request body and query params that will be
* merged with the original validator of the route.
* Custom validator to validate the `additional_data` property in
* requests that allows for additional_data
*/
extendedValidators?: {
body?: ZodObject<any, any>
queryParams?: ZodObject<any, any>
}
additionalDataValidator?: ZodObject<any, any>
}

export interface AuthContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MiddlewareVerb,
ParserConfig,
} from "../types"
import { ZodObject } from "zod"
import zod, { ZodRawShape } from "zod"

/**
* A helper function to configure the routes by defining custom middleware,
Expand All @@ -19,10 +19,7 @@ export function defineMiddlewares<
method?: MiddlewareVerb | MiddlewareVerb[]
matcher: string | RegExp
bodyParser?: ParserConfig
extendedValidators?: {
body?: ZodObject<any, any>
queryParams?: ZodObject<any, any>
}
additionalDataValidator?: ZodRawShape
// eslint-disable-next-line space-before-function-paren
middlewares?: (<Req extends MedusaRequest>(
req: Req,
Expand All @@ -41,17 +38,16 @@ export function defineMiddlewares<
return {
errorHandler,
routes: routes.map((route) => {
const { middlewares, extendedValidators, ...rest } = route
const { middlewares, additionalDataValidator, ...rest } = route
const customMiddleware: MedusaRequestHandler[] = []

/**
* Define a custom validator when "extendedValidators.body" or
* "extendedValidators.queryParams" validation schema is
* provided.
* Define a custom validator when a zod schema is provided via
* "additionalDataValidator" property
*/
if (extendedValidators?.body || extendedValidators?.queryParams) {
if (additionalDataValidator) {
customMiddleware.push((req, _, next) => {
req.extendedValidators = extendedValidators
req.additionalDataValidator = zod.object(additionalDataValidator)
next()
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"build": "rimraf dist && tsc --build",
"serve": "node dist/app.js",
"test": "jest --silent --bail --maxWorkers=50% --forceExit"
"test": "jest --silent=false --bail --maxWorkers=50% --forceExit"
thetutlage marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@inquirer/checkbox": "^2.3.11",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminUpdateProductOption>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminUpdateProductOption & {
additional_data?: Record<string, unknown>
}
>,
res: MedusaResponse<HttpTypes.AdminProductResponse>
) => {
const productId = req.params.id
const optionId = req.params.option_id
const { additional_data, ...update } = req.validatedBody

await updateProductOptionsWorkflow(req.scope).run({
input: {
selector: { id: optionId, product_id: productId },
update: req.validatedBody,
update,
additional_data,
},
})

Expand Down
11 changes: 9 additions & 2 deletions packages/medusa/src/api/admin/products/[id]/options/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminCreateProductOption>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminCreateProductOption & {
additional_data?: Record<string, unknown>
}
>,
res: MedusaResponse<HttpTypes.AdminProductResponse>
) => {
const productId = req.params.id
const { additional_data, ...rest } = req.validatedBody

await createProductOptionsWorkflow(req.scope).run({
input: {
product_options: [
{
...req.validatedBody,
...rest,
product_id: productId,
},
],
additional_data,
},
})

Expand Down
11 changes: 9 additions & 2 deletions packages/medusa/src/api/admin/products/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminUpdateProduct>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminUpdateProduct & {
additional_data?: Record<string, unknown>
}
>,
res: MedusaResponse<HttpTypes.AdminProductResponse>
) => {
const { additional_data, ...update } = req.validatedBody

const { result } = await updateProductsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
update,
additional_data,
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminUpdateProductVariant>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminUpdateProductVariant & {
additional_data?: Record<string, unknown>
}
>,
res: MedusaResponse<HttpTypes.AdminProductResponse>
) => {
const productId = req.params.id
const variantId = req.params.variant_id
const { additional_data, ...update } = req.validatedBody

await updateProductVariantsWorkflow(req.scope).run({
input: {
selector: { id: variantId, product_id: productId },
update: req.validatedBody,
update: update,
additional_data,
},
})

Expand Down
12 changes: 9 additions & 3 deletions packages/medusa/src/api/admin/products/[id]/variants/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,25 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminCreateProductVariant>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminCreateProductVariant & {
additional_data?: Record<string, unknown>
}
>,
res: MedusaResponse<HttpTypes.AdminProductResponse>
) => {
const productId = req.params.id
const { additional_data, ...rest } = req.validatedBody

const input = [
{
...req.validatedBody,
...rest,
product_id: productId,
},
]

await createProductVariantsWorkflow(req.scope).run({
input: { product_variants: input },
input: { product_variants: input, additional_data },
})

const product = await refetchEntity(
Expand Down
9 changes: 4 additions & 5 deletions packages/medusa/src/api/admin/products/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
AdminUpdateProductOption,
AdminUpdateProductVariant,
AdminUpdateVariantInventoryItem,
CreateProduct,
CreateProductVariant,
} from "./validators"
import multer from "multer"

Expand Down Expand Up @@ -68,7 +70,7 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/admin/products/batch",
middlewares: [
validateAndTransformBody(
createBatchBody(AdminCreateProduct, AdminBatchUpdateProduct)
createBatchBody(CreateProduct, AdminBatchUpdateProduct)
),
validateAndTransformQuery(
AdminGetProductParams,
Expand Down Expand Up @@ -166,10 +168,7 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/admin/products/:id/variants/batch",
middlewares: [
validateAndTransformBody(
createBatchBody(
AdminCreateProductVariant,
AdminBatchUpdateProductVariant
)
createBatchBody(CreateProductVariant, AdminBatchUpdateProductVariant)
),
validateAndTransformQuery(
AdminGetProductVariantParams,
Expand Down
10 changes: 8 additions & 2 deletions packages/medusa/src/api/admin/products/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminCreateProduct>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminCreateProduct & {
additional_data?: Record<string, unknown>
}
thetutlage marked this conversation as resolved.
Show resolved Hide resolved
>,
res: MedusaResponse<HttpTypes.AdminProductResponse>
) => {
const { additional_data, ...products } = req.validatedBody

const { result } = await createProductsWorkflow(req.scope).run({
input: { products: [req.validatedBody] },
input: { products: [products], additional_data },
})

const product = await refetchEntity(
Expand Down
Loading
Loading