Skip to content

Commit

Permalink
docs: Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Jun 24, 2021
1 parent 232d7fa commit b60ea57
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
14 changes: 14 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -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
}
73 changes: 63 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
# @typeofweb/server

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

[![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

&lt;your name here>

See [opencollective.com/typeofweb](https://opencollective.com/typeofweb) or [github.com/sponsors/typeofweb](https://github.com/sponsors/typeofweb)! ❤️

<svg class="octicon octicon-heart text-pink" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4.25 2.5c-1.336 0-2.75 1.164-2.75 3 0 2.15 1.58 4.144 3.365 5.682A20.565 20.565 0 008 13.393a20.561 20.561 0 003.135-2.211C12.92 9.644 14.5 7.65 14.5 5.5c0-1.836-1.414-3-2.75-3-1.373 0-2.609.986-3.029 2.456a.75.75 0 01-1.442 0C6.859 3.486 5.623 2.5 4.25 2.5zM8 14.25l-.345.666-.002-.001-.006-.003-.018-.01a7.643 7.643 0 01-.31-.17 22.075 22.075 0 01-3.434-2.414C2.045 10.731 0 8.35 0 5.5 0 2.836 2.086 1 4.25 1 5.797 1 7.153 1.802 8 3.02 8.847 1.802 10.203 1 11.75 1 13.914 1 16 2.836 16 5.5c0 2.85-2.045 5.231-3.885 6.818a22.08 22.08 0 01-3.744 2.584l-.018.01-.006.003h-.002L8 14.25zm0 0l.345.666a.752.752 0 01-.69 0L8 14.25z"></path></svg>

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://typeofweb.com/"><img src="https://avatars0.githubusercontent.com/u/1338731?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michał Miszczyszyn</b></sub></a><br /><a href="https://github.com/typeofweb/server/commits?author=mmiszy" title="Code">💻</a> <a href="#maintenance-mmiszy" title="Maintenance">🚧</a> <a href="#projectManagement-mmiszy" title="Project Management">📆</a> <a href="https://github.com/typeofweb/server/pulls?q=is%3Apr+reviewed-by%3Ammiszy" title="Reviewed Pull Requests">👀</a></td>
</tr>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

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,
Expand All @@ -9,8 +54,6 @@ const app = await createApp({
app.plugin(dbPlugin);
app.plugin(authPlugin);

app._rawRouter;

app.route({
path: '/health-check/:count',
method: 'GET',
Expand All @@ -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;
},
});

Expand All @@ -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;
Expand All @@ -50,7 +97,7 @@ declare module '@typeofweb/server' {
}

interface TypeOfWebServerEvents {
readonly 'health-check': number;
readonly found: User;
}
}

Expand All @@ -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 };
},
};
Expand Down

0 comments on commit b60ea57

Please sign in to comment.