From b60ea576751805ca77a382607dc0019392af38e2 Mon Sep 17 00:00:00 2001 From: Michal Miszczyszyn Date: Thu, 24 Jun 2021 12:12:42 +0200 Subject: [PATCH] docs: Update readme --- .all-contributorsrc | 14 +++++++++ README.md | 73 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 .all-contributorsrc diff --git a/.all-contributorsrc b/.all-contributorsrc new file mode 100644 index 0000000..b8680b0 --- /dev/null +++ b/.all-contributorsrc @@ -0,0 +1,14 @@ +{ + "projectName": "server", + "projectOwner": "typeofweb", + "repoType": "github", + "repoHost": "https://github.com", + "files": [ + "README.md" + ], + "imageSize": 100, + "commit": true, + "commitConvention": "angular", + "contributors": [], + "contributorsPerLine": 7 +} diff --git a/README.md b/README.md index 16d0176..812190d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,51 @@ # @typeofweb/server + + +[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-) + + + +[![codecov](https://codecov.io/gh/typeofweb/server/branch/main/graph/badge.svg?token=6DNCIHEEUO)](https://codecov.io/gh/typeofweb/server) +[![npm](https://img.shields.io/npm/v/@typeofweb/server.svg)](https://www.npmjs.com/package/@typeofweb/server) + +## Docs + +## Sponsors + +<your name here> + +See [opencollective.com/typeofweb](https://opencollective.com/typeofweb) or [github.com/sponsors/typeofweb](https://github.com/sponsors/typeofweb)! ❤️ + + + +## Contributors ✨ + +Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): + + + + + + + + +

Michał Miszczyszyn

💻 🚧 📆 👀
+ + + + + +This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! + +## Example + ```ts +import { createApp } from '@typeofweb/server'; + +import { dbPlugin } from './dbPlugin'; +import { authPlugin } from './authPlugin'; + const app = await createApp({ host: 'localhost', port: 3000, @@ -9,8 +54,6 @@ const app = await createApp({ app.plugin(dbPlugin); app.plugin(authPlugin); -app._rawRouter; - app.route({ path: '/health-check/:count', method: 'GET', @@ -22,15 +65,17 @@ app.route({ payload: {}, response: {}, }, - _rawMiddlewares: [], async handler(request) { - const { query, params, payload, response } = request; - await request.plugins.db.findOne(); + if (!request.plugins.auth.session) { + throw new HttpError(HttpStatusCode.Unauthorized); + } - request.events.emit('health-check', params.count); + const { params } = request; + const result = await request.server.plugins.db.user.findOne(params.count); - request._rawReq; - request._rawRes; + request.events.emit('found', result); + + return result; }, }); @@ -40,6 +85,8 @@ const server = await app.listen(); ```ts // dbPlugin.ts +import { createPlugin } from '@typeofweb/server'; + declare module '@typeofweb/server' { interface TypeOfWebServerMeta { readonly db: PrismaClient; @@ -50,7 +97,7 @@ declare module '@typeofweb/server' { } interface TypeOfWebServerEvents { - readonly 'health-check': number; + readonly found: User; } } @@ -59,11 +106,17 @@ export const dbPlugin = createPlugin('db', async (app) => { server: new Prisma(), }; }); +``` + +```ts +// authPlugin.ts + +import { createPlugin } from '@typeofweb/server'; export const authPlugin = createPlugin('auth', async (app) => { return { request(request) { - const session = await request.plugins.db.sessions.findOne({ id: request.cookies.session }); + const session = await request.plugins.db.session.findOne({ id: request.cookies.session }); return { session }; }, };