feat(medusa, medusa-js, medusa-react): PublishableApiKey "update" endpoint & add "title" property (#2609)

**What**
- update PK endpoint
  - medusa-js/react implementation
- add a title property to the entity
  - update the migration file
  - pass a title on create
  - list PKs by title
  - update the client libs with new param signatures
- change id prefix to: "pk_"
This commit is contained in:
Frane Polić
2022-11-16 05:35:22 +01:00
committed by GitHub
parent ccfc5f666d
commit 03fc9e18e9
17 changed files with 390 additions and 30 deletions
@@ -1,5 +1,6 @@
import { Request, Response } from "express"
import { EntityManager } from "typeorm"
import { IsString } from "class-validator"
import PublishableApiKeyService from "../../../../services/publishable-api-key"
@@ -8,6 +9,16 @@ import PublishableApiKeyService from "../../../../services/publishable-api-key"
* operationId: "PostPublishableApiKeys"
* summary: "Create a PublishableApiKey"
* description: "Creates a PublishableApiKey."
* requestBody:
* content:
* application/json:
* schema:
* required:
* - title
* properties:
* title:
* description: A title for the publishable api key
* type: string
* x-authenticated: true
* x-codeSamples:
* - lang: JavaScript
@@ -59,14 +70,20 @@ export default async (req: Request, res: Response) => {
) as PublishableApiKeyService
const manager = req.scope.resolve("manager") as EntityManager
const data = req.validatedBody as AdminPostPublishableApiKeysReq
const loggedInUserId = (req.user?.id ?? req.user?.userId) as string
const pubKey = await manager.transaction(async (transactionManager) => {
return await publishableApiKeyService
.withTransaction(transactionManager)
.create({ loggedInUserId })
.create(data, { loggedInUserId })
})
return res.status(200).json({ publishable_api_key: pubKey })
}
export class AdminPostPublishableApiKeysReq {
@IsString()
title: string
}
@@ -2,10 +2,15 @@ import { Router } from "express"
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
import PublishableAPIKeysFeatureFlag from "../../../../loaders/feature-flags/publishable-api-keys"
import middlewares, { transformQuery } from "../../../middlewares"
import middlewares, {
transformBody,
transformQuery,
} from "../../../middlewares"
import { GetPublishableApiKeysParams } from "./list-publishable-api-keys"
import { PublishableApiKey } from "../../../../models"
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
import { AdminPostPublishableApiKeysReq } from "./create-publishable-api-key"
import { AdminPostPublishableApiKeysPublishableApiKeyReq } from "./update-publishable-api-key"
const route = Router()
@@ -18,6 +23,7 @@ export default (app) => {
route.post(
"/",
transformBody(AdminPostPublishableApiKeysReq),
middlewares.wrap(require("./create-publishable-api-key").default)
)
@@ -26,6 +32,12 @@ export default (app) => {
middlewares.wrap(require("./get-publishable-api-key").default)
)
route.post(
"/:id",
transformBody(AdminPostPublishableApiKeysPublishableApiKeyReq),
middlewares.wrap(require("./update-publishable-api-key").default)
)
route.delete(
"/:id",
middlewares.wrap(require("./delete-publishable-api-key").default)
@@ -54,3 +66,5 @@ export type AdminPublishableApiKeysListRes = PaginatedResponse & {
export type AdminPublishableApiKeyDeleteRes = DeleteResponse
export * from "./list-publishable-api-keys"
export * from "./create-publishable-api-key"
export * from "./update-publishable-api-key"
@@ -11,7 +11,7 @@ import PublishableApiKeyService from "../../../../services/publishable-api-key"
* description: "List PublishableApiKeys."
* x-authenticated: true
* parameters:
* - (query) order_id {string} List publishable keys by id.
* - (query) q {string} Query used for searching publishable api keys by title.
* - (query) limit=20 {number} The number of items in the response
* - (query) offset=0 {number} The offset of items in response
* - (query) expand {string} Comma separated list of relations to include in the results.
@@ -84,4 +84,8 @@ export default async (req: Request, res: Response) => {
export class GetPublishableApiKeysParams extends extendedFindParamsMixin({
limit: 20,
offset: 0,
}) {}
}) {
@IsString()
@IsOptional()
q?: string
}
@@ -0,0 +1,97 @@
import { Request, Response } from "express"
import { IsOptional, IsString } from "class-validator"
import { EntityManager } from "typeorm"
import PublishableApiKeyService from "../../../../services/publishable-api-key"
/**
* @oas [post] /publishable-api-key/{id}
* operationId: "PostPublishableApiKysPublishableApiKey"
* summary: "Updates a PublishableApiKey"
* description: "Updates a PublishableApiKey."
* x-authenticated: true
* parameters:
* - (path) id=* {string} The ID of the PublishableApiKey.
* requestBody:
* content:
* application/json:
* schema:
* properties:
* title:
* description: A title to update for the key.
* type: string
* 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.publishableApiKey.update(publishable_key_id, {
* title: "new title"
* })
* .then(({ publishable_api_key }) => {
* console.log(publishable_api_key.id)
* })
* - lang: Shell
* label: cURL
* source: |
* curl --location --request POST 'https://medusa-url.com/admin/publishable-api-key/{id}' \
* --header 'Authorization: Bearer {api_token}' \
* --header 'Content-Type: application/json' \
* --data-raw '{
* "title": "updated title"
* }'
* security:
* - api_token: []
* - cookie_auth: []
* tags:
* - PublishableApiKey
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* publishable_api_key:
* $ref: "#/components/schemas/publishable_api_key"
* "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 { id } = req.params
const { validatedBody } = req as {
validatedBody: AdminPostPublishableApiKeysPublishableApiKeyReq
}
const publishableApiKeysService: PublishableApiKeyService = req.scope.resolve(
"publishableApiKeyService"
)
const manager: EntityManager = req.scope.resolve("manager")
const updatedKey = await manager.transaction(async (transactionManager) => {
return await publishableApiKeysService
.withTransaction(transactionManager)
.update(id, validatedBody)
})
res.status(200).json({ publishable_api_key: updatedKey })
}
export class AdminPostPublishableApiKeysPublishableApiKeyReq {
@IsString()
@IsOptional()
title?: string
}