feat(medusa, medusa-js, medusa-react): Add store queries to react medusa (#3436)

What:

- Adds queries to medusa react for core
- Fix naming issues in types

RESOLVES CORE-1131
This commit is contained in:
Riqwan Thamir
2023-03-10 11:14:48 +01:00
committed by GitHub
parent 966aea65c2
commit 9ba09ba4d7
7 changed files with 74 additions and 13 deletions

View File

@@ -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

View File

@@ -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<string, any> = {}
): ResponsePromise<StoreGetProductCategoriesCategoryRes> {
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<StoreProductCategoriesListRes>}
* @return {ResponsePromise<StoreGetProductCategoriesRes>}
*/
list(
query?: StoreGetProductCategoriesParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<StoreProductCategoriesListRes> {
): ResponsePromise<StoreGetProductCategoriesRes> {
let path = `/store/product-categories`
if (query) {

View File

@@ -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 useProductCategories = (
query?: StoreGetProductCategoriesParams,
options?: UseQueryOptionsWrapper<
Response<StoreGetProductCategoriesRes>,
Error,
ReturnType<ProductCategoryQueryKeys["list"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
storeProductCategoryKeys.list(query),
() => client.productCategories.list(query),
options
)
return { ...data, ...rest } as const
}
export const useProductCategory = (
id: string,
query?: StoreGetProductCategoriesCategoryParams,
options?: UseQueryOptionsWrapper<
Response<StoreGetProductCategoriesCategoryRes>,
Error,
ReturnType<ProductCategoryQueryKeys["detail"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
storeProductCategoryKeys.detail(id),
() => client.productCategories.retrieve(id, query),
options
)
return { ...data, ...rest } as const
}

View File

@@ -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 {}

View File

@@ -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[]
}

View File

@@ -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":