From 0ba580c1605df2e787a8b86b226d951f514b4a15 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 9 Mar 2023 17:33:30 +0100 Subject: [PATCH 1/2] chore: Add store queries to react medusa --- .changeset/neat-days-brush.md | 7 +++ .../src/resources/product-categories.ts | 12 ++--- .../hooks/store/product-categories/index.ts | 0 .../hooks/store/product-categories/queries.ts | 54 +++++++++++++++++++ .../get-product-category.ts | 4 +- .../routes/store/product-categories/index.ts | 8 +-- .../list-product-categories.ts | 2 +- 7 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 .changeset/neat-days-brush.md create mode 100644 packages/medusa-react/src/hooks/store/product-categories/index.ts create mode 100644 packages/medusa-react/src/hooks/store/product-categories/queries.ts diff --git a/.changeset/neat-days-brush.md b/.changeset/neat-days-brush.md new file mode 100644 index 0000000000000..910fc445c6e95 --- /dev/null +++ b/.changeset/neat-days-brush.md @@ -0,0 +1,7 @@ +--- +"medusa-react": patch +"@medusajs/medusa-js": patch +"@medusajs/medusa": patch +--- + +feat(medusa, medusa-js, medusa-react): Add store queries to react medusa diff --git a/packages/medusa-js/src/resources/product-categories.ts b/packages/medusa-js/src/resources/product-categories.ts index 2766a337cecc2..7362278f58804 100644 --- a/packages/medusa-js/src/resources/product-categories.ts +++ b/packages/medusa-js/src/resources/product-categories.ts @@ -1,8 +1,8 @@ import { - StoreGetProductCategoriesCategoryRes, - StoreProductCategoriesListRes, StoreGetProductCategoriesParams, - StoreGetProductCategoryParams, + StoreGetProductCategoriesRes, + StoreGetProductCategoriesCategoryParams, + StoreGetProductCategoriesCategoryRes, } from "@medusajs/medusa" import qs from "qs" import { ResponsePromise } from "../typings" @@ -18,7 +18,7 @@ class ProductCategoriesResource extends BaseResource { */ retrieve( id: string, - query?: StoreGetProductCategoryParams, + query?: StoreGetProductCategoriesCategoryParams, customHeaders: Record = {} ): ResponsePromise { let path = `/store/product-categories/${id}` @@ -35,12 +35,12 @@ class ProductCategoriesResource extends BaseResource { * @description Retrieves a list of product categories * @param {string} query is optional. Can contain a limit and offset for the returned list * @param customHeaders - * @return {ResponsePromise} + * @return {ResponsePromise} */ list( query?: StoreGetProductCategoriesParams, customHeaders: Record = {} - ): ResponsePromise { + ): ResponsePromise { let path = `/store/product-categories` if (query) { diff --git a/packages/medusa-react/src/hooks/store/product-categories/index.ts b/packages/medusa-react/src/hooks/store/product-categories/index.ts new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/medusa-react/src/hooks/store/product-categories/queries.ts b/packages/medusa-react/src/hooks/store/product-categories/queries.ts new file mode 100644 index 0000000000000..07d6acad2c20f --- /dev/null +++ b/packages/medusa-react/src/hooks/store/product-categories/queries.ts @@ -0,0 +1,54 @@ +import { + StoreGetProductCategoriesParams, + StoreGetProductCategoriesRes, + StoreGetProductCategoriesCategoryParams, + StoreGetProductCategoriesCategoryRes, +} from "@medusajs/medusa" +import { Response } from "@medusajs/medusa-js" +import { useQuery } from "@tanstack/react-query" + +import { useMedusa } from "../../../contexts" +import { UseQueryOptionsWrapper } from "../../../types" +import { queryKeysFactory } from "../../utils" + +const STORE_PRODUCT_CATEGORIES_QUERY_KEY = `product_categories` as const +export const storeProductCategoryKeys = queryKeysFactory( + STORE_PRODUCT_CATEGORIES_QUERY_KEY +) +type ProductCategoryQueryKeys = typeof storeProductCategoryKeys + +export const useStoreProductCategories = ( + query?: StoreGetProductCategoriesParams, + options?: UseQueryOptionsWrapper< + Response, + Error, + ReturnType + > +) => { + const { client } = useMedusa() + const { data, ...rest } = useQuery( + storeProductCategoryKeys.list(query), + () => client.store.productCategories.list(query), + options + ) + return { ...data, ...rest } as const +} + +export const useStoreProductCategory = ( + id: string, + query?: StoreGetProductCategoriesCategoryParams, + options?: UseQueryOptionsWrapper< + Response, + Error, + ReturnType + > +) => { + const { client } = useMedusa() + const { data, ...rest } = useQuery( + storeProductCategoryKeys.detail(id), + () => client.store.productCategories.retrieve(id, query), + options + ) + + return { ...data, ...rest } as const +} diff --git a/packages/medusa/src/api/routes/store/product-categories/get-product-category.ts b/packages/medusa/src/api/routes/store/product-categories/get-product-category.ts index ce22f4d0aafea..ff978bda7f00c 100644 --- a/packages/medusa/src/api/routes/store/product-categories/get-product-category.ts +++ b/packages/medusa/src/api/routes/store/product-categories/get-product-category.ts @@ -17,7 +17,7 @@ import { defaultStoreScope } from "." * - (query) fields {string} (Comma separated) Which fields should be retrieved in each product category. * x-codegen: * method: retrieve - * queryParams: StoreGetProductCategoryParams + * queryParams: StoreGetProductCategoriesCategoryParams * x-codeSamples: * - lang: JavaScript * label: JS Client @@ -85,4 +85,4 @@ export default async (req: Request, res: Response) => { }) } -export class StoreGetProductCategoryParams extends FindParams {} +export class StoreGetProductCategoriesCategoryParams extends FindParams {} diff --git a/packages/medusa/src/api/routes/store/product-categories/index.ts b/packages/medusa/src/api/routes/store/product-categories/index.ts index 01c9b84948ddb..d91a1a4d16aec 100644 --- a/packages/medusa/src/api/routes/store/product-categories/index.ts +++ b/packages/medusa/src/api/routes/store/product-categories/index.ts @@ -8,7 +8,7 @@ import listProductCategories, { } from "./list-product-categories" import getProductCategory, { - StoreGetProductCategoryParams, + StoreGetProductCategoriesCategoryParams, } from "./get-product-category" const route = Router() @@ -29,7 +29,7 @@ export default (app) => { route.get( "/:id", - transformStoreQuery(StoreGetProductCategoryParams, { + transformStoreQuery(StoreGetProductCategoriesCategoryParams, { defaultFields: defaultStoreProductCategoryFields, allowedFields: allowedStoreProductCategoryFields, defaultRelations: defaultStoreProductCategoryRelations, @@ -85,7 +85,7 @@ export type StoreGetProductCategoriesCategoryRes = { } /** - * @schema StoreProductCategoriesListRes + * @schema StoreGetProductCategoriesRes * type: object * required: * - product_categories @@ -107,7 +107,7 @@ export type StoreGetProductCategoriesCategoryRes = { * type: integer * description: The number of items per page */ -export type StoreProductCategoriesListRes = PaginatedResponse & { +export type StoreGetProductCategoriesRes = PaginatedResponse & { product_categories: ProductCategory[] } diff --git a/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts b/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts index 1dd02e8b9d75b..490a8eed53550 100644 --- a/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts +++ b/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts @@ -48,7 +48,7 @@ import { defaultStoreScope } from "." * content: * application/json: * schema: - * $ref: "#/components/schemas/StoreProductCategoriesListRes" + * $ref: "#/components/schemas/StoreGetProductCategoriesRes" * "400": * $ref: "#/components/responses/400_error" * "401": From 2efc47bcb4f67e92b908e46439b468de29483203 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Fri, 10 Mar 2023 10:14:59 +0100 Subject: [PATCH 2/2] chore: Fix scoping issues mentioned in PR review Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> --- .../src/hooks/store/product-categories/queries.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/medusa-react/src/hooks/store/product-categories/queries.ts b/packages/medusa-react/src/hooks/store/product-categories/queries.ts index 07d6acad2c20f..26339dbbc6c45 100644 --- a/packages/medusa-react/src/hooks/store/product-categories/queries.ts +++ b/packages/medusa-react/src/hooks/store/product-categories/queries.ts @@ -17,7 +17,7 @@ export const storeProductCategoryKeys = queryKeysFactory( ) type ProductCategoryQueryKeys = typeof storeProductCategoryKeys -export const useStoreProductCategories = ( +export const useProductCategories = ( query?: StoreGetProductCategoriesParams, options?: UseQueryOptionsWrapper< Response, @@ -28,13 +28,13 @@ export const useStoreProductCategories = ( const { client } = useMedusa() const { data, ...rest } = useQuery( storeProductCategoryKeys.list(query), - () => client.store.productCategories.list(query), + () => client.productCategories.list(query), options ) return { ...data, ...rest } as const } -export const useStoreProductCategory = ( +export const useProductCategory = ( id: string, query?: StoreGetProductCategoriesCategoryParams, options?: UseQueryOptionsWrapper< @@ -46,7 +46,7 @@ export const useStoreProductCategory = ( const { client } = useMedusa() const { data, ...rest } = useQuery( storeProductCategoryKeys.detail(id), - () => client.store.productCategories.retrieve(id, query), + () => client.productCategories.retrieve(id, query), options )