Skip to content

Commit

Permalink
feature: introduce additional_data to the product endpoints (#8405)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored and riqwan committed Aug 5, 2024
1 parent 6af2860 commit ad07140
Show file tree
Hide file tree
Showing 30 changed files with 219 additions and 258 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -9,8 +9,7 @@ import { createCollectionsStep } from "../steps"

type WorkflowInput = {
collections: ProductTypes.CreateProductCollectionDTO[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

export const createCollectionsWorkflowId = "create-collections"
export const createCollectionsWorkflow = createWorkflow(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -9,8 +9,7 @@ import { createProductOptionsStep } from "../steps"

type WorkflowInput = {
product_options: ProductTypes.CreateProductOptionDTO[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

export const createProductOptionsWorkflowId = "create-product-options"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -9,8 +9,7 @@ import { createProductTagsStep } from "../steps"

type WorkflowInput = {
product_tags: ProductTypes.CreateProductTagDTO[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

export const createProductTagsWorkflowId = "create-product-tags"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -9,8 +9,7 @@ import { createProductTypesStep } from "../steps"

type WorkflowInput = {
product_types: ProductTypes.CreateProductTypeDTO[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

export const createProductTypesWorkflowId = "create-product-types"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { LinkDefinition } from "@medusajs/modules-sdk"
import { InventoryTypes, PricingTypes, ProductTypes } from "@medusajs/types"
import {
AdditionalData,
InventoryTypes,
PricingTypes,
ProductTypes,
} from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -25,8 +30,7 @@ type WorkflowInput = {
required_quantity?: number
}[]
})[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

const buildLink = (
variant_id: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AdditionalData,
CreateProductWorkflowInputDTO,
PricingTypes,
ProductTypes,
Expand All @@ -17,8 +18,7 @@ import { createProductVariantsWorkflow } from "./create-product-variants"

type WorkflowInput = {
products: CreateProductWorkflowInputDTO[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

export const createProductsWorkflowId = "create-products"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -10,8 +10,7 @@ import { updateCollectionsStep } from "../steps"
type UpdateCollectionsStepInput = {
selector: ProductTypes.FilterableProductCollectionProps
update: ProductTypes.UpdateProductCollectionDTO
additional_data?: Record<string, unknown>
}
} & AdditionalData

type WorkflowInput = UpdateCollectionsStepInput

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -10,8 +10,7 @@ import { updateProductOptionsStep } from "../steps"
type UpdateProductOptionsStepInput = {
selector: ProductTypes.FilterableProductOptionProps
update: ProductTypes.UpdateProductOptionDTO
additional_data?: Record<string, unknown>
}
} & AdditionalData

type WorkflowInput = UpdateProductOptionsStepInput

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -10,8 +10,7 @@ import { updateProductTagsStep } from "../steps"
type UpdateProductTagsStepInput = {
selector: ProductTypes.FilterableProductTypeProps
update: ProductTypes.UpdateProductTypeDTO
additional_data?: Record<string, unknown>
}
} & AdditionalData

type WorkflowInput = UpdateProductTagsStepInput

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductTypes } from "@medusajs/types"
import { AdditionalData, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -10,8 +10,7 @@ import { updateProductTypesStep } from "../steps"
type UpdateProductTypesStepInput = {
selector: ProductTypes.FilterableProductTypeProps
update: ProductTypes.UpdateProductTypeDTO
additional_data?: Record<string, unknown>
}
} & AdditionalData

type WorkflowInput = UpdateProductTypesStepInput

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PricingTypes, ProductTypes } from "@medusajs/types"
import { AdditionalData, PricingTypes, ProductTypes } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -23,9 +23,7 @@ type UpdateProductVariantsStepInput =
})[]
}

type WorkflowInput = UpdateProductVariantsStepInput & {
additional_data?: Record<string, unknown>
}
type WorkflowInput = UpdateProductVariantsStepInput & AdditionalData

export const updateProductVariantsWorkflowId = "update-product-variants"
export const updateProductVariantsWorkflow = createWorkflow(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { updateProductsStep } from "../steps/update-products"

import {
AdditionalData,
CreateMoneyAmountDTO,
ProductTypes,
UpdateProductVariantWorkflowInputDTO,
Expand All @@ -26,16 +27,14 @@ type UpdateProductsStepInputSelector = {
sales_channels?: { id: string }[]
variants?: UpdateProductVariantWorkflowInputDTO[]
}
additional_data?: Record<string, unknown>
}
} & AdditionalData

type UpdateProductsStepInputProducts = {
products: (Omit<ProductTypes.UpsertProductDTO, "variants"> & {
sales_channels?: { id: string }[]
variants?: UpdateProductVariantWorkflowInputDTO[]
})[]
additional_data?: Record<string, unknown>
}
} & AdditionalData

type UpdateProductsStepInput =
| UpdateProductsStepInputSelector
Expand Down
7 changes: 7 additions & 0 deletions packages/core/types/src/http/common/additional_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Represents the additional_data property accepted in HTTP
* requests to allow arbitrary values
*/
export type AdditionalData = {
additional_data?: Record<string, unknown>
}
1 change: 1 addition & 0 deletions packages/core/types/src/http/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./request"
export * from "./response"
export * from "./additional_data"
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
18 changes: 7 additions & 11 deletions packages/framework/framework/src/http/utils/define-middlewares.ts
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@medusajs/core-flows"

import { remapKeysForProduct, remapProductResponse } from "../../../helpers"
import { HttpTypes } from "@medusajs/types"
import { AdditionalData, HttpTypes } from "@medusajs/types"
import { refetchEntity } from "../../../../../utils/refetch-entity"

export const GET = async (
Expand All @@ -28,16 +28,20 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminUpdateProductOption>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminUpdateProductOption & AdditionalData
>,
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: 8 additions & 3 deletions packages/medusa/src/api/admin/products/[id]/options/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {

import { createProductOptionsWorkflow } from "@medusajs/core-flows"
import { remapKeysForProduct, remapProductResponse } from "../../helpers"
import { HttpTypes } from "@medusajs/types"
import { AdditionalData, HttpTypes } from "@medusajs/types"
import {
refetchEntities,
refetchEntity,
Expand Down Expand Up @@ -33,18 +33,23 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminCreateProductOption>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminCreateProductOption & AdditionalData
>,
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: 8 additions & 3 deletions packages/medusa/src/api/admin/products/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../../../../types/routing"
import { remapKeysForProduct, remapProductResponse } from "../helpers"
import { MedusaError } from "@medusajs/utils"
import { HttpTypes } from "@medusajs/types"
import { AdditionalData, HttpTypes } from "@medusajs/types"
import { refetchEntity } from "../../../utils/refetch-entity"

export const GET = async (
Expand All @@ -31,13 +31,18 @@ export const GET = async (
}

export const POST = async (
req: AuthenticatedMedusaRequest<HttpTypes.AdminUpdateProduct>,
req: AuthenticatedMedusaRequest<
HttpTypes.AdminUpdateProduct & AdditionalData
>,
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
Loading

0 comments on commit ad07140

Please sign in to comment.