Skip to content

Commit

Permalink
chore: Add store queries to react medusa
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Mar 9, 2023
1 parent cdbc5ff commit 0ba580c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changeset/neat-days-brush.md
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions packages/medusa-js/src/resources/product-categories.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
StoreGetProductCategoriesCategoryRes,
StoreProductCategoriesListRes,
StoreGetProductCategoriesParams,
StoreGetProductCategoryParams,
StoreGetProductCategoriesRes,
StoreGetProductCategoriesCategoryParams,
StoreGetProductCategoriesCategoryRes,
} from "@medusajs/medusa"
import qs from "qs"
import { ResponsePromise } from "../typings"
Expand All @@ -18,7 +18,7 @@ class ProductCategoriesResource extends BaseResource {
*/
retrieve(
id: string,
query?: StoreGetProductCategoryParams,
query?: StoreGetProductCategoriesCategoryParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<StoreGetProductCategoriesCategoryRes> {
let path = `/store/product-categories/${id}`
Expand All @@ -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<StoreProductCategoriesListRes>}
* @return {ResponsePromise<StoreGetProductCategoriesRes>}
*/
list(
query?: StoreGetProductCategoriesParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<StoreProductCategoriesListRes> {
): ResponsePromise<StoreGetProductCategoriesRes> {
let path = `/store/product-categories`

if (query) {
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -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<StoreGetProductCategoriesRes>,
Error,
ReturnType<ProductCategoryQueryKeys["list"]>
>
) => {
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<StoreGetProductCategoriesCategoryRes>,
Error,
ReturnType<ProductCategoryQueryKeys["detail"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
storeProductCategoryKeys.detail(id),
() => client.store.productCategories.retrieve(id, query),
options
)

return { ...data, ...rest } as const
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,4 +85,4 @@ export default async (req: Request, res: Response) => {
})
}

export class StoreGetProductCategoryParams extends FindParams {}
export class StoreGetProductCategoriesCategoryParams extends FindParams {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import listProductCategories, {
} from "./list-product-categories"

import getProductCategory, {
StoreGetProductCategoryParams,
StoreGetProductCategoriesCategoryParams,
} from "./get-product-category"

const route = Router()
Expand All @@ -29,7 +29,7 @@ export default (app) => {

route.get(
"/:id",
transformStoreQuery(StoreGetProductCategoryParams, {
transformStoreQuery(StoreGetProductCategoriesCategoryParams, {
defaultFields: defaultStoreProductCategoryFields,
allowedFields: allowedStoreProductCategoryFields,
defaultRelations: defaultStoreProductCategoryRelations,
Expand Down Expand Up @@ -85,7 +85,7 @@ export type StoreGetProductCategoriesCategoryRes = {
}

/**
* @schema StoreProductCategoriesListRes
* @schema StoreGetProductCategoriesRes
* type: object
* required:
* - product_categories
Expand All @@ -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[]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 0ba580c

Please sign in to comment.