@tws-js/server
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

TWS - Type-Safe Web Server Framework

Test NPM

TWS Logo
TWS is a framework built with TypeScript to create type-safe web servers.

✅ 100% code coverage

Usage

Installation

npm install @tws-js/server

Example

Start the server:

import { Operation, Schema, createServer } from '@tws-js/server';

const schema = new Schema({
  hello: new Operation({
    input: {
      name: {
        type: 'string',
        required: true,
        description: 'The name to greet',
      },
    },
    output: {
      type: 'object',
      properties: {
        message: {
          type: 'string',
          description: 'The greeting message',
        },
      },
    },
    handler: ({ name }, metadata) => { // "name" automatically typed as string
      return {
        // Typescript will warn if "message" is not a string,
        // as required by the output
        message: `Hello ${name}`,
      };
    },
  }),
});

const server = createServer({
  schema,
  path: '/tws',
  logger: {
    error: (message) => console.error(message),
  },
  enablePlayground: true,
});

server.listen(3000);

Send a request to the server:

curl -X POST \
  http://localhost:3000/tws \
  -H "Content-Type: application/json" \
  -d '{ "operation": "hello", "input": { "name": "TWS" } }'

Check the response:

{
  "data": {
    "message": "Hello TWS"
  }
}

Collaborating

Setup

nvm use 18
npm install

Running

npm start

Testing

npm run lint
npm test

Readme

Keywords

none

Package Sidebar

Install

npm i @tws-js/server

Weekly Downloads

145

Version

1.0.6

License

MIT

Unpacked Size

57.9 kB

Total Files

15

Last publish

Collaborators

  • sergiodeveloper