feat: Switch to zod for the product API and implement missing primitives (#6978)
This commit is contained in:
1 parent
65794f4bb5
commit
883a75c4f3
16 files changed
+491
-829
No files matched your search
@@ -2742,7 +2742,12 @@ medusaIntegrationTestRunner({
|
|||||||
await api.post("/admin/collections", payload, adminHeaders)
|
await api.post("/admin/collections", payload, adminHeaders)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error.response.data.message).toMatch(
|
expect(error.response.data.message).toMatch(
|
||||||
`Product_collection with handle ${baseCollection.handle} already exists.`
|
breaking(
|
||||||
|
() =>
|
||||||
|
`Product_collection with handle ${baseCollection.handle} already exists.`,
|
||||||
|
() =>
|
||||||
|
`Product collection with handle: ${baseCollection.handle} already exists.`
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -2764,10 +2769,10 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
title: "Second variant",
|
title: "Second variant",
|
||||||
sku: variant.sku,
|
sku: "new-sku",
|
||||||
ean: variant.ean,
|
ean: "new-ean",
|
||||||
upc: variant.upc,
|
upc: "new-upc",
|
||||||
barcode: variant.barcode,
|
barcode: "new-barcode",
|
||||||
...breaking(
|
...breaking(
|
||||||
() => ({
|
() => ({
|
||||||
options: [
|
options: [
|
||||||
@@ -2803,10 +2808,10 @@ medusaIntegrationTestRunner({
|
|||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
title: "Second variant",
|
title: "Second variant",
|
||||||
sku: variant.sku,
|
sku: "new-sku",
|
||||||
ean: variant.ean,
|
ean: "new-ean",
|
||||||
upc: variant.upc,
|
upc: "new-upc",
|
||||||
barcode: variant.barcode,
|
barcode: "new-barcode",
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import { getVariantPricingLinkStep } from "../steps/get-variant-pricing-link"
|
|||||||
type UpdateProductVariantsStepInput = {
|
type UpdateProductVariantsStepInput = {
|
||||||
selector: ProductTypes.FilterableProductVariantProps
|
selector: ProductTypes.FilterableProductVariantProps
|
||||||
update: ProductTypes.UpdateProductVariantDTO & {
|
update: ProductTypes.UpdateProductVariantDTO & {
|
||||||
prices?: PricingTypes.CreateMoneyAmountDTO[]
|
prices?: Partial<PricingTypes.CreateMoneyAmountDTO>[]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import {
|
|||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import { UpdateProductOptionDTO } from "../../../../../../../../types/dist"
|
import { refetchProduct, remapProductResponse } from "../../../helpers"
|
||||||
import { refetchProduct, remapProduct } from "../../../helpers"
|
import { AdminUpdateProductOptionType } from "../../../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -33,7 +33,7 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<UpdateProductOptionDTO>,
|
req: AuthenticatedMedusaRequest<AdminUpdateProductOptionType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
@@ -56,7 +56,7 @@ export const POST = async (
|
|||||||
req.scope,
|
req.scope,
|
||||||
req.remoteQueryConfig.fields
|
req.remoteQueryConfig.fields
|
||||||
)
|
)
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DELETE = async (
|
export const DELETE = async (
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
|
|
||||||
import { CreateProductOptionDTO } from "@medusajs/types"
|
|
||||||
import { createProductOptionsWorkflow } from "@medusajs/core-flows"
|
import { createProductOptionsWorkflow } from "@medusajs/core-flows"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import { refetchProduct, remapProduct } from "../../helpers"
|
import { refetchProduct, remapProductResponse } from "../../helpers"
|
||||||
|
import { AdminCreateProductOptionType } from "../../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -37,7 +37,7 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<CreateProductOptionDTO>,
|
req: AuthenticatedMedusaRequest<AdminCreateProductOptionType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
@@ -62,5 +62,5 @@ export const POST = async (
|
|||||||
req.scope,
|
req.scope,
|
||||||
req.remoteQueryConfig.fields
|
req.remoteQueryConfig.fields
|
||||||
)
|
)
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
}
|
}
|
||||||
@@ -7,9 +7,14 @@ import {
|
|||||||
updateProductsWorkflow,
|
updateProductsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
|
||||||
import { UpdateProductDTO } from "@medusajs/types"
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import { refetchProduct, remapKeysForProduct, remapProduct } from "../helpers"
|
import {
|
||||||
|
refetchProduct,
|
||||||
|
remapKeysForProduct,
|
||||||
|
remapProductResponse,
|
||||||
|
} from "../helpers"
|
||||||
|
import { AdminUpdateProductType } from "../validators"
|
||||||
|
import { UpdateProductDTO } from "@medusajs/types"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -28,17 +33,17 @@ export const GET = async (
|
|||||||
|
|
||||||
const [product] = await remoteQuery(queryObject)
|
const [product] = await remoteQuery(queryObject)
|
||||||
|
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<UpdateProductDTO>,
|
req: AuthenticatedMedusaRequest<AdminUpdateProductType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const { result, errors } = await updateProductsWorkflow(req.scope).run({
|
const { result, errors } = await updateProductsWorkflow(req.scope).run({
|
||||||
input: {
|
input: {
|
||||||
selector: { id: req.params.id },
|
selector: { id: req.params.id },
|
||||||
update: req.validatedBody,
|
update: req.validatedBody as UpdateProductDTO,
|
||||||
},
|
},
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
@@ -52,7 +57,7 @@ export const POST = async (
|
|||||||
req.scope,
|
req.scope,
|
||||||
req.remoteQueryConfig.fields
|
req.remoteQueryConfig.fields
|
||||||
)
|
)
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DELETE = async (
|
export const DELETE = async (
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ import {
|
|||||||
updateProductVariantsWorkflow,
|
updateProductVariantsWorkflow,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
|
||||||
import { UpdateProductVariantDTO } from "@medusajs/types"
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
refetchProduct,
|
refetchProduct,
|
||||||
remapKeysForVariant,
|
remapKeysForVariant,
|
||||||
remapProduct,
|
remapProductResponse,
|
||||||
remapVariant,
|
remapVariantResponse,
|
||||||
} from "../../../helpers"
|
} from "../../../helpers"
|
||||||
|
import { AdminUpdateProductVariantType } from "../../../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -35,26 +35,24 @@ export const GET = async (
|
|||||||
})
|
})
|
||||||
|
|
||||||
const [variant] = await remoteQuery(queryObject)
|
const [variant] = await remoteQuery(queryObject)
|
||||||
res.status(200).json({ variant: remapVariant(variant) })
|
res.status(200).json({ variant: remapVariantResponse(variant) })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<UpdateProductVariantDTO>,
|
req: AuthenticatedMedusaRequest<AdminUpdateProductVariantType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
// TODO: Should we allow fetching a variant without knowing the product ID? In such case we'll need to change the route to /admin/products/variants/:id
|
// TODO: Should we allow fetching a variant without knowing the product ID? In such case we'll need to change the route to /admin/products/variants/:id
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
const variantId = req.params.variant_id
|
const variantId = req.params.variant_id
|
||||||
|
|
||||||
const { result, errors } = await updateProductVariantsWorkflow(req.scope).run(
|
const { errors } = await updateProductVariantsWorkflow(req.scope).run({
|
||||||
{
|
input: {
|
||||||
input: {
|
selector: { id: variantId, product_id: productId },
|
||||||
selector: { id: variantId, product_id: productId },
|
update: req.validatedBody,
|
||||||
update: req.validatedBody,
|
},
|
||||||
},
|
throwOnError: false,
|
||||||
throwOnError: false,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if (Array.isArray(errors) && errors[0]) {
|
if (Array.isArray(errors) && errors[0]) {
|
||||||
throw errors[0].error
|
throw errors[0].error
|
||||||
@@ -65,7 +63,8 @@ export const POST = async (
|
|||||||
req.scope,
|
req.scope,
|
||||||
req.remoteQueryConfig.fields
|
req.remoteQueryConfig.fields
|
||||||
)
|
)
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
|
Response
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DELETE = async (
|
export const DELETE = async (
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
|
|
||||||
import { CreateProductVariantDTO } from "@medusajs/types"
|
|
||||||
import { createProductVariantsWorkflow } from "@medusajs/core-flows"
|
import { createProductVariantsWorkflow } from "@medusajs/core-flows"
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
refetchProduct,
|
refetchProduct,
|
||||||
remapKeysForVariant,
|
remapKeysForVariant,
|
||||||
remapProduct,
|
remapProductResponse,
|
||||||
remapVariant,
|
remapVariantResponse,
|
||||||
} from "../../helpers"
|
} from "../../helpers"
|
||||||
|
import { AdminCreateProductVariantType } from "../../validators"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest,
|
||||||
@@ -34,7 +34,7 @@ export const GET = async (
|
|||||||
const { rows: variants, metadata } = await remoteQuery(queryObject)
|
const { rows: variants, metadata } = await remoteQuery(queryObject)
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
variants: variants.map(remapVariant),
|
variants: variants.map(remapVariantResponse),
|
||||||
count: metadata.count,
|
count: metadata.count,
|
||||||
offset: metadata.skip,
|
offset: metadata.skip,
|
||||||
limit: metadata.take,
|
limit: metadata.take,
|
||||||
@@ -42,7 +42,7 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<CreateProductVariantDTO>,
|
req: AuthenticatedMedusaRequest<AdminCreateProductVariantType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const productId = req.params.id
|
const productId = req.params.id
|
||||||
@@ -69,5 +69,5 @@ export const POST = async (
|
|||||||
req.scope,
|
req.scope,
|
||||||
req.remoteQueryConfig.fields
|
req.remoteQueryConfig.fields
|
||||||
)
|
)
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,4 @@
|
|||||||
import {
|
import { MedusaContainer, ProductDTO, ProductVariantDTO } from "@medusajs/types"
|
||||||
CreateProductDTO,
|
|
||||||
MedusaContainer,
|
|
||||||
ProductDTO,
|
|
||||||
ProductVariantDTO,
|
|
||||||
} from "@medusajs/types"
|
|
||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
|
|
||||||
const isPricing = (fieldName: string) =>
|
const isPricing = (fieldName: string) =>
|
||||||
@@ -39,27 +34,27 @@ export const remapKeysForVariant = (selectFields: string[]) => {
|
|||||||
return [...variantFields, ...pricingFields]
|
return [...variantFields, ...pricingFields]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const remapProduct = (p: ProductDTO) => {
|
export const remapProductResponse = (product: ProductDTO) => {
|
||||||
return {
|
return {
|
||||||
...p,
|
...product,
|
||||||
variants: p.variants?.map(remapVariant),
|
variants: product.variants?.map(remapVariantResponse),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const remapVariant = (v: ProductVariantDTO) => {
|
export const remapVariantResponse = (variant: ProductVariantDTO) => {
|
||||||
if (!v) {
|
if (!variant) {
|
||||||
return v
|
return variant
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...v,
|
...variant,
|
||||||
prices: (v as any).price_set?.prices?.map((price) => ({
|
prices: (variant as any).price_set?.prices?.map((price) => ({
|
||||||
id: price.id,
|
id: price.id,
|
||||||
amount: price.amount,
|
amount: price.amount,
|
||||||
currency_code: price.currency_code,
|
currency_code: price.currency_code,
|
||||||
min_quantity: price.min_quantity,
|
min_quantity: price.min_quantity,
|
||||||
max_quantity: price.max_quantity,
|
max_quantity: price.max_quantity,
|
||||||
variant_id: v.id,
|
variant_id: variant.id,
|
||||||
created_at: price.created_at,
|
created_at: price.created_at,
|
||||||
updated_at: price.updated_at,
|
updated_at: price.updated_at,
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -2,21 +2,23 @@ import { transformBody, transformQuery } from "../../../api/middlewares"
|
|||||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
|
import { maybeApplyLinkFilter } from "../../utils/maybe-apply-link-filter"
|
||||||
|
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||||
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
import { maybeApplyPriceListsFilter } from "./utils"
|
import { maybeApplyPriceListsFilter } from "./utils"
|
||||||
import {
|
import {
|
||||||
AdminGetProductsOptionsParams,
|
|
||||||
AdminGetProductsParams,
|
AdminGetProductsParams,
|
||||||
AdminGetProductsProductOptionsOptionParams,
|
AdminCreateProduct,
|
||||||
AdminGetProductsProductParams,
|
AdminCreateProductOption,
|
||||||
AdminGetProductsProductVariantsVariantParams,
|
AdminCreateProductVariant,
|
||||||
AdminGetProductsVariantsParams,
|
AdminUpdateProduct,
|
||||||
AdminPostProductsProductOptionsOptionReq,
|
AdminUpdateProductOption,
|
||||||
AdminPostProductsProductOptionsReq,
|
AdminGetProductParams,
|
||||||
AdminPostProductsProductReq,
|
AdminGetProductVariantsParams,
|
||||||
AdminPostProductsProductVariantsReq,
|
AdminGetProductVariantParams,
|
||||||
AdminPostProductsProductVariantsVariantReq,
|
AdminUpdateProductVariant,
|
||||||
AdminPostProductsReq,
|
AdminGetProductOptionsParams,
|
||||||
|
AdminGetProductOptionParams,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
|
|
||||||
export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
@@ -29,7 +31,7 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/products",
|
matcher: "/admin/products",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsParams,
|
AdminGetProductsParams,
|
||||||
QueryConfig.listProductQueryConfig
|
QueryConfig.listProductQueryConfig
|
||||||
),
|
),
|
||||||
@@ -45,8 +47,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/products/:id",
|
matcher: "/admin/products/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -55,9 +57,9 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/products",
|
matcher: "/admin/products",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductsReq),
|
validateAndTransformBody(AdminCreateProduct),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -66,9 +68,9 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/products/:id",
|
matcher: "/admin/products/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductsProductReq),
|
validateAndTransformBody(AdminUpdateProduct),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -77,8 +79,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["DELETE"],
|
method: ["DELETE"],
|
||||||
matcher: "/admin/products/:id",
|
matcher: "/admin/products/:id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -88,8 +90,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/products/:id/variants",
|
matcher: "/admin/products/:id/variants",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsVariantsParams,
|
AdminGetProductVariantsParams,
|
||||||
QueryConfig.listVariantConfig
|
QueryConfig.listVariantConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -99,8 +101,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/products/:id/variants/:variant_id",
|
matcher: "/admin/products/:id/variants/:variant_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductVariantsVariantParams,
|
AdminGetProductVariantParams,
|
||||||
QueryConfig.retrieveVariantConfig
|
QueryConfig.retrieveVariantConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -109,9 +111,9 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/products/:id/variants",
|
matcher: "/admin/products/:id/variants",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductsProductVariantsReq),
|
validateAndTransformBody(AdminCreateProductVariant),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -120,9 +122,9 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/products/:id/variants/:variant_id",
|
matcher: "/admin/products/:id/variants/:variant_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductsProductVariantsVariantReq),
|
validateAndTransformBody(AdminUpdateProductVariant),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -131,8 +133,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["DELETE"],
|
method: ["DELETE"],
|
||||||
matcher: "/admin/products/:id/variants/:variant_id",
|
matcher: "/admin/products/:id/variants/:variant_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -143,8 +145,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/products/:id/options",
|
matcher: "/admin/products/:id/options",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsOptionsParams,
|
AdminGetProductOptionsParams,
|
||||||
QueryConfig.listOptionConfig
|
QueryConfig.listOptionConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -154,8 +156,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/products/:id/options/:option_id",
|
matcher: "/admin/products/:id/options/:option_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductOptionsOptionParams,
|
AdminGetProductOptionParams,
|
||||||
QueryConfig.retrieveOptionConfig
|
QueryConfig.retrieveOptionConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -164,9 +166,9 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/products/:id/options",
|
matcher: "/admin/products/:id/options",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductsProductOptionsReq),
|
validateAndTransformBody(AdminCreateProductOption),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -175,9 +177,9 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/products/:id/options/:option_id",
|
matcher: "/admin/products/:id/options/:option_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformBody(AdminPostProductsProductOptionsOptionReq),
|
validateAndTransformBody(AdminUpdateProductOption),
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -186,8 +188,8 @@ export const adminProductRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["DELETE"],
|
method: ["DELETE"],
|
||||||
matcher: "/admin/products/:id/options/:option_id",
|
matcher: "/admin/products/:id/options/:option_id",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetProductsProductParams,
|
AdminGetProductParams,
|
||||||
QueryConfig.retrieveProductQueryConfig
|
QueryConfig.retrieveProductQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ export const defaultAdminProductsVariantFields = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export const retrieveVariantConfig = {
|
export const retrieveVariantConfig = {
|
||||||
defaultFields: defaultAdminProductsVariantFields,
|
defaults: defaultAdminProductsVariantFields,
|
||||||
defaultRelations: [],
|
|
||||||
allowedRelations: [],
|
|
||||||
isList: false,
|
isList: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +40,7 @@ export const listVariantConfig = {
|
|||||||
export const defaultAdminProductsOptionFields = ["id", "title"]
|
export const defaultAdminProductsOptionFields = ["id", "title"]
|
||||||
|
|
||||||
export const retrieveOptionConfig = {
|
export const retrieveOptionConfig = {
|
||||||
defaultFields: defaultAdminProductsOptionFields,
|
defaults: defaultAdminProductsOptionFields,
|
||||||
isList: false,
|
isList: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,29 @@
|
|||||||
import { createProductsWorkflow } from "@medusajs/core-flows"
|
import { createProductsWorkflow } from "@medusajs/core-flows"
|
||||||
import { CreateProductDTO } from "@medusajs/types"
|
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
|
ProductStatus,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../types/routing"
|
} from "../../../types/routing"
|
||||||
import { refetchProduct, remapKeysForProduct, remapProduct } from "./helpers"
|
import {
|
||||||
import { AdminGetProductsParams } from "./validators"
|
refetchProduct,
|
||||||
|
remapKeysForProduct,
|
||||||
|
remapProductResponse,
|
||||||
|
} from "./helpers"
|
||||||
|
import {
|
||||||
|
AdminCreateProductType,
|
||||||
|
AdminGetProductsParamsType,
|
||||||
|
} from "./validators"
|
||||||
|
import { CreateProductDTO } from "@medusajs/types"
|
||||||
|
|
||||||
export const GET = async (
|
export const GET = async (
|
||||||
req: AuthenticatedMedusaRequest<AdminGetProductsParams>,
|
req: AuthenticatedMedusaRequest<AdminGetProductsParamsType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
const selectFields = remapKeysForProduct(req.remoteQueryConfig.fields ?? [])
|
const selectFields = remapKeysForProduct(req.remoteQueryConfig.fields ?? [])
|
||||||
|
|
||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
@@ -31,7 +38,7 @@ export const GET = async (
|
|||||||
const { rows: products, metadata } = await remoteQuery(queryObject)
|
const { rows: products, metadata } = await remoteQuery(queryObject)
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
products: products.map(remapProduct),
|
products: products.map(remapProductResponse),
|
||||||
count: metadata.count,
|
count: metadata.count,
|
||||||
offset: metadata.skip,
|
offset: metadata.skip,
|
||||||
limit: metadata.take,
|
limit: metadata.take,
|
||||||
@@ -39,10 +46,10 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest<CreateProductDTO>,
|
req: AuthenticatedMedusaRequest<AdminCreateProductType>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const input = [req.validatedBody]
|
const input = [req.validatedBody as CreateProductDTO]
|
||||||
|
|
||||||
const { result, errors } = await createProductsWorkflow(req.scope).run({
|
const { result, errors } = await createProductsWorkflow(req.scope).run({
|
||||||
input: { products: input },
|
input: { products: input },
|
||||||
@@ -58,5 +65,5 @@ export const POST = async (
|
|||||||
req.scope,
|
req.scope,
|
||||||
req.remoteQueryConfig.fields
|
req.remoteQueryConfig.fields
|
||||||
)
|
)
|
||||||
res.status(200).json({ product: remapProduct(product) })
|
res.status(200).json({ product: remapProductResponse(product) })
|
||||||
}
|
}
|
||||||
@@ -4,11 +4,11 @@ import {
|
|||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { NextFunction } from "express"
|
import { NextFunction } from "express"
|
||||||
import { MedusaRequest } from "../../../../types/routing"
|
import { MedusaRequest } from "../../../../types/routing"
|
||||||
import { AdminGetProductsParams } from "../validators"
|
import { AdminGetProductsParamsType } from "../validators"
|
||||||
|
|
||||||
export function maybeApplyPriceListsFilter() {
|
export function maybeApplyPriceListsFilter() {
|
||||||
return async (req: MedusaRequest, _, next: NextFunction) => {
|
return async (req: MedusaRequest, _, next: NextFunction) => {
|
||||||
const filterableFields: AdminGetProductsParams = req.filterableFields
|
const filterableFields: AdminGetProductsParamsType = req.filterableFields
|
||||||
|
|
||||||
if (!filterableFields.price_list_id) {
|
if (!filterableFields.price_list_id) {
|
||||||
return next()
|
return next()
|
||||||
|
|||||||
@@ -1,725 +1,242 @@
|
|||||||
import { OperatorMap } from "@medusajs/types"
|
|
||||||
import { ProductStatus } from "@medusajs/utils"
|
import { ProductStatus } from "@medusajs/utils"
|
||||||
import { Transform, Type } from "class-transformer"
|
import { z } from "zod"
|
||||||
import {
|
import {
|
||||||
IsArray,
|
createFindParams,
|
||||||
IsBoolean,
|
createOperatorMap,
|
||||||
IsEnum,
|
createSelectParams,
|
||||||
IsInt,
|
} from "../../utils/validators"
|
||||||
IsNumber,
|
|
||||||
IsObject,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
NotEquals,
|
|
||||||
ValidateIf,
|
|
||||||
ValidateNested,
|
|
||||||
} from "class-validator"
|
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
import { OperatorMapValidator } from "../../../types/validators/operator-map"
|
|
||||||
import { IsType } from "../../../utils"
|
|
||||||
import { optionalBooleanMapper } from "../../../utils/validators/is-boolean"
|
import { optionalBooleanMapper } from "../../../utils/validators/is-boolean"
|
||||||
|
|
||||||
export class AdminGetProductsProductParams extends FindParams {}
|
const statusEnum = z.nativeEnum(ProductStatus)
|
||||||
export class AdminGetProductsProductVariantsVariantParams extends FindParams {}
|
|
||||||
export class AdminGetProductsProductOptionsOptionParams extends FindParams {}
|
|
||||||
|
|
||||||
/**
|
export const AdminGetProductParams = createSelectParams()
|
||||||
* Parameters used to filter and configure the pagination of the retrieved regions.
|
export const AdminGetProductVariantParams = createSelectParams()
|
||||||
*/
|
export const AdminGetProductOptionParams = createSelectParams()
|
||||||
export class AdminGetProductsParams extends extendedFindParamsMixin({
|
|
||||||
limit: 50,
|
export type AdminGetProductsParamsType = z.infer<typeof AdminGetProductsParams>
|
||||||
|
export const AdminGetProductsParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
|
||||||
// TODO: Will search be handled the same way? Should it be part of the `findParams` class instead, or the mixin?
|
|
||||||
/**
|
|
||||||
* Search term to search products' title, description, variants' title and sku, and collections' title.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
q?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* IDs to filter products by.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsType([String, [String]])
|
|
||||||
id?: string | string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Statuses to filter products by.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsEnum(ProductStatus, { each: true })
|
|
||||||
status?: ProductStatus[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Title to filter products by.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
title?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle to filter products by.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
handle?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter products by whether they're gift cards.
|
|
||||||
*/
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase()))
|
|
||||||
is_giftcard?: boolean
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter products by their associated price lists' ID.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsArray()
|
|
||||||
price_list_id?: string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter products by associated sales channel IDs.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsArray()
|
|
||||||
sales_channel_id?: string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter products by their associated product collection's ID.
|
|
||||||
*/
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
collection_id?: string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter products by their associated tags' value.
|
|
||||||
*/
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
tags?: string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter products by their associated product type's ID.
|
|
||||||
*/
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
type_id?: string[]
|
|
||||||
|
|
||||||
// TODO: Replace this with AdminGetProductVariantsParams when its available
|
|
||||||
@IsOptional()
|
|
||||||
@IsObject()
|
|
||||||
variants?: Record<any, any>
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Filter products by their associated discount condition's ID.
|
|
||||||
// */
|
|
||||||
// @IsString()
|
|
||||||
// @IsOptional()
|
|
||||||
// discount_condition_id?: string
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Filter products by their associated product category's ID.
|
|
||||||
// */
|
|
||||||
// @IsArray()
|
|
||||||
// @IsOptional()
|
|
||||||
// category_id?: string[]
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Whether to include product category children in the response.
|
|
||||||
// *
|
|
||||||
// * @featureFlag product_categories
|
|
||||||
// */
|
|
||||||
// @IsBoolean()
|
|
||||||
// @IsOptional()
|
|
||||||
// @Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase()))
|
|
||||||
// include_category_children?: boolean
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
created_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
updated_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
@ValidateNested()
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
deleted_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
// Note: These are new in v2
|
|
||||||
// Additional filters from BaseFilterable
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetProductsParams)
|
|
||||||
$and?: AdminGetProductsParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetProductsParams)
|
|
||||||
$or?: AdminGetProductsParams[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminGetProductsVariantsParams extends extendedFindParamsMixin({
|
|
||||||
limit: 50,
|
limit: 50,
|
||||||
|
}).merge(
|
||||||
|
z.object({
|
||||||
|
q: z.string().optional(),
|
||||||
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
status: statusEnum.array().optional(),
|
||||||
|
title: z.string().optional(),
|
||||||
|
handle: z.string().optional(),
|
||||||
|
is_giftcard: z.preprocess(
|
||||||
|
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()),
|
||||||
|
z.boolean().optional()
|
||||||
|
),
|
||||||
|
price_list_id: z.string().array().optional(),
|
||||||
|
sales_channel_id: z.string().array().optional(),
|
||||||
|
collection_id: z.string().array().optional(),
|
||||||
|
tags: z.string().array().optional(),
|
||||||
|
type_id: z.string().array().optional(),
|
||||||
|
// TODO: Replace this with AdminGetProductVariantsParams when its available
|
||||||
|
variants: z.record(z.unknown()).optional(),
|
||||||
|
created_at: createOperatorMap().optional(),
|
||||||
|
updated_at: createOperatorMap().optional(),
|
||||||
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
$and: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||||
|
$or: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
// TODO: These were part of the products find query, add them once supported
|
||||||
|
// @IsString()
|
||||||
|
// @IsOptional()
|
||||||
|
// discount_condition_id?: string
|
||||||
|
|
||||||
|
// @IsArray()
|
||||||
|
// @IsOptional()
|
||||||
|
// category_id?: string[]
|
||||||
|
|
||||||
|
// @IsBoolean()
|
||||||
|
// @IsOptional()
|
||||||
|
// @Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase()))
|
||||||
|
// include_category_children?: boolean
|
||||||
|
|
||||||
|
export type AdminGetProductVariantsParamsType = z.infer<
|
||||||
|
typeof AdminGetProductVariantsParams
|
||||||
|
>
|
||||||
|
export const AdminGetProductVariantsParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
|
||||||
// TODO: Will search be handled the same way? Should it be part of the `findParams` class instead, or the mixin?
|
|
||||||
/**
|
|
||||||
* Search term to search product variants' title, sku, and products' title.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
q?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* IDs to filter product variants by.
|
|
||||||
*/
|
|
||||||
@IsOptional()
|
|
||||||
@IsType([String, [String]])
|
|
||||||
id?: string | string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter product variants by whether their inventory is managed or not.
|
|
||||||
*/
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase()))
|
|
||||||
manage_inventory?: boolean
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter product variants by whether they are allowed to be backordered or not.
|
|
||||||
*/
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase()))
|
|
||||||
allow_backorder?: boolean
|
|
||||||
|
|
||||||
// TODO: The OperatorMap and DateOperator are slightly different, so the date comparisons is a breaking change.
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
created_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
updated_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
@ValidateNested()
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => OperatorMapValidator)
|
|
||||||
deleted_at?: OperatorMap<string>
|
|
||||||
|
|
||||||
// Note: These are new in v2
|
|
||||||
// Additional filters from BaseFilterable
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetProductsVariantsParams)
|
|
||||||
$and?: AdminGetProductsVariantsParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetProductsVariantsParams)
|
|
||||||
$or?: AdminGetProductsVariantsParams[]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: This model and endpoint are new in v2
|
|
||||||
export class AdminGetProductsOptionsParams extends extendedFindParamsMixin({
|
|
||||||
limit: 50,
|
limit: 50,
|
||||||
|
}).merge(
|
||||||
|
z.object({
|
||||||
|
q: z.string().optional(),
|
||||||
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
manage_inventory: z.boolean().optional(),
|
||||||
|
allow_backorder: z.boolean().optional(),
|
||||||
|
created_at: createOperatorMap().optional(),
|
||||||
|
updated_at: createOperatorMap().optional(),
|
||||||
|
deleted_at: createOperatorMap().optional(),
|
||||||
|
$and: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||||
|
$or: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
export type AdminGetProductOptionsParamsType = z.infer<
|
||||||
|
typeof AdminGetProductOptionsParams
|
||||||
|
>
|
||||||
|
export const AdminGetProductOptionsParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
limit: 50,
|
||||||
@IsOptional()
|
}).merge(
|
||||||
@IsString()
|
z.object({
|
||||||
title?: string
|
id: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
title: z.string().optional(),
|
||||||
// Note: These are new in v2
|
$and: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||||
// Additional filters from BaseFilterable
|
$or: z.lazy(() => AdminGetProductsParams.array()).optional(),
|
||||||
@IsOptional()
|
})
|
||||||
@ValidateNested({ each: true })
|
)
|
||||||
@Type(() => AdminGetProductsOptionsParams)
|
|
||||||
$and?: AdminGetProductsOptionsParams[]
|
export type AdminCreateProductTagType = z.infer<typeof AdminCreateProductTag>
|
||||||
|
export const AdminCreateProductTag = z.object({
|
||||||
@IsOptional()
|
value: z.string().optional(),
|
||||||
@ValidateNested({ each: true })
|
})
|
||||||
@Type(() => AdminGetProductsOptionsParams)
|
|
||||||
$or?: AdminGetProductsOptionsParams[]
|
export type AdminUpdateProductTagType = z.infer<typeof AdminUpdateProductTag>
|
||||||
}
|
export const AdminUpdateProductTag = z.object({
|
||||||
|
id: z.string().optional(),
|
||||||
export class AdminPostProductsReq {
|
value: z.string().optional(),
|
||||||
@IsString()
|
})
|
||||||
title: string
|
|
||||||
|
export type AdminCreateProductOptionType = z.infer<
|
||||||
@IsString()
|
typeof AdminCreateProductOption
|
||||||
@IsOptional()
|
>
|
||||||
subtitle?: string
|
export const AdminCreateProductOption = z.object({
|
||||||
|
title: z.string(),
|
||||||
@IsString()
|
values: z.array(z.string()),
|
||||||
@IsOptional()
|
})
|
||||||
description?: string
|
|
||||||
|
export type AdminUpdateProductOptionType = z.infer<
|
||||||
@IsBoolean()
|
typeof AdminUpdateProductOption
|
||||||
is_giftcard = false
|
>
|
||||||
|
export const AdminUpdateProductOption = z.object({
|
||||||
@IsBoolean()
|
id: z.string().optional(),
|
||||||
discountable = true
|
title: z.string().optional(),
|
||||||
|
values: z.array(z.string()).optional(),
|
||||||
@IsArray()
|
})
|
||||||
@IsOptional()
|
|
||||||
images?: string[]
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
thumbnail?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
handle?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsEnum(ProductStatus)
|
|
||||||
status?: ProductStatus = ProductStatus.DRAFT
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
type_id?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
collection_id?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => ProductTagReq)
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@IsArray()
|
|
||||||
tags?: ProductTagReq[]
|
|
||||||
|
|
||||||
// @IsOptional()
|
|
||||||
// @Type(() => ProductProductCategoryReq)
|
|
||||||
// @ValidateNested({ each: true })
|
|
||||||
// @IsArray()
|
|
||||||
// categories?: ProductProductCategoryReq[]
|
|
||||||
|
|
||||||
// TODO: Deal with in next iteration
|
|
||||||
// @FeatureFlagDecorators(SalesChannelFeatureFlag.key, [
|
|
||||||
// IsOptional(),
|
|
||||||
// Type(() => ProductSalesChannelReq),
|
|
||||||
// ValidateNested({ each: true }),
|
|
||||||
// IsArray(),
|
|
||||||
// ])
|
|
||||||
// sales_channels?: ProductSalesChannelReq[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => AdminPostProductsProductOptionsReq)
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@IsArray()
|
|
||||||
options?: AdminPostProductsProductOptionsReq[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => AdminPostProductsProductVariantsReq)
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@IsArray()
|
|
||||||
variants?: AdminPostProductsProductVariantsReq[]
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
weight?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
length?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
height?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
width?: number
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
hs_code?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
origin_country?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
mid_code?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
material?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductsProductReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
title?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
subtitle?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
description?: string
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
discountable?: boolean
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
images?: string[]
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
thumbnail?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
handle?: string
|
|
||||||
|
|
||||||
@IsEnum(ProductStatus)
|
|
||||||
@NotEquals(null)
|
|
||||||
@ValidateIf((_, value) => value !== undefined)
|
|
||||||
status?: ProductStatus
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
type_id?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
collection_id?: string
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => ProductTagReq)
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@IsArray()
|
|
||||||
tags?: ProductTagReq[]
|
|
||||||
|
|
||||||
// @IsOptional()
|
|
||||||
// @Type(() => ProductProductCategoryReq)
|
|
||||||
// @ValidateNested({ each: true })
|
|
||||||
// @IsArray()
|
|
||||||
// categories?: ProductProductCategoryReq[]
|
|
||||||
|
|
||||||
// @FeatureFlagDecorators(SalesChannelFeatureFlag.key, [
|
|
||||||
// IsOptional(),
|
|
||||||
// Type(() => ProductSalesChannelReq),
|
|
||||||
// ValidateNested({ each: true }),
|
|
||||||
// IsArray(),
|
|
||||||
// ])
|
|
||||||
// sales_channels?: ProductSalesChannelReq[] | null
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Type(() => ProductVariantReq)
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@IsArray()
|
|
||||||
variants?: ProductVariantReq[]
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
weight?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
length?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
height?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
width?: number
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
hs_code?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
origin_country?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
mid_code?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
material?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductsProductVariantsReq {
|
|
||||||
@IsString()
|
|
||||||
title: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
sku?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
ean?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
upc?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
barcode?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
hs_code?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
mid_code?: string
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
inventory_quantity?: number = 0
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
allow_backorder?: boolean
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
manage_inventory?: boolean = true
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
variant_rank?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
weight?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
length?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
height?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
width?: number
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
origin_country?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
material?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => ProductVariantPricesCreateReq)
|
|
||||||
prices?: ProductVariantPricesCreateReq[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsObject()
|
|
||||||
options?: Record<string, string>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductsProductVariantsVariantReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
title?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
sku?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
ean?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
upc?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
barcode?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
hs_code?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
mid_code?: string
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
inventory_quantity?: number
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
allow_backorder?: boolean
|
|
||||||
|
|
||||||
@IsBoolean()
|
|
||||||
@IsOptional()
|
|
||||||
manage_inventory?: boolean
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
variant_rank?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
weight?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
length?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
height?: number
|
|
||||||
|
|
||||||
@IsNumber()
|
|
||||||
@IsOptional()
|
|
||||||
width?: number
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
origin_country?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
material?: string
|
|
||||||
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
metadata?: Record<string, unknown>
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => ProductVariantPricesUpdateReq)
|
|
||||||
prices?: ProductVariantPricesUpdateReq[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsObject()
|
|
||||||
options?: Record<string, string>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductsProductOptionsReq {
|
|
||||||
@IsString()
|
|
||||||
title: string
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
values: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AdminPostProductsProductOptionsOptionReq {
|
|
||||||
@IsString()
|
|
||||||
title: string
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
values: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line max-len
|
|
||||||
export class ProductVariantReq extends AdminPostProductsProductVariantsVariantReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
id?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ProductTagReq {
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
id?: string
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
value: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The details of a product type, used to create or update an existing product type.
|
|
||||||
*/
|
|
||||||
export class ProductTypeReq {
|
|
||||||
/**
|
|
||||||
* The ID of the product type. It's only required when referring to an existing product type.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
id?: string
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the product type.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
value: string
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Add support for rules
|
// TODO: Add support for rules
|
||||||
export class ProductVariantPricesCreateReq {
|
export type AdminCreateVariantPriceType = z.infer<
|
||||||
@IsString()
|
typeof AdminCreateVariantPrice
|
||||||
currency_code: string
|
>
|
||||||
|
export const AdminCreateVariantPrice = z.object({
|
||||||
|
currency_code: z.string(),
|
||||||
|
amount: z.number(),
|
||||||
|
min_quantity: z.number().optional(),
|
||||||
|
max_quantity: z.number().optional(),
|
||||||
|
})
|
||||||
|
|
||||||
@IsInt()
|
// TODO: Add support for rules
|
||||||
amount: number
|
export type AdminUpdateVariantPriceType = z.infer<
|
||||||
|
typeof AdminUpdateVariantPrice
|
||||||
|
>
|
||||||
|
export const AdminUpdateVariantPrice = z.object({
|
||||||
|
id: z.string().optional(),
|
||||||
|
currency_code: z.string().optional(),
|
||||||
|
amount: z.number().optional(),
|
||||||
|
min_quantity: z.number().optional(),
|
||||||
|
max_quantity: z.number().optional(),
|
||||||
|
})
|
||||||
|
|
||||||
@IsOptional()
|
export type AdminCreateProductTypeType = z.infer<typeof AdminCreateProductType>
|
||||||
@IsInt()
|
export const AdminCreateProductType = z.object({
|
||||||
min_quantity?: number
|
value: z.string(),
|
||||||
|
})
|
||||||
|
|
||||||
@IsOptional()
|
export type AdminCreateProductVariantType = z.infer<
|
||||||
@IsInt()
|
typeof AdminCreateProductVariant
|
||||||
max_quantity?: number
|
>
|
||||||
}
|
export const AdminCreateProductVariant = z.object({
|
||||||
|
title: z.string(),
|
||||||
|
sku: z.string().optional(),
|
||||||
|
ean: z.string().optional(),
|
||||||
|
upc: z.string().optional(),
|
||||||
|
barcode: z.string().optional(),
|
||||||
|
hs_code: z.string().optional(),
|
||||||
|
mid_code: z.string().optional(),
|
||||||
|
inventory_quantity: z.number().optional().default(0),
|
||||||
|
allow_backorder: z.boolean().optional().default(false),
|
||||||
|
manage_inventory: z.boolean().optional().default(true),
|
||||||
|
variant_rank: z.number().optional(),
|
||||||
|
weight: z.number().optional(),
|
||||||
|
length: z.number().optional(),
|
||||||
|
height: z.number().optional(),
|
||||||
|
width: z.number().optional(),
|
||||||
|
origin_country: z.string().optional(),
|
||||||
|
material: z.string().optional(),
|
||||||
|
metadata: z.record(z.unknown()).optional(),
|
||||||
|
prices: z.array(AdminCreateVariantPrice),
|
||||||
|
options: z.record(z.string()).optional(),
|
||||||
|
})
|
||||||
|
|
||||||
export class ProductVariantPricesUpdateReq {
|
export type AdminUpdateProductVariantType = z.infer<
|
||||||
@IsString()
|
typeof AdminUpdateProductVariant
|
||||||
@IsOptional()
|
>
|
||||||
id?: string
|
export const AdminUpdateProductVariant = AdminCreateProductVariant.extend({
|
||||||
|
id: z.string().optional(),
|
||||||
|
title: z.string().optional(),
|
||||||
|
prices: z.array(AdminUpdateVariantPrice).optional(),
|
||||||
|
inventory_quantity: z.number().optional(),
|
||||||
|
allow_backorder: z.boolean().optional(),
|
||||||
|
manage_inventory: z.boolean().optional(),
|
||||||
|
}).strict()
|
||||||
|
|
||||||
@IsString()
|
export type AdminCreateProductType = z.infer<typeof AdminCreateProduct>
|
||||||
@IsOptional()
|
export const AdminCreateProduct = z
|
||||||
currency_code?: string
|
.object({
|
||||||
|
title: z.string(),
|
||||||
|
subtitle: z.string().optional(),
|
||||||
|
description: z.string().optional(),
|
||||||
|
is_giftcard: z.boolean().optional().default(false),
|
||||||
|
discountable: z.boolean().optional().default(true),
|
||||||
|
images: z.array(z.object({ url: z.string() })).optional(),
|
||||||
|
thumbnail: z.string().optional(),
|
||||||
|
handle: z.string().optional(),
|
||||||
|
status: statusEnum.optional().default(ProductStatus.DRAFT),
|
||||||
|
type_id: z.string().nullable().optional(),
|
||||||
|
collection_id: z.string().nullable().optional(),
|
||||||
|
tags: z.array(AdminUpdateProductTag).optional(),
|
||||||
|
options: z.array(AdminCreateProductOption).optional(),
|
||||||
|
variants: z.array(AdminCreateProductVariant).optional(),
|
||||||
|
weight: z.number().optional(),
|
||||||
|
length: z.number().optional(),
|
||||||
|
height: z.number().optional(),
|
||||||
|
width: z.number().optional(),
|
||||||
|
hs_code: z.string().optional(),
|
||||||
|
mid_code: z.string().optional(),
|
||||||
|
origin_country: z.string().optional(),
|
||||||
|
material: z.string().optional(),
|
||||||
|
metadata: z.record(z.unknown()).optional(),
|
||||||
|
})
|
||||||
|
.strict()
|
||||||
|
|
||||||
@IsInt()
|
export type AdminUpdateProductType = z.infer<typeof AdminUpdateProduct>
|
||||||
amount: number
|
export const AdminUpdateProduct = AdminCreateProduct.omit({ is_giftcard: true })
|
||||||
|
.extend({
|
||||||
|
title: z.string().optional(),
|
||||||
|
discountable: z.boolean().optional(),
|
||||||
|
options: z.array(AdminUpdateProductOption).optional(),
|
||||||
|
variants: z.array(AdminUpdateProductVariant).optional(),
|
||||||
|
status: statusEnum.optional(),
|
||||||
|
})
|
||||||
|
.strict()
|
||||||
|
|
||||||
@IsOptional()
|
// TODO: Handle in create and update product once ready
|
||||||
@IsInt()
|
// @IsOptional()
|
||||||
min_quantity?: number
|
// @Type(() => ProductProductCategoryReq)
|
||||||
|
// @ValidateNested({ each: true })
|
||||||
|
// @IsArray()
|
||||||
|
// categories?: ProductProductCategoryReq[]
|
||||||
|
|
||||||
@IsOptional()
|
// TODO: Deal with in next iteration
|
||||||
@IsInt()
|
// @FeatureFlagDecorators(SalesChannelFeatureFlag.key, [
|
||||||
max_quantity?: number
|
// IsOptional(),
|
||||||
}
|
// Type(() => ProductSalesChannelReq),
|
||||||
|
// ValidateNested({ each: true }),
|
||||||
|
// IsArray(),
|
||||||
|
// ])
|
||||||
|
// sales_channels?: ProductSalesChannelReq[]
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
import multer from "multer"
|
import multer from "multer"
|
||||||
import { transformQuery } from "../../../api/middlewares"
|
|
||||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
import * as QueryConfig from "./query-config"
|
|
||||||
import { AdminGetProductsProductParams } from "../products/validators"
|
|
||||||
|
|
||||||
const upload = multer({ dest: "uploads/" })
|
const upload = multer({ dest: "uploads/" })
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import { NextFunction } from "express"
|
||||||
|
import { MedusaRequest, MedusaResponse } from "../../types/routing"
|
||||||
|
import { zodValidator } from "./validate-body"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { removeUndefinedProperties } from "../../utils"
|
||||||
|
import { omit } from "lodash"
|
||||||
|
import { BaseEntity, QueryConfig, RequestQueryFields } from "@medusajs/types"
|
||||||
|
import {
|
||||||
|
prepareListQuery,
|
||||||
|
prepareRetrieveQuery,
|
||||||
|
} from "../../utils/get-query-config"
|
||||||
|
import { FindConfig } from "@medusajs/types"
|
||||||
|
/**
|
||||||
|
* Normalize an input query, especially from array like query params to an array type
|
||||||
|
* e.g: /admin/orders/?fields[]=id,status,cart_id becomes { fields: ["id", "status", "cart_id"] }
|
||||||
|
*/
|
||||||
|
const normalizeQuery = (req: MedusaRequest) => {
|
||||||
|
return Object.entries(req.query).reduce((acc, [key, val]) => {
|
||||||
|
if (Array.isArray(val) && val.length === 1) {
|
||||||
|
acc[key] = (val as string[])[0].split(",")
|
||||||
|
} else {
|
||||||
|
acc[key] = val
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Omit the non filterable config from the validated object
|
||||||
|
* @param obj
|
||||||
|
*/
|
||||||
|
const getFilterableFields = <T extends RequestQueryFields>(obj: T): T => {
|
||||||
|
const result = omit(obj, ["limit", "offset", "fields", "order"]) as T
|
||||||
|
return removeUndefinedProperties(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateAndTransformQuery<TEntity extends BaseEntity>(
|
||||||
|
zodSchema: z.ZodObject<any, any>,
|
||||||
|
queryConfig: QueryConfig<TEntity>,
|
||||||
|
config?: {
|
||||||
|
strict?: boolean
|
||||||
|
}
|
||||||
|
): (
|
||||||
|
req: MedusaRequest,
|
||||||
|
res: MedusaResponse,
|
||||||
|
next: NextFunction
|
||||||
|
) => Promise<void> {
|
||||||
|
return async (req: MedusaRequest, _: MedusaResponse, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const query = normalizeQuery(req)
|
||||||
|
const validated = await zodValidator(zodSchema, query, config)
|
||||||
|
const cnf = queryConfig.isList
|
||||||
|
? prepareListQuery(validated, queryConfig)
|
||||||
|
: prepareRetrieveQuery(validated, queryConfig)
|
||||||
|
|
||||||
|
req.validatedQuery = validated
|
||||||
|
req.filterableFields = getFilterableFields(req.validatedQuery)
|
||||||
|
req.remoteQueryConfig = cnf.remoteQueryConfig
|
||||||
|
req.listConfig = (cnf as any).listConfig
|
||||||
|
req.retrieveConfig = (cnf as any).retrieveConfig
|
||||||
|
|
||||||
|
next()
|
||||||
|
} catch (e) {
|
||||||
|
next(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
export const createSelectParams = () => {
|
||||||
|
return z.object({
|
||||||
|
fields: z.string().optional(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createFindParams = ({
|
||||||
|
offset,
|
||||||
|
limit,
|
||||||
|
order,
|
||||||
|
}: {
|
||||||
|
offset?: number
|
||||||
|
limit?: number
|
||||||
|
order?: string
|
||||||
|
} = {}) => {
|
||||||
|
const selectParams = createSelectParams()
|
||||||
|
|
||||||
|
return selectParams.merge(
|
||||||
|
z.object({
|
||||||
|
offset: z.preprocess(
|
||||||
|
(val) => {
|
||||||
|
if (val && typeof val === "string") {
|
||||||
|
return parseInt(val)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
},
|
||||||
|
z
|
||||||
|
.number()
|
||||||
|
.optional()
|
||||||
|
.default(offset ?? 0)
|
||||||
|
),
|
||||||
|
limit: z.preprocess(
|
||||||
|
(val) => {
|
||||||
|
if (val && typeof val === "string") {
|
||||||
|
return parseInt(val)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
},
|
||||||
|
z
|
||||||
|
.number()
|
||||||
|
.optional()
|
||||||
|
.default(limit ?? 20)
|
||||||
|
),
|
||||||
|
order: order
|
||||||
|
? z.string().optional().default(order)
|
||||||
|
: z.string().optional(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createOperatorMap = (type?: z.ZodType) => {
|
||||||
|
if (!type) {
|
||||||
|
type = z.string()
|
||||||
|
}
|
||||||
|
return z.object({
|
||||||
|
$eq: z.union([type, z.array(type)]).optional(),
|
||||||
|
$ne: z.union([type, z.array(type)]).optional(),
|
||||||
|
$in: z.array(type).optional(),
|
||||||
|
$nin: z.array(type).optional(),
|
||||||
|
$like: type.optional(),
|
||||||
|
$re: type.optional(),
|
||||||
|
$contains: type.optional(),
|
||||||
|
$gt: type.optional(),
|
||||||
|
$gte: type.optional(),
|
||||||
|
$lt: type.optional(),
|
||||||
|
$lte: type.optional(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in new issue
Block a user