Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/typeofweb-org/server
100% typesafe wrapper around Express.js
https://github.com/typeofweb-org/server
express hacktoberfest nodejs typescript
Last synced: 9 days ago
JSON representation
100% typesafe wrapper around Express.js
- Host: GitHub
- URL: https://github.com/typeofweb-org/server
- Owner: typeofweb-org
- License: mit
- Created: 2021-06-09T16:07:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-07T13:06:18.000Z (about 3 years ago)
- Last Synced: 2024-10-16T08:08:02.939Z (about 1 month ago)
- Topics: express, hacktoberfest, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 824 KB
- Stars: 13
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# @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=X1Z8BQ0TFG)](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)):
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,
});app.plugin(dbPlugin);
app.plugin(authPlugin);app.route({
path: '/health-check/:count',
method: 'GET',
validation: {
query: {},
params: {
count: number(),
},
payload: {},
response: {},
},
async handler(request) {
if (!request.plugins.auth.session) {
throw new HttpError(HttpStatusCode.Unauthorized);
}const { params } = request;
const result = await request.server.plugins.db.user.findOne(params.count);request.events.emit('found', result);
return result;
},
});const server = await app.listen();
``````ts
// dbPlugin.tsimport { createPlugin } from '@typeofweb/server';
declare module '@typeofweb/server' {
interface TypeOfWebServerMeta {
readonly db: PrismaClient;
}interface TypeOfWebRequestMeta {
readonly auth: { readonly session: Session };
}interface TypeOfWebServerEvents {
readonly found: User;
}
}export const dbPlugin = createPlugin('db', async (app) => {
return {
server: new Prisma(),
};
});
``````ts
// authPlugin.tsimport { createPlugin } from '@typeofweb/server';
export const authPlugin = createPlugin('auth', async (app) => {
return {
request(request) {
const session = await request.plugins.db.session.findOne({ id: request.cookies.session });
return { session };
},
};
});
```