Feat: Client admin products (#930)
* add exports in product * add product resource * fix failling tests * import reflect metadata Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import AdminDraftOrdersResource from "./draft-orders"
|
||||
import AdminGiftCardsResource from "./gift-cards"
|
||||
import AdminInvitesResource from "./invites"
|
||||
import AdminNotesResource from "./notes"
|
||||
import AdminProductsResource from "./products"
|
||||
import AdminUsersResource from "./users"
|
||||
import AdminReturnsResource from "./returns"
|
||||
import AdminOrdersResource from "./orders"
|
||||
@@ -27,6 +28,7 @@ class Admin extends BaseResource {
|
||||
public giftCards = new AdminGiftCardsResource(this.client)
|
||||
public invites = new AdminInvitesResource(this.client)
|
||||
public notes = new AdminNotesResource(this.client)
|
||||
public products = new AdminProductsResource(this.client)
|
||||
public users = new AdminUsersResource(this.client)
|
||||
public returns = new AdminReturnsResource(this.client)
|
||||
public orders = new AdminOrdersResource(this.client)
|
||||
|
||||
133
packages/medusa-js/src/resources/admin/products.ts
Normal file
133
packages/medusa-js/src/resources/admin/products.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
import {
|
||||
AdminGetNotesParams,
|
||||
AdminNotesDeleteRes,
|
||||
AdminNotesListRes,
|
||||
AdminNotesRes,
|
||||
AdminPostNotesNoteReq,
|
||||
AdminPostNotesReq,
|
||||
AdminPostProductsProductReq,
|
||||
AdminPostProductsReq,
|
||||
AdminProductsListTypesRes,
|
||||
AdminProductsListTagsRes,
|
||||
AdminProductsRes,
|
||||
AdminPostProductsProductVariantsReq,
|
||||
AdminPostProductsProductVariantsVariantReq,
|
||||
AdminPostProductsProductOptionsReq,
|
||||
AdminPostProductsProductOptionsOption,
|
||||
AdminProductsDeleteVariantRes,
|
||||
AdminProductsDeleteRes,
|
||||
AdminProductsDeleteOptionRes,
|
||||
AdminPostProductsProductMetadataReq,
|
||||
AdminGetProductsParams,
|
||||
AdminProductsListRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { ResponsePromise } from "../../typings"
|
||||
import BaseResource from "../base"
|
||||
|
||||
class AdminProductsResource extends BaseResource {
|
||||
create(payload: AdminPostProductsReq): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
retrieve(id: string): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
update(
|
||||
id: string,
|
||||
payload: AdminPostProductsProductReq
|
||||
): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
delete(id: string): ResponsePromise<AdminProductsDeleteRes> {
|
||||
const path = `/admin/products/${id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
list(query: AdminGetProductsParams): ResponsePromise<AdminProductsListRes> {
|
||||
let path = `/admin/products`
|
||||
|
||||
if (query) {
|
||||
const queryString = Object.entries(query).map(([key, value]) => {
|
||||
return typeof value !== "undefined" ? `${key}=${value}` : ""
|
||||
})
|
||||
path = `/admin/products?${queryString.join("&")}`
|
||||
}
|
||||
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
listTypes(): ResponsePromise<AdminProductsListTypesRes> {
|
||||
const path = `/admin/products/types`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
listTags(): ResponsePromise<AdminProductsListTagsRes> {
|
||||
const path = `/admin/products/tag-usage`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
setMetadata(
|
||||
id: string,
|
||||
payload: AdminPostProductsProductMetadataReq
|
||||
): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}/metadata`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
createVariant(
|
||||
id: string,
|
||||
payload: AdminPostProductsProductVariantsReq
|
||||
): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}/variants`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
updateVariant(
|
||||
id: string,
|
||||
variantId: string,
|
||||
payload: AdminPostProductsProductVariantsVariantReq
|
||||
): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}/variants/${variantId}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
deleteVariant(
|
||||
id: string,
|
||||
variantId: string
|
||||
): ResponsePromise<AdminProductsDeleteVariantRes> {
|
||||
const path = `/admin/products/${id}/variants/${variantId}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
addOption(
|
||||
id: string,
|
||||
payload: AdminPostProductsProductOptionsReq
|
||||
): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}/options`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
updateOption(
|
||||
id: string,
|
||||
optionId: string,
|
||||
payload: AdminPostProductsProductOptionsOption
|
||||
): ResponsePromise<AdminProductsRes> {
|
||||
const path = `/admin/products/${id}/options/${optionId}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
deleteOption(
|
||||
id: string,
|
||||
optionId: string
|
||||
): ResponsePromise<AdminProductsDeleteOptionRes> {
|
||||
const path = `/admin/products/${id}/options/${optionId}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminProductsResource
|
||||
@@ -27,6 +27,7 @@ export * from "./routes/admin/notes"
|
||||
export * from "./routes/admin/notifications"
|
||||
export * from "./routes/admin/shipping-profiles"
|
||||
export * from "./routes/admin/store"
|
||||
export * from "./routes/admin/products"
|
||||
export * from "./routes/admin/users"
|
||||
export * from "./routes/admin/orders"
|
||||
export * from "./routes/admin/variants"
|
||||
|
||||
@@ -52,7 +52,7 @@ export default async (req, res) => {
|
||||
res.json({ product })
|
||||
}
|
||||
|
||||
class AdminPostProductsProductOptionsReq {
|
||||
export class AdminPostProductsProductOptionsReq {
|
||||
@IsString()
|
||||
title: string
|
||||
}
|
||||
|
||||
@@ -12,16 +12,13 @@ import {
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
import {
|
||||
defaultAdminProductFields,
|
||||
defaultAdminProductRelations,
|
||||
ProductStatus,
|
||||
} from "."
|
||||
import { defaultAdminProductFields, defaultAdminProductRelations } from "."
|
||||
import {
|
||||
ProductService,
|
||||
ProductVariantService,
|
||||
ShippingProfileService,
|
||||
} from "../../../../services"
|
||||
import { ProductStatus } from "../../../../types/product"
|
||||
import { XorConstraint } from "../../../../types/validators/xor"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Router } from "express"
|
||||
import { Product } from "../../../.."
|
||||
import { Product, ProductTag, ProductType } from "../../../.."
|
||||
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares from "../../../middlewares"
|
||||
import "reflect-metadata"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -128,13 +129,6 @@ export const allowedAdminProductRelations = [
|
||||
"collection",
|
||||
]
|
||||
|
||||
export enum ProductStatus {
|
||||
DRAFT = "draft",
|
||||
PROPOSED = "proposed",
|
||||
PUBLISHED = "published",
|
||||
REJECTED = "rejected",
|
||||
}
|
||||
|
||||
export type AdminProductsDeleteOptionRes = {
|
||||
option_id: string
|
||||
object: "option"
|
||||
@@ -159,6 +153,30 @@ export type AdminProductsListRes = PaginatedResponse & {
|
||||
products: Product[]
|
||||
}
|
||||
|
||||
export type AdminProductsListTypesRes = {
|
||||
types: ProductType[]
|
||||
}
|
||||
|
||||
export type AdminProductsListTagsRes = {
|
||||
types: ProductTag[]
|
||||
}
|
||||
|
||||
export type AdminProductsRes = {
|
||||
product: Product
|
||||
}
|
||||
|
||||
export * from "./add-option"
|
||||
export * from "./create-product"
|
||||
export * from "./create-variant"
|
||||
export * from "./delete-option"
|
||||
export * from "./delete-product"
|
||||
export * from "./delete-variant"
|
||||
export * from "./get-product"
|
||||
export * from "./get-variants"
|
||||
export * from "./list-products"
|
||||
export * from "./list-tag-usage-count"
|
||||
export * from "./list-types"
|
||||
export * from "./set-metadata"
|
||||
export * from "./update-option"
|
||||
export * from "./update-product"
|
||||
export * from "./update-variant"
|
||||
|
||||
@@ -167,7 +167,7 @@ class ProductVariantPricesReq {
|
||||
sale_amount?: number
|
||||
}
|
||||
|
||||
class AdminPostProductsProductVariantsVariantReq {
|
||||
export class AdminPostProductsProductVariantsVariantReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
title?: string
|
||||
|
||||
6
packages/medusa/src/types/product.ts
Normal file
6
packages/medusa/src/types/product.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export enum ProductStatus {
|
||||
DRAFT = "draft",
|
||||
PROPOSED = "proposed",
|
||||
PUBLISHED = "published",
|
||||
REJECTED = "rejected",
|
||||
}
|
||||
Reference in New Issue
Block a user