Skip to content

Ronmi/rtoolkit-jsonapi-fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Communicating with server written with rtoolkit/jsonapi

Need polyfill for Promise and fetch if running on legacy browser.

Synopsis (in typescript)

import * as api from 'rtoolkit-jsonapi-fetch';

interface MyParam {
    param1: string;
    param2: string;
}

interface MyResp {
    data1: string;
    data2: string;
}

async function myApi(params: MyParam): Promise<MyResp> {
    return await api.post<MyResp>('/api/my_endpoint', params)
}

try {
    const resp = await myApi(params);
    console.log(resp);
} catch (e) {
    const msg = api.handleError<string>(e, (x) => {
        // api returns error code and message
        console.log("Error code: " + x.code);
        console.log("Error detail: " + x.detail);
        return 'Server returns error code: ' + x.code);
    }, (e) => {
        // general error like invalid response/connot connect to server/...
        console.log(e);
        return 'Unexpected error: ' + e.message;
    });

    alert(msg);
} finally {
    cleanup();
}

About

client for rtoolkit/jsonapi implemented in fetch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published