1.0.0 • Published 3 months ago

generic-client-interface v1.0.0

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
3 months ago

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 '..';

/** 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 '..';

const myClientInterface = defineClientInterface({
    /** Establish the client names and where they're imported from. */
    clientImports: {
        exampleClient: () => import('./define-client.example'),
    },
    /** When set to true, mock clients will be used. */
    isTestEnv: false,
    /** Optionally provide mock implementations. */
    mockClients: {
        exampleClient: {
            doStuff() {
                return '';
            },
            sendRequest() {
                return Promise.resolve({});
            },
        },
    },
});

/** Async wrapper function for async example purposes. */
async function main() {
    /** 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 = awaitAllClients(myClientInterface);
    (await clients).exampleClient.doStuff();
}
1.0.0

3 months ago

0.1.1

6 months ago

0.1.0

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago

0.0.0

7 months ago