Comparing version 0.2.0 to 0.2.1
@@ -15,3 +15,3 @@ /// <reference types="@cloudflare/workers-types" /> | ||
private _statusText; | ||
body: (body: BodyInit, init?: ResponseInit) => Response; | ||
render: (template: string, params?: object, options?: object) => Promise<Response>; | ||
constructor(req: Request, opts?: { | ||
@@ -25,3 +25,4 @@ res: Response; | ||
statusText(text: string): void; | ||
newResponse(body: BodyInit, init?: ResponseInit): Response; | ||
newResponse(data: any, init?: ResponseInit): Response; | ||
body(data: any, status?: number, headers?: Headers): Response; | ||
text(text: string, status?: number, headers?: Headers): Response; | ||
@@ -28,0 +29,0 @@ json(object: object, status?: number, headers?: Headers): Response; |
@@ -14,3 +14,2 @@ "use strict"; | ||
this._headers = {}; | ||
this.body = this.newResponse; | ||
} | ||
@@ -26,9 +25,17 @@ header(name, value) { | ||
} | ||
newResponse(body, init = {}) { | ||
init.status = this._status || init.status; | ||
init.statusText = this._statusText || init.statusText; | ||
init.headers = Object.assign(Object.assign({}, init.headers), this._headers); | ||
return new Response(body, init); | ||
newResponse(data, init = {}) { | ||
init.status = init.status || this._status; | ||
init.statusText = init.statusText || this._statusText; | ||
const Encoder = new TextEncoder(); | ||
const length = data ? data.bytelength || Encoder.encode(data).byteLength : 0; | ||
init.headers = Object.assign(Object.assign(Object.assign({}, this._headers), init.headers), { 'Content-Length': String(length) }); | ||
return new Response(data, init); | ||
} | ||
text(text, status = 200, headers = {}) { | ||
body(data, status = this._status, headers = this._headers) { | ||
return this.newResponse(data, { | ||
status: status, | ||
headers: headers, | ||
}); | ||
} | ||
text(text, status = this._status, headers = {}) { | ||
if (typeof text !== 'string') { | ||
@@ -38,8 +45,5 @@ throw new TypeError('text method arg must be a string!'); | ||
headers['Content-Type'] = 'text/plain'; | ||
return this.newResponse(text, { | ||
status: status, | ||
headers: headers, | ||
}); | ||
return this.body(text, status, headers); | ||
} | ||
json(object, status = 200, headers = {}) { | ||
json(object, status = this._status, headers = {}) { | ||
if (typeof object !== 'object') { | ||
@@ -50,8 +54,5 @@ throw new TypeError('json method arg must be a object!'); | ||
headers['Content-Type'] = 'application/json; charset=UTF-8'; | ||
return this.newResponse(body, { | ||
status: status, | ||
headers: headers, | ||
}); | ||
return this.body(body, status, headers); | ||
} | ||
html(html, status = 200, headers = {}) { | ||
html(html, status = this._status, headers = {}) { | ||
if (typeof html !== 'string') { | ||
@@ -61,6 +62,3 @@ throw new TypeError('html method arg must be a string!'); | ||
headers['Content-Type'] = 'text/html; charset=UTF-8'; | ||
return this.newResponse(html, { | ||
status: status, | ||
headers: headers, | ||
}); | ||
return this.body(html, status, headers); | ||
} | ||
@@ -67,0 +65,0 @@ redirect(location, status = 302) { |
@@ -11,3 +11,2 @@ /// <reference types="@cloudflare/workers-types" /> | ||
header: (name: string) => string; | ||
parsedBody: any; | ||
} | ||
@@ -14,0 +13,0 @@ } |
@@ -140,9 +140,21 @@ "use strict"; | ||
onError(err) { | ||
console.error(err); | ||
return new Response('Internal Server Error', { status: 500 }); | ||
console.error(`${err}`); | ||
const message = 'Internal Server Error'; | ||
return new Response(message, { | ||
status: 500, | ||
headers: { | ||
'Content-Length': message.length.toString(), | ||
}, | ||
}); | ||
} | ||
notFound() { | ||
return new Response('Not Found', { status: 404 }); | ||
const message = 'Not Found'; | ||
return new Response('Not Found', { | ||
status: 404, | ||
headers: { | ||
'Content-Length': message.length.toString(), | ||
}, | ||
}); | ||
} | ||
} | ||
exports.Hono = Hono; |
@@ -23,2 +23,4 @@ export declare class Middleware { | ||
}) => (c: import("./context").Context, next: Function) => Promise<void>; | ||
static cookie: () => (c: import("./context").Context, next: Function) => Promise<void>; | ||
static mustache: () => (c: import("./context").Context, next: Function) => Promise<void>; | ||
} |
@@ -10,2 +10,4 @@ "use strict"; | ||
const cors_1 = require("./middleware/cors/cors"); | ||
const cookie_1 = require("./middleware/cookie/cookie"); | ||
const mustache_1 = require("./middleware/mustache/mustache"); | ||
class Middleware { | ||
@@ -20,1 +22,3 @@ } | ||
Middleware.cors = cors_1.cors; | ||
Middleware.cookie = cookie_1.cookie; | ||
Middleware.mustache = mustache_1.mustache; |
import type { Context } from '../../context'; | ||
declare global { | ||
interface Request { | ||
parsedBody: any; | ||
} | ||
} | ||
export declare const bodyParse: () => (ctx: Context, next: Function) => Promise<void>; |
@@ -6,3 +6,5 @@ import type { Context } from '../../context'; | ||
} | ||
interface Response { | ||
} | ||
declare module '../../context' { | ||
interface Context { | ||
cookie: (name: string, value: string, options?: CookieOptions) => void; | ||
@@ -9,0 +11,0 @@ } |
@@ -12,8 +12,7 @@ "use strict"; | ||
}; | ||
c.cookie = (name, value, opt) => { | ||
const cookie = serialize(name, value, opt); | ||
c.header('Set-Cookie', cookie); | ||
}; | ||
await next(); | ||
c.res.cookie = (name, value, options) => { | ||
console.log('c.res.cookie!'); | ||
const cookie = serialize(name, value); | ||
c.res.headers.set('Set-Cookie', cookie); | ||
}; | ||
}; | ||
@@ -31,6 +30,27 @@ }; | ||
}; | ||
const serialize = (name, value, options = {}) => { | ||
const serialize = (name, value, opt = {}) => { | ||
value = encodeURIComponent(value); | ||
const cookie = `${name}=${value}`; | ||
let cookie = `${name}=${value}`; | ||
if (opt.maxAge) { | ||
cookie += `; Max-Age=${Math.floor(opt.maxAge)}`; | ||
} | ||
if (opt.domain) { | ||
cookie += '; Domain=' + opt.domain; | ||
} | ||
if (opt.path) { | ||
cookie += '; Path=' + opt.path; | ||
} | ||
if (opt.expires) { | ||
cookie += '; Expires=' + opt.expires.toUTCString(); | ||
} | ||
if (opt.httpOnly) { | ||
cookie += '; HttpOnly'; | ||
} | ||
if (opt.secure) { | ||
cookie += '; Secure'; | ||
} | ||
if (opt.sameSite) { | ||
cookie += `; SameSaite=${opt.sameSite}`; | ||
} | ||
return cookie; | ||
}; |
@@ -48,2 +48,4 @@ "use strict"; | ||
} | ||
c.res.headers.delete('Content-Length'); | ||
c.res.headers.delete('Content-Type'); | ||
c.res = new Response(null, { | ||
@@ -50,0 +52,0 @@ headers: c.res.headers, |
@@ -14,14 +14,3 @@ "use strict"; | ||
await next(); | ||
if (c.res.body) { | ||
// Do not clone Response, ex: c.res.clone().arrayBuffer() | ||
const buffer = await c.res.arrayBuffer(); | ||
const res = new Response(buffer, { | ||
status: c.res.status, | ||
statusText: c.res.statusText, | ||
headers: c.res.headers, | ||
}); | ||
res.headers.append('Content-Length', buffer.byteLength.toString()); | ||
c.res = res; | ||
} | ||
}; | ||
exports.defaultMiddleware = defaultMiddleware; |
{ | ||
"name": "hono", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "[炎] Ultrafast web framework for Cloudflare Workers.", | ||
@@ -39,2 +39,3 @@ "main": "dist/index.js", | ||
"@types/jest": "^27.4.0", | ||
"@types/mustache": "^4.1.2", | ||
"@types/node": "^17.0.8", | ||
@@ -55,2 +56,3 @@ "@typescript-eslint/eslint-plugin": "^5.9.0", | ||
"jest-environment-miniflare": "^2.0.0", | ||
"mustache": "^4.2.0", | ||
"rimraf": "^3.0.2", | ||
@@ -57,0 +59,0 @@ "ts-jest": "^27.1.2", |
@@ -26,8 +26,8 @@ # Hono | ||
```plain | ||
hono x 748,188 ops/sec ±5.40% (77 runs sampled) | ||
itty-router x 158,817 ops/sec ±3.62% (87 runs sampled) | ||
sunder x 332,339 ops/sec ±1.11% (95 runs sampled) | ||
worktop x 205,906 ops/sec ±4.43% (83 runs sampled) | ||
hono x 708,671 ops/sec ±2.58% (58 runs sampled) | ||
itty-router x 159,610 ops/sec ±2.86% (87 runs sampled) | ||
sunder x 322,846 ops/sec ±2.24% (86 runs sampled) | ||
worktop x 223,625 ops/sec ±2.01% (95 runs sampled) | ||
Fastest is hono | ||
✨ Done in 52.79s. | ||
✨ Done in 57.83s. | ||
``` | ||
@@ -118,2 +118,12 @@ | ||
### Custom 404 Response | ||
You can customize 404 Not Found response: | ||
```js | ||
app.get('*', (c) => { | ||
return c.text('Custom 404 Error', 404) | ||
}) | ||
``` | ||
## async/await | ||
@@ -170,15 +180,2 @@ | ||
### Custom 404 Response | ||
You can customize 404 Not Found response: | ||
```js | ||
app.use('*', async (c, next) => { | ||
await next() | ||
if (c.res.status === 404) { | ||
c.res = new Response('Custom 404 Not Found', { status: 404 }) | ||
} | ||
}) | ||
``` | ||
### Handling Error | ||
@@ -192,3 +189,3 @@ | ||
console.error(`${err}`) | ||
c.res = new Response('Custom Error Message', { status: 500 }) | ||
c.res = c.text('Custom Error Message', { status: 500 }) | ||
} | ||
@@ -259,3 +256,5 @@ }) | ||
c.statusText('201 Content Created') | ||
return c.body('Thank you for comming') | ||
/* | ||
@@ -268,3 +267,4 @@ Same as: | ||
'X-Message': 'Hello', | ||
'Content-Type': 'text/plain' | ||
'Content-Type': 'text/plain', | ||
'Content-Length: '22' | ||
} | ||
@@ -271,0 +271,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46584
37
1032
22