# @andco/bank-sdk

> Definición del Resource Server del Banco de Andco — scopes, RAR y el cliente ligado.


```bash
npm install @andco/sdk @andco/bank-sdk
```

## Bank

```ts
import { Bank } from "@andco/bank-sdk";

const bank = new Bank();
```

Igual que cualquier Resource Server Definition (`AndcoResourceServer`), es un valor inerte: no hace red ni necesita sesión para existir. Se construye una vez y se comparte.

| Opción | Por omisión | Qué hace |
| --- | --- | --- |
| `resource` | `https://api.andco.cl` | Resource Indicator (RFC 8707) de este servidor |
| `endpoint` | igual a `resource` | origen HTTP real de las peticiones |
| `apiVersion` | `_` (contrato vigente) | versión fija del contrato OAuth |
| `initialScopes` | `[]` | scopes que `authorization()` agrega cuando no le pasas otros |

## authorization

```ts
bank.authorization(options?: {
  scopes?: readonly BankScope[];
  authorizationDetails?: readonly (BankAccountAuthorizationDetail | AutomaticChargeAuthorization)[];
  orgId?: string;
}): AndcoResourceAuthorization
```

Produce la contribución que va dentro de `authorizations` en `signIn`:

```ts
const { data: session, error } = await andco.auth.signIn({
  authorizations: [bank.authorization({ scopes: ["bank_accounts:read"] })],
  presentation: "popup",
});
```

`BankScope` es `"bank_accounts:read" | "bank_accounts.movements:read"`. Para permisos más finos sobre una cuenta puntual, un `BankAccountAuthorizationDetail` (RFC 9396) en vez de un scope:

```ts
bank.authorization({
  orgId: "2",
  authorizationDetails: [
    {
      type: "bank_account",
      actions: ["read", "read_movements"],
      subject: { type: "profile" },
      identifiers: ["acc_123"],
    },
  ],
});
```

`BankAccountAuthorizationAction` es `"read" | "read_movements" | "create_deposit_intent" | "read_deposit_intents" | "create_withdrawal_intent" | "execute_withdrawal" | "read_withdrawal_intents"`.

## use

```ts
bank.use(credentials: AndcoCredentials): Bank & { client: AndcoRest; intents: BankIntents }
```

Liga la definición a una credencial y expone `client`, un `AndcoRest` normal apuntando al recurso del Banco, y `intents`, el ciclo de vida de Intents bancarios:

```ts
const bound = bank.use(andco.with(session));
const { data, error } = await bound.client.http.GET("/accounts");
```

`use()` se llama sobre la definición, no sobre el cliente — así ningún método nuevo tiene que agregarse a `AndcoCredentials` para que exista un Resource Server. Ver [Ser un Resource Server](/docs/resource-servers/getting-started) para escribir el equivalente de esta librería para tu propio recurso.

## BankIntents

Todo lo específico del dominio bancario que ya no vive en el `intents` neutro del cliente principal. Compuesto sobre él exactamente como lo haría un Resource Server de terceros:

```ts
createDeposit<T>(input, options?): Promise<Result<T>>
createWithdrawal<T>(input, options?): Promise<Result<T>>
createAutomaticCharge<T>(input, options?): Promise<Result<T>>
get<T>(intentId): Promise<Result<T>>
execute(intentId): Promise<Result<void>>
closeDeposit<T>(intentId): Promise<Result<T>>
getEvents(intentId, after?, signal?): Promise<Result<AndCoEventPage>>
getAccountEvents(accountId, after?, signal?): Promise<Result<AndCoEventPage>>
onIntentDepositEvent(options: { intentId } & AndcoEventSubscriptionOptions, handler): () => void
onIntentWithdrawalEvent(options: { intentId } & AndcoEventSubscriptionOptions, handler): () => void
onDepositEvent(options: { accountId } & AndcoEventSubscriptionOptions, handler): () => void
onWithdrawalEvent(options: { accountId } & AndcoEventSubscriptionOptions, handler): () => void
```

```ts
const { data: deposit, error } = await bound.intents.createDeposit(
  { type: "deposit", expires_in: "30 minutes", amount: { currency: "CLP", suggested: "40000", editable: false } },
  { idempotencyKey: "deposit:8472" },
);
```
