Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yornaath/fp-ts-http
Opinionated, strongly typed http middleware library using fp-ts fp-ts-routing and io-ts for decoding of io
https://github.com/yornaath/fp-ts-http
fp-ts functional functional-programming http io-ts koa koa2 rest routing types
Last synced: about 2 months ago
JSON representation
Opinionated, strongly typed http middleware library using fp-ts fp-ts-routing and io-ts for decoding of io
- Host: GitHub
- URL: https://github.com/yornaath/fp-ts-http
- Owner: yornaath
- Created: 2019-06-28T20:27:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T18:26:05.000Z (about 2 years ago)
- Last Synced: 2024-11-12T19:48:39.673Z (about 2 months ago)
- Topics: fp-ts, functional, functional-programming, http, io-ts, koa, koa2, rest, routing, types
- Language: TypeScript
- Homepage:
- Size: 782 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# fp-ts-http
### Install
Includes `fp-ts` `fp-ts-routing` and `io-ts`
```bash
npm i fp-ts-http
```## Example
```typescript
import { end, lit, int } from 'fp-ts-routing'
import * as io from "io-ts"
import { get, post, driver } from "./"
import { TMiddlewareStack } from './Middleware';
import { none } from 'fp-ts/lib/Option';const stack: TMiddlewareStack = []
const userById = lit('users').then(int("userid"))
const stack2 = [
...stack,
...get<{userid: number}, {}, string>(userById.then(end), io.strict({}),
async(req) => {
return {
status: 200,
headers: none,
body: JSON.stringify(req.path)
}
})]const userMessages = userById.then(lit("messages"))
const userMessageDto = io.type({ message: io.string })const stack3 = [
...stack2,
...post<{userid: number}, {}, {message: string}, string>(userMessages.then(end), io.strict({}), userMessageDto, async(req) => {
return {
status: 200,
headers: none,
body: JSON.stringify(req.path)
}
})]driver(stack3, 3000).run()
.then(() => console.log("server running"))
```## Advanced query parsing example
```typescriptimport { end, lit, query, str } from 'fp-ts-routing'
import * as io from "io-ts"
import { get, post, driver } from "."
import { TMiddlewareStack } from './Middleware';
import { none } from 'fp-ts/lib/Option';
import { NumberFromString, BooleanFromString, ArrayFromString } from './Query';const stack: TMiddlewareStack = []
type TUsersPath = {
type: string;
}const usersQuery = io.strict({
filter: io.union([ io.undefined, io.string ]),
validStatusCodes: io.union([ io.undefined, ArrayFromString(NumberFromString) ]),
sortBy: io.union([ io.undefined, io.string]),
sortDirection: io.union([ io.undefined, BooleanFromString])
})type TUsersQuery = io.TypeOf
type TUsersResponseBody = string
const users = lit("users").then(str("type"))
const stack2 = [
...stack,
...get(users.then(end), usersQuery,
async(req) => {
return {
status: 200,
headers: none,
body: await UserRepository.find({
type: req.path.type,
sortBy: req.path.sortBy,
sortDirection: req.path.sortDirection
})
}
})]type TUserMessageDto = {
message: string
}const userMessages = users.then(lit("messages"))
const userMessageDto = io.type({ message: io.string })type TUserMessagesResponseBody = string
const stack3 = [
...stack2,
...post(userMessages.then(end), usersQuery, userMessageDto,
async(req) => {const users = await UserRepository.find({
type: req.path.type,
sortBy: req.path.sortBy,
sortDirection: req.path.sortDirection
})await Promise.all(users.map(async user => sendMessageToUser(user, req.body)))
return {
status: 200,
headers: none,
body: JSON.stringify(req.path)
}
})]driver(stack3, 3000).run()
.then(() => console.log("server running"))```