Comparing version 2.7.2 to 2.7.3
@@ -31,2 +31,71 @@ "use strict"; | ||
this._prettySpace = 2; | ||
this.header = (name, value, options) => { | ||
this._headers || (this._headers = {}); | ||
const key = name.toLowerCase(); | ||
let shouldAppend = false; | ||
if (options && options.append) { | ||
const vAlreadySet = this._headers[key]; | ||
if (vAlreadySet && vAlreadySet.length) { | ||
shouldAppend = true; | ||
} | ||
} | ||
if (shouldAppend) { | ||
this._headers[key].push(value); | ||
} else { | ||
this._headers[key] = [value]; | ||
} | ||
if (this.finalized) { | ||
if (shouldAppend) { | ||
this.res.headers.append(name, value); | ||
} else { | ||
this.res.headers.set(name, value); | ||
} | ||
} | ||
}; | ||
this.status = (status) => { | ||
this._status = status; | ||
}; | ||
this.pretty = (prettyJSON, space = 2) => { | ||
this._pretty = prettyJSON; | ||
this._prettySpace = space; | ||
}; | ||
this.newResponse = (data, status, headers = {}) => { | ||
return new Response(data, { | ||
status, | ||
headers: this._finalizeHeaders(headers) | ||
}); | ||
}; | ||
this.body = (data, status = this._status, headers = {}) => { | ||
return this.newResponse(data, status, headers); | ||
}; | ||
this.text = (text, status, headers) => { | ||
if (!headers && !status && !this._res && !this._headers) { | ||
return new Response(text); | ||
} | ||
status || (status = this._status); | ||
headers || (headers = {}); | ||
headers["content-type"] = "text/plain; charset=UTF-8"; | ||
return this.newResponse(text, status, headers); | ||
}; | ||
this.json = (object, status = this._status, headers = {}) => { | ||
const body = this._pretty ? JSON.stringify(object, null, this._prettySpace) : JSON.stringify(object); | ||
headers["content-type"] = "application/json; charset=UTF-8"; | ||
return this.newResponse(body, status, headers); | ||
}; | ||
this.html = (html, status = this._status, headers = {}) => { | ||
headers["content-type"] = "text/html; charset=UTF-8"; | ||
return this.newResponse(html, status, headers); | ||
}; | ||
this.redirect = (location, status = 302) => { | ||
return this.newResponse(null, status, { | ||
Location: location | ||
}); | ||
}; | ||
this.cookie = (name, value, opt) => { | ||
const cookie = (0, import_cookie.serialize)(name, value, opt); | ||
this.header("set-cookie", cookie, { append: true }); | ||
}; | ||
this.notFound = () => { | ||
return this.notFoundHandler(this); | ||
}; | ||
this._executionCtx = executionCtx; | ||
@@ -59,28 +128,2 @@ this.req = req; | ||
} | ||
header(name, value, options) { | ||
this._headers || (this._headers = {}); | ||
const key = name.toLowerCase(); | ||
let shouldAppend = false; | ||
if (options && options.append) { | ||
const vAlreadySet = this._headers[key]; | ||
if (vAlreadySet && vAlreadySet.length) { | ||
shouldAppend = true; | ||
} | ||
} | ||
if (shouldAppend) { | ||
this._headers[key].push(value); | ||
} else { | ||
this._headers[key] = [value]; | ||
} | ||
if (this.finalized) { | ||
if (shouldAppend) { | ||
this.res.headers.append(name, value); | ||
} else { | ||
this.res.headers.set(name, value); | ||
} | ||
} | ||
} | ||
status(status) { | ||
this._status = status; | ||
} | ||
set(key, value) { | ||
@@ -96,12 +139,2 @@ this._map || (this._map = {}); | ||
} | ||
pretty(prettyJSON, space = 2) { | ||
this._pretty = prettyJSON; | ||
this._prettySpace = space; | ||
} | ||
newResponse(data, status, headers = {}) { | ||
return new Response(data, { | ||
status, | ||
headers: this._finalizeHeaders(headers) | ||
}); | ||
} | ||
_finalizeHeaders(incomingHeaders) { | ||
@@ -134,35 +167,2 @@ const finalizedHeaders = []; | ||
} | ||
body(data, status = this._status, headers = {}) { | ||
return this.newResponse(data, status, headers); | ||
} | ||
text(text, status, headers) { | ||
if (!headers && !status && !this._res && !this._headers) { | ||
return new Response(text); | ||
} | ||
status || (status = this._status); | ||
headers || (headers = {}); | ||
headers["content-type"] = "text/plain; charset=UTF-8"; | ||
return this.newResponse(text, status, headers); | ||
} | ||
json(object, status = this._status, headers = {}) { | ||
const body = this._pretty ? JSON.stringify(object, null, this._prettySpace) : JSON.stringify(object); | ||
headers["content-type"] = "application/json; charset=UTF-8"; | ||
return this.newResponse(body, status, headers); | ||
} | ||
html(html, status = this._status, headers = {}) { | ||
headers["content-type"] = "text/html; charset=UTF-8"; | ||
return this.newResponse(html, status, headers); | ||
} | ||
redirect(location, status = 302) { | ||
return this.newResponse(null, status, { | ||
Location: location | ||
}); | ||
} | ||
cookie(name, value, opt) { | ||
const cookie = (0, import_cookie.serialize)(name, value, opt); | ||
this.header("set-cookie", cookie, { append: true }); | ||
} | ||
notFound() { | ||
return this.notFoundHandler(this); | ||
} | ||
get runtime() { | ||
@@ -169,0 +169,0 @@ const global = globalThis; |
@@ -9,2 +9,71 @@ // src/context.ts | ||
this._prettySpace = 2; | ||
this.header = (name, value, options) => { | ||
this._headers || (this._headers = {}); | ||
const key = name.toLowerCase(); | ||
let shouldAppend = false; | ||
if (options && options.append) { | ||
const vAlreadySet = this._headers[key]; | ||
if (vAlreadySet && vAlreadySet.length) { | ||
shouldAppend = true; | ||
} | ||
} | ||
if (shouldAppend) { | ||
this._headers[key].push(value); | ||
} else { | ||
this._headers[key] = [value]; | ||
} | ||
if (this.finalized) { | ||
if (shouldAppend) { | ||
this.res.headers.append(name, value); | ||
} else { | ||
this.res.headers.set(name, value); | ||
} | ||
} | ||
}; | ||
this.status = (status) => { | ||
this._status = status; | ||
}; | ||
this.pretty = (prettyJSON, space = 2) => { | ||
this._pretty = prettyJSON; | ||
this._prettySpace = space; | ||
}; | ||
this.newResponse = (data, status, headers = {}) => { | ||
return new Response(data, { | ||
status, | ||
headers: this._finalizeHeaders(headers) | ||
}); | ||
}; | ||
this.body = (data, status = this._status, headers = {}) => { | ||
return this.newResponse(data, status, headers); | ||
}; | ||
this.text = (text, status, headers) => { | ||
if (!headers && !status && !this._res && !this._headers) { | ||
return new Response(text); | ||
} | ||
status || (status = this._status); | ||
headers || (headers = {}); | ||
headers["content-type"] = "text/plain; charset=UTF-8"; | ||
return this.newResponse(text, status, headers); | ||
}; | ||
this.json = (object, status = this._status, headers = {}) => { | ||
const body = this._pretty ? JSON.stringify(object, null, this._prettySpace) : JSON.stringify(object); | ||
headers["content-type"] = "application/json; charset=UTF-8"; | ||
return this.newResponse(body, status, headers); | ||
}; | ||
this.html = (html, status = this._status, headers = {}) => { | ||
headers["content-type"] = "text/html; charset=UTF-8"; | ||
return this.newResponse(html, status, headers); | ||
}; | ||
this.redirect = (location, status = 302) => { | ||
return this.newResponse(null, status, { | ||
Location: location | ||
}); | ||
}; | ||
this.cookie = (name, value, opt) => { | ||
const cookie = serialize(name, value, opt); | ||
this.header("set-cookie", cookie, { append: true }); | ||
}; | ||
this.notFound = () => { | ||
return this.notFoundHandler(this); | ||
}; | ||
this._executionCtx = executionCtx; | ||
@@ -37,28 +106,2 @@ this.req = req; | ||
} | ||
header(name, value, options) { | ||
this._headers || (this._headers = {}); | ||
const key = name.toLowerCase(); | ||
let shouldAppend = false; | ||
if (options && options.append) { | ||
const vAlreadySet = this._headers[key]; | ||
if (vAlreadySet && vAlreadySet.length) { | ||
shouldAppend = true; | ||
} | ||
} | ||
if (shouldAppend) { | ||
this._headers[key].push(value); | ||
} else { | ||
this._headers[key] = [value]; | ||
} | ||
if (this.finalized) { | ||
if (shouldAppend) { | ||
this.res.headers.append(name, value); | ||
} else { | ||
this.res.headers.set(name, value); | ||
} | ||
} | ||
} | ||
status(status) { | ||
this._status = status; | ||
} | ||
set(key, value) { | ||
@@ -74,12 +117,2 @@ this._map || (this._map = {}); | ||
} | ||
pretty(prettyJSON, space = 2) { | ||
this._pretty = prettyJSON; | ||
this._prettySpace = space; | ||
} | ||
newResponse(data, status, headers = {}) { | ||
return new Response(data, { | ||
status, | ||
headers: this._finalizeHeaders(headers) | ||
}); | ||
} | ||
_finalizeHeaders(incomingHeaders) { | ||
@@ -112,35 +145,2 @@ const finalizedHeaders = []; | ||
} | ||
body(data, status = this._status, headers = {}) { | ||
return this.newResponse(data, status, headers); | ||
} | ||
text(text, status, headers) { | ||
if (!headers && !status && !this._res && !this._headers) { | ||
return new Response(text); | ||
} | ||
status || (status = this._status); | ||
headers || (headers = {}); | ||
headers["content-type"] = "text/plain; charset=UTF-8"; | ||
return this.newResponse(text, status, headers); | ||
} | ||
json(object, status = this._status, headers = {}) { | ||
const body = this._pretty ? JSON.stringify(object, null, this._prettySpace) : JSON.stringify(object); | ||
headers["content-type"] = "application/json; charset=UTF-8"; | ||
return this.newResponse(body, status, headers); | ||
} | ||
html(html, status = this._status, headers = {}) { | ||
headers["content-type"] = "text/html; charset=UTF-8"; | ||
return this.newResponse(html, status, headers); | ||
} | ||
redirect(location, status = 302) { | ||
return this.newResponse(null, status, { | ||
Location: location | ||
}); | ||
} | ||
cookie(name, value, opt) { | ||
const cookie = serialize(name, value, opt); | ||
this.header("set-cookie", cookie, { append: true }); | ||
} | ||
notFound() { | ||
return this.notFoundHandler(this); | ||
} | ||
get runtime() { | ||
@@ -147,0 +147,0 @@ const global = globalThis; |
@@ -27,6 +27,6 @@ import type { ExecutionContext } from './types'; | ||
set res(_res: Response); | ||
header(name: string, value: string, options?: { | ||
header: (name: string, value: string, options?: { | ||
append?: boolean; | ||
}): void; | ||
status(status: StatusCode): void; | ||
}) => void; | ||
status: (status: StatusCode) => void; | ||
set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void; | ||
@@ -38,14 +38,14 @@ set<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void; | ||
get<T>(key: string): T; | ||
pretty(prettyJSON: boolean, space?: number): void; | ||
newResponse(data: Data | null, status: StatusCode, headers?: Headers): Response; | ||
pretty: (prettyJSON: boolean, space?: number) => void; | ||
newResponse: (data: Data | null, status: StatusCode, headers?: Headers) => Response; | ||
private _finalizeHeaders; | ||
body(data: Data | null, status?: StatusCode, headers?: Headers): Response; | ||
text(text: string, status?: StatusCode, headers?: Headers): Response; | ||
json<T>(object: T, status?: StatusCode, headers?: Headers): Response; | ||
html(html: string, status?: StatusCode, headers?: Headers): Response; | ||
redirect(location: string, status?: StatusCode): Response; | ||
cookie(name: string, value: string, opt?: CookieOptions): void; | ||
notFound(): Response | Promise<Response>; | ||
body: (data: Data | null, status?: StatusCode, headers?: Headers) => Response; | ||
text: (text: string, status?: StatusCode, headers?: Headers) => Response; | ||
json: <T>(object: T, status?: StatusCode, headers?: Headers) => Response; | ||
html: (html: string, status?: StatusCode, headers?: Headers) => Response; | ||
redirect: (location: string, status?: StatusCode) => Response; | ||
cookie: (name: string, value: string, opt?: CookieOptions) => void; | ||
notFound: () => Response | Promise<Response>; | ||
get runtime(): Runtime; | ||
} | ||
export {}; |
@@ -16,3 +16,3 @@ import type { Context } from './context'; | ||
declare type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}` ? Name : NameWithPattern; | ||
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never; | ||
declare type ParamKey<Component> = Component extends `:${infer NameWithPattern}?` ? ParamKeyName<NameWithPattern> : Component extends `:${infer NameWithPattern}` ? ParamKeyName<NameWithPattern> : never; | ||
export declare type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}` ? ParamKey<Component> | ParamKeys<Rest> : ParamKey<Path>; | ||
@@ -19,0 +19,0 @@ export interface CustomHandler<P = string, E = Partial<Environment>, S = any> { |
{ | ||
"name": "hono", | ||
"version": "2.7.2", | ||
"version": "2.7.3", | ||
"description": "Ultrafast web framework for Cloudflare Workers, Deno, and Bun.", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
293288
91