feat(js-sdk): Add API key (#8838)
* feat(js-sdk): Add API key * address PR comments * Allow params to create + update
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { AdminApiKeyListResponse, AdminApiKeyResponse } from "@medusajs/types"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import {
|
||||
MutationOptions,
|
||||
QueryKey,
|
||||
@@ -7,11 +8,9 @@ import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { CreateApiKeyReq, UpdateApiKeyReq } from "../../types/api-payloads"
|
||||
import { ApiKeyDeleteRes } from "../../types/api-responses"
|
||||
import { salesChannelsQueryKeys } from "./sales-channels"
|
||||
|
||||
const API_KEYS_QUERY_KEY = "api_keys" as const
|
||||
@@ -19,14 +18,18 @@ export const apiKeysQueryKeys = queryKeysFactory(API_KEYS_QUERY_KEY)
|
||||
|
||||
export const useApiKey = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: Omit<
|
||||
UseQueryOptions<AdminApiKeyResponse, Error, AdminApiKeyResponse, QueryKey>,
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.apiKeys.retrieve(id, query),
|
||||
queryFn: () => sdk.admin.apiKey.retrieve(id),
|
||||
queryKey: apiKeysQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
@@ -35,19 +38,19 @@ export const useApiKey = (
|
||||
}
|
||||
|
||||
export const useApiKeys = (
|
||||
query?: Record<string, any>,
|
||||
query?: HttpTypes.AdminGetApiKeysParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
AdminApiKeyListResponse,
|
||||
Error,
|
||||
AdminApiKeyListResponse,
|
||||
HttpTypes.AdminGetApiKeysParams,
|
||||
FetchError,
|
||||
HttpTypes.AdminApiKeyListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.apiKeys.list(query),
|
||||
queryFn: () => sdk.admin.apiKey.list(query),
|
||||
queryKey: apiKeysQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
@@ -56,10 +59,14 @@ export const useApiKeys = (
|
||||
}
|
||||
|
||||
export const useCreateApiKey = (
|
||||
options?: UseMutationOptions<AdminApiKeyResponse, Error, CreateApiKeyReq>
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateApiKey
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.apiKeys.create(payload),
|
||||
mutationFn: (payload) => sdk.admin.apiKey.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
|
||||
@@ -71,10 +78,14 @@ export const useCreateApiKey = (
|
||||
|
||||
export const useUpdateApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<AdminApiKeyResponse, Error, UpdateApiKeyReq>
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateApiKey
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.apiKeys.update(id, payload),
|
||||
mutationFn: (payload) => sdk.admin.apiKey.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
@@ -87,10 +98,10 @@ export const useUpdateApiKey = (
|
||||
|
||||
export const useRevokeApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<AdminApiKeyResponse, Error, void>
|
||||
options?: UseMutationOptions<HttpTypes.AdminApiKeyResponse, FetchError, void>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.apiKeys.revoke(id),
|
||||
mutationFn: () => sdk.admin.apiKey.revoke(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
@@ -102,10 +113,14 @@ export const useRevokeApiKey = (
|
||||
|
||||
export const useDeleteApiKey = (
|
||||
id: string,
|
||||
options?: MutationOptions<ApiKeyDeleteRes, Error, void>
|
||||
options?: MutationOptions<
|
||||
HttpTypes.DeleteResponse<"api_key">,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.apiKeys.delete(id),
|
||||
mutationFn: () => sdk.admin.apiKey.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
@@ -118,14 +133,14 @@ export const useDeleteApiKey = (
|
||||
export const useBatchRemoveSalesChannelsFromApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
AdminApiKeyResponse,
|
||||
Error,
|
||||
{ sales_channel_ids: string[] }
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["remove"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) =>
|
||||
client.apiKeys.batchRemoveSalesChannels(id, payload),
|
||||
sdk.admin.apiKey.batchSalesChannels(id, { remove: payload }),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
@@ -142,13 +157,14 @@ export const useBatchRemoveSalesChannelsFromApiKey = (
|
||||
export const useBatchAddSalesChannelsToApiKey = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
AdminApiKeyResponse,
|
||||
Error,
|
||||
{ sales_channel_ids: string[] }
|
||||
HttpTypes.AdminApiKeyResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminBatchLink["add"]
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.apiKeys.batchAddSalesChannels(id, payload),
|
||||
mutationFn: (payload) =>
|
||||
sdk.admin.apiKey.batchSalesChannels(id, { add: payload }),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({ queryKey: apiKeysQueryKeys.detail(id) })
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
import { AdminApiKeyListResponse, AdminApiKeyResponse } from "@medusajs/types"
|
||||
import { CreateApiKeyReq, UpdateApiKeyReq } from "../../types/api-payloads"
|
||||
import { ApiKeyDeleteRes } from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
const retrieveApiKey = async (id: string, query?: Record<string, any>) => {
|
||||
return getRequest<AdminApiKeyResponse>(`/admin/api-keys/${id}`, query)
|
||||
}
|
||||
|
||||
const listApiKeys = async (query?: Record<string, any>) => {
|
||||
return getRequest<AdminApiKeyListResponse>(`/admin/api-keys`, query)
|
||||
}
|
||||
|
||||
const deleteApiKey = async (id: string) => {
|
||||
return deleteRequest<ApiKeyDeleteRes>(`/admin/api-keys/${id}`)
|
||||
}
|
||||
|
||||
const revokeApiKey = async (id: string) => {
|
||||
return postRequest<AdminApiKeyResponse>(`/admin/api-keys/${id}/revoke`)
|
||||
}
|
||||
|
||||
const createApiKey = async (payload: CreateApiKeyReq) => {
|
||||
return postRequest<AdminApiKeyResponse>(`/admin/api-keys`, payload)
|
||||
}
|
||||
|
||||
const updateApiKey = async (id: string, payload: UpdateApiKeyReq) => {
|
||||
return postRequest<AdminApiKeyResponse>(`/admin/api-keys/${id}`, payload)
|
||||
}
|
||||
|
||||
const batchRemoveSalesChannelsFromApiKey = async (
|
||||
id: string,
|
||||
payload: { sales_channel_ids: string[] }
|
||||
) => {
|
||||
return postRequest<AdminApiKeyResponse>(
|
||||
`/admin/api-keys/${id}/sales-channels`,
|
||||
{
|
||||
remove: payload.sales_channel_ids,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const batchAddSalesChannelsFromApiKey = async (
|
||||
id: string,
|
||||
payload: { sales_channel_ids: string[] }
|
||||
) => {
|
||||
return postRequest<AdminApiKeyResponse>(
|
||||
`/admin/api-keys/${id}/sales-channels`,
|
||||
{
|
||||
add: payload.sales_channel_ids,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const apiKeys = {
|
||||
retrieve: retrieveApiKey,
|
||||
list: listApiKeys,
|
||||
delete: deleteApiKey,
|
||||
create: createApiKey,
|
||||
update: updateApiKey,
|
||||
revoke: revokeApiKey,
|
||||
batchRemoveSalesChannels: batchRemoveSalesChannelsFromApiKey,
|
||||
batchAddSalesChannels: batchAddSalesChannelsFromApiKey,
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import Medusa from "@medusajs/js-sdk"
|
||||
import { apiKeys } from "./api-keys"
|
||||
import { campaigns } from "./campaigns"
|
||||
import { categories } from "./categories"
|
||||
import { currencies } from "./currencies"
|
||||
@@ -24,7 +23,6 @@ import { workflowExecutions } from "./workflow-executions"
|
||||
export const backendUrl = __BACKEND_URL__ ?? "http://localhost:9000"
|
||||
|
||||
export const client = {
|
||||
apiKeys: apiKeys,
|
||||
campaigns: campaigns,
|
||||
categories: categories,
|
||||
customerGroups: customerGroups,
|
||||
|
||||
@@ -76,24 +76,19 @@ export const ApiKeySalesChannelSection = ({
|
||||
return
|
||||
}
|
||||
|
||||
await mutateAsync(
|
||||
{
|
||||
sales_channel_ids: keys,
|
||||
await mutateAsync(keys, {
|
||||
onSuccess: () => {
|
||||
toast.success(
|
||||
t("apiKeyManagement.removeSalesChannel.successToastBatch", {
|
||||
count: keys.length,
|
||||
})
|
||||
)
|
||||
setRowSelection({})
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(
|
||||
t("apiKeyManagement.removeSalesChannel.successToastBatch", {
|
||||
count: keys.length,
|
||||
})
|
||||
)
|
||||
setRowSelection({})
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
}
|
||||
)
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -167,23 +162,18 @@ const SalesChannelActions = ({
|
||||
return
|
||||
}
|
||||
|
||||
await mutateAsync(
|
||||
{
|
||||
sales_channel_ids: [salesChannel.id],
|
||||
await mutateAsync([salesChannel.id], {
|
||||
onSuccess: () => {
|
||||
toast.success(
|
||||
t("apiKeyManagement.removeSalesChannel.successToast", {
|
||||
count: 1,
|
||||
})
|
||||
)
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(
|
||||
t("apiKeyManagement.removeSalesChannel.successToast", {
|
||||
count: 1,
|
||||
})
|
||||
)
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
}
|
||||
)
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -2,12 +2,12 @@ import { LoaderFunctionArgs } from "react-router-dom"
|
||||
|
||||
import { AdminApiKeyResponse } from "@medusajs/types"
|
||||
import { apiKeysQueryKeys } from "../../../hooks/api/api-keys"
|
||||
import { client } from "../../../lib/client"
|
||||
import { sdk } from "../../../lib/client"
|
||||
import { queryClient } from "../../../lib/query-client"
|
||||
|
||||
const apiKeyDetailQuery = (id: string) => ({
|
||||
queryKey: apiKeysQueryKeys.detail(id),
|
||||
queryFn: async () => client.apiKeys.retrieve(id),
|
||||
queryFn: async () => sdk.admin.apiKey.retrieve(id),
|
||||
})
|
||||
|
||||
export const apiKeyLoader = async ({ params }: LoaderFunctionArgs) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GetPublishableApiKeysParams } from "@medusajs/medusa"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { useQueryParams } from "../../../../../hooks/use-query-params"
|
||||
|
||||
type UseApiKeyManagementTableQueryProps = {
|
||||
@@ -17,7 +17,7 @@ export const useApiKeyManagementTableQuery = ({
|
||||
|
||||
const { offset, created_at, updated_at, revoked_at, q, order } = queryObject
|
||||
|
||||
const searchParams: GetPublishableApiKeysParams = {
|
||||
const searchParams: HttpTypes.AdminGetApiKeysParams = {
|
||||
limit: pageSize,
|
||||
offset: offset ? Number(offset) : 0,
|
||||
created_at: created_at ? JSON.parse(created_at) : undefined,
|
||||
|
||||
@@ -96,25 +96,20 @@ export const ApiKeySalesChannelsForm = ({
|
||||
})
|
||||
|
||||
const handleSubmit = form.handleSubmit(async (values) => {
|
||||
await mutateAsync(
|
||||
{
|
||||
sales_channel_ids: values.sales_channel_ids,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(
|
||||
t("apiKeyManagement.salesChannels.successToast", {
|
||||
count: values.sales_channel_ids.length,
|
||||
})
|
||||
)
|
||||
await mutateAsync(values.sales_channel_ids, {
|
||||
onSuccess: () => {
|
||||
toast.success(
|
||||
t("apiKeyManagement.salesChannels.successToast", {
|
||||
count: values.sales_channel_ids.length,
|
||||
})
|
||||
)
|
||||
|
||||
handleSuccess()
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
}
|
||||
)
|
||||
handleSuccess()
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user