# @serve.zone/dcrouter-apiclient

`@serve.zone/dcrouter-apiclient` is the object-oriented TypeScript client for the dcrouter OpsServer API. It wraps `/typedrequest` calls in managers, builders, and resource classes for routes, certificates, API tokens, remote ingress, email, stats, config, logs, RADIUS, and gateway-client integrations.

## Issue Reporting and Security

For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.

## Install

```bash
pnpm add @serve.zone/dcrouter-apiclient
```

The same client is also exposed as a subpath of the main package:

```typescript
import { DcRouterApiClient } from '@serve.zone/dcrouter/apiclient';
```

## Quick Start

```typescript
import { DcRouterApiClient } from '@serve.zone/dcrouter-apiclient';

const client = new DcRouterApiClient({
  baseUrl: 'https://dcrouter.example.com',
});

await client.login('admin@example.com', 'strong-password');

const { routes, warnings } = await client.routes.list();
console.log(routes.length, warnings.length);

const route = await client.routes.build()
  .setName('api-gateway')
  .setMatch({ ports: 443, domains: ['api.example.com'] })
  .setAction({ type: 'forward', targets: [{ host: '127.0.0.1', port: 8080 }] })
  .save();

await route.toggle(true);
```

## Authentication

The client supports persisted-admin session login and API-token authentication. Initial admin creation is a bootstrap flow exposed by the Ops dashboard and raw TypedRequest contracts; after a persisted admin exists, use that account with `login()`.

```typescript
const sessionClient = new DcRouterApiClient({
  baseUrl: 'https://dcrouter.example.com',
});
await sessionClient.login('admin@example.com', 'strong-password');

const tokenClient = new DcRouterApiClient({
  baseUrl: 'https://dcrouter.example.com',
  apiToken: 'dcr_token_value',
});
```

`baseUrl` is normalized by removing trailing slashes. Requests are sent to `${baseUrl}/typedrequest`. `buildRequestPayload()` injects the current identity and optional API token for manager methods.

## Manager Map

| Manager | Purpose |
| --- | --- |
| `client.routes` | List merged routes, build/update/delete ordinary operator routes, and toggle routes. |
| `client.certificates` | Inspect certificate summaries and trigger certificate operations. |
| `client.apiTokens` | Create, list, toggle, roll, and revoke API tokens. |
| `client.remoteIngress` | Manage edge registrations, statuses, ports, tags, and connection tokens. |
| `client.emails` | Inspect received/cached email items and trigger resend flows. |
| `client.gatewayClients` | Manage gateway-client route, DNS, and domain integration calls. |
| `client.stats` | Read health, counters, summaries, and runtime status. |
| `client.config` | Read the current configuration view. |
| `client.logs` | Read recent log information. |
| `client.radius` | Manage RADIUS clients, VLAN mappings, and accounting sessions. |

## Route Builder

```typescript
const route = await client.routes.build()
  .setName('internal-app')
  .setMatch({
    ports: 443,
    domains: ['internal.example.com'],
  })
  .setAction({
    type: 'forward',
    targets: [{ host: '127.0.0.1', port: 3000 }],
  })
  .setEnabled(true)
  .save();

await route.update({
  action: {
    type: 'forward',
    targets: [{ host: '127.0.0.1', port: 3001 }],
  },
});
```

System routes from `config`, `email`, and `dns` origins are designed to be toggled, not edited. Generic create/update/delete behavior is for ordinary operator-owned routes with origin `api`. API-origin routes carrying managed ownership metadata, including gateway-client routes and HTTP-01 Special Forwards, must be changed through their owning specialized TypedRequest workflow; `client.routes` does not bypass that protection.

## API Tokens and Remote Ingress

```typescript
const token = await client.apiTokens.build()
  .setName('automation')
  .setScopes(['routes:read', 'routes:write'])
  .setExpiresInDays(30)
  .save();

const edge = await client.remoteIngress.build()
  .setName('edge-eu-1')
  .setListenPorts([80, 443])
  .setAutoDerivePorts(true)
  .setEgress({
    enabled: true,
    allowedPorts: [25],
    allowedHostPatterns: ['*.example.com'],
    maxConcurrentStreams: 100,
  })
  .setTags(['production', 'eu'])
  .save();

const connectionToken = await edge.getConnectionToken();
console.log(token.tokenValue, connectionToken);
```

RemoteIngress resources expose `performance` and `egress` fields. `create()` and `update()` accept the same fields as the builder. Egress is default-disabled; dcrouter currently accepts outbound SMTP egress on port `25` only.

## What This Package Is Not

- It does not start dcrouter.
- It does not serve or bundle the Ops dashboard.
- It does not replace `@serve.zone/dcrouter-interfaces` when you want dcrouter-local raw TypedRequest contracts.

Use `@serve.zone/dcrouter` for the server runtime and `@serve.zone/dcrouter-interfaces` for dcrouter-local request/data types. The canonical machine-facing gateway client route, DNS, and domain contracts used by `client.gatewayClients` come from `@serve.zone/interfaces`.

## Development

This folder is published from the dcrouter monorepo via `tspublish.json` with order `5`.

Useful source entry points:

- `index.ts` exports the public client surface.
- `classes.dcrouterapiclient.ts` owns authentication and request dispatch.
- `classes.route.ts` owns route resources and builders.
- `classes.remoteingress.ts`, `classes.apitoken.ts`, `classes.radius.ts`, and the other manager files wrap focused API domains.

## License and Legal Information

This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](../license) file.

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.

Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.

### Company Information

Task Venture Capital GmbH  
Registered at District Court Bremen HRB 35230 HB, Germany

For any legal inquiries or further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
