@tws-js/server
v2.2.1
Published
Type-Safe Web Server Framework
Downloads
395
Readme
TWS - Type-Safe Web Server Framework
This framework was created with the purpose of enhancing the developer and user experience when developing and communicating with strongly typed APIs.
Usage
Installation
npm install @tws-js/server
Example
Start the server:
import { Operation, Schema, HTTPServerHelper } 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}`,
};
},
}),
}, {
logger: {
error: (message) => console.error(message),
},
enablePlayground: true,
});
HTTPServerHelper.create({
port: 3000,
schema,
path: '/tws',
});
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 20
npm install
Running
npm start
Testing
npm run lint
npm test