Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
593 additions
and
354 deletions.
There are no files selected for viewing
646 changes: 328 additions & 318 deletions
646
integration-tests/api/__tests__/store/product-category.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/medusa/src/api-v2/store/product-categories/[id]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { StoreProductCategoryResponse } from "@medusajs/types" | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../types/routing" | ||
import { refetchCategory } from "../helpers" | ||
import { StoreProductCategoryParamsType } from "../validators" | ||
import { MedusaError } from "@medusajs/utils" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest<StoreProductCategoryParamsType>, | ||
res: MedusaResponse<StoreProductCategoryResponse> | ||
) => { | ||
const category = await refetchCategory( | ||
req.params.id, | ||
req.scope, | ||
req.remoteQueryConfig.fields, | ||
req.filterableFields | ||
) | ||
|
||
if (!category) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_FOUND, | ||
`Product category with id: ${req.params.id} was not found` | ||
) | ||
} | ||
res.json({ product_category: category }) | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/medusa/src/api-v2/store/product-categories/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MedusaContainer } from "@medusajs/types" | ||
import { | ||
ContainerRegistrationKeys, | ||
remoteQueryObjectFromString, | ||
} from "@medusajs/utils" | ||
|
||
export const refetchCategory = async ( | ||
categoryId: string, | ||
scope: MedusaContainer, | ||
fields: string[], | ||
filterableFields: Record<string, any> = {} | ||
) => { | ||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) | ||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "product_category", | ||
variables: { | ||
filters: { ...filterableFields, id: categoryId }, | ||
}, | ||
fields: fields, | ||
}) | ||
|
||
const categories = await remoteQuery(queryObject) | ||
return categories[0] | ||
} | ||
|
||
export const applyCategoryFilters = (req, res, next) => { | ||
if (!req.filterableFields) { | ||
req.filterableFields = {} | ||
} | ||
|
||
req.filterableFields = { | ||
...req.filterableFields, | ||
is_active: true, | ||
is_internal: false, | ||
} | ||
|
||
next() | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/medusa/src/api-v2/store/product-categories/middlewares.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" | ||
import { validateAndTransformQuery } from "../../utils/validate-query" | ||
import { applyCategoryFilters } from "./helpers" | ||
import * as QueryConfig from "./query-config" | ||
import { | ||
StoreProductCategoriesParams, | ||
StoreProductCategoryParams, | ||
} from "./validators" | ||
|
||
export const storeProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
method: ["GET"], | ||
matcher: "/store/product-categories", | ||
middlewares: [ | ||
validateAndTransformQuery( | ||
StoreProductCategoriesParams, | ||
QueryConfig.listProductCategoryConfig | ||
), | ||
applyCategoryFilters, | ||
], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/store/product-categories/:id", | ||
middlewares: [ | ||
validateAndTransformQuery( | ||
StoreProductCategoryParams, | ||
QueryConfig.retrieveProductCategoryConfig | ||
), | ||
applyCategoryFilters, | ||
], | ||
}, | ||
] |
24 changes: 24 additions & 0 deletions
24
packages/medusa/src/api-v2/store/product-categories/query-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const defaults = [ | ||
"id", | ||
"name", | ||
"description", | ||
"handle", | ||
"rank", | ||
"parent_category_id", | ||
"created_at", | ||
"updated_at", | ||
"metadata", | ||
"*parent_category", | ||
"*category_children", | ||
] | ||
|
||
export const retrieveProductCategoryConfig = { | ||
defaults, | ||
isList: false, | ||
} | ||
|
||
export const listProductCategoryConfig = { | ||
defaults, | ||
defaultLimit: 50, | ||
isList: true, | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/medusa/src/api-v2/store/product-categories/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { StoreProductCategoryListResponse } from "@medusajs/types" | ||
import { | ||
ContainerRegistrationKeys, | ||
remoteQueryObjectFromString, | ||
} from "@medusajs/utils" | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../types/routing" | ||
import { StoreProductCategoriesParamsType } from "./validators" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest<StoreProductCategoriesParamsType>, | ||
res: MedusaResponse<StoreProductCategoryListResponse> | ||
) => { | ||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) | ||
|
||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "product_category", | ||
variables: { | ||
filters: req.filterableFields, | ||
...req.remoteQueryConfig.pagination, | ||
}, | ||
fields: req.remoteQueryConfig.fields, | ||
}) | ||
|
||
const { rows: product_categories, metadata } = await remoteQuery(queryObject) | ||
|
||
res.json({ | ||
product_categories, | ||
count: metadata.count, | ||
offset: metadata.skip, | ||
limit: metadata.take, | ||
}) | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/medusa/src/api-v2/store/product-categories/validators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { z } from "zod" | ||
import { optionalBooleanMapper } from "../../../utils/validators/is-boolean" | ||
import { | ||
createFindParams, | ||
createOperatorMap, | ||
createSelectParams, | ||
} from "../../utils/validators" | ||
|
||
export type StoreProductCategoryParamsType = z.infer< | ||
typeof StoreProductCategoryParams | ||
> | ||
export const StoreProductCategoryParams = createSelectParams().merge( | ||
z.object({ | ||
include_ancestors_tree: z.preprocess( | ||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()), | ||
z.boolean().optional() | ||
), | ||
include_descendants_tree: z.preprocess( | ||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()), | ||
z.boolean().optional() | ||
), | ||
}) | ||
) | ||
|
||
export type StoreProductCategoriesParamsType = z.infer< | ||
typeof StoreProductCategoriesParams | ||
> | ||
export const StoreProductCategoriesParams = createFindParams({ | ||
offset: 0, | ||
limit: 50, | ||
}).merge( | ||
z.object({ | ||
q: z.string().optional(), | ||
id: z.union([z.string(), z.array(z.string())]).optional(), | ||
description: z.union([z.string(), z.array(z.string())]).optional(), | ||
handle: z.union([z.string(), z.array(z.string())]).optional(), | ||
parent_category_id: z.union([z.string(), z.array(z.string())]).optional(), | ||
include_ancestors_tree: z.preprocess( | ||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()), | ||
z.boolean().optional() | ||
), | ||
include_descendants_tree: z.preprocess( | ||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()), | ||
z.boolean().optional() | ||
), | ||
created_at: createOperatorMap().optional(), | ||
updated_at: createOperatorMap().optional(), | ||
deleted_at: createOperatorMap().optional(), | ||
$and: z.lazy(() => StoreProductCategoriesParams.array()).optional(), | ||
$or: z.lazy(() => StoreProductCategoriesParams.array()).optional(), | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PaginatedResponse } from "../../common" | ||
import { ProductCategoryResponse } from "./common" | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export interface AdminProductCategoryResponse { | ||
product_category: ProductCategoryResponse | ||
} | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export interface AdminProductCategoryListResponse extends PaginatedResponse { | ||
product_categories: ProductCategoryResponse[] | ||
} |
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
packages/types/src/http/product-category/admin/product-category.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @experimental | ||
*/ | ||
export interface ProductCategoryResponse { | ||
id: string | ||
name: string | ||
description: string | null | ||
handle: string | null | ||
is_active: boolean | ||
is_internal: boolean | ||
rank: number | null | ||
parent_category_id: string | null | ||
created_at: string | Date | ||
updated_at: string | Date | ||
|
||
parent_category: ProductCategoryResponse | ||
category_children: ProductCategoryResponse[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./admin" | ||
export * from "./store" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PaginatedResponse } from "../../common" | ||
import { ProductCategoryResponse } from "./common" | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export interface StoreProductCategoryResponse { | ||
product_category: ProductCategoryResponse | ||
} | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export interface StoreProductCategoryListResponse extends PaginatedResponse { | ||
product_categories: ProductCategoryResponse[] | ||
} |