Skip to content

Commit 564a88d

Browse files
committed
Init
0 parents  commit 564a88d

File tree

14 files changed

+2534
-0
lines changed

14 files changed

+2534
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
dist
3+
coverage
4+
.DS_Store
5+
.ncu-output

.ncurc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"upgrade": true,
3+
"reject": ["@types/react"]
4+
}

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22.13

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# @webcarrot/api · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/webcarrot/api/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/@webcarrot/api.svg?style=flat)](https://www.npmjs.com/package/@webcarrot/api)
2+
3+
Tiny, isomorphic TypeScript framework to build "call action" APIs.
4+
5+
## Instalation
6+
7+
`npm i @webcarrot/api`
8+
9+
## General
10+
11+
This library provides generic TypeScript interfaces plus simple implementations / helpers for node and browsers enviroment.
12+
13+
```typescript
14+
// interfaces - the essence
15+
import { ApiResolver, ActionFunction } from "@webcarrot/api";
16+
// implementations / helpers
17+
import { makeApi as nodeMakeApi } from "@webcarrot/api/node";
18+
import { makeApi as browserMakeApi } from "@webcarrot/api/browser";
19+
20+
type ActionContext = { context: string; zee: number };
21+
22+
const action: ActionFunction<
23+
{ payload: string; foo: number },
24+
{ output: string; bar: number },
25+
ActionContext
26+
> = async ({ payload, foo }, { context, zee }) => ({
27+
output: `payload: ${payload} context: ${context}`,
28+
bar: foo + zee,
29+
});
30+
31+
// Types are build from plain object like:
32+
const actions = {
33+
actionName: action,
34+
};
35+
36+
type Api = ApiResolver<typeof actions>;
37+
38+
// cusom implementation
39+
const someCustomApiProvider: Api = (actionName, actionPayload) => {
40+
// call api function implementation
41+
};
42+
43+
someCustomApiProvider("actionName", { payload: "c", foo: 1 }).then(
44+
({ output, bar }) => console.log({ output, bar }),
45+
);
46+
47+
// node helper usage
48+
const nodeApiProvider = nodeMakeApi<typeof actions, ActionContext>({
49+
actions,
50+
context: { context: "z", zee: 4 },
51+
});
52+
53+
nodeApiProvider("actionName", { payload: "n", foo: 2 }).then(
54+
({ output, bar }) => console.log({ output, bar }),
55+
);
56+
57+
// browser helper usage
58+
const browserApiProvider = browserMakeApi<typeof actions>({
59+
endpoint: "/api",
60+
headers: {
61+
"X-Foo": "Bar",
62+
},
63+
});
64+
65+
browserApiProvider("actionName", { payload: "b", foo: 3 }).then(
66+
({ output, bar }) => console.log({ output, bar }),
67+
);
68+
```

eslint.config.mjs

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// @ts-check
2+
3+
import js from "@eslint/js";
4+
import { config, configs as tsConfigs } from "typescript-eslint";
5+
6+
export default config(
7+
js.configs.recommended,
8+
...tsConfigs.recommended,
9+
{
10+
files: [
11+
"src/**/*.{ts,tsx,cts,mts}",
12+
"tests/**/*.{ts,tsx,cts,mts}",
13+
"types/**/*.{ts,tsx,cts,mts}",
14+
],
15+
rules: {
16+
"@typescript-eslint/no-explicit-any": "off",
17+
"@typescript-eslint/no-unused-vars": [
18+
"error",
19+
{
20+
args: "all",
21+
argsIgnorePattern: "^_",
22+
caughtErrors: "all",
23+
caughtErrorsIgnorePattern: "^_",
24+
destructuredArrayIgnorePattern: "^_",
25+
varsIgnorePattern: "^_",
26+
ignoreRestSiblings: true,
27+
},
28+
],
29+
"prefer-const": [
30+
"error",
31+
{
32+
destructuring: "all",
33+
ignoreReadBeforeAssign: false,
34+
},
35+
],
36+
"no-mixed-spaces-and-tabs": "off",
37+
"no-restricted-globals": [
38+
2,
39+
"window",
40+
"self",
41+
"document",
42+
"name",
43+
"location",
44+
"customElements",
45+
"history",
46+
"locationbar",
47+
"menubar",
48+
"personalbar",
49+
"scrollbars",
50+
"statusbar",
51+
"toolbar",
52+
"status",
53+
"closed",
54+
"frames",
55+
"length",
56+
"top",
57+
"opener",
58+
"parent",
59+
"frameElement",
60+
"navigator",
61+
"origin",
62+
"external",
63+
"screen",
64+
"innerWidth",
65+
"innerHeight",
66+
"scrollX",
67+
"pageXOffset",
68+
"scrollY",
69+
"pageYOffset",
70+
"visualViewport",
71+
"screenX",
72+
"screenY",
73+
"outerWidth",
74+
"outerHeight",
75+
"devicePixelRatio",
76+
"clientInformation",
77+
"screenLeft",
78+
"screenTop",
79+
"defaultStatus",
80+
"defaultstatus",
81+
"styleMedia",
82+
"onsearch",
83+
"isSecureContext",
84+
"performance",
85+
"onappinstalled",
86+
"onbeforeinstallprompt",
87+
"crypto",
88+
"indexedDB",
89+
"webkitStorageInfo",
90+
"sessionStorage",
91+
"localStorage",
92+
"onbeforexrselect",
93+
"onabort",
94+
"onblur",
95+
"oncancel",
96+
"oncanplay",
97+
"oncanplaythrough",
98+
"onchange",
99+
"onclick",
100+
"onclose",
101+
"oncontextmenu",
102+
"oncuechange",
103+
"ondblclick",
104+
"ondrag",
105+
"ondragend",
106+
"ondragenter",
107+
"ondragleave",
108+
"ondragover",
109+
"ondragstart",
110+
"ondrop",
111+
"ondurationchange",
112+
"onemptied",
113+
"onended",
114+
"onerror",
115+
"onfocus",
116+
"onformdata",
117+
"oninput",
118+
"oninvalid",
119+
"onkeydown",
120+
"onkeypress",
121+
"onkeyup",
122+
"onload",
123+
"onloadeddata",
124+
"onloadedmetadata",
125+
"onloadstart",
126+
"onmousedown",
127+
"onmouseenter",
128+
"onmouseleave",
129+
"onmousemove",
130+
"onmouseout",
131+
"onmouseover",
132+
"onmouseup",
133+
"onmousewheel",
134+
"onpause",
135+
"onplay",
136+
"onplaying",
137+
"onprogress",
138+
"onratechange",
139+
"onreset",
140+
"onresize",
141+
"onscroll",
142+
"onseeked",
143+
"onseeking",
144+
"onselect",
145+
"onstalled",
146+
"onsubmit",
147+
"onsuspend",
148+
"ontimeupdate",
149+
"ontoggle",
150+
"onvolumechange",
151+
"onwaiting",
152+
"onwebkitanimationend",
153+
"onwebkitanimationiteration",
154+
"onwebkitanimationstart",
155+
"onwebkittransitionend",
156+
"onwheel",
157+
"onauxclick",
158+
"ongotpointercapture",
159+
"onlostpointercapture",
160+
"onpointerdown",
161+
"onpointermove",
162+
"onpointerup",
163+
"onpointercancel",
164+
"onpointerover",
165+
"onpointerout",
166+
"onpointerenter",
167+
"onpointerleave",
168+
"onselectstart",
169+
"onselectionchange",
170+
"onanimationend",
171+
"onanimationiteration",
172+
"onanimationstart",
173+
"ontransitionrun",
174+
"ontransitionstart",
175+
"ontransitionend",
176+
"ontransitioncancel",
177+
"onafterprint",
178+
"onbeforeprint",
179+
"onbeforeunload",
180+
"onhashchange",
181+
"onlanguagechange",
182+
"onmessage",
183+
"onmessageerror",
184+
"onoffline",
185+
"ononline",
186+
"onpagehide",
187+
"onpageshow",
188+
"onpopstate",
189+
"onrejectionhandled",
190+
"onstorage",
191+
"onunhandledrejection",
192+
"onunload",
193+
"alert",
194+
"atob",
195+
"blur",
196+
"btoa",
197+
"cancelAnimationFrame",
198+
"cancelIdleCallback",
199+
"captureEvents",
200+
"close",
201+
"confirm",
202+
"createImageBitmap",
203+
"find",
204+
"focus",
205+
"getComputedStyle",
206+
"getSelection",
207+
"matchMedia",
208+
"moveBy",
209+
"moveTo",
210+
"open",
211+
"postMessage",
212+
"print",
213+
"prompt",
214+
"queueMicrotask",
215+
"releaseEvents",
216+
"requestAnimationFrame",
217+
"requestIdleCallback",
218+
"resizeBy",
219+
"resizeTo",
220+
"scroll",
221+
"scrollBy",
222+
"scrollTo",
223+
"stop",
224+
"webkitCancelAnimationFrame",
225+
"webkitRequestAnimationFrame",
226+
"chrome",
227+
"originAgentCluster",
228+
"trustedTypes",
229+
"speechSynthesis",
230+
"onpointerrawupdate",
231+
"crossOriginIsolated",
232+
"openDatabase",
233+
"webkitRequestFileSystem",
234+
"webkitResolveLocalFileSystemURL",
235+
],
236+
},
237+
},
238+
{
239+
ignores: ["coverage/**", "dist/**", "**/*.mjs", "**/*.cjs"],
240+
},
241+
);

0 commit comments

Comments
 (0)