mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 08:34:52 +00:00
Finish basic scaffolding for service token v3
This commit is contained in:
@@ -1 +1,8 @@
|
||||
export { useCreateServiceToken, useDeleteServiceToken, useGetUserWsServiceTokens } from "./queries";
|
||||
export {
|
||||
useCreateServiceToken,
|
||||
useDeleteServiceToken,
|
||||
useGetUserWsServiceTokens,
|
||||
useCreateServiceTokenV3,
|
||||
useUpdateServiceTokenV3,
|
||||
useDeleteServiceTokenV3
|
||||
} from "./queries";
|
||||
|
||||
@@ -6,8 +6,14 @@ import {
|
||||
CreateServiceTokenDTO,
|
||||
CreateServiceTokenRes,
|
||||
DeleteServiceTokenRes,
|
||||
ServiceToken
|
||||
ServiceToken,
|
||||
ServiceTokenDataV3,
|
||||
CreateServiceTokenDataV3DTO,
|
||||
CreateServiceTokenDataV3Res,
|
||||
UpdateServiceTokenDataV3DTO,
|
||||
DeleteServiceTokenDataV3DTO
|
||||
} from "./types";
|
||||
import { workspaceKeys } from "../workspace/queries";
|
||||
|
||||
const serviceTokenKeys = {
|
||||
getAllWorkspaceServiceToken: (workspaceID: string) => [{ workspaceID }, "service-tokens"] as const
|
||||
@@ -32,12 +38,11 @@ export const useGetUserWsServiceTokens = ({ workspaceID }: UseGetWorkspaceServic
|
||||
}
|
||||
|
||||
// mutation
|
||||
export const useCreateServiceToken = () => {
|
||||
export const useCreateServiceToken = () => { // TODO: deprecate
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation<CreateServiceTokenRes, {}, CreateServiceTokenDTO>({
|
||||
mutationFn: async (body) => {
|
||||
console.log("useCreateServiceToken");
|
||||
const { data } = await apiRequest.post("/api/v2/service-token/", body);
|
||||
data.serviceToken += `.${body.randomBytes}`;
|
||||
return data;
|
||||
@@ -62,3 +67,55 @@ export const useDeleteServiceToken = () => {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useCreateServiceTokenV3 = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<CreateServiceTokenDataV3Res, {}, CreateServiceTokenDataV3DTO>({
|
||||
mutationFn: async (body) => {
|
||||
const { data } = await apiRequest.post("/api/v3/service-token/", body);
|
||||
return data;
|
||||
},
|
||||
onSuccess: ({ serviceTokenData }) => {
|
||||
queryClient.invalidateQueries(workspaceKeys.getWorkspaceServiceTokenDataV3(serviceTokenData.workspace));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useUpdateServiceTokenV3 = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<ServiceTokenDataV3, {}, UpdateServiceTokenDataV3DTO>({
|
||||
mutationFn: async ({
|
||||
serviceTokenDataId,
|
||||
name,
|
||||
isActive
|
||||
}) => {
|
||||
const { data: { serviceTokenData } } = await apiRequest.patch(`/api/v3/service-token/${serviceTokenDataId}`, {
|
||||
name,
|
||||
isActive
|
||||
});
|
||||
|
||||
return serviceTokenData;
|
||||
},
|
||||
onSuccess: ({ workspace }) => {
|
||||
queryClient.invalidateQueries(workspaceKeys.getWorkspaceServiceTokenDataV3(workspace));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteServiceTokenV3 = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<ServiceTokenDataV3, {}, DeleteServiceTokenDataV3DTO>({
|
||||
mutationFn: async ({
|
||||
serviceTokenDataId
|
||||
}) => {
|
||||
console.log("useDeleteServiceTokenV3");
|
||||
const { data: { serviceTokenData } } = await apiRequest.delete(`/api/v3/service-token/${serviceTokenDataId}`);
|
||||
console.log("useDeleteServiceTokenV3 serviceTokenData: ", serviceTokenData);
|
||||
return serviceTokenData;
|
||||
},
|
||||
onSuccess: ({ workspace }) => {
|
||||
console.log("useDeleteServiceTokenV3 onSuccess: ", workspace);
|
||||
queryClient.invalidateQueries(workspaceKeys.getWorkspaceServiceTokenDataV3(workspace));
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -33,3 +33,38 @@ export type CreateServiceTokenRes = {
|
||||
};
|
||||
|
||||
export type DeleteServiceTokenRes = { serviceTokenData: ServiceToken };
|
||||
|
||||
// --- v3
|
||||
|
||||
export type ServiceTokenDataV3 = {
|
||||
_id: string;
|
||||
name: string;
|
||||
workspace: string;
|
||||
isActive: boolean;
|
||||
lastUsed?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
// TODO: add scopes
|
||||
// TODO: encrypted key info
|
||||
export type CreateServiceTokenDataV3DTO = {
|
||||
name: string;
|
||||
workspaceId: string;
|
||||
publicKey: string;
|
||||
}
|
||||
|
||||
export type CreateServiceTokenDataV3Res = {
|
||||
serviceToken: string;
|
||||
serviceTokenData: ServiceTokenDataV3;
|
||||
}
|
||||
|
||||
export type UpdateServiceTokenDataV3DTO = {
|
||||
serviceTokenDataId: string;
|
||||
name?: string;
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export type DeleteServiceTokenDataV3DTO = {
|
||||
serviceTokenDataId: string;
|
||||
}
|
||||
@@ -19,4 +19,6 @@ export {
|
||||
useReorderWsEnvironment,
|
||||
useToggleAutoCapitalization,
|
||||
useUpdateUserWorkspaceRole,
|
||||
useUpdateWsEnvironment} from "./queries";
|
||||
useUpdateWsEnvironment,
|
||||
useGetWorkspaceServiceTokenDataV3
|
||||
} from "./queries";
|
||||
|
||||
@@ -6,6 +6,7 @@ import { IntegrationAuth } from "../integrationAuth/types";
|
||||
import { TIntegration } from "../integrations/types";
|
||||
import { EncryptedSecret } from "../secrets/types";
|
||||
import { TWorkspaceUser } from "../users/types";
|
||||
import { ServiceTokenDataV3 } from "../serviceTokens/types";
|
||||
import {
|
||||
CreateEnvironmentDTO,
|
||||
CreateWorkspaceDTO,
|
||||
@@ -32,7 +33,8 @@ export const workspaceKeys = {
|
||||
getAllUserWorkspace: ["workspaces"] as const,
|
||||
getUserWsEnvironments: (workspaceId: string) => ["workspace-env", { workspaceId }] as const,
|
||||
getWorkspaceAuditLogs: (workspaceId: string) => [{ workspaceId }] as const,
|
||||
getWorkspaceUsers: (workspaceId: string) => [{ workspaceId }] as const
|
||||
getWorkspaceUsers: (workspaceId: string) => [{ workspaceId }] as const,
|
||||
getWorkspaceServiceTokenDataV3: (workspaceId: string) => [{ workspaceId }, "workspace-service-token-data-v3"] as const
|
||||
};
|
||||
|
||||
const fetchWorkspaceById = async (workspaceId: string) => {
|
||||
@@ -361,3 +363,19 @@ export const useUpdateUserWorkspaceRole = () => {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetWorkspaceServiceTokenDataV3 = (workspaceId: string) => {
|
||||
return useQuery({
|
||||
queryKey: workspaceKeys.getWorkspaceServiceTokenDataV3(workspaceId),
|
||||
queryFn: async () => {
|
||||
const {
|
||||
data: { serviceTokenData }
|
||||
} = await apiRequest.get<{ serviceTokenData: ServiceTokenDataV3[] }>(
|
||||
`/api/v3/workspaces/${workspaceId}/service-token`
|
||||
);
|
||||
|
||||
return serviceTokenData;
|
||||
},
|
||||
enabled: true
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user