From 657db218e15cc5469b916a568f0b2bc641b4867e Mon Sep 17 00:00:00 2001 From: kielbasa-elp Date: Mon, 26 Feb 2024 17:56:37 +0100 Subject: [PATCH] Extract secret queries to SecretApi --- apps/web-remix/app/api/secrets/SecretsApi.ts | 48 +++++++++++++++++++ .../secrets/secrets.contracts.ts} | 9 ++++ .../pages/secrets/list/EditSecretKeyModal.tsx | 6 +-- .../pages/secrets/list/action.server.tsx | 25 +++++----- .../pages/secrets/list/loader.server.tsx | 8 ++-- .../components/pages/secrets/list/schema.ts | 6 --- .../pages/secrets/newSecret/action.server.tsx | 14 +++--- .../pages/secrets/newSecret/page.tsx | 10 ++-- .../pages/secrets/newSecret/schema.ts | 6 --- .../pages/secrets/variables.types.ts | 2 +- 10 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 apps/web-remix/app/api/secrets/SecretsApi.ts rename apps/web-remix/app/{components/pages/secrets/contracts.ts => api/secrets/secrets.contracts.ts} (67%) delete mode 100644 apps/web-remix/app/components/pages/secrets/list/schema.ts delete mode 100644 apps/web-remix/app/components/pages/secrets/newSecret/schema.ts diff --git a/apps/web-remix/app/api/secrets/SecretsApi.ts b/apps/web-remix/app/api/secrets/SecretsApi.ts new file mode 100644 index 000000000..b649a46a0 --- /dev/null +++ b/apps/web-remix/app/api/secrets/SecretsApi.ts @@ -0,0 +1,48 @@ +import { fetchTyped } from "~/utils/fetch.server"; +import { + ICreateUpdateSecretSchema, + SecretKeyListResponse, + SecretKeyResponse, +} from "./secrets.contracts"; +import { z } from "zod"; + +export class SecretsApi { + constructor(private client: typeof fetchTyped) {} + + async getSecrets(organizationId: string | number) { + return this.client( + SecretKeyListResponse, + `/organizations/${organizationId}/secrets` + ); + } + + async deleteSecret(organizationId: string | number, name: string) { + return this.client( + z.any(), + `/organizations/${organizationId}/secrets/${name}`, + { method: "DELETE" } + ); + } + + async updateSecret( + organizationId: string | number, + data: ICreateUpdateSecretSchema + ) { + return this.client( + z.any(), + `/organizations/${organizationId}/secrets/${data.name}`, + { method: "PUT", body: JSON.stringify({ value: data.value }) } + ); + } + + async createSecret( + organizationId: string | number, + data: ICreateUpdateSecretSchema + ) { + return this.client( + SecretKeyResponse, + `/organizations/${organizationId}/secrets`, + { method: "POST", body: JSON.stringify(data) } + ); + } +} diff --git a/apps/web-remix/app/components/pages/secrets/contracts.ts b/apps/web-remix/app/api/secrets/secrets.contracts.ts similarity index 67% rename from apps/web-remix/app/components/pages/secrets/contracts.ts rename to apps/web-remix/app/api/secrets/secrets.contracts.ts index cc02ba159..17fff2d85 100644 --- a/apps/web-remix/app/components/pages/secrets/contracts.ts +++ b/apps/web-remix/app/api/secrets/secrets.contracts.ts @@ -18,3 +18,12 @@ export const SecretKeyListResponse = z data: SecretKeyList, }) .transform((res) => res.data); + +export const CreateUpdateSecretSchema = z.object({ + name: z.string().min(2), + value: z.string().min(2), +}); + +export type ICreateUpdateSecretSchema = z.TypeOf< + typeof CreateUpdateSecretSchema +>; diff --git a/apps/web-remix/app/components/pages/secrets/list/EditSecretKeyModal.tsx b/apps/web-remix/app/components/pages/secrets/list/EditSecretKeyModal.tsx index f23a3e8af..adcdd5aef 100644 --- a/apps/web-remix/app/components/pages/secrets/list/EditSecretKeyModal.tsx +++ b/apps/web-remix/app/components/pages/secrets/list/EditSecretKeyModal.tsx @@ -2,14 +2,14 @@ import React, { useMemo } from "react"; import { ValidatedForm } from "remix-validated-form"; import { Modal } from "@elpassion/taco/Modal"; import { withZod } from "@remix-validated-form/with-zod"; -import { Button } from "@elpassion/taco"; + import { Field } from "~/components/form/fields/field.context"; import { PasswordInputField, TextInputField, } from "~/components/form/fields/text.field"; import { ISecretKey } from "../variables.types"; -import { schema } from "./schema"; +import { CreateUpdateSecretSchema } from "~/api/secrets/secrets.contracts"; import { SubmitButton } from "~/components/form/submit"; interface EditSecretModalProps { isOpen: boolean; @@ -22,7 +22,7 @@ export const EditSecretKeyModal: React.FC = ({ onClose, initialData, }) => { - const validator = useMemo(() => withZod(schema), []); + const validator = useMemo(() => withZod(CreateUpdateSecretSchema), []); return ( { await requireLogin(request); invariant(params.organizationId, "organizationId not found"); - const secrets = await fetch( - SecretKeyListResponse, - `/organizations/${params.organizationId}/secrets` - ); + const secretsApi = new SecretsApi(fetch); + const secrets = await secretsApi.getSecrets(params.organizationId); return json({ organizationId: params.organizationId, secrets: secrets.data, diff --git a/apps/web-remix/app/components/pages/secrets/list/schema.ts b/apps/web-remix/app/components/pages/secrets/list/schema.ts deleted file mode 100644 index cdc019030..000000000 --- a/apps/web-remix/app/components/pages/secrets/list/schema.ts +++ /dev/null @@ -1,6 +0,0 @@ -import z from "zod"; - -export const schema = z.object({ - name: z.string().min(2), - value: z.string().min(2), -}); diff --git a/apps/web-remix/app/components/pages/secrets/newSecret/action.server.tsx b/apps/web-remix/app/components/pages/secrets/newSecret/action.server.tsx index 5d724f96f..95126ecae 100644 --- a/apps/web-remix/app/components/pages/secrets/newSecret/action.server.tsx +++ b/apps/web-remix/app/components/pages/secrets/newSecret/action.server.tsx @@ -1,28 +1,26 @@ import { ActionFunctionArgs, redirect } from "@remix-run/node"; import invariant from "tiny-invariant"; import { actionBuilder } from "~/utils.server"; -import { SecretKeyResponse } from "../contracts"; import { validationError } from "remix-validated-form"; import { withZod } from "@remix-validated-form/with-zod"; -import { schema } from "./schema"; +import { CreateUpdateSecretSchema } from "~/api/secrets/secrets.contracts"; import { routes } from "~/utils/routes.utils"; import { setServerToast } from "~/utils/toast.server"; +import { SecretsApi } from "~/api/secrets/SecretsApi"; export async function action(actionArgs: ActionFunctionArgs) { return actionBuilder({ post: async ({ params, request }, { fetch }) => { - const validator = withZod(schema); + const validator = withZod(CreateUpdateSecretSchema); invariant(params.organizationId, "organizationId not found"); const result = await validator.validate(await request.formData()); if (result.error) return validationError(result.error); - await fetch( - SecretKeyResponse, - `/organizations/${params.organizationId}/secrets`, - { method: "POST", body: JSON.stringify(result.data) } - ); + const secretsApi = new SecretsApi(fetch); + + await secretsApi.createSecret(params.organizationId, result.data); return redirect(routes.secrets(params.organizationId), { headers: { diff --git a/apps/web-remix/app/components/pages/secrets/newSecret/page.tsx b/apps/web-remix/app/components/pages/secrets/newSecret/page.tsx index 49782d28d..92065af75 100644 --- a/apps/web-remix/app/components/pages/secrets/newSecret/page.tsx +++ b/apps/web-remix/app/components/pages/secrets/newSecret/page.tsx @@ -9,10 +9,9 @@ import { TextInputField, } from "~/components/form/fields/text.field"; import { SubmitButton } from "~/components/form/submit"; -import { schema } from "./schema"; - +import { CreateUpdateSecretSchema } from "~/api/secrets/secrets.contracts"; export function NewSecret() { - const validator = useMemo(() => withZod(schema), []); + const validator = useMemo(() => withZod(CreateUpdateSecretSchema), []); return ( - + Save the Secret diff --git a/apps/web-remix/app/components/pages/secrets/newSecret/schema.ts b/apps/web-remix/app/components/pages/secrets/newSecret/schema.ts deleted file mode 100644 index cdc019030..000000000 --- a/apps/web-remix/app/components/pages/secrets/newSecret/schema.ts +++ /dev/null @@ -1,6 +0,0 @@ -import z from "zod"; - -export const schema = z.object({ - name: z.string().min(2), - value: z.string().min(2), -}); diff --git a/apps/web-remix/app/components/pages/secrets/variables.types.ts b/apps/web-remix/app/components/pages/secrets/variables.types.ts index a107c0bbb..c4bcb6889 100644 --- a/apps/web-remix/app/components/pages/secrets/variables.types.ts +++ b/apps/web-remix/app/components/pages/secrets/variables.types.ts @@ -1,5 +1,5 @@ import { z } from "zod"; -import { SecretKey, SecretKeyList } from "./contracts"; +import { SecretKey, SecretKeyList } from "~/api/secrets/secrets.contracts"; export type ISecretKey = z.TypeOf;