| … | … | @@ -0,0 +1,81 @@ |
| 1 | # hyper-web-ui |
| 2 | |
| 3 | A shared web framework for P2P apps — JSON API router, SSR + SPA serving, identity client. |
| 4 | |
| 5 | Small and dependency-light (one runtime dep, `hypercore-crypto`). It is the HTTP layer several |
| 6 | of the P2P daemons in this tree serve their web UI through, rather than each growing its own. |
| 7 | |
| 8 | ## Install |
| 9 | |
| 10 | ```sh |
| 11 | npm install hyper-web-ui |
| 12 | ``` |
| 13 | |
| 14 | ## Usage |
| 15 | |
| 16 | ```js |
| 17 | import { Router, json, html, serveStatic, authMiddleware } from 'hyper-web-ui' |
| 18 | |
| 19 | const router = new Router('/api', { cors: true }) |
| 20 | |
| 21 | router.get('/repos/:name', async (req, res, params) => { |
| 22 | json(res, await lookup(params.name)) |
| 23 | }) |
| 24 | |
| 25 | router.post('/repos', async (req, res) => { |
| 26 | const body = await readJson(req) |
| 27 | json(res, await create(body), 201) |
| 28 | }) |
| 29 | ``` |
| 30 | |
| 31 | ## API |
| 32 | |
| 33 | #### `new Router(prefix = '', { cors, corsOrigin })` |
| 34 | |
| 35 | `.get()`, `.post()`, `.patch()`, `.del()` — each takes `(pattern, handler, opts)`. Patterns |
| 36 | support `:params`. |
| 37 | |
| 38 | #### Responses |
| 39 | |
| 40 | ```js |
| 41 | json(res, data, status = 200, corsOrigin = null) |
| 42 | html(res, content, status = 200, corsOrigin = null) |
| 43 | jsonCached(res, data, maxAge = 10) |
| 44 | cacheControl(res, opts) |
| 45 | withEtag(req, res, etag, sendFn) // 304 when the etag matches |
| 46 | ``` |
| 47 | |
| 48 | #### Requests |
| 49 | |
| 50 | ```js |
| 51 | await readBody(req) |
| 52 | await readJson(req) |
| 53 | parsePagination(query, opts) // -> { page, pageSize, offset } |
| 54 | ``` |
| 55 | |
| 56 | #### `authMiddleware(opts)` |
| 57 | |
| 58 | Identity-based auth for the router. |
| 59 | |
| 60 | #### `serveStatic(webDir, opts)` |
| 61 | |
| 62 | Serve a directory, with SPA fallback. |
| 63 | |
| 64 | #### Rendering |
| 65 | |
| 66 | ```js |
| 67 | renderMarkdown(text) |
| 68 | renderDiff(patch) |
| 69 | renderCode(content, opts) |
| 70 | renderPagination(total, page, pageSize, baseUrl) |
| 71 | renderBranchSelector(branches, current, baseUrl) |
| 72 | detectLanguage(filename) |
| 73 | ``` |
| 74 | |
| 75 | The `renderDiff` / `renderBranchSelector` / `detectLanguage` set exists because this was |
| 76 | factored out of a git forge UI; they are useful beyond that but that is where the shape comes |
| 77 | from. |
| 78 | |
| 79 | ## License |
| 80 | |
| 81 | Not yet declared. |