generic-client-interface

A wrapper for creation a group of clients that are independent from each other, easily testable, and asynchronously imported.

CC0-1.0 License

Downloads
574
Stars
0
Committers
1

generic-client-interface

A wrapper for creation a group of clients that are independent from each other, easily testable, and asynchronously imported.

Installation

npm i generic-client-interface

Usage

Full api reference: https://electrovir.github.io/generic-client-interface/

  • Define individual clients (one exported per file) with defineClient.
  • Define a whole client interface with defineClientInterface.

Defining a client

import {defineClient} from 'generic-client-interface';

/** Clients must be exported. Only one client can be exported per file. */
export const exampleClient1 = defineClient({
    /** Define the client's members. */
    doStuff() {
        console.log('hello');
    },
    async sendRequest(url: string) {
        return await (await fetch(url)).json();
    },
});

Defining a whole client interface

import {awaitAllClients, defineClientInterface} from 'generic-client-interface';

const myClientInterface = defineClientInterface({
    /** Establish the client names and where they're imported from. */
    exampleClient: () => import('./define-client.example'),
});

async function exampleMain() {
    /** A client must be awaited before it can be accessed. */
    (await myClientInterface.exampleClient).doStuff();

    /** All clients can be awaited in a single promise. */
    const clients = await awaitAllClients(myClientInterface);
    clients.exampleClient.doStuff();
}