Somnolence is a simple framework for providing strongly typed inputs and outputs to your REST API (soon in multiple languages!). The best part is that users of your API don't need access to your source code to generate a type safe client. They can just use Somnolence CLI to generate one, automatically!
Sometimes it's better to show than tell:
# NPM
$ npm install @somnolence/node
$ npm install -g @somnolence/cli
# PNPM
$ pnpm add @somnolence/node
$ pnpm add -g @somnolence/cli
# Yarn
$ yarn add @somnolence/node
$ yarn global add @somnolence/cli
# Bun runs on the much faster (2.5x) Bun HTTP server!
$ bun add @somnolence/bun
$ bun add -g @somnolence/cli
import { createRoute, createSomnolenceServer, t } from '@somnolence/bun' // Or @somnolence/node
const somnolence = createSomnolenceServer({
routes: {
hello: {
GET: createRoute({
query: t.Object({ name: t.String() }),
response: t.String(),
handler: ({ query: { name } }) => `Hello, ${name}!`,
}),
},
},
})
somnolence.start()
The "name" property in the handler automatically shows that it's a string:
Incorrectly putting the "body" property throws up an error:
curl http://localhost:3000/hello\?name\=Gabriel
# Hello, Gabriel!
curl http://localhost:3000/hello\?name\=1
# Expected string
curl http://localhost:3000/hello\?name\=false
# Expected string
curl http://localhost:3000/hello\?name\=comma,delimited,values,make,an,array
# Expected string
# path/to/your/client
$ somnolence-ts --endpoint http://localhost:3000
# 💤 Generated Somnolence Client at node_modules/@somnolence/client
import somnolence from '@somnolence/client'
somnolence
.hello({ query: { name: 'world' } })
.then(response => console.log(response.body)) // 'Hello, world!'
The client automatically knows what inputs are required
The client automatically knows the response is a string
(Coming soon!)
(Coming later!)
(Coming even later!)
(Coming never!)
Let me know what languages you'd like to see Somnolence implemented in. I'll likely be willing to take a crack at it!