@budarin/json-rpc-api-provider
v1.0.5
Published
Provider for any JSON RPC API.
Downloads
321
Readme
json-rpc-api-provider
Provider for any JSON RPC API.
Install the package^
// npm
npm i @budarin/json-rpc-api-provider;
// pnpm
pnpm add @budarin/json-rpc-api-provider;
Create an API provider for your API:
//apiProvider.ts
import type { Todo } from '../types.ts';
import type { JsonRpcResponse } from '@budarin/json-rpc-request';
import { request } from '../../request.ts';
import { createApiProvider } from '@budarin/json-rpc-api-provider';
// Describe your API interface
interface API {
getTodo: () => Promise<JsonRpcResponse<Todo>>;
createTodo: (category: object) => Promise<JsonRpcResponse<Todo>>;
...
}
export const apiProvider = createApiProvider<API>(request); // optional you can pass a pino logger instance
And somwhere in the code:
import { apiProvider } from '../providers/apiProvider.ts'
const { error, result} = apiProvider.getTodo(); // => call 'get_todo' POST method
if (error) {
// log the error
return;
}
...
const newTodo = { ... }
const createTodoResponse = apiProvider.createTodo(newTodo); // => call 'create_todo' POST method
if (createTodoResponse.error) {
//
// log the error
return;
}
...