NeXTSTEP Programming Mastermind

    trmi-http
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.3 • Public • Published

    trmi-http npm

    This is an HTTP RMI implementation based on Fastify.

    Installation

    yarn add trmi-http
    npm i trmi-http

    Getting started

    You can find a more detailed example here.

    Define and implement a remote service

    type HelloResponse = {
        message: string;
    }
    
    interface HelloWorldSpecification {
        hello(world: string): Promise<HelloResponse>;
        bye(): Promise<void>;
    }
    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

    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

    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.

    @RemoteService({ name: 'MyServer_HelloWorld' })
    class HelloWorld implements HelloWorldSpecification
    client.getService<HelloWorldSpecification>('MyServer_HelloWorld');

    Authorization

    By default, server accepts requests from everywhere. You can change that by passing a AuthorizationProvider instance.

    HttpRemoteServer.create({ authorization: TokenAuthorizationProvider.create('secret'), ... });
    HttpRemoteClient.create({ authorization: TokenAuthorizationProvider.create('secret'), ... });

    Implementation

    Server uses Fastify to receive requests from clients.

    Client uses got to send requests.

    Install

    npm i trmi-http

    DownloadsWeekly Downloads

    7

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    20.7 kB

    Total Files

    29

    Last publish

    Collaborators

    • alexnzarov