feat(medusa-js, medusa-react): PublishableKeys SC management (#2734)
This commit is contained in:
@@ -7,6 +7,10 @@ import {
|
||||
AdminPublishableApiKeysListRes,
|
||||
AdminPostPublishableApiKeysReq,
|
||||
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
||||
AdminPostPublishableApiKeySalesChannelsBatchReq,
|
||||
AdminDeletePublishableApiKeySalesChannelsBatchReq,
|
||||
GetPublishableApiKeySalesChannelsParams,
|
||||
AdminSalesChannelsListRes,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { ResponsePromise } from "../../typings"
|
||||
@@ -74,6 +78,39 @@ class AdminPublishableApiKeyResource extends BaseResource {
|
||||
const path = `/admin/publishable-api-keys/${id}/revoke`
|
||||
return this.client.request("POST", path, {}, {}, customHeaders)
|
||||
}
|
||||
|
||||
addSalesChannelsBatch(
|
||||
id: string,
|
||||
payload: AdminPostPublishableApiKeySalesChannelsBatchReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminPublishableApiKeysRes> {
|
||||
const path = `/admin/publishable-api-keys/${id}/sales-channels/batch`
|
||||
return this.client.request("POST", path, payload, {}, customHeaders)
|
||||
}
|
||||
|
||||
deleteSalesChannelsBatch(
|
||||
id: string,
|
||||
payload: AdminDeletePublishableApiKeySalesChannelsBatchReq,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminPublishableApiKeysRes> {
|
||||
const path = `/admin/publishable-api-keys/${id}/sales-channels/batch`
|
||||
return this.client.request("DELETE", path, payload, {}, customHeaders)
|
||||
}
|
||||
|
||||
listSalesChannels(
|
||||
id: string,
|
||||
query?: GetPublishableApiKeySalesChannelsParams,
|
||||
customHeaders: Record<string, any> = {}
|
||||
): ResponsePromise<AdminSalesChannelsListRes> {
|
||||
let path = `/admin/publishable-api-keys/${id}/sales-channels`
|
||||
|
||||
if (query) {
|
||||
const queryString = qs.stringify(query)
|
||||
path += `?${queryString}`
|
||||
}
|
||||
|
||||
return this.client.request("GET", path, undefined, {}, customHeaders)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminPublishableApiKeyResource
|
||||
|
||||
@@ -992,6 +992,48 @@ export const adminHandlers = [
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post(
|
||||
"/admin/publishable-api-keys/:id/sales-channels/batch",
|
||||
(req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
publishable_api_key: {
|
||||
...fixtures.get("publishable_api_key"),
|
||||
...(req.body as any),
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.delete(
|
||||
"/admin/publishable-api-keys/:id/sales-channels/batch",
|
||||
(req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
publishable_api_key: {
|
||||
...fixtures.get("publishable_api_key"),
|
||||
...(req.body as any),
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.get(
|
||||
"/admin/publishable-api-keys/:id/sales-channels",
|
||||
(req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
sales_channels: fixtures.get("sales_channels"),
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.post("/admin/publishable-api-keys/", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
AdminPublishableApiKeysRes,
|
||||
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
||||
AdminPostPublishableApiKeysReq,
|
||||
AdminPostPublishableApiKeySalesChannelsBatchReq,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
@@ -97,3 +98,47 @@ export const useAdminRevokePublishableApiKey = (
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminAddPublishableKeySalesChannelsBatch = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPublishableApiKeysRes>,
|
||||
Error,
|
||||
AdminPostPublishableApiKeySalesChannelsBatchReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostPublishableApiKeySalesChannelsBatchReq) =>
|
||||
client.admin.publishableApiKeys.addSalesChannelsBatch(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminPublishableApiKeysKeys.detailSalesChannels(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminRemovePublishableKeySalesChannelsBatch = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPublishableApiKeysRes>,
|
||||
Error,
|
||||
AdminPostPublishableApiKeySalesChannelsBatchReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostPublishableApiKeySalesChannelsBatchReq) =>
|
||||
client.admin.publishableApiKeys.deleteSalesChannelsBatch(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminPublishableApiKeysKeys.detailSalesChannels(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import {
|
||||
AdminPublishableApiKeysListRes,
|
||||
AdminPublishableApiKeysRes,
|
||||
AdminSalesChannelsListRes,
|
||||
GetPublishableApiKeySalesChannelsParams,
|
||||
GetPublishableApiKeysParams,
|
||||
} from "@medusajs/medusa"
|
||||
import { useQuery } from "react-query"
|
||||
@@ -13,9 +15,17 @@ import { UseQueryOptionsWrapper } from "../../../types"
|
||||
const ADMIN_PUBLISHABLE_API_KEYS_QUERY_KEY =
|
||||
`admin_publishable_api_keys` as const
|
||||
|
||||
export const adminPublishableApiKeysKeys = queryKeysFactory(
|
||||
ADMIN_PUBLISHABLE_API_KEYS_QUERY_KEY
|
||||
)
|
||||
export const adminPublishableApiKeysKeys = {
|
||||
...queryKeysFactory(ADMIN_PUBLISHABLE_API_KEYS_QUERY_KEY),
|
||||
detailSalesChannels(id: string, query?: any) {
|
||||
return [
|
||||
...this.detail(id),
|
||||
"sales_channels" as const,
|
||||
{ ...(query || {}) },
|
||||
] as const
|
||||
},
|
||||
}
|
||||
|
||||
type PublishableApiKeyQueryKeys = typeof adminPublishableApiKeysKeys
|
||||
|
||||
export const useAdminPublishableApiKey = (
|
||||
@@ -52,3 +62,21 @@ export const useAdminPublishableApiKeys = (
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
|
||||
export const useAdminPublishableApiKeySalesChannels = (
|
||||
id: string,
|
||||
query?: GetPublishableApiKeySalesChannelsParams,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminSalesChannelsListRes>,
|
||||
Error,
|
||||
ReturnType<PublishableApiKeyQueryKeys["detailSalesChannels"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminPublishableApiKeysKeys.detailSalesChannels(id, query),
|
||||
() => client.admin.publishableApiKeys.listSalesChannels(id, query),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
useAdminRevokePublishableApiKey,
|
||||
useAdminUpdatePublishableApiKey,
|
||||
useAdminCreatePublishableApiKey,
|
||||
useAdminAddPublishableKeySalesChannelsBatch,
|
||||
useAdminRemovePublishableKeySalesChannelsBatch,
|
||||
} from "../../../../src"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
@@ -113,3 +115,53 @@ describe("useAdminDeletePublishableApiKey hook", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminAddPublishableKeySalesChannelsBatch hook", () => {
|
||||
test("Adds a SC to the publishable api key scope", async () => {
|
||||
const id = "pubkey_1234"
|
||||
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminAddPublishableKeySalesChannelsBatch(id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
sales_channel_ids: [{ id: "rand_id" }],
|
||||
})
|
||||
|
||||
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"),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminRemovePublishableKeySalesChannelsBatch hook", () => {
|
||||
test("Deletes a SC from the publishable api key scope", async () => {
|
||||
const id = "pubkey_1234"
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminRemovePublishableKeySalesChannelsBatch(id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
sales_channel_ids: [{ id: "rand_id" }],
|
||||
})
|
||||
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"),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import { renderHook } from "@testing-library/react-hooks"
|
||||
import {
|
||||
useAdminPublishableApiKey,
|
||||
useAdminPublishableApiKeys,
|
||||
useAdminPublishableApiKeySalesChannels,
|
||||
} from "../../../../src"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
@@ -39,3 +40,23 @@ describe("useAdminPublishableApiKeys hook", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminPublishableApiKeySalesChannels hook", () => {
|
||||
test("returns a list of sales channels of a publishable api key", async () => {
|
||||
const publishable_api_key = fixtures.get("publishable_api_key")
|
||||
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminPublishableApiKeySalesChannels(publishable_api_key.id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.response.status).toEqual(200)
|
||||
expect(result.current.sales_channels).toEqual(
|
||||
fixtures.get("sales_channels")
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user