3.0.0 • Published 5 months ago
@webcarrot/api v3.0.0
@webcarrot/api ·

Tiny, isomorphic TypeScript framework to build "call action" APIs.
Instalation
npm i @webcarrot/api
General
This library provides generic TypeScript interfaces plus simple implementations / helpers for node and browsers enviroment.
// interfaces - the essence
import { ApiResolver, ActionFunction } from "@webcarrot/api";
// implementations / helpers
import { makeApi as nodeMakeApi } from "@webcarrot/api/node";
import { makeApi as browserMakeApi } from "@webcarrot/api/browser";
type ActionContext = { context: string; zee: number };
const action: ActionFunction<
{ payload: string; foo: number },
{ output: string; bar: number },
ActionContext
> = async ({ payload, foo }, { context, zee }) => ({
output: `payload: ${payload} context: ${context}`,
bar: foo + zee,
});
// Types are build from plain object like:
const actions = {
actionName: action,
};
type Api = ApiResolver<typeof actions>;
// cusom implementation
const someCustomApiProvider: Api = (actionName, actionPayload) => {
// call api function implementation
};
someCustomApiProvider("actionName", { payload: "c", foo: 1 }).then(
({ output, bar }) => console.log({ output, bar }),
);
// node helper usage
const nodeApiProvider = nodeMakeApi<typeof actions, ActionContext>({
actions,
context: { context: "z", zee: 4 },
});
nodeApiProvider("actionName", { payload: "n", foo: 2 }).then(
({ output, bar }) => console.log({ output, bar }),
);
// browser helper usage
const browserApiProvider = browserMakeApi<typeof actions>({
endpoint: "/api",
headers: {
"X-Foo": "Bar",
},
});
browserApiProvider("actionName", { payload: "b", foo: 3 }).then(
({ output, bar }) => console.log({ output, bar }),
);
3.0.0
5 months ago
1.8.1
3 years ago
2.0.0
3 years ago
1.8.0
5 years ago
1.7.0
5 years ago
1.6.3
5 years ago
1.6.2
5 years ago
1.6.1
5 years ago
1.6.0
5 years ago
1.5.2
5 years ago
1.5.1
5 years ago
1.5.0
5 years ago
1.4.0
6 years ago
1.3.1
6 years ago
1.3.0
6 years ago
1.2.0
6 years ago
1.1.4
6 years ago
1.1.3
6 years ago
1.1.2
6 years ago
1.1.1
6 years ago
1.1.0
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago
0.0.1
6 years ago