TWS is a framework built with TypeScript to create type-safe web servers.
✅ 100% code coverage
npm install @tws-js/server
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"
}
}
nvm use 18
npm install
npm start
npm run lint
npm test