diff --git a/apps/integrations/src/core/authentication/credentials.ts b/apps/integrations/src/core/authentication/credentials.ts index 268d835b3..760bb1c54 100644 --- a/apps/integrations/src/core/authentication/credentials.ts +++ b/apps/integrations/src/core/authentication/credentials.ts @@ -28,6 +28,19 @@ export function applyCredentials( throw error; } + return addCredentialsToConfig(fetch, { authentication, credentials }); +} + +export function addCredentialsToConfig( + fetch: FetchConfig, + { + authentication, + credentials, + }: { + authentication: IntegrationAuthentication; + credentials: AuthCredentials; + } +) { // apply the credentials switch (credentials.type) { case "oauth2": { diff --git a/apps/integrations/src/core/fetch/index.ts b/apps/integrations/src/core/fetch/index.ts new file mode 100644 index 000000000..5184b04b9 --- /dev/null +++ b/apps/integrations/src/core/fetch/index.ts @@ -0,0 +1,71 @@ +import { addCredentialsToConfig } from "core/authentication/credentials"; +import { + AuthCredentials, + IntegrationAuthentication, +} from "core/authentication/types"; +import { HTTPMethod } from "core/endpoint/types"; +import { getFetch, safeGetJson } from "core/request/requestEndpoint"; +import { FetchConfig } from "core/request/types"; +import { type Response } from "node-fetch"; + +export type FetchOptions = { + url: string; + method: HTTPMethod; + headers?: Record; + body?: any; + authentication: IntegrationAuthentication; + credentials?: AuthCredentials; +}; + +export async function serviceFetch({ + url, + method = "GET", + headers, + body, + credentials, + authentication, +}: FetchOptions) { + let fetchConfig: FetchConfig = { + url, + method, + headers: { + ...headers, + }, + body: JSON.stringify(body), + }; + + if (credentials == null) { + throw { + type: "missing_credentials", + }; + } + fetchConfig = addCredentialsToConfig(fetchConfig, { + authentication, + credentials, + }); + + try { + const fetch = await getFetch(); + const response = await fetch(url, { + method, + headers, + body, + }); + + const json = await safeGetJson(response); + + return { + success: response.ok, + status: response.status, + headers: response.headers, + body: json, + }; + } catch (error) { + return { + success: false, + status: 400, + headers: {}, + body: error, + }; + } +} diff --git a/apps/integrations/src/core/request/requestEndpoint.ts b/apps/integrations/src/core/request/requestEndpoint.ts index c33e706b3..643176fed 100644 --- a/apps/integrations/src/core/request/requestEndpoint.ts +++ b/apps/integrations/src/core/request/requestEndpoint.ts @@ -136,6 +136,7 @@ export async function requestEndpoint( headers: fetchConfig.headers, body: fetchConfig.body, }; + const fetch = await getFetch(); const response = await fetch(fetchConfig.url, fetchObject); const json = await safeGetJson(response); @@ -178,11 +179,11 @@ export async function requestEndpoint( }; } -async function getFetch() { +export async function getFetch() { return (await import("node-fetch")).default; } -async function safeGetJson(response: Response) { +export async function safeGetJson(response: Response) { try { return await response.json(); } catch (error) {