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

Function connectionsCreate

packages/core/sdk/src/executor.ts:3808–4389  ·  view source on GitHub ↗
(
      input: CreateConnectionInput,
    )

Source from the content-addressed store, hash-verified

3806 // ------------------------------------------------------------------
3807
3808 const connectionsCreate = (
3809 input: CreateConnectionInput,
3810 ): Effect.Effect<
3811 Connection,
3812 | IntegrationNotFoundError
3813 | ConnectionAlreadyExistsError
3814 | CredentialProviderNotRegisteredError
3815 | InvalidConnectionInputError
3816 | OrgWriteDeniedError
3817 | StorageFailure
3818 > =>
3819 Effect.gen(function* () {
3820 yield* guardOrgWrite(input.owner);
3821 const name = connectionIdentifier(String(input.name));
3822 // Typed (not StorageError) so the HTTP edge can answer 400 with the
3823 // reason instead of an opaque 500 — callers can act on it.
3824 if (input.owner === "user" && subject == null) {
3825 return yield* new InvalidConnectionInputError({
3826 message:
3827 'Cannot create a personal connection: this context has no user subject. Create it with owner "org", or connect as a signed-in user.',
3828 });
3829 }
3830 const integrationRow = yield* findIntegrationRow(input.integration);
3831 if (!integrationRow) {
3832 return yield* new IntegrationNotFoundError({
3833 slug: input.integration,
3834 });
3835 }
3836
3837 // Create is never a replace. This early check answers the common case
3838 // with a typed 409 before any other work, but it is NOT the guard
3839 // against concurrent creates — the row insert below is: the
3840 // transaction re-checks, the primary key breaks the tie, and the
3841 // provider write happens only after the insert wins.
3842 const duplicate = yield* findConnectionRow({
3843 owner: input.owner,
3844 integration: input.integration,
3845 name,
3846 });
3847 let retryingRowId: string | null = null;
3848 let retryingItemIds: readonly string[] = [];
3849 if (duplicate) {
3850 const duplicateItemIds = Object.values(connectionItemIds(duplicate));
3851 const duplicateProvider = credentialProviders.get(duplicate.provider);
3852 const duplicateAttempt = parseCredentialWriteAttempt(duplicate.credential_write);
3853 const belongsToCrashedRuntime =
3854 duplicateItemIds.length > 0 &&
3855 duplicateAttempt !== null &&
3856 duplicateAttempt.runtimeId !== credentialWriteRuntimeId;
3857 const hasMissingCredential =
3858 belongsToCrashedRuntime && duplicateProvider?.set
3859 ? (yield* Effect.forEach(duplicateItemIds, (itemId) =>
3860 duplicateProvider
3861 .get(ProviderItemId.make(itemId))
3862 .pipe(Effect.map((value) => value === null)),
3863 )).some(Boolean)
3864 : false;
3865 retryingRowId = hasMissingCredential ? storageRowId(duplicate) : null;

Callers 1

createExecutorFunction · 0.85

Calls 15

connectionIdentifierFunction · 0.90
credentialAttemptItemIdFunction · 0.90
snapshotCredentialWritesFunction · 0.90
afterCommitFunction · 0.90
afterCommitRequiredFunction · 0.90
touchSubjectFunction · 0.90
guardOrgWriteFunction · 0.85
findIntegrationRowFunction · 0.85
findConnectionRowFunction · 0.85

Tested by

no test coverage detected