From a87fd6d4bd2ec8fd014297a5b3c5076a6c5996ed Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Sat, 29 Apr 2023 17:27:06 +0100 Subject: [PATCH] Create a single ApiCatalog (and keep the Api naming consistent) --- .../components/integrations/ConnectButton.tsx | 6 ++-- .../$organizationSlug/__org/integrations.tsx | 19 ++++-------- .../apiAuthenticationRepository.server.ts | 31 ++++++++++--------- .../externalApis/apiCatalog.server.ts | 25 +++++++++++++++ .../app/services/externalApis/apiCatalog.ts | 4 --- .../app/services/externalApis/apiStore.ts | 25 --------------- .../services/externalApis/apis/airtable.ts | 4 +-- .../app/services/externalApis/apis/slack.ts | 4 +-- .../webapp/app/services/externalApis/types.ts | 8 ++--- examples/smoke-test/src/index.ts | 4 +-- 10 files changed, 61 insertions(+), 69 deletions(-) create mode 100644 apps/webapp/app/services/externalApis/apiCatalog.server.ts delete mode 100644 apps/webapp/app/services/externalApis/apiCatalog.ts delete mode 100644 apps/webapp/app/services/externalApis/apiStore.ts diff --git a/apps/webapp/app/components/integrations/ConnectButton.tsx b/apps/webapp/app/components/integrations/ConnectButton.tsx index 38a46ea9e..5fdbc7389 100644 --- a/apps/webapp/app/components/integrations/ConnectButton.tsx +++ b/apps/webapp/app/components/integrations/ConnectButton.tsx @@ -2,7 +2,7 @@ import { useLocation, useTransition } from "@remix-run/react"; import classNames from "classnames"; import { useTypedFetcher } from "remix-typedjson"; import type { action } from "~/routes/resources/connection/oauth2"; -import type { ExternalAPI } from "~/services/externalApis/types"; +import type { ExternalApi } from "~/services/externalApis/types"; import { NamedIcon } from "../Icon"; import { PrimaryButton } from "../primitives/Buttons"; import { @@ -26,7 +26,7 @@ export function ConnectButton({ children, className, }: { - api: ExternalAPI; + api: ExternalApi; authMethodKey: string; organizationId: string; children: React.ReactNode; @@ -149,7 +149,7 @@ export function BasicConnectButton({ authMethodKey, organizationId, }: { - api: ExternalAPI; + api: ExternalApi; authMethodKey: string; organizationId: string; }) { diff --git a/apps/webapp/app/routes/__app/orgs/$organizationSlug/__org/integrations.tsx b/apps/webapp/app/routes/__app/orgs/$organizationSlug/__org/integrations.tsx index f31dd5cec..1fe103c38 100644 --- a/apps/webapp/app/routes/__app/orgs/$organizationSlug/__org/integrations.tsx +++ b/apps/webapp/app/routes/__app/orgs/$organizationSlug/__org/integrations.tsx @@ -1,8 +1,4 @@ -import { - CursorArrowRaysIcon, - PlusCircleIcon, - PlusIcon, -} from "@heroicons/react/24/outline"; +import { CursorArrowRaysIcon, PlusIcon } from "@heroicons/react/24/outline"; import type { LoaderArgs } from "@remix-run/server-runtime"; import { SliderButton } from "@typeform/embed-react"; import { typedjson, useTypedLoaderData } from "remix-typedjson"; @@ -16,7 +12,6 @@ import { List } from "~/components/layout/List"; import { OrganizationsSideMenu } from "~/components/navigation/SideMenu"; import { Badge } from "~/components/primitives/Badge"; import { - SecondaryButton, primaryClasses, secondaryClasses, } from "~/components/primitives/Buttons"; @@ -26,14 +21,14 @@ import { PopoverTrigger, } from "~/components/primitives/Popover"; import { Body } from "~/components/primitives/text/Body"; -import { Header3, Header4 } from "~/components/primitives/text/Headers"; +import { Header3 } from "~/components/primitives/text/Headers"; import { SubTitle } from "~/components/primitives/text/SubTitle"; import { Title } from "~/components/primitives/text/Title"; import { useCurrentOrganization } from "~/hooks/useOrganizations"; import { getOrganizationFromSlug } from "~/models/organization.server"; import { apiConnectionRepository } from "~/services/externalApis/apiAuthenticationRepository.server"; -import { apiStore } from "~/services/externalApis/apiStore"; -import type { ExternalAPI } from "~/services/externalApis/types"; +import { apiCatalog } from "~/services/externalApis/apiCatalog.server"; +import type { ExternalApi } from "~/services/externalApis/types"; import { requireUser } from "~/services/session.server"; import { formatDateTime } from "~/utils"; @@ -51,11 +46,9 @@ export const loader = async ({ request, params }: LoaderArgs) => { organization.id ); - const apis = apiStore.getApis(); - return typedjson({ connections, - apis, + apis: apiCatalog.getApis(), }); }; @@ -187,7 +180,7 @@ export default function Integrations() { ); } -function AddApiConnection({ api }: { api: ExternalAPI }) { +function AddApiConnection({ api }: { api: ExternalApi }) { return (
; + + constructor(apis: Record) { + this.#apis = apis; + } + + public getApis() { + return this.#apis; + } + + public getApi(identifier: string) { + const api = this.#apis[identifier]; + if (!api) { + return undefined; + } + return api; + } +} + +export const apiCatalog = new ApiCatalog({ slack, airtable }); diff --git a/apps/webapp/app/services/externalApis/apiCatalog.ts b/apps/webapp/app/services/externalApis/apiCatalog.ts deleted file mode 100644 index 3a91e364a..000000000 --- a/apps/webapp/app/services/externalApis/apiCatalog.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { airtable } from "./apis/airtable"; -import { slack } from "./apis/slack"; - -export const apis = { slack, airtable }; diff --git a/apps/webapp/app/services/externalApis/apiStore.ts b/apps/webapp/app/services/externalApis/apiStore.ts deleted file mode 100644 index 7d8c7dbb3..000000000 --- a/apps/webapp/app/services/externalApis/apiStore.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { apis } from "./apiCatalog"; -import type { ExternalAPI } from "./types"; - -/** Used to get External APIs */ -export class APIStore { - #apis: Record; - - constructor(apis: Record) { - this.#apis = apis; - } - - public getApis() { - return this.#apis; - } - - public getApi(identifier: string) { - const api = this.#apis[identifier]; - if (!api) { - return undefined; - } - return api; - } -} - -export const apiStore = new APIStore(apis); diff --git a/apps/webapp/app/services/externalApis/apis/airtable.ts b/apps/webapp/app/services/externalApis/apis/airtable.ts index 7d0ec9c87..d20a9447a 100644 --- a/apps/webapp/app/services/externalApis/apis/airtable.ts +++ b/apps/webapp/app/services/externalApis/apis/airtable.ts @@ -1,6 +1,6 @@ -import type { ExternalAPI } from "../types"; +import type { ExternalApi } from "../types"; -export const airtable: ExternalAPI = { +export const airtable: ExternalApi = { identifier: "airtable", name: "Airtable", authenticationMethods: { diff --git a/apps/webapp/app/services/externalApis/apis/slack.ts b/apps/webapp/app/services/externalApis/apis/slack.ts index 36064e6a9..d89275316 100644 --- a/apps/webapp/app/services/externalApis/apis/slack.ts +++ b/apps/webapp/app/services/externalApis/apis/slack.ts @@ -1,4 +1,4 @@ -import type { ExternalAPI, ScopeAnnotation } from "../types"; +import type { ExternalApi, ScopeAnnotation } from "../types"; const userAnnotation: ScopeAnnotation = { label: "User", @@ -10,7 +10,7 @@ const botAnnotation: ScopeAnnotation = { color: "#FFF067", }; -export const slack: ExternalAPI = { +export const slack: ExternalApi = { identifier: "slack", name: "Slack", authenticationMethods: { diff --git a/apps/webapp/app/services/externalApis/types.ts b/apps/webapp/app/services/externalApis/types.ts index 72c8e96e6..33c8bd310 100644 --- a/apps/webapp/app/services/externalApis/types.ts +++ b/apps/webapp/app/services/externalApis/types.ts @@ -1,16 +1,16 @@ import { z } from "zod"; -export type ExternalAPI = { +export type ExternalApi = { /** Used to uniquely identify an API */ identifier: string; /** The name of the API */ name: string; /** The possible authentication methods we support for this API */ - authenticationMethods: Record; + authenticationMethods: Record; }; /** An authentication method that can be used */ -export type APIAuthenticationMethod = APIAuthenticationMethodOAuth2; +export type ApiAuthenticationMethod = ApiAuthenticationMethodOAuth2; export type AuthorizationLocation = "header" | "body"; @@ -58,7 +58,7 @@ export type RefreshTokenParams = { }; //A useful reference is the Simple OAuth2 npm library: https://github.com/lelylan/simple-oauth2/blob/HEAD/API.md#options -export type APIAuthenticationMethodOAuth2 = { +export type ApiAuthenticationMethodOAuth2 = { /** The displayable name of the authentication method */ name: string; /** The type of authentication method */ diff --git a/examples/smoke-test/src/index.ts b/examples/smoke-test/src/index.ts index 53f68652e..496de8e3f 100644 --- a/examples/smoke-test/src/index.ts +++ b/examples/smoke-test/src/index.ts @@ -99,7 +99,7 @@ new Job({ gh, }, // issueEvent is a helper function that creates a trigger for a GitHub issue event webhook - trigger: gh.onIssueOpened({ + trigger: gh.triggers.onIssueOpened({ repo: "ericallam/basic-starter-100k", }), run: async (event, io, ctx) => { @@ -150,7 +150,7 @@ new Job({ gh, }, // issueEvent is a helper function that creates a trigger for a GitHub issue event webhook - trigger: gh.onIssueComment({ + trigger: gh.triggers.onIssueComment({ repo: "ericallam/basic-starter-100k", }), run: async (event, io, ctx) => {},