# trmi-http

> This is an HTTP RMI implementation based on [Fastify](https://github.com/fastify/fastify).

Latest version **1.0.3** (published 2021-05-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install trmi-http
pnpm add trmi-http
yarn add trmi-http
bun add trmi-http
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2021-05-30 |
| First published | 2021-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 20.2 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | alexnzarov |
| Maintainers | alexnzarov |
| Keywords | typescript, rmi, remote-method-invocation, trmi, trmi-http, http, remote, invocation |

## Links

- npm: https://www.npmjs.com/package/trmi-http
- Repository: https://github.com/alexnzarov/trmi
- Homepage: https://github.com/alexnzarov/trmi/tree/master/trmi-http#readme
- Issues: https://github.com/alexnzarov/trmi/issues
- npm.io page: https://npm.io/package/trmi-http

## Dependencies (3)

- [got](https://npm.io/package/got.md) ^11.8.2
- [trmi](https://npm.io/package/trmi.md) ^1.0.4
- [fastify](https://npm.io/package/fastify.md) ^3.17.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2021-05-30
- 1.0.2 — 2021-05-30
- 1.0.1 — 2021-05-30
- 1.0.0 — 2021-05-30

## README

# trmi-http [![npm](https://img.shields.io/npm/v/trmi-http?label=trmi-http)](https://www.npmjs.com/package/trmi-http)

This is an HTTP RMI implementation based on [Fastify](https://github.com/fastify/fastify).

## Installation

```sh
yarn add trmi-http
```

```sh
npm i trmi-http
```

## Getting started

You can find a more detailed example [here](https://github.com/alexnzarov/trmi/tree/master/examples/trmi-http).

**Define and implement a remote service**

```ts
type HelloResponse = {
    message: string;
}

interface HelloWorldSpecification {
    hello(world: string): Promise<HelloResponse>;
    bye(): Promise<void>;
}
```

```ts
import { RemoteService, RemoteMethod } from 'trmi-http';

@RemoteService()
class HelloWorld implements HelloWorldSpecification {
    @RemoteMethod()
    async hello(world: string): Promise<HelloResponse> {
        return {
            message: `Hello ${world}!`,
        };
    }

    @RemoteMethod()
    async bye(): Promise<void> {
        throw new Error('"bye" method is not implemented');
    }
}
```

**Start a remote service server**

```ts
import { HttpRemoteServer } from 'trmi-http';

HttpRemoteServer.create({ port: 3478 })
    .from(HelloWorld) // it is possible to pass varargs here
    .start()
    .catch(e => console.error('Failed to start a server', e));
```

**Start a remote client**

```ts
import { HttpRemoteServer } from 'trmi-http';

const client = HttpRemoteClient.create({ url: 'http://127.0.0.1:3478' }).start();

const helloWorld = client.getService<HelloWorldSpecification>('HelloWorld');
const response = await helloWorld.hello('world');

console.log(response.message);

helloWorld.bye().catch(console.log);

```

## Configuration

**Remote service name**

By default, the remote service name defaults to the class name. You can override this behaviour by passing a name property. Characters `.~` are restricted as they are used in internal key generation.

```ts
@RemoteService({ name: 'MyServer_HelloWorld' })
class HelloWorld implements HelloWorldSpecification
```

```ts
client.getService<HelloWorldSpecification>('MyServer_HelloWorld');
```

**Authorization**

By default, server accepts requests from everywhere. You can change that by passing a AuthorizationProvider instance.

```ts
HttpRemoteServer.create({ authorization: TokenAuthorizationProvider.create('secret'), ... });
```

```ts
HttpRemoteClient.create({ authorization: TokenAuthorizationProvider.create('secret'), ... });
```

## Implementation

Server uses [Fastify](https://github.com/fastify/fastify) to receive requests from clients.

Client uses [got](https://github.com/sindresorhus/got) to send requests.

---
_Source: https://npm.io/package/trmi-http · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
