Client

Every component in appframe-data that calls server APIs uses a Client class to make those requests. If no client is specified in the options when constructing the class, the default Client will be used. For applications running in the browser, this is a client using the host name of the application origin, while in Node.js applications, you'll have to specify a default client using the setDefaultClient export.

Example

This example shows how to create a new client with a specified hostname, log in, and then set the default client in an application.

import { Client, setDefaultClient } from "@olenbetong/appframe-data";

let client = new Client("synergi.olenbetong.no");
await client.login(process.env.APPFRAME_USER, process.env.APPFRAME_PWD);

setDefaultClient(client);

API

export declare type LoginOptions = {
    remember?: boolean;
};
export declare class Client {
    private __hostname;
    get hostname(): string;
    constructor(hostname?: string);
    getRequestUrl(path: string): string;
    /**
     * Wraps fetch and adds JSON headers (Accept and Content-Type) and X-Requested-With
     * which is required by some handlers. If timeout is set in `init`, the request will
     * be aborted if not finished by the amount of milliseconds set.
     *
     * If Content-Type should not be JSON, either pass in another Content-Type, or set
     * isJson to false in `init`
     */
    afFetch(url: string, init: RequestInit & {
        timeout?: number;
        isJson?: boolean;
    }): Promise<Response>;
    fetch(input: any, init?: any): Promise<Response>;
    login(username: string, password: string, options?: LoginOptions): Promise<any>;
    setHostname(hostname?: string): void;
}
export declare function getDefaultClient(): Client;
export declare function setDefaultClient(client: Client): void;

Last updated

Was this helpful?