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:
@@ -980,6 +980,18 @@ export const adminHandlers = [
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/publishable-api-keys/:id", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
publishable_api_key: {
|
||||
...fixtures.get("publishable_api_key"),
|
||||
...(req.body as any),
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/publishable-api-keys/", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
|
||||
@@ -4,6 +4,8 @@ import { Response } from "@medusajs/medusa-js"
|
||||
import {
|
||||
AdminPublishableApiKeyDeleteRes,
|
||||
AdminPublishableApiKeysRes,
|
||||
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
||||
AdminPostPublishableApiKeysReq,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
@@ -11,16 +13,47 @@ import { useMedusa } from "../../../contexts"
|
||||
import { adminPublishableApiKeysKeys } from "."
|
||||
|
||||
export const useAdminCreatePublishableApiKey = (
|
||||
options?: UseMutationOptions<Response<AdminPublishableApiKeysRes>, Error, {}>
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPublishableApiKeysRes>,
|
||||
Error,
|
||||
AdminPostPublishableApiKeysReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
(payload: {}) => client.admin.publishableApiKeys.create(payload),
|
||||
(payload: AdminPostPublishableApiKeysReq) =>
|
||||
client.admin.publishableApiKeys.create(payload),
|
||||
buildOptions(queryClient, [adminPublishableApiKeysKeys.lists()], options)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminUpdatePublishableApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPublishableApiKeysRes>,
|
||||
Error,
|
||||
AdminPostPublishableApiKeysPublishableApiKeyReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostPublishableApiKeysPublishableApiKeyReq) =>
|
||||
client.admin.publishableApiKeys.update(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminPublishableApiKeysKeys.lists(),
|
||||
adminPublishableApiKeysKeys.detail(id),
|
||||
adminPublishableApiKeysKeys.details(),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminDeletePublishableApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
|
||||
import { createWrapper } from "../../../utils"
|
||||
import {
|
||||
useAdminDeletePublishableApiKey,
|
||||
useAdminRevokePublishableApiKey,
|
||||
useAdminUpdatePublishableApiKey,
|
||||
useAdminCreatePublishableApiKey,
|
||||
} from "../../../../src"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { useAdminCreatePublishableApiKey } from "../../../../src"
|
||||
|
||||
describe("useAdminCreatePublishableApiKey hook", () => {
|
||||
test("Created a publishable api key", async () => {
|
||||
@@ -17,7 +18,7 @@ describe("useAdminCreatePublishableApiKey hook", () => {
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({})
|
||||
result.current.mutate({ title: "Mandatory title" })
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
@@ -25,6 +26,7 @@ describe("useAdminCreatePublishableApiKey hook", () => {
|
||||
expect(result.current.data).toEqual(
|
||||
expect.objectContaining({
|
||||
publishable_api_key: {
|
||||
title: "Mandatory title",
|
||||
...fixtures.get("publishable_api_key"),
|
||||
},
|
||||
})
|
||||
@@ -32,6 +34,34 @@ describe("useAdminCreatePublishableApiKey hook", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminUpdatePublishableApiKey hook", () => {
|
||||
test("updates an publishable key and returns it", async () => {
|
||||
const pubKey = {
|
||||
title: "changed title",
|
||||
}
|
||||
|
||||
const { result, waitFor } = renderHook(
|
||||
() =>
|
||||
useAdminUpdatePublishableApiKey(fixtures.get("publishable_api_key").id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate(pubKey)
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.publishable_api_key).toEqual(
|
||||
expect.objectContaining({
|
||||
...fixtures.get("publishable_api_key"),
|
||||
...pubKey,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminRevokePublishableApiKey hook", () => {
|
||||
test("Revoke a publishable api key", async () => {
|
||||
const id = "pubkey_1234"
|
||||
|
||||
Reference in New Issue
Block a user