MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / mintOAuthConnection

Function mintOAuthConnection

packages/core/sdk/src/executor.ts:2469–2586  ·  view source on GitHub ↗
(
      input: MintOAuthConnectionInput,
    )

Source from the content-addressed store, hash-verified

2467 // by the OAuth service) + produce the connection's tools. Mirrors
2468 // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns.
2469 const mintOAuthConnection = (
2470 input: MintOAuthConnectionInput,
2471 ): Effect.Effect<Connection, StorageFailure> =>
2472 Effect.gen(function* () {
2473 const name = connectionIdentifier(String(input.name));
2474 yield* requireUserSubject(input.owner);
2475 const integrationRow = yield* findIntegrationRow(input.integration);
2476 if (!integrationRow) {
2477 return yield* new StorageError({
2478 message: `Integration not found: ${input.integration}`,
2479 cause: undefined,
2480 });
2481 }
2482 const keys = yield* Effect.try({
2483 try: () => ownedKeys(input.owner),
2484 catch: (cause) => storageFailureFromUnknown("invalid owner", cause),
2485 });
2486 const now = new Date();
2487 const ref: ConnectionRef = {
2488 owner: input.owner,
2489 integration: input.integration,
2490 name,
2491 };
2492 yield* transaction(
2493 Effect.gen(function* () {
2494 const existing = yield* findConnectionRow(ref);
2495 const set: Record<string, unknown> = {
2496 template: String(input.template),
2497 provider: input.provider,
2498 item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId },
2499 identity_label: input.identityLabel ?? null,
2500 oauth_client: String(input.oauthClient),
2501 oauth_client_owner: input.oauthClientOwner,
2502 refresh_item_id: input.refreshItemId,
2503 expires_at: input.expiresAt,
2504 oauth_scope: input.oauthScope,
2505 oauth_token_url: input.oauthTokenUrl ?? null,
2506 provider_state:
2507 input.missingOAuthScopes && input.missingOAuthScopes.length > 0
2508 ? { missingOAuthScopes: input.missingOAuthScopes }
2509 : null,
2510 updated_at: now,
2511 };
2512 if (existing) {
2513 yield* core.updateMany("connection", {
2514 where: (b: AnyCb) =>
2515 b.and(
2516 byOwner(input.owner)(b),
2517 b("integration", "=", String(input.integration)),
2518 b("name", "=", String(name)),
2519 ),
2520 set,
2521 });
2522 } else {
2523 yield* core.create("connection", {
2524 tenant: keys.tenant,
2525 owner: keys.owner,
2526 subject: keys.subject,

Callers 1

createExecutorFunction · 0.85

Calls 10

connectionIdentifierFunction · 0.90
requireUserSubjectFunction · 0.85
findIntegrationRowFunction · 0.85
ownedKeysFunction · 0.85
findConnectionRowFunction · 0.85
byOwnerFunction · 0.85
produceConnectionToolsFunction · 0.85
rowToConnectionFunction · 0.85
transactionFunction · 0.70

Tested by

no test coverage detected