WIP on integration.fetch
This commit is contained in:
@@ -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": {
|
||||
|
||||
@@ -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<string, string>;
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user