Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Shouldn't this be client.productCategories.list?

options
)
return { ...data, ...rest } as const
}

export const useStoreProductCategory = (
riqwan marked this conversation as resolved.
Show resolved Hide resolved
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Shouldn't this be client.productCategories.retrieve

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