feature: introduce additional_data to the product endpoints (#8405)

This commit is contained in:
Harminder Virk
2024-08-05 09:24:49 +05:30
committed by GitHub
parent bcad5052af
commit 0706bab663
30 changed files with 219 additions and 258 deletions

View File

@@ -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 {

View File

@@ -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,
@@ -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,
@@ -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()
})
}