feat(medusa-js): added resources for product categories (#3157)

What:

JS client resources for product categories

Why:

To manage product categories and managing product-category relationship

How:

- adds a JS client resource for product categories.

RESOLVES CORE-1066
RESOLVES CORE-969
RESOLVES CORE-1061
This commit is contained in:
Riqwan Thamir
2023-02-01 17:52:39 +00:00
committed by GitHub
parent 4105405f28
commit be0d36432a
18 changed files with 390 additions and 72 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa-js": patch
---
feat(medusa-js): added resources for product categories
@@ -337,7 +337,7 @@ describe("/admin/product-categories", () => {
expect(response.status).toEqual(200)
expect(response.data.id).toEqual("invalid-id")
expect(response.data.deleted).toBeTruthy()
expect(response.data.object).toEqual("product_category")
expect(response.data.object).toEqual("product-category")
})
it("throws a not allowed error for a category with children", async () => {
@@ -366,7 +366,7 @@ describe("/admin/product-categories", () => {
expect(deleteResponse.status).toEqual(200)
expect(deleteResponse.data.id).toEqual(productCategory.id)
expect(deleteResponse.data.deleted).toBeTruthy()
expect(deleteResponse.data.object).toEqual("product_category")
expect(deleteResponse.data.object).toEqual("product-category")
const errorFetchingDeleted = await api.get(
`/admin/product-categories/${productCategory.id}`,
+3
View File
@@ -19,6 +19,7 @@ import ReturnReasonsResource from "./resources/return-reasons"
import ReturnsResource from "./resources/returns"
import ShippingOptionsResource from "./resources/shipping-options"
import SwapsResource from "./resources/swaps"
import ProductCategoriesResource from "./resources/product-categories"
class Medusa {
private client: Client
@@ -42,6 +43,7 @@ class Medusa {
public paymentMethods: PaymentMethodsResource
public paymentCollections: PaymentCollectionsResource
public productTags: ProductTagsResource
public productCategories: ProductCategoriesResource
constructor(config: Config) {
this.client = new Client(config)
@@ -66,6 +68,7 @@ class Medusa {
this.paymentMethods = new PaymentMethodsResource(this.client)
this.paymentCollections = new PaymentCollectionsResource(this.client)
this.productTags = new ProductTagsResource(this.client)
this.productCategories = new ProductCategoriesResource(this.client)
}
/**
@@ -33,6 +33,7 @@ import AdminUsersResource from "./users"
import AdminVariantsResource from "./variants"
import AdminPaymentCollectionsResource from "./payment-collections"
import AdminPaymentsResource from "./payments"
import AdminProductCategoriesResource from "./product-categories"
class Admin extends BaseResource {
public auth = new AdminAuthResource(this.client)
@@ -69,6 +70,7 @@ class Admin extends BaseResource {
public uploads = new AdminUploadsResource(this.client)
public paymentCollections = new AdminPaymentCollectionsResource(this.client)
public payments = new AdminPaymentsResource(this.client)
public productCategories = new AdminProductCategoriesResource(this.client)
}
export default Admin
@@ -0,0 +1,134 @@
import {
AdminDeleteProductCategoriesCategoryProductsBatchReq,
AdminGetProductCategoriesParams,
AdminPostProductCategoriesCategoryProductsBatchReq,
AdminPostProductCategoriesReq,
AdminPostProductCategoriesCategoryParams,
AdminProductCategoriesCategoryDeleteRes,
AdminProductCategoriesListRes,
AdminProductCategoriesCategoryRes,
AdminGetProductCategoryParams,
} from "@medusajs/medusa"
import qs from "qs"
import { ResponsePromise } from "../../typings"
import BaseResource from "../base"
class AdminProductCategoriesResource extends BaseResource {
/** retrieve a product category
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
* @description gets a product category
* @returns a medusa product category
*/
retrieve(
productCategoryId: string,
query?: AdminGetProductCategoryParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesCategoryRes> {
let path = `/admin/product-categories/${productCategoryId}`
if (query) {
const queryString = qs.stringify(query)
path = `${path}?${queryString}`
}
return this.client.request("GET", path, undefined, {}, customHeaders)
}
/* *
* Create a medusa product category
* @returns the created product category
*/
create(
payload: AdminPostProductCategoriesReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesCategoryRes> {
const path = `/admin/product-categories`
return this.client.request("POST", path, payload, {}, customHeaders)
}
/** update a product category
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
* @description updates a product category
* @returns the updated medusa product category
*/
update(
productCategoryId: string,
payload: AdminPostProductCategoriesCategoryParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesCategoryRes> {
const path = `/admin/product-categories/${productCategoryId}`
return this.client.request("POST", path, payload, {}, customHeaders)
}
/**
* Retrieve a list of product categorys
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
* @description Retrieve a list of product categorys
* @returns the list of product category as well as the pagination properties
*/
list(
query?: AdminGetProductCategoriesParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesListRes> {
let path = `/admin/product-categories`
if (query) {
const queryString = qs.stringify(query)
path += `?${queryString}`
}
return this.client.request("GET", path, undefined, {}, customHeaders)
}
/**
* Delete a product category
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
* @description gets a product category
* @returns an deletion result
*/
delete(
productCategoryId: string,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesCategoryDeleteRes> {
const path = `/admin/product-categories/${productCategoryId}`
return this.client.request("DELETE", path, undefined, {}, customHeaders)
}
/**
* Remove products from a product category
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
* @description Remove products from a product category
* @returns a medusa product category
*/
removeProducts(
productCategoryId: string,
payload: AdminDeleteProductCategoriesCategoryProductsBatchReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesCategoryRes> {
const path = `/admin/product-categories/${productCategoryId}/products/batch`
return this.client.request("DELETE", path, payload, {}, customHeaders)
}
/**
* Add products to a product category
* @experimental This feature is under development and may change in the future.
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
* @description Add products to a product category
* @returns a medusa product category
*/
addProducts(
productCategoryId: string,
payload: AdminPostProductCategoriesCategoryProductsBatchReq,
customHeaders: Record<string, any> = {}
): ResponsePromise<AdminProductCategoriesCategoryRes> {
const path = `/admin/product-categories/${productCategoryId}/products/batch`
return this.client.request("POST", path, payload, {}, customHeaders)
}
}
export default AdminProductCategoriesResource
@@ -0,0 +1,55 @@
import {
StoreGetProductCategoriesCategoryRes,
StoreProductCategoriesListRes,
StoreGetProductCategoriesParams,
StoreGetProductCategoryParams,
} from "@medusajs/medusa"
import qs from "qs"
import { ResponsePromise } from "../typings"
import BaseResource from "./base"
class ProductCategoriesResource extends BaseResource {
/**
* @description Retrieves a single product category
* @param {string} id - id of the product category
* @param {string} query is optional. Can contain a fields or relations for the returned list
* @param customHeaders
* @return {ResponsePromise<StoreGetProductCategoriesCategoryRes>}
*/
retrieve(
id: string,
query?: StoreGetProductCategoryParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<StoreGetProductCategoriesCategoryRes> {
let path = `/store/product-categories/${id}`
if (query) {
const queryString = qs.stringify(query)
path = `${path}?${queryString}`
}
return this.client.request("GET", path, undefined, {}, customHeaders)
}
/**
* @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>}
*/
list(
query?: StoreGetProductCategoriesParams,
customHeaders: Record<string, any> = {}
): ResponsePromise<StoreProductCategoriesListRes> {
let path = `/store/product-categories`
if (query) {
const queryString = qs.stringify(query)
path = `${path}?${queryString}`
}
return this.client.request("GET", path, undefined, {}, customHeaders)
}
}
export default ProductCategoriesResource
+2
View File
@@ -35,6 +35,7 @@ export * from "./routes/admin/orders"
export * from "./routes/admin/payment-collections"
export * from "./routes/admin/payments"
export * from "./routes/admin/price-lists"
export * from "./routes/admin/product-categories"
export * from "./routes/admin/product-tags"
export * from "./routes/admin/product-types"
export * from "./routes/admin/products"
@@ -61,6 +62,7 @@ export * from "./routes/store/gift-cards"
export * from "./routes/store/order-edits"
export * from "./routes/store/orders"
export * from "./routes/store/payment-collections"
export * from "./routes/store/product-categories"
export * from "./routes/store/product-tags"
export * from "./routes/store/product-types"
export * from "./routes/store/products"
@@ -24,7 +24,24 @@ import { FindParams } from "../../../../types/common"
* $ref: "#/components/schemas/AdminPostProductCategoriesCategoryProductsBatchReq"
* x-codegen:
* method: addProducts
* queryParams: AdminPostProductCategoriesCategoryProductsBatchParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.addProducts(product_category_id, {
* product_ids: [
* {
* id: product_id
* }
* ]
* })
* .then(({ product_category }) => {
* console.log(product_category.id);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -50,7 +67,7 @@ import { FindParams } from "../../../../types/common"
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/AdminProductCategoriesRes"
* $ref: "#/components/schemas/AdminProductCategoriesCategoryRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -80,7 +97,7 @@ export default async (req: Request, res: Response): Promise<void> => {
.withTransaction(transactionManager)
.addProducts(
id,
validatedBody.product_ids.map((p) => p.id),
validatedBody.product_ids.map((p) => p.id)
)
})
@@ -117,4 +134,5 @@ export class AdminPostProductCategoriesCategoryProductsBatchReq {
product_ids: ProductBatchProductCategory[]
}
// eslint-disable-next-line max-len
export class AdminPostProductCategoriesCategoryProductsBatchParams extends FindParams {}
@@ -22,7 +22,20 @@ import { FindParams } from "../../../../types/common"
* $ref: "#/components/schemas/AdminPostProductCategoriesReq"
* x-codegen:
* method: create
* queryParams: AdminPostProductCategoriesParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.create({
* name: 'App',
* })
* .then(({ product_category }) => {
* console.log(product_category.id);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -43,10 +56,7 @@ import { FindParams } from "../../../../types/common"
* content:
* application/json:
* schema:
* type: object
* properties:
* product_category:
* $ref: "#/components/schemas/ProductCategory"
* $ref: "#/components/schemas/AdminProductCategoriesCategoryRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -14,6 +14,16 @@ import { ProductCategoryService } from "../../../../services"
* x-codegen:
* method: delete
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.delete(product_category_id)
* .then(({ id, object, deleted }) => {
* console.log(id);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -25,24 +35,12 @@ import { ProductCategoryService } from "../../../../services"
* tags:
* - Product Category
* responses:
* 200:
* "200":
* description: OK
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: string
* description: The ID of the deleted product category.
* object:
* type: string
* description: The type of the object that was deleted.
* default: product_category
* deleted:
* type: boolean
* description: Whether the product category was deleted successfully or not.
* default: true
* $ref: "#/components/schemas/AdminProductCategoriesCategoryDeleteRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -74,7 +72,7 @@ export default async (req: Request, res: Response) => {
res.json({
id: id,
object: "product_category",
object: "product-category",
deleted: true,
})
}
@@ -26,6 +26,22 @@ import { FindParams } from "../../../../types/common"
* method: removeProducts
* queryParams: AdminDeleteProductCategoriesCategoryProductsBatchParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.removeProducts(product_category_id, {
* product_ids: [
* {
* id: product_id
* }
* ]
* })
* .then(({ product_category }) => {
* console.log(product_category.id);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -45,12 +61,12 @@ import { FindParams } from "../../../../types/common"
* tags:
* - Product Category
* responses:
* 200:
* "200":
* description: OK
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/AdminProductCategoriesRes"
* $ref: "#/components/schemas/AdminProductCategoriesCategoryRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -18,6 +18,16 @@ import { defaultAdminProductCategoryRelations } from "."
* method: retrieve
* queryParams: AdminGetProductCategoryParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.retrieve(product_category_id)
* .then(({ product_category }) => {
* console.log(product_category.id);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -34,10 +44,7 @@ import { defaultAdminProductCategoryRelations } from "."
* content:
* application/json:
* schema:
* type: object
* properties:
* product_category:
* $ref: "#/components/schemas/ProductCategory"
* $ref: "#/components/schemas/AdminProductCategoriesCategoryRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -5,6 +5,7 @@ import middlewares, {
transformBody,
} from "../../../middlewares"
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
import deleteProductCategory from "./delete-product-category"
import { validateProductsExist } from "../../../middlewares/validators/product-existence"
@@ -123,6 +124,9 @@ export * from "./get-product-category"
export * from "./delete-product-category"
export * from "./list-product-categories"
export * from "./create-product-category"
export * from "./update-product-category"
export * from "./add-products-batch"
export * from "./delete-products-batch"
export const defaultAdminProductCategoryRelations = [
"parent_category",
@@ -145,12 +149,52 @@ export const defaultProductCategoryFields = [
]
/**
* @schema AdminProductCategoriesRes
* @schema AdminProductCategoriesCategoryRes
* type: object
* properties:
* product_category:
* $ref: "#/components/schemas/ProductCategory"
*/
export type AdminProductCategoriesRes = {
export type AdminProductCategoriesCategoryRes = {
product_category: ProductCategory
}
/**
* @schema AdminProductCategoriesCategoryDeleteRes
* type: object
* properties:
* id:
* type: string
* description: The ID of the deleted product category
* object:
* type: string
* description: The type of the object that was deleted.
* default: product-category
* deleted:
* type: boolean
* description: Whether or not the items were deleted.
* default: true
*/
export type AdminProductCategoriesCategoryDeleteRes = DeleteResponse
/**
* @schema AdminProductCategoriesListRes
* type: object
* properties:
* product_categories:
* type: array
* items:
* $ref: "#/components/schemas/ProductCategory"
* count:
* type: integer
* description: The total number of items available
* offset:
* type: integer
* description: The number of items skipped before these items
* limit:
* type: integer
* description: The number of items per page
*/
export type AdminProductCategoriesListRes = PaginatedResponse & {
product_categories: ProductCategory[]
}
@@ -24,6 +24,16 @@ import { extendedFindParamsMixin } from "../../../../types/common"
* method: list
* queryParams: AdminGetProductCategoriesParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.list()
* .then(({ product_categories, limit, offset, count }) => {
* console.log(product_categories.length);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -40,21 +50,7 @@ import { extendedFindParamsMixin } from "../../../../types/common"
* content:
* application/json:
* schema:
* type: object
* properties:
* product_categories:
* type: array
* items:
* $ref: "#/components/schemas/ProductCategory"
* count:
* type: integer
* description: The total number of items available
* offset:
* type: integer
* description: The number of items skipped before these items
* limit:
* type: integer
* description: The number of items per page
* $ref: "#/components/schemas/AdminProductCategoriesListRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -25,6 +25,18 @@ import { FindParams } from "../../../../types/common"
* method: update
* queryParams: AdminPostProductCategoriesCategoryParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.update(product_category_id, {
* name: 'App'
* })
* .then(({ product_category }) => {
* console.log(product_category.id);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -45,10 +57,7 @@ import { FindParams } from "../../../../types/common"
* content:
* application/json:
* schema:
* type: object
* properties:
* product_category:
* $ref: "#/components/schemas/ProductCategory"
* $ref: "#/components/schemas/AdminProductCategoriesCategoryRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -45,7 +45,7 @@ import { defaultStoreScope } from "."
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/StoreGetProductCategoryRes"
* $ref: "#/components/schemas/StoreGetProductCategoriesCategoryRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
@@ -1,14 +1,16 @@
import { Router } from "express"
import middlewares, { transformQuery } from "../../../middlewares"
import getProductCategory, {
StoreGetProductCategoryParams,
} from "./get-product-category"
import { ProductCategory } from "../../../../models"
import { PaginatedResponse } from "../../../../types/common"
import listProductCategories, {
StoreGetProductCategoriesParams,
} from "./list-product-categories"
import getProductCategory, {
StoreGetProductCategoryParams,
} from "./get-product-category"
const route = Router()
export default (app) => {
@@ -68,15 +70,37 @@ export const allowedStoreProductCategoryFields = [
]
/**
* @schema StoreGetProductCategoryRes
* @schema StoreGetProductCategoriesCategoryRes
* type: object
* properties:
* product_category:
* $ref: "#/components/schemas/ProductCategory"
*/
export type StoreGetProductCategoryRes = {
export type StoreGetProductCategoriesCategoryRes = {
product_category: ProductCategory
}
/**
* @schema StoreProductCategoriesListRes
* type: object
* properties:
* product_categories:
* type: array
* items:
* $ref: "#/components/schemas/ProductCategory"
* count:
* type: integer
* description: The total number of items available
* offset:
* type: integer
* description: The number of items skipped before these items
* limit:
* type: integer
* description: The number of items per page
*/
export type StoreProductCategoriesListRes = PaginatedResponse & {
product_categories: ProductCategory[]
}
export * from "./get-product-category"
export * from "./list-product-categories"
@@ -21,6 +21,15 @@ import { defaultStoreScope } from "."
* method: list
* queryParams: StoreGetProductCategoriesParams
* x-codeSamples:
* - lang: JavaScript
* label: JS Client
* source: |
* import Medusa from "@medusajs/medusa-js"
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* medusa.productCategories.list()
* .then(({ product_categories, limit, offset, count }) => {
* console.log(product_categories.length);
* });
* - lang: Shell
* label: cURL
* source: |
@@ -32,26 +41,12 @@ import { defaultStoreScope } from "."
* tags:
* - Product Category
* responses:
* 200:
* "200":
* description: OK
* content:
* application/json:
* schema:
* type: object
* properties:
* product_categories:
* type: array
* items:
* $ref: "#/components/schemas/ProductCategory"
* count:
* type: integer
* description: The total number of items available
* offset:
* type: integer
* description: The number of items skipped before these items
* limit:
* type: integer
* description: The number of items per page
* $ref: "#/components/schemas/StoreProductCategoriesListRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":