diff --git a/.changeset/old-meals-run.md b/.changeset/old-meals-run.md new file mode 100644 index 0000000000..15f26f4504 --- /dev/null +++ b/.changeset/old-meals-run.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"@medusajs/medusa-js": patch +--- + +fix(medusa, medusa-js): publishable api key bugs diff --git a/packages/medusa-js/src/resources/admin/publishable-api-keys.ts b/packages/medusa-js/src/resources/admin/publishable-api-keys.ts index 971f3a1799..67aa3504f3 100644 --- a/packages/medusa-js/src/resources/admin/publishable-api-keys.ts +++ b/packages/medusa-js/src/resources/admin/publishable-api-keys.ts @@ -1,32 +1,32 @@ import qs from "qs" import { - AdminPublishableApiKeyDeleteRes, - AdminPublishableApiKeysRes, - GetPublishableApiKeysParams, - AdminPublishableApiKeysListRes, - AdminPostPublishableApiKeysReq, - AdminPostPublishableApiKeysPublishableApiKeyReq, - AdminPostPublishableApiKeySalesChannelsBatchReq, AdminDeletePublishableApiKeySalesChannelsBatchReq, + AdminPostPublishableApiKeySalesChannelsBatchReq, + AdminPostPublishableApiKeysPublishableApiKeyReq, + AdminPostPublishableApiKeysReq, + AdminPublishableApiKeyDeleteRes, + AdminPublishableApiKeysListRes, + AdminPublishableApiKeysRes, GetPublishableApiKeySalesChannelsParams, + GetPublishableApiKeysParams, } from "@medusajs/medusa" +import { AdminPublishableApiKeysListSalesChannelsRes } from "@medusajs/medusa" import { ResponsePromise } from "../../typings" import BaseResource from "../base" -import { AdminPublishableApiKeysListSalesChannelsRes } from "@medusajs/medusa" /** * This class is used to send requests to [Admin Publishable API Key API Routes](https://docs.medusajs.com/api/admin#publishable-api-keys). All its method * are available in the JS Client under the `medusa.admin.publishableApiKeys` property. - * + * * All methods in this class require {@link AdminAuthResource.createSession | user authentication}. - * + * * Publishable API Keys can be used to scope Store API calls with an API key, determining what resources are retrieved when querying the API. * For example, a publishable API key can be associated with one or more sales channels. When it is passed in the header of a request to the List Product store API Route, * the sales channels are inferred from the key and only products associated with those sales channels are retrieved. * Admins can manage publishable API keys and their associated resources. Currently, only Sales Channels are supported as a resource. - * + * * Related Guide: [How to manage publishable API keys](https://docs.medusajs.com/development/publishable-api-keys/admin/manage-publishable-api-keys). */ class AdminPublishableApiKeyResource extends BaseResource { @@ -37,7 +37,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {Record} query * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the publishable API key's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -49,16 +49,10 @@ class AdminPublishableApiKeyResource extends BaseResource { */ retrieve( id: string, - query: Record = {}, customHeaders: Record = {} ): ResponsePromise { let path = `/admin/publishable-api-keys/${id}` - if (query) { - const queryString = qs.stringify(query) - path += `?${queryString}` - } - return this.client.request("GET", path, undefined, {}, customHeaders) } @@ -67,10 +61,10 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {GetPublishableApiKeysParams} query - Filters and pagination configurations to apply on the retrieved publishable API keys. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the list of publishable API keys with pagination fields. - * + * * @example * To list publishable API keys: - * + * * ```ts * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -80,9 +74,9 @@ class AdminPublishableApiKeyResource extends BaseResource { * console.log(publishable_api_keys) * }) * ``` - * + * * By default, only the first `20` records are retrieved. You can control pagination by specifying the `limit` and `offset` properties: - * + * * ```ts * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -115,7 +109,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {AdminPostPublishableApiKeysReq} payload - The publishable API key to be created. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the publishbale API key's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -141,7 +135,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {AdminPostPublishableApiKeysPublishableApiKeyReq} payload - The attributes to update in the publishable API key. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the publishbale API key's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -167,7 +161,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {string} id - The ID of the publishable API key * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the delete operation's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -190,7 +184,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {string} id - The ID of the publishable API key. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the publishbale API key's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -214,7 +208,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {AdminPostPublishableApiKeySalesChannelsBatchReq} payload - The sales channels to add. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the publishbale API key's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -245,7 +239,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {AdminDeletePublishableApiKeySalesChannelsBatchReq} payload - The sales channels to delete from the publishable API key. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the publishbale API key's details. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) @@ -276,7 +270,7 @@ class AdminPublishableApiKeyResource extends BaseResource { * @param {GetPublishableApiKeySalesChannelsParams} query - Filters to apply on the retrieved sales channels. * @param {Record} customHeaders - Custom headers to attach to the request. * @returns {ResponsePromise} Resolves to the list of sales channels. - * + * * @example * import Medusa from "@medusajs/medusa-js" * const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 }) diff --git a/packages/medusa/src/api/routes/admin/publishable-api-keys/index.ts b/packages/medusa/src/api/routes/admin/publishable-api-keys/index.ts index 42de40524d..1616f6564e 100644 --- a/packages/medusa/src/api/routes/admin/publishable-api-keys/index.ts +++ b/packages/medusa/src/api/routes/admin/publishable-api-keys/index.ts @@ -1,17 +1,16 @@ import { Router } from "express" +import { PublishableApiKey, SalesChannel } from "../../../../models" +import { DeleteResponse, PaginatedResponse } from "../../../../types/common" import middlewares, { transformBody, transformQuery, } from "../../../middlewares" -import { GetPublishableApiKeysParams } from "./list-publishable-api-keys" -import { PublishableApiKey, SalesChannel } from "../../../../models" -import { DeleteResponse, PaginatedResponse } from "../../../../types/common" -import { AdminPostPublishableApiKeysReq } from "./create-publishable-api-key" -import { AdminPostPublishableApiKeysPublishableApiKeyReq } from "./update-publishable-api-key" -import { AdminDeletePublishableApiKeySalesChannelsBatchReq } from "./delete-channels-batch" import { AdminPostPublishableApiKeySalesChannelsBatchReq } from "./add-channels-batch" -import { GetPublishableApiKeySalesChannelsParams } from "./list-publishable-api-key-sales-channels" +import { AdminPostPublishableApiKeysReq } from "./create-publishable-api-key" +import { AdminDeletePublishableApiKeySalesChannelsBatchReq } from "./delete-channels-batch" +import { GetPublishableApiKeysParams } from "./list-publishable-api-keys" +import { AdminPostPublishableApiKeysPublishableApiKeyReq } from "./update-publishable-api-key" const route = Router() @@ -55,7 +54,6 @@ export default (app) => { route.get( "/:id/sales-channels", - transformQuery(GetPublishableApiKeySalesChannelsParams, { isList: true }), middlewares.wrap( require("./list-publishable-api-key-sales-channels").default ) @@ -158,8 +156,8 @@ export type AdminPublishableApiKeysListSalesChannelsRes = { } export * from "./add-channels-batch" -export * from "./delete-channels-batch" -export * from "./list-publishable-api-keys" -export * from "./list-publishable-api-key-sales-channels" export * from "./create-publishable-api-key" +export * from "./delete-channels-batch" +export * from "./list-publishable-api-key-sales-channels" +export * from "./list-publishable-api-keys" export * from "./update-publishable-api-key" diff --git a/packages/medusa/src/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.ts b/packages/medusa/src/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.ts index ab95214378..5b12577378 100644 --- a/packages/medusa/src/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.ts +++ b/packages/medusa/src/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.ts @@ -1,8 +1,8 @@ -import { Request, Response } from "express" import { IsOptional, IsString } from "class-validator" +import { Request, Response } from "express" import PublishableApiKeyService from "../../../../services/publishable-api-key" -import { extendedFindParamsMixin } from "../../../../types/common" +import { validator } from "../../../../utils/validator" /** * @oas [get] /admin/publishable-api-keys/{id}/sales-channels @@ -64,10 +64,13 @@ export default async (req: Request, res: Response) => { "publishableApiKeyService" ) - const filterableFields = req.filterableFields + const validated = await validator( + GetPublishableApiKeySalesChannelsParams, + req.query + ) const salesChannels = await publishableApiKeyService.listSalesChannels(id, { - q: filterableFields.q as string | undefined, + q: validated.q, }) return res.json({ @@ -78,7 +81,7 @@ export default async (req: Request, res: Response) => { /** * Parameters used to filter the sales channels. */ -export class GetPublishableApiKeySalesChannelsParams extends extendedFindParamsMixin() { +export class GetPublishableApiKeySalesChannelsParams { /** * Search term to search sales channels' names and descriptions. */