Files
js/@3rdweb-sdk/react/hooks/useApi.ts
T
859320b6ed feat: add bundle ids to keys (#1404)
* feat: add bundle ids to keys

completes FTD-1886

* typo

* fix ui bug

* Update sdks

* Merge branch 'ta/keys-add-bundle-ids' of https://github.com/thirdweb-dev/dashboard into ta/keys-add-bundle-ids

* Fix build

* Update sdks

* hide services

* fix count

* update nightly

* update to [email protected]

* fixes

* dont hide rpc

* copy - remove thirdwebs

* missing form values

* dont be smart about none

* feat: validate api keys (#1406)

completes FTD-1826

* Add descriptions

---------

Co-authored-by: Nacho Iacovino <[email protected]>
Co-authored-by: Jonas Daniels <[email protected]>
2023-07-17 22:10:55 +00:00

215 lines
4.9 KiB
TypeScript

import { THIRDWEB_API_HOST } from "../../../constants/urls";
import { apiKeys } from "../cache-keys";
import { useMutationWithInvalidate } from "./query/useQueryWithNetwork";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useUser } from "@thirdweb-dev/react";
import invariant from "tiny-invariant";
export type ApiKeyService = {
id: string;
name: string;
targetAddresses: string[];
actions: string[];
};
export type ApiKey = {
id: string;
name: string;
key: string;
secret?: string;
secretMasked: string;
accountId: string;
creatorWalletAddress: string;
walletAddresses: string[];
domains: string[];
bundleIds: string[];
revokedAt: string;
lastAccessedAt: string;
createdAt: string;
updatedAt: string;
services?: ApiKeyService[];
};
export interface UpdateKeyServiceInput {
name: string;
targetAddresses: string[];
}
export interface CreateKeyInput {
name?: string;
domains?: string[];
bundleIds?: string[];
walletAddresses?: string[];
services?: UpdateKeyServiceInput[];
}
export interface UpdateKeyInput {
id: string;
name: string;
domains: string[];
bundleIds: string[];
walletAddresses?: string[];
services?: UpdateKeyServiceInput[];
}
export function useApiKeys() {
const { user } = useUser();
return useQuery(
apiKeys.keys(user?.address as string),
async () => {
const res = await fetch(`${THIRDWEB_API_HOST}/v1/keys`, {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
});
const json = await res.json();
if (json.error) {
throw new Error(json.error?.message || json.error);
}
return json.data as ApiKey[];
},
{ enabled: !!user?.address },
);
}
export function useCreateApiKey() {
const { user } = useUser();
const queryClient = useQueryClient();
return useMutation(
async (input: CreateKeyInput) => {
invariant(user, "No user is logged in");
const res = await fetch(`${THIRDWEB_API_HOST}/v1/keys`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(input),
});
const json = await res.json();
if (json.error) {
throw new Error(json.error?.message || json.error);
}
return json.data as ApiKey;
},
{
onSuccess: () => {
return queryClient.invalidateQueries(
apiKeys.keys(user?.address as string),
);
},
},
);
}
export function useUpdateApiKey() {
const { user } = useUser();
const queryClient = useQueryClient();
return useMutationWithInvalidate(
async (input: UpdateKeyInput) => {
invariant(user, "No user is logged in");
const res = await fetch(`${THIRDWEB_API_HOST}/v1/keys/${input.id}`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(input),
});
const json = await res.json();
if (json.error) {
throw new Error(json.error?.message || json.error);
}
return json.data;
},
{
onSuccess: () => {
return queryClient.invalidateQueries(
apiKeys.keys(user?.address as string),
);
},
},
);
}
export function useRevokeApiKey() {
const { user } = useUser();
const queryClient = useQueryClient();
return useMutationWithInvalidate(
async (id: string) => {
invariant(user, "No user is logged in");
const res = await fetch(`${THIRDWEB_API_HOST}/v1/keys/${id}/revoke`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
const json = await res.json();
if (json.error) {
throw new Error(json.error?.message || json.error);
}
return json.data;
},
{
onSuccess: () => {
return queryClient.invalidateQueries(
apiKeys.keys(user?.address as string),
);
},
},
);
}
export function useGenerateApiKey() {
const { user } = useUser();
const queryClient = useQueryClient();
return useMutationWithInvalidate(
async (id: string) => {
invariant(user, "No user is logged in");
const res = await fetch(`${THIRDWEB_API_HOST}/v1/keys/${id}/generate`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
const json = await res.json();
if (json.error) {
throw new Error(json.error?.message || json.error);
}
return json.data;
},
{
onSuccess: () => {
return queryClient.invalidateQueries(
apiKeys.keys(user?.address as string),
);
},
},
);
}