mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-04 02:26:19 +00:00
Add frontend files for cloudflare workers integration
This commit is contained in:
@@ -0,0 +1,92 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useRouter } from "next/router";
|
||||||
|
|
||||||
|
import {
|
||||||
|
useSaveIntegrationAccessToken
|
||||||
|
} from "@app/hooks/api";
|
||||||
|
|
||||||
|
import { Button,Card, CardTitle, FormControl, Input } from "../../../components/v2";
|
||||||
|
|
||||||
|
export default function CloudflareWorkersIntegrationPage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const { mutateAsync } = useSaveIntegrationAccessToken();
|
||||||
|
|
||||||
|
const [accessKey, setAccessKey] = useState("");
|
||||||
|
const [accessKeyErrorText, setAccessKeyErrorText] = useState("");
|
||||||
|
const [accountId, setAccountId] = useState("");
|
||||||
|
const [accountIdErrorText, setAccountIdErrorText] = useState("");
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
|
const handleButtonClick = async () => {
|
||||||
|
try {
|
||||||
|
setAccessKeyErrorText("");
|
||||||
|
setAccountIdErrorText("");
|
||||||
|
if (accessKey.length === 0 || accountId.length === 0) {
|
||||||
|
if (accessKey.length === 0) setAccessKeyErrorText("API token cannot be blank!");
|
||||||
|
if (accountId.length === 0) setAccountIdErrorText("Account ID cannot be blank!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
const integrationAuth = await mutateAsync({
|
||||||
|
workspaceId: localStorage.getItem("projectData.id"),
|
||||||
|
integration: "cloudflare-workers",
|
||||||
|
accessId: accountId,
|
||||||
|
accessToken: accessKey
|
||||||
|
});
|
||||||
|
|
||||||
|
setAccessKey("");
|
||||||
|
setAccountId("");
|
||||||
|
setIsLoading(false);
|
||||||
|
|
||||||
|
router.push(`/integrations/cloudflare-workers/create?integrationAuthId=${integrationAuth._id}`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex h-full w-full items-center justify-center">
|
||||||
|
<Card className="max-w-lg rounded-md border border-mineshaft-600 mb-12">
|
||||||
|
<CardTitle className="text-left px-6" subTitle="After adding your API-key, you will be prompted to set up an integration for a particular Infisical project and environment.">Cloudflare Workers Integration</CardTitle>
|
||||||
|
<FormControl
|
||||||
|
label="Cloudflare Workers API token"
|
||||||
|
errorText={accessKeyErrorText}
|
||||||
|
isError={accessKeyErrorText !== "" ?? false}
|
||||||
|
className="mx-6"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
placeholder=""
|
||||||
|
value={accessKey}
|
||||||
|
onChange={(e) => setAccessKey(e.target.value)}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormControl
|
||||||
|
label="Cloudflare Workers Account ID"
|
||||||
|
errorText={accountIdErrorText}
|
||||||
|
isError={accountIdErrorText !== "" ?? false}
|
||||||
|
className="mx-6"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
placeholder=""
|
||||||
|
value={accountId}
|
||||||
|
onChange={(e) => setAccountId(e.target.value)}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<Button
|
||||||
|
onClick={handleButtonClick}
|
||||||
|
color="mineshaft"
|
||||||
|
variant="outline_bg"
|
||||||
|
className="mb-6 mt-2 ml-auto mr-6 w-min"
|
||||||
|
isFullWidth={false}
|
||||||
|
isLoading={isLoading}
|
||||||
|
>
|
||||||
|
Connect to Cloudflare Workers
|
||||||
|
</Button>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
CloudflareWorkersIntegrationPage.requireAuth = true;
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
import { useEffect,useState } from "react";
|
||||||
|
import { useRouter } from "next/router";
|
||||||
|
import queryString from "query-string";
|
||||||
|
|
||||||
|
import {
|
||||||
|
useCreateIntegration
|
||||||
|
, useGetWorkspaceById } from "@app/hooks/api";
|
||||||
|
|
||||||
|
import { Button, Card, CardTitle, FormControl, Select, SelectItem } from "../../../components/v2";
|
||||||
|
import { useGetIntegrationAuthApps, useGetIntegrationAuthById } from "../../../hooks/api/integrationAuth";
|
||||||
|
|
||||||
|
const cloudflareEnvironments = [
|
||||||
|
{ name: "Production", slug: "production" }
|
||||||
|
];
|
||||||
|
|
||||||
|
export default function CloudflareWorkersIntegrationPage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const { mutateAsync } = useCreateIntegration();
|
||||||
|
|
||||||
|
const { integrationAuthId } = queryString.parse(router.asPath.split("?")[1]);
|
||||||
|
const { data: workspace } = useGetWorkspaceById(localStorage.getItem("projectData.id") ?? "");
|
||||||
|
const { data: integrationAuth } = useGetIntegrationAuthById((integrationAuthId as string) ?? "");
|
||||||
|
const { data: integrationAuthApps } = useGetIntegrationAuthApps({
|
||||||
|
integrationAuthId: (integrationAuthId as string) ?? ""
|
||||||
|
});
|
||||||
|
|
||||||
|
const [selectedSourceEnvironment, setSelectedSourceEnvironment] = useState("");
|
||||||
|
const [targetApp, setTargetApp] = useState("");
|
||||||
|
const [targetAppId, setTargetAppId] = useState("");
|
||||||
|
const [targetEnvironment, setTargetEnvironment] = useState("");
|
||||||
|
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (workspace) {
|
||||||
|
setSelectedSourceEnvironment(workspace.environments[0].slug);
|
||||||
|
}
|
||||||
|
}, [workspace]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (integrationAuthApps) {
|
||||||
|
if (integrationAuthApps.length > 0) {
|
||||||
|
setTargetApp(integrationAuthApps[0].name);
|
||||||
|
setTargetAppId(String(integrationAuthApps[0].appId));
|
||||||
|
setTargetEnvironment(cloudflareEnvironments[0].slug);
|
||||||
|
} else {
|
||||||
|
setTargetApp("none");
|
||||||
|
setTargetEnvironment(cloudflareEnvironments[0].slug);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [integrationAuthApps]);
|
||||||
|
|
||||||
|
const handleButtonClick = async () => {
|
||||||
|
try {
|
||||||
|
if (!integrationAuth?._id) return;
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
await mutateAsync({
|
||||||
|
integrationAuthId: integrationAuth?._id,
|
||||||
|
isActive: true,
|
||||||
|
app: targetApp,
|
||||||
|
appId: targetAppId,
|
||||||
|
sourceEnvironment: selectedSourceEnvironment,
|
||||||
|
targetEnvironment,
|
||||||
|
secretPath: "/",
|
||||||
|
});
|
||||||
|
|
||||||
|
setIsLoading(false);
|
||||||
|
|
||||||
|
router.push(`/integrations/${localStorage.getItem("projectData.id")}`);
|
||||||
|
} catch(err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return integrationAuth &&
|
||||||
|
workspace &&
|
||||||
|
selectedSourceEnvironment &&
|
||||||
|
integrationAuthApps &&
|
||||||
|
targetEnvironment &&
|
||||||
|
targetApp ? (
|
||||||
|
<div className="flex h-full w-full items-center justify-center bg-gradient-to-tr from-mineshaft-900 to-bunker-900">
|
||||||
|
<Card className="max-w-lg rounded-md p-0 border border-mineshaft-600">
|
||||||
|
<CardTitle className="text-left px-6" subTitle="Choose which environment in Infisical you want to sync with your Cloudflare Workers project.">Cloudflare Workers Integration</CardTitle>
|
||||||
|
<FormControl label="Infisical Project Environment" className="mt-2 px-6">
|
||||||
|
<Select
|
||||||
|
value={selectedSourceEnvironment}
|
||||||
|
onValueChange={(val) => setSelectedSourceEnvironment(val)}
|
||||||
|
className="w-full border border-mineshaft-500"
|
||||||
|
>
|
||||||
|
{workspace?.environments.map((sourceEnvironment) => (
|
||||||
|
<SelectItem
|
||||||
|
value={sourceEnvironment.slug}
|
||||||
|
key={`source-environment-${sourceEnvironment.slug}`}
|
||||||
|
>
|
||||||
|
{sourceEnvironment.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
<FormControl label="Cloudflare Workers Project" className="mt-4 px-6">
|
||||||
|
<Select
|
||||||
|
value={targetApp}
|
||||||
|
onValueChange={(val) => setTargetApp(val)}
|
||||||
|
className="w-full border border-mineshaft-500"
|
||||||
|
isDisabled={integrationAuthApps.length === 0}
|
||||||
|
>
|
||||||
|
{integrationAuthApps.length > 0 ? (
|
||||||
|
integrationAuthApps.map((integrationAuthApp) => (
|
||||||
|
<SelectItem
|
||||||
|
value={integrationAuthApp.name}
|
||||||
|
key={`target-app-${integrationAuthApp.name}`}
|
||||||
|
>
|
||||||
|
{integrationAuthApp.name}
|
||||||
|
</SelectItem>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<SelectItem value="none" key="target-app-none">
|
||||||
|
No apps found
|
||||||
|
</SelectItem>
|
||||||
|
)}
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
<FormControl label="Cloudflare Workers Environment" className="mt-4 px-6">
|
||||||
|
<Select
|
||||||
|
value={targetEnvironment}
|
||||||
|
onValueChange={(val) => setTargetEnvironment(val)}
|
||||||
|
className="w-full border border-mineshaft-500"
|
||||||
|
>
|
||||||
|
{cloudflareEnvironments.map((cloudflareEnvironment) => (
|
||||||
|
<SelectItem
|
||||||
|
value={cloudflareEnvironment.slug}
|
||||||
|
key={`target-environment-${cloudflareEnvironment.slug}`}
|
||||||
|
>
|
||||||
|
{cloudflareEnvironment.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
<Button
|
||||||
|
onClick={handleButtonClick}
|
||||||
|
color="mineshaft"
|
||||||
|
variant="outline_bg"
|
||||||
|
className="mt-2 mb-6 ml-auto mr-6"
|
||||||
|
isFullWidth={false}
|
||||||
|
isLoading={isLoading}
|
||||||
|
>
|
||||||
|
Create Integration
|
||||||
|
</Button>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
CloudflareWorkersIntegrationPage.requireAuth = true;
|
||||||
Reference in New Issue
Block a user