Files
triggerdotdev--trigger.dev/apps/webapp/app/models/runConnection.server.ts
Matt Aitken 9ff4c0dbd5 Project-wide Prettier setup (#237)
* Setup project-wide prettier

* Remove old workspace file

* Remove old debugging directives

* New top-level .prettierignore

* Updated Prettier config settings

* Contrubuting guide: Fix for some bad code blocks

* Added more ignores

* Improved the format script command

* printWidth set to 100

* Formatted entire repo (pnpm run format)
2023-08-01 10:21:22 +01:00

75 lines
1.8 KiB
TypeScript

import type { Integration, RunConnection } from "@trigger.dev/database";
import type { ConnectionAuth } from "@trigger.dev/core";
import type { ConnectionWithSecretReference } from "~/services/externalApis/integrationAuthRepository.server";
import { integrationAuthRepository } from "~/services/externalApis/integrationAuthRepository.server";
export type ResolvableRunConnection = RunConnection & {
integration: Integration;
connection: ConnectionWithSecretReference | null;
};
export async function resolveRunConnections(
connections: Array<ResolvableRunConnection>
): Promise<{ auth: Record<string, ConnectionAuth>; success: boolean }> {
let allResolved = true;
const result: Record<string, ConnectionAuth> = {};
for (const connection of connections) {
if (connection.integration.authSource === "LOCAL") {
continue;
}
const auth = await resolveRunConnection(connection);
if (!auth) {
allResolved = false;
continue;
}
result[connection.key] = auth;
}
return { auth: result, success: allResolved };
}
export async function resolveRunConnection(
connection: ResolvableRunConnection
): Promise<ConnectionAuth | undefined> {
if (!connection.connection) {
return;
}
const response = await integrationAuthRepository.getCredentials(connection.connection);
if (!response) {
return;
}
return {
type: "oauth2",
scopes: response.scopes,
accessToken: response.accessToken,
};
}
export async function resolveApiConnection(
connection?: ConnectionWithSecretReference
): Promise<ConnectionAuth | undefined> {
if (!connection) {
return;
}
const response = await integrationAuthRepository.getCredentials(connection);
if (!response) {
return;
}
return {
type: "oauth2",
scopes: response.scopes,
accessToken: response.accessToken,
};
}