(
row: ConnectionRow,
provider: CredentialProvider,
token: OAuth2TokenResponse,
storedRefreshToken?: string | undefined,
)
| 2087 | * has not changed; a rotated token never matches, so the write that |
| 2088 | * actually matters is never skipped. */ |
| 2089 | const persistRefreshedToken = ( |
| 2090 | row: ConnectionRow, |
| 2091 | provider: CredentialProvider, |
| 2092 | token: OAuth2TokenResponse, |
| 2093 | storedRefreshToken?: string | undefined, |
| 2094 | ): Effect.Effect<void, StorageFailure> => |
| 2095 | Effect.gen(function* () { |
| 2096 | if (provider.set) { |
| 2097 | // OAuth is always single-input: the access token lives in the `token` |
| 2098 | // item. Fall back to a deterministic id if the map is somehow empty. |
| 2099 | const tokenItemId = |
| 2100 | connectionItemIds(row)[PRIMARY_INPUT_VARIABLE] ?? |
| 2101 | `connection:${row.owner}:${row.integration}:${row.name}:${PRIMARY_INPUT_VARIABLE}`; |
| 2102 | if ( |
| 2103 | token.refresh_token && |
| 2104 | row.refresh_item_id && |
| 2105 | token.refresh_token !== storedRefreshToken |
| 2106 | ) { |
| 2107 | yield* provider.set(ProviderItemId.make(row.refresh_item_id), token.refresh_token); |
| 2108 | } |
| 2109 | yield* provider.set(ProviderItemId.make(tokenItemId), token.access_token); |
| 2110 | } |
| 2111 | |
| 2112 | const nextExpiresAt = |
| 2113 | typeof token.expires_in === "number" ? Date.now() + token.expires_in * 1000 : null; |
| 2114 | const set: Record<string, unknown> = { |
| 2115 | expires_at: nextExpiresAt, |
| 2116 | updated_at: new Date(), |
| 2117 | }; |
| 2118 | if (token.scope !== undefined) set.oauth_scope = token.scope; |
| 2119 | yield* core.updateMany("connection", { |
| 2120 | where: (b: AnyCb) => |
| 2121 | b.and( |
| 2122 | byOwner(row.owner as Owner)(b), |
| 2123 | b("integration", "=", String(row.integration)), |
| 2124 | b("name", "=", String(row.name)), |
| 2125 | ), |
| 2126 | set, |
| 2127 | }); |
| 2128 | }); |
| 2129 | |
| 2130 | /** The rendered message of a typed enterprise-managed failure. */ |
| 2131 | const enterpriseManagedMessage = (cause: EnterpriseManagedMintError): string => |
no test coverage detected