Skip to content

100% typesafe wrapper around Express.js

License

Notifications You must be signed in to change notification settings

typeofweb-org/server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Oct 7, 2021
e789666 · Oct 7, 2021

History

32 Commits
Jun 29, 2021
Jul 15, 2021
Jun 30, 2021
Jun 30, 2021
Jul 3, 2021
Jul 16, 2021
Jul 15, 2021
Jun 29, 2021
Oct 7, 2021
Jul 16, 2021
Jun 30, 2021
Jun 29, 2021
Jun 30, 2021
Jul 15, 2021
Jun 9, 2021
Jun 9, 2021
Jun 30, 2021
Jun 13, 2021
Jun 9, 2021
Jun 21, 2021
Jun 9, 2021
Jul 16, 2021
Jul 15, 2021
Jun 9, 2021
Jun 14, 2021
Oct 7, 2021
Jun 19, 2021
Jun 13, 2021
Jun 30, 2021
Jun 30, 2021
Jul 15, 2021

Repository files navigation

@typeofweb/server

All Contributors

codecov npm

Docs

Sponsors

<your name here>

See opencollective.com/typeofweb or github.com/sponsors/typeofweb! ❤️

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Bartłomiej Wiśniewski

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Example

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();
// dbPlugin.ts

import { 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(),
  };
});
// authPlugin.ts

import { 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 };
    },
  };
});