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 authored and GitHub committed 2023-03-10 10:14:48 +00:00
1 parent 966aea65c2
commit 9ba09ba4d7
7 files changed
+74 -13

No files matched your search

+7
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
@@ -1,8 +1,8 @@
import { import {
StoreGetProductCategoriesCategoryRes,
StoreProductCategoriesListRes,
StoreGetProductCategoriesParams, StoreGetProductCategoriesParams,
StoreGetProductCategoryParams, StoreGetProductCategoriesRes,
StoreGetProductCategoriesCategoryParams,
StoreGetProductCategoriesCategoryRes,
} from "@medusajs/medusa" } from "@medusajs/medusa"
import qs from "qs" import qs from "qs"
import { ResponsePromise } from "../typings" import { ResponsePromise } from "../typings"
@@ -18,7 +18,7 @@ class ProductCategoriesResource extends BaseResource {
*/ */
retrieve( retrieve(
id: string, id: string,
query?: StoreGetProductCategoryParams, query?: StoreGetProductCategoriesCategoryParams,
customHeaders: Record<string, any> = {} customHeaders: Record<string, any> = {}
): ResponsePromise<StoreGetProductCategoriesCategoryRes> { ): ResponsePromise<StoreGetProductCategoriesCategoryRes> {
let path = `/store/product-categories/${id}` let path = `/store/product-categories/${id}`
@@ -35,12 +35,12 @@ class ProductCategoriesResource extends BaseResource {
* @description Retrieves a list of product categories * @description Retrieves a list of product categories
* @param {string} query is optional. Can contain a limit and offset for the returned list * @param {string} query is optional. Can contain a limit and offset for the returned list
* @param customHeaders * @param customHeaders
* @return {ResponsePromise<StoreProductCategoriesListRes>} * @return {ResponsePromise<StoreGetProductCategoriesRes>}
*/ */
list( list(
query?: StoreGetProductCategoriesParams, query?: StoreGetProductCategoriesParams,
customHeaders: Record<string, any> = {} customHeaders: Record<string, any> = {}
): ResponsePromise<StoreProductCategoriesListRes> { ): ResponsePromise<StoreGetProductCategoriesRes> {
let path = `/store/product-categories` let path = `/store/product-categories`
if (query) { if (query) {
Whitespace-only changes.
@@ -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
}
@@ -17,7 +17,7 @@ import { defaultStoreScope } from "."
* - (query) fields {string} (Comma separated) Which fields should be retrieved in each product category. * - (query) fields {string} (Comma separated) Which fields should be retrieved in each product category.
* x-codegen: * x-codegen:
* method: retrieve * method: retrieve
* queryParams: StoreGetProductCategoryParams * queryParams: StoreGetProductCategoriesCategoryParams
* x-codeSamples: * x-codeSamples:
* - lang: JavaScript * - lang: JavaScript
* label: JS Client * label: JS Client
@@ -85,4 +85,4 @@ export default async (req: Request, res: Response) => {
}) })
} }
export class StoreGetProductCategoryParams extends FindParams {} export class StoreGetProductCategoriesCategoryParams extends FindParams {}
@@ -8,7 +8,7 @@ import listProductCategories, {
} from "./list-product-categories" } from "./list-product-categories"
import getProductCategory, { import getProductCategory, {
StoreGetProductCategoryParams, StoreGetProductCategoriesCategoryParams,
} from "./get-product-category" } from "./get-product-category"
const route = Router() const route = Router()
@@ -29,7 +29,7 @@ export default (app) => {
route.get( route.get(
"/:id", "/:id",
transformStoreQuery(StoreGetProductCategoryParams, { transformStoreQuery(StoreGetProductCategoriesCategoryParams, {
defaultFields: defaultStoreProductCategoryFields, defaultFields: defaultStoreProductCategoryFields,
allowedFields: allowedStoreProductCategoryFields, allowedFields: allowedStoreProductCategoryFields,
defaultRelations: defaultStoreProductCategoryRelations, defaultRelations: defaultStoreProductCategoryRelations,
@@ -85,7 +85,7 @@ export type StoreGetProductCategoriesCategoryRes = {
} }
/** /**
* @schema StoreProductCategoriesListRes * @schema StoreGetProductCategoriesRes
* type: object * type: object
* required: * required:
* - product_categories * - product_categories
@@ -107,7 +107,7 @@ export type StoreGetProductCategoriesCategoryRes = {
* type: integer * type: integer
* description: The number of items per page * description: The number of items per page
*/ */
export type StoreProductCategoriesListRes = PaginatedResponse & { export type StoreGetProductCategoriesRes = PaginatedResponse & {
product_categories: ProductCategory[] product_categories: ProductCategory[]
} }
@@ -48,7 +48,7 @@ import { defaultStoreScope } from "."
* content: * content:
* application/json: * application/json:
* schema: * schema:
* $ref: "#/components/schemas/StoreProductCategoriesListRes" * $ref: "#/components/schemas/StoreGetProductCategoriesRes"
* "400": * "400":
* $ref: "#/components/responses/400_error" * $ref: "#/components/responses/400_error"
* "401": * "401":