feat(medusa-js, medusa-react): PublishableKeys SC management (#2734)
This commit is contained in:
1 parent
322d462311
commit
3b2c929408
6 files changed
+228
-3
No files matched your search
@@ -7,6 +7,10 @@ import {
|
|||||||
AdminPublishableApiKeysListRes,
|
AdminPublishableApiKeysListRes,
|
||||||
AdminPostPublishableApiKeysReq,
|
AdminPostPublishableApiKeysReq,
|
||||||
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
||||||
|
AdminPostPublishableApiKeySalesChannelsBatchReq,
|
||||||
|
AdminDeletePublishableApiKeySalesChannelsBatchReq,
|
||||||
|
GetPublishableApiKeySalesChannelsParams,
|
||||||
|
AdminSalesChannelsListRes,
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
|
|
||||||
import { ResponsePromise } from "../../typings"
|
import { ResponsePromise } from "../../typings"
|
||||||
@@ -74,6 +78,39 @@ class AdminPublishableApiKeyResource extends BaseResource {
|
|||||||
const path = `/admin/publishable-api-keys/${id}/revoke`
|
const path = `/admin/publishable-api-keys/${id}/revoke`
|
||||||
return this.client.request("POST", path, {}, {}, customHeaders)
|
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
|
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) => {
|
rest.post("/admin/publishable-api-keys/", (req, res, ctx) => {
|
||||||
return res(
|
return res(
|
||||||
ctx.status(200),
|
ctx.status(200),
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
AdminPublishableApiKeysRes,
|
AdminPublishableApiKeysRes,
|
||||||
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
AdminPostPublishableApiKeysPublishableApiKeyReq,
|
||||||
AdminPostPublishableApiKeysReq,
|
AdminPostPublishableApiKeysReq,
|
||||||
|
AdminPostPublishableApiKeySalesChannelsBatchReq,
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
|
|
||||||
import { buildOptions } from "../../utils/buildOptions"
|
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 {
|
import {
|
||||||
AdminPublishableApiKeysListRes,
|
AdminPublishableApiKeysListRes,
|
||||||
AdminPublishableApiKeysRes,
|
AdminPublishableApiKeysRes,
|
||||||
|
AdminSalesChannelsListRes,
|
||||||
|
GetPublishableApiKeySalesChannelsParams,
|
||||||
GetPublishableApiKeysParams,
|
GetPublishableApiKeysParams,
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
import { useQuery } from "react-query"
|
import { useQuery } from "react-query"
|
||||||
@@ -13,9 +15,17 @@ import { UseQueryOptionsWrapper } from "../../../types"
|
|||||||
const ADMIN_PUBLISHABLE_API_KEYS_QUERY_KEY =
|
const ADMIN_PUBLISHABLE_API_KEYS_QUERY_KEY =
|
||||||
`admin_publishable_api_keys` as const
|
`admin_publishable_api_keys` as const
|
||||||
|
|
||||||
export const adminPublishableApiKeysKeys = queryKeysFactory(
|
export const adminPublishableApiKeysKeys = {
|
||||||
ADMIN_PUBLISHABLE_API_KEYS_QUERY_KEY
|
...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
|
type PublishableApiKeyQueryKeys = typeof adminPublishableApiKeysKeys
|
||||||
|
|
||||||
export const useAdminPublishableApiKey = (
|
export const useAdminPublishableApiKey = (
|
||||||
@@ -52,3 +62,21 @@ export const useAdminPublishableApiKeys = (
|
|||||||
)
|
)
|
||||||
return { ...data, ...rest } as const
|
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,
|
useAdminRevokePublishableApiKey,
|
||||||
useAdminUpdatePublishableApiKey,
|
useAdminUpdatePublishableApiKey,
|
||||||
useAdminCreatePublishableApiKey,
|
useAdminCreatePublishableApiKey,
|
||||||
|
useAdminAddPublishableKeySalesChannelsBatch,
|
||||||
|
useAdminRemovePublishableKeySalesChannelsBatch,
|
||||||
} from "../../../../src"
|
} from "../../../../src"
|
||||||
import { createWrapper } from "../../../utils"
|
import { createWrapper } from "../../../utils"
|
||||||
import { fixtures } from "../../../../mocks/data"
|
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 {
|
import {
|
||||||
useAdminPublishableApiKey,
|
useAdminPublishableApiKey,
|
||||||
useAdminPublishableApiKeys,
|
useAdminPublishableApiKeys,
|
||||||
|
useAdminPublishableApiKeySalesChannels,
|
||||||
} from "../../../../src"
|
} from "../../../../src"
|
||||||
import { createWrapper } from "../../../utils"
|
import { createWrapper } from "../../../utils"
|
||||||
import { fixtures } from "../../../../mocks/data"
|
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