Skip to content

Commit 82ec67c

Browse files
author
Wojciech Wierchola
committed
fixs docs
1 parent b668c6a commit 82ec67c

File tree

12 files changed

+120
-67
lines changed

12 files changed

+120
-67
lines changed

README.md

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,75 @@ Tiny, isomorphic TypeScript framework to build "call action" APIs.
66

77
`npm i @webcarrot/api`
88

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+
```
69+
70+
## TODO
71+
72+
- Allow to define an optional payload.
73+
- Firebase helper.
74+
975
## Example code
1076

11-
See [example](https://github.com/webcarrot/api/tree/master/example)
77+
See [example code](https://github.com/webcarrot/api/tree/master/example)
1278

1379
Anyway same code:
1480

@@ -34,7 +100,7 @@ export type AppState = {
34100

35101
### Node only side
36102

37-
#### Hi action (`example/api/hi.ts`):
103+
#### Hi action (`example/api/hi.ts`)
38104

39105
```typescript
40106
import { Context as KoaContext } from "koa";
@@ -151,7 +217,7 @@ const api = makeApi<ApiData>({
151217

152218
### React
153219

154-
#### Make context (`example/apiContext.ts`);
220+
#### Make context (`example/apiContext.ts`)
155221

156222
```typescript
157223
import { makeContext } from "@webcarrot/api/context";
@@ -160,7 +226,7 @@ import { ApiData } from "./types";
160226
export const Context = makeContext<ApiData>();
161227
```
162228

163-
#### App (`example/app.tsx`)
229+
#### React main "App" Component (`example/app.tsx`)
164230

165231
```typescript
166232
import * as React from "react";
@@ -184,24 +250,16 @@ const IUseApi = ({ value = "" }) => {
184250
);
185251
};
186252

187-
export const App = ({
188-
api,
189-
hiFromServer = ""
190-
}: {
191-
api: ApiContextValue;
192-
hiFromServer: string;
193-
}) => {
253+
export const App = ({ api, hi = "" }: { api: ApiContextValue; hi: string }) => {
194254
return (
195255
<ApiContext.Provider value={api}>
196-
<IUseApi value={hiFromServer} />
256+
<IUseApi value={hi} />
197257
</ApiContext.Provider>
198258
);
199259
};
200260
```
201261

202-
#### Server side
203-
204-
Render react app on server (`example/node/react.ts`)
262+
#### Render react app on server (`example/node/react.ts`)
205263

206264
```typescript
207265
import * as React from "react";
@@ -237,19 +295,21 @@ export const handler = async (context: KoaContext) => {
237295
<div id="app">${ReactDOM.renderToString(
238296
React.createElement(App, {
239297
api,
240-
hiFromServer: hi
298+
hi
241299
})
242300
)}</div>
243-
<script>APP_STATE=${JSON.stringify(APP_STATE)};</script>
301+
<script src="https://unpkg.com/react@16.8.3/umd/react.production.min.js"></script>
302+
<script src="https://unpkg.com/react-dom@16.8.3/umd/react-dom.production.min.js"></script>
303+
<script>window.process={env:{NODE_ENV:"production"}};APP_STATE=${JSON.stringify(
304+
APP_STATE
305+
)};</script>
244306
<script src="/build/react.js" async defer></script>
245307
</body>
246308
</html>`;
247309
};
248310
```
249311

250-
#### Browser side
251-
252-
Render react app in browser (`example/browser/react.ts`)
312+
#### Render react app in browser (`example/browser/react.ts`)
253313

254314
```typescript
255315
import * as React from "react";
@@ -259,12 +319,12 @@ import { ApiData, AppState } from "../types";
259319
import { App } from "../app";
260320

261321
declare var APP_STATE: AppState;
262-
const appState = APP_STATE;
322+
const { api: apiConf, hi } = APP_STATE;
263323

264-
const api = makeApi<ApiData>(appState.api);
324+
const api = makeApi<ApiData>(apiConf);
265325

266326
ReactDOM.hydrate(
267-
React.createElement(App, { api, hiFromServer: appState.hi }),
327+
React.createElement(App, { api, hi }),
268328
document.getElementById("app")
269329
);
270330
```

browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResolver, ApiData } from "./index";
1+
import { ApiResolver, ApiData } from "@webcarrot/api";
22

33
export const makeApi = <Data extends ApiData>({
44
endpoint,

context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { ApiResolver, ApiData } from "./index";
2+
import { ApiResolver, ApiData } from "@webcarrot/api";
33

44
const defaultValue = () => Promise.reject();
55

example/app.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@ const IUseApi = ({ value = "" }) => {
1919
);
2020
};
2121

22-
export const App = ({
23-
api,
24-
hiFromServer = ""
25-
}: {
26-
api: ApiContextValue;
27-
hiFromServer: string;
28-
}) => {
22+
export const App = ({ api, hi = "" }: { api: ApiContextValue; hi: string }) => {
2923
return (
3024
<ApiContext.Provider value={api}>
31-
<IUseApi value={hiFromServer} />
25+
<IUseApi value={hi} />
3226
</ApiContext.Provider>
3327
);
3428
};

example/browser/react.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { ApiData, AppState } from "../types";
55
import { App } from "../app";
66

77
declare var APP_STATE: AppState;
8-
const appState = APP_STATE;
8+
const { api: apiConf, hi } = APP_STATE;
99

10-
const api = makeApi<ApiData>(appState.api);
10+
const api = makeApi<ApiData>(apiConf);
1111

1212
ReactDOM.hydrate(
13-
React.createElement(App, { api, hiFromServer: appState.hi }),
13+
React.createElement(App, { api, hi }),
1414
document.getElementById("app")
1515
);

example/node/react.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ export const handler = async (context: KoaContext) => {
3131
<div id="app">${ReactDOM.renderToString(
3232
React.createElement(App, {
3333
api,
34-
hiFromServer: hi
34+
hi
3535
})
3636
)}</div>
37-
<script>APP_STATE=${JSON.stringify(APP_STATE)};</script>
37+
<script src="https://unpkg.com/react@16.8.3/umd/react.production.min.js"></script>
38+
<script src="https://unpkg.com/react-dom@16.8.3/umd/react-dom.production.min.js"></script>
39+
<script>window.process={env:{NODE_ENV:"production"}};APP_STATE=${JSON.stringify(
40+
APP_STATE
41+
)};</script>
3842
<script src="/build/react.js" async defer></script>
3943
</body>
4044
</html>`;

example/tsconfig.webpack.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"sourceMap": true
5+
},
6+
"exclude": []
7+
}

example/webpack.config.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ module.exports = {
88
output: {
99
path: join(__dirname, "./build"),
1010
filename: "./build/react.js",
11-
publicPath: "/"
11+
publicPath: "/build"
1212
},
1313
watch: false,
1414
mode: "production",
15-
devtool: "none",
15+
devtool: "inline-source-map",
16+
externals: {
17+
react: "React",
18+
"react-dom": "ReactDOM"
19+
},
1620
resolve: {
1721
extensions: [".ts", ".tsx", ".js"],
1822
plugins: [
1923
new TsConfigPathsPlugin({
20-
tsconfig: join(__dirname + "./tsconfig.json"),
24+
tsconfig: join(__dirname + "./tsconfig.webpack.json"),
2125
compiler: "typescript"
2226
})
2327
]
@@ -37,6 +41,7 @@ module.exports = {
3741
]
3842
},
3943
optimization: {
40-
nodeEnv: "production"
44+
nodeEnv: "production",
45+
minimize: false
4146
}
4247
};

index.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResolver, ApiData } from "./index";
1+
import { ApiResolver, ApiData } from "@webcarrot/api";
22

33
export const makeApi = <Data extends ApiData, Context>({
44
actions,

0 commit comments

Comments
 (0)