https://github.com/erfanium/sweet-fastify
To create clean and beautiful fastify routes
https://github.com/erfanium/sweet-fastify
Last synced: 9 months ago
JSON representation
To create clean and beautiful fastify routes
- Host: GitHub
- URL: https://github.com/erfanium/sweet-fastify
- Owner: erfanium
- License: mit
- Archived: true
- Created: 2020-11-07T23:10:28.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-15T05:36:46.000Z (over 5 years ago)
- Last Synced: 2024-10-19T22:15:02.266Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sweet-fastify
Build HTTP API much faster, cleaner and safer without losing performance
- There's no separated `req.query`, `req.params` or `req.body` objects, all of them merged into single object called `params`
- Validator schema checker via typescript for extra safely
- Clean and safe authentication middleware handler
## Examples
These examples are used in production by me!
### Basic route
```ts
interface Request {
phone: string
}
interface Response {
digits: number
code?: string
exp: number
next: 'login' | 'register'
}
const logger = new Logger('Code')
const mock = true
export const code = sweet({
method: 'POST',
url: '/users/code',
params: {
phone: 'phone',
},
async handler(params: Request): Promise {
const user = await User.findOneByPhone(params.phone)
const otp = await OTP.generate(params.phone)
logger.info(params.phone, otp.code)
await sendOTP(params.phone, otp.code)
return {
digits: OTP.codeDigits,
code: mock ? otp.code : undefined,
exp: dateToUnixTimestamp(otp.expiredAt),
next: user ? 'login' : 'register',
}
},
})
```
### With authentication
```ts
interface Request {
name: string
}
export const createBook = sweet({
method: 'POST',
url: '/books',
auth: users,
params: {
name: 'string|max:30',
},
async handler(params: Request, { userID }): Promise {
const book: Book = {
id: oidHex(),
v: oidHex(),
creator: userID,
deleted: false,
name: params.name,
}
await Book.repo.insert(book)
return book
},
})
```