# NoClick

> Connection settings for the platform API.

## NoClick

```typescript
class NoClick
```

Connection settings for the platform API. Most code never builds one: the
default client reads `NOCLICK_API_KEY` / `NOCLICK_API_URL` (inside a deployed
app without a key, it signs in as the app with `NOCLICK_DEPLOYMENT_TOKEN`). Pass
`{ client }` to any method to use another account or endpoint.

| Attribute | Type | Description |
| --- | --- | --- |
| `baseUrl` | `string` |  |
| `timeoutMs` | `number` |  |
| `maxRetries` | `number` |  |
| `backoffMs` | `number` |  |
| `maxBackoffMs` | `number` |  |
| `apiKey` | `string | ApiKeyProvider | undefined` |  |
| `inApp` | `boolean` | Whether this client signs in as a deployed app's own code (its deployment's credential), where `fromName` resolves names in the app's own namespace for the environment it runs in. |

Types:

```typescript
/** Returns the bearer for one attempt; called again on every retry. */
type ApiKeyProvider = () => string | Promise<string>;
```

### NoClick.forSession

```typescript
static forSession(
  session: string | { token: string; baseUrl?: string },
  options?: Omit<ClientOptions, "apiKey">,
): NoClick
```

A client that authenticates with a user session (a `Session` or its
`nk_sess_...` token): every call acts for the session's user with its
scopes. The same as `new NoClick({ apiKey: token })`.

Parameters:

- `session` (`string | { token: string; baseUrl?: string }`, required)
- `options.baseUrl` (`string`): Defaults to `NOCLICK_API_URL`, then `https://api.noclick.com`.
- `options.timeoutMs` (`number`): Per-attempt request timeout (default 60s).
- `options.maxRetries` (`number`): Retries for 408/429/5xx (never a POST's 500, which is replayed) and network errors (default 3).
- `options.backoffMs` (`number`): First backoff step; doubles per attempt with jitter (default 500ms).
- `options.maxBackoffMs` (`number`)
- `options.headers` (`Record<string, string>`)
- `options.fetch` (`typeof fetch`): A custom `fetch` (a proxy agent, a test double). Defaults to the global one.

Returns: A client for that session, on the session's own API endpoint unless you pass `base_url`.

### NoClick.request

```typescript
request(method: string, path: string, options?: RequestOptions): Promise<T>
```

Send one API call and return its decoded body. Writes carry an
`Idempotency-Key` reused on every retry, so a retried POST can't do the work twice.

Parameters:

- `method` (`string`, required)
- `path` (`string`, required)
- `options.json` (`unknown`)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user, `null` = the account.
- `options.idempotencyKey` (`string`): Reused on every retry; generated for each write when omitted.
- `options.body` (`string | Uint8Array | ArrayBuffer | Blob`): A raw body (bytes or text) instead of `json`.
- `options.contentType` (`string`, default `"application/octet-stream"`)
- `options.query` (`Record<string, QueryValue>`)
- `options.timeoutMs` (`number`)
- `options.signal` (`AbortSignal`)
- `options.responseType` (`"json" | "bytes"`): `bytes` returns a `Uint8Array` instead of decoded JSON.
- `options.keepalive` (`boolean`): In a browser, let the request outlive the page that sent it (fetch's `keepalive`), as a closing tab's last save
needs. The browser caps such a body at 64 KB.

Returns: The response's decoded JSON (`None` for an empty body), or its raw bytes with `binary=True`.
An error response raises its `noclick.Error` instead.

Types:

```typescript
type QueryValue = string | number | boolean | null | undefined;

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### NoClick.consoleUrl

```typescript
consoleUrl(section: string, id: string): string | null
```

The developer console page for an object (`operations?op=`, `agents/`, `users/`), or
null when this client doesn't talk to the hosted API.

Parameters:

- `section` (`string`, required)
- `id` (`string`, required)

Returns: The page's URL, or `None` when this client doesn't talk to the hosted API.

### NoClick.asUser

```typescript
asUser(user: UserRef | null, fn: () => T | Promise<T>): Promise<T>
```

Run `fn` acting for `user`; the same as the module-level `asUser`.

Parameters:

- `user` (`UserRef | null`, required)
- `fn` (`() => T | Promise<T>`, required)

Types:

```typescript
/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

## configure

```typescript
function configure(options?: ClientOptions): NoClick
```

Replace the default client, e.g. `configure({ apiKey: "nk_live_..." })`.

Parameters:

- `options.apiKey` (`string | ApiKeyProvider`): A key, or a function returning one per attempt (a refreshing token). Defaults to `NOCLICK_API_KEY`
(inside a deployed app without a key, its deployment's own credential, `NOCLICK_DEPLOYMENT_TOKEN`).
- `options.baseUrl` (`string`): Defaults to `NOCLICK_API_URL`, then `https://api.noclick.com`.
- `options.timeoutMs` (`number`): Per-attempt request timeout (default 60s).
- `options.maxRetries` (`number`): Retries for 408/429/5xx (never a POST's 500, which is replayed) and network errors (default 3).
- `options.backoffMs` (`number`): First backoff step; doubles per attempt with jitter (default 500ms).
- `options.maxBackoffMs` (`number`)
- `options.headers` (`Record<string, string>`)
- `options.fetch` (`typeof fetch`): A custom `fetch` (a proxy agent, a test double). Defaults to the global one.

Returns: The new default client, which every call without `client=` now uses.

Types:

```typescript
/** Returns the bearer for one attempt; called again on every retry. */
type ApiKeyProvider = () => string | Promise<string>;
```

## defaultClient

```typescript
function defaultClient(): NoClick
```

The client used when no `{ client }` is given (built from the environment).

Returns: The shared default client: the one `configure` last set, else one built from `NOCLICK_API_KEY`
and `NOCLICK_API_URL` (inside a deployed app without a key, signed in as the app with
`NOCLICK_DEPLOYMENT_TOKEN`).

## VERSION

```typescript
const VERSION: "0.2.3"
```

This client's version.
## DEFAULT_BASE_URL

```typescript
const DEFAULT_BASE_URL: "https://api.noclick.com"
```

The hosted API; `NOCLICK_API_URL` (or `base_url=`) points a client elsewhere.
## CONSOLE_BASE_URL

```typescript
const CONSOLE_BASE_URL: "https://noclick.com/dashboard/developers"
```

Where the hosted API's objects are shown (only for the hosted base URL).
