feat(medusa, medusa-js, medusa-react): Add endpoint to retrieve product tags from the storefront (#3051)
This commit is contained in:
@@ -29,10 +29,10 @@ export * from "./routes/admin/gift-cards"
|
||||
export * from "./routes/admin/invites"
|
||||
export * from "./routes/admin/notes"
|
||||
export * from "./routes/admin/notifications"
|
||||
export * from "./routes/admin/payment-collections"
|
||||
export * from "./routes/admin/payments"
|
||||
export * from "./routes/admin/order-edits"
|
||||
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-tags"
|
||||
export * from "./routes/admin/product-types"
|
||||
@@ -59,12 +59,13 @@ export * from "./routes/store/customers"
|
||||
export * from "./routes/store/gift-cards"
|
||||
export * from "./routes/store/order-edits"
|
||||
export * from "./routes/store/orders"
|
||||
export * from "./routes/store/products"
|
||||
export * from "./routes/store/payment-collections"
|
||||
export * from "./routes/store/product-tags"
|
||||
export * from "./routes/store/product-types"
|
||||
export * from "./routes/store/products"
|
||||
export * from "./routes/store/regions"
|
||||
export * from "./routes/store/return-reasons"
|
||||
export * from "./routes/store/returns"
|
||||
export * from "./routes/store/shipping-options"
|
||||
export * from "./routes/store/swaps"
|
||||
export * from "./routes/store/variants"
|
||||
export * from "./routes/store/payment-collections"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import cors from "cors"
|
||||
import { Router } from "express"
|
||||
import { parseCorsOrigins } from "medusa-core-utils"
|
||||
import middlewares from "../../middlewares"
|
||||
import productTypesRoutes from "../admin/product-types"
|
||||
import authRoutes from "./auth"
|
||||
@@ -9,6 +10,9 @@ import customerRoutes from "./customers"
|
||||
import giftCardRoutes from "./gift-cards"
|
||||
import orderEditRoutes from "./order-edits"
|
||||
import orderRoutes from "./orders"
|
||||
import paymentCollectionRoutes from "./payment-collections"
|
||||
import productCategoryRoutes from "./product-categories"
|
||||
import productTagsRoutes from "./product-tags"
|
||||
import productRoutes from "./products"
|
||||
import regionRoutes from "./regions"
|
||||
import returnReasonRoutes from "./return-reasons"
|
||||
@@ -16,9 +20,6 @@ import returnRoutes from "./returns"
|
||||
import shippingOptionRoutes from "./shipping-options"
|
||||
import swapRoutes from "./swaps"
|
||||
import variantRoutes from "./variants"
|
||||
import paymentCollectionRoutes from "./payment-collections"
|
||||
import productCategoryRoutes from "./product-categories"
|
||||
import { parseCorsOrigins } from "medusa-core-utils"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -41,6 +42,7 @@ export default (app, container, config) => {
|
||||
collectionRoutes(route)
|
||||
customerRoutes(route, container)
|
||||
productRoutes(route, featureFlagRouter)
|
||||
productTagsRoutes(route)
|
||||
productTypesRoutes(route)
|
||||
orderRoutes(route)
|
||||
orderEditRoutes(route)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Router } from "express"
|
||||
import { ProductTag } from "../../../../models"
|
||||
import { PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares, { transformQuery } from "../../../middlewares"
|
||||
import { StoreGetProductTagsParams } from "./list-product-tags"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app: Router) => {
|
||||
app.use("/product-tags", route)
|
||||
|
||||
route.get(
|
||||
"/",
|
||||
transformQuery(StoreGetProductTagsParams, {
|
||||
defaultFields: defaultStoreProductTagFields,
|
||||
defaultRelations: defaultStoreProductTagRelations,
|
||||
allowedFields: allowedStoreProductTagFields,
|
||||
isList: true,
|
||||
}),
|
||||
middlewares.wrap(require("./list-product-tags").default)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
export const defaultStoreProductTagFields = [
|
||||
"id",
|
||||
"value",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
export const allowedStoreProductTagFields = [...defaultStoreProductTagFields]
|
||||
|
||||
export const defaultStoreProductTagRelations = []
|
||||
|
||||
export type StoreProductTagsListRes = PaginatedResponse & {
|
||||
product_tags: ProductTag[]
|
||||
}
|
||||
|
||||
export * from "./list-product-tags"
|
||||
@@ -0,0 +1,181 @@
|
||||
import { IsOptional, IsString } from "class-validator"
|
||||
import {
|
||||
DateComparisonOperator,
|
||||
FindPaginationParams,
|
||||
StringComparisonOperator,
|
||||
} from "../../../../types/common"
|
||||
|
||||
import { Request, Response } from "express"
|
||||
import ProductTagService from "../../../../services/product-tag"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
|
||||
/**
|
||||
* @oas [get] /product-tags
|
||||
* operationId: "GetProductTags"
|
||||
* summary: "List Product Tags"
|
||||
* description: "Retrieve a list of Product Tags."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (query) limit=20 {integer} The number of types to return.
|
||||
* - (query) offset=0 {integer} The number of items to skip before the results.
|
||||
* - (query) order {string} The field to sort items by.
|
||||
* - (query) discount_condition_id {string} The discount condition id on which to filter the product tags.
|
||||
* - in: query
|
||||
* name: value
|
||||
* style: form
|
||||
* explode: false
|
||||
* description: The tag values to search for
|
||||
* schema:
|
||||
* type: array
|
||||
* items:
|
||||
* type: string
|
||||
* - in: query
|
||||
* name: id
|
||||
* style: form
|
||||
* explode: false
|
||||
* description: The tag IDs to search for
|
||||
* schema:
|
||||
* type: array
|
||||
* items:
|
||||
* type: string
|
||||
* - (query) q {string} A query string to search values for
|
||||
* - in: query
|
||||
* name: created_at
|
||||
* description: Date comparison for when resulting product tags were created.
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* lt:
|
||||
* type: string
|
||||
* description: filter by dates less than this date
|
||||
* format: date
|
||||
* gt:
|
||||
* type: string
|
||||
* description: filter by dates greater than this date
|
||||
* format: date
|
||||
* lte:
|
||||
* type: string
|
||||
* description: filter by dates less than or equal to this date
|
||||
* format: date
|
||||
* gte:
|
||||
* type: string
|
||||
* description: filter by dates greater than or equal to this date
|
||||
* format: date
|
||||
* - in: query
|
||||
* name: updated_at
|
||||
* description: Date comparison for when resulting product tags were updated.
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* lt:
|
||||
* type: string
|
||||
* description: filter by dates less than this date
|
||||
* format: date
|
||||
* gt:
|
||||
* type: string
|
||||
* description: filter by dates greater than this date
|
||||
* format: date
|
||||
* lte:
|
||||
* type: string
|
||||
* description: filter by dates less than or equal to this date
|
||||
* format: date
|
||||
* gte:
|
||||
* type: string
|
||||
* description: filter by dates greater than or equal to this date
|
||||
* format: date
|
||||
* 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.store.productTags.list()
|
||||
* .then(({ product_tags }) => {
|
||||
* console.log(product_tags.length);
|
||||
* });
|
||||
* - lang: Shell
|
||||
* label: cURL
|
||||
* source: |
|
||||
* curl --location --request GET 'https://medusa-url.com/store/product-tags'
|
||||
* tags:
|
||||
* - Product Tag
|
||||
* responses:
|
||||
* "200":
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* product_tags:
|
||||
* $ref: "#/components/schemas/ProductTag"
|
||||
* 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
|
||||
* "400":
|
||||
* $ref: "#/components/responses/400_error"
|
||||
* "401":
|
||||
* $ref: "#/components/responses/unauthorized"
|
||||
* "404":
|
||||
* $ref: "#/components/responses/not_found_error"
|
||||
* "409":
|
||||
* $ref: "#/components/responses/invalid_state_error"
|
||||
* "422":
|
||||
* $ref: "#/components/responses/invalid_request_error"
|
||||
* "500":
|
||||
* $ref: "#/components/responses/500_error"
|
||||
*/
|
||||
export default async (req: Request, res: Response) => {
|
||||
const tagService: ProductTagService = req.scope.resolve("productTagService")
|
||||
|
||||
const { listConfig, filterableFields } = req
|
||||
const { skip, take } = req.listConfig
|
||||
|
||||
const [tags, count] = await tagService.listAndCount(
|
||||
filterableFields,
|
||||
listConfig
|
||||
)
|
||||
|
||||
res.status(200).json({
|
||||
product_tags: tags,
|
||||
count,
|
||||
offset: skip,
|
||||
limit: take,
|
||||
})
|
||||
}
|
||||
|
||||
export class StoreGetProductTagsParams extends FindPaginationParams {
|
||||
@IsType([String, [String], StringComparisonOperator])
|
||||
@IsOptional()
|
||||
id?: string | string[] | StringComparisonOperator
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
q?: string
|
||||
|
||||
@IsType([String, [String], StringComparisonOperator])
|
||||
@IsOptional()
|
||||
value?: string | string[] | StringComparisonOperator
|
||||
|
||||
@IsType([DateComparisonOperator])
|
||||
@IsOptional()
|
||||
created_at?: DateComparisonOperator
|
||||
|
||||
@IsType([DateComparisonOperator])
|
||||
@IsOptional()
|
||||
updated_at?: DateComparisonOperator
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
order?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
discount_condition_id?: string
|
||||
}
|
||||
Reference in New Issue
Block a user