quick-res
Version:
A set of small utilities that makes Response. q.json(), q.text(), q.html(), .... can make your code shorter and more readable while also providing good support for Tree-Shaking.
97 lines (64 loc) • 2.06 kB
Markdown
> 🚀 A set of small utilities that makes Response. q.json(), q.text(), q.html(), .... can make your code shorter and more readable while also providing good support for Tree-Shaking.
```
npm install quick-res
```
- Works fine with any kinds of worker that supports Web Standard API.
- Supports both CommonJS and ESM.
- Small! Only 922 bytes (ESM). See [screenshot](
Cloudflare workers example:
```ts
import * as quick from 'quick-res';
export default {
async fetch(req: Request): Promise<Response> {
const u = new URL(req.url);
if (u.pathname === '/check') {
const version = u.searchParams.get('version');
if (!version) {
return quick.text('Invalid', 400);
}
return quick.json({ version });
}
return quick.notFound();
},
};
```
[](https://webpack.js.org/guides/tree-shaking/) is enabled by default. Your bundler should only include functions that you use.
Responds as text/plain.
Responds as text/html.
Responds as application/json.
`object` will be automatically converted to JSON using `JSON.stringify`.
Responds stream or whatever you want.
404.
Not included. Use `Response.redirect` instead.
```ts
import * as quick from 'quick-res';
export function forbidden() {
return quick.text('Forbidden', 403);
}
export function badRequest(info: unknown) {
return quick.json(info, 400);
}
export function internalError(error: Error) {
return quick.text(error.message, 500);
}
```
Only 922 bytes (ESM)
<img src="https://github.com/ReeganExE/quick-res/blob/main/code.webp?raw=true" alt="small code">
New BSD License ©Ninh Pham - ReeganExE