Skip to content
Myr

refactor: enable `strictPropertyInitialization` (#333)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor: enable `strictPropertyInitialization` * checkout `0de127fd74879445b0910fd6505ca22a22a5d791`

authored by Yusuke Wada · committed by GitHub · parent 11d3d4e · Browse files · 11 files+78 added−68 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+78 added−68 removed

  • tests:+6 added−5 removed
  • other:+72 added−63 removed

11 files+78 added−68 removed

modified:src/middleware/body-parse/index.test.ts+3 added-5 removed
modified:src/middleware/body-parse/index.ts+2 added-2 removed
modified:src/middleware/cookie/index.ts+2 added-2 removed
modified:src/middleware/mustache/mustache.ts+6 added
12 unmodified lines
13interface Mustache {14 render: (content: string, params: object, partials: Partials) => string15}1617declare module '../../context' {18 interface Context {19 render: (content: string, params?: object, options?: object) => Response | Promise<Response>20 }21}2223export type MustacheOptions = {24 root: string25 manifest?: object | string26 namespace?: KVNamespace
49 unmodified lines
modified:src/router/trie-router/node.ts+1 added
35 unmodified lines
3637 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {38 this.children = children || {}39 this.methods = []40 this.name = ''41 if (method && handler) {42 const m: Record<string, HandlerSet<T>> = {}43 m[method] = { handler: handler, score: 0, name: this.name }44 this.methods = [m]
159 unmodified lines
modified:src/compose.test.ts+3 added
1import { compose } from './compose'2import { Context } from './context'3import { extendRequestPrototype } from './request'45extendRequestPrototype()67type C = {8 req: Record<string, string>9 res: Record<string, string>10}
666 unmodified lines
modified:src/context.ts+6 added-17 removed
1import type { NotFoundHandler } from './hono'2import { extendHonoRequest } from './request'3import type { HonoRequest } from './request'2import type { StatusCode } from './utils/http-status'3import { isAbsoluteURL } from './utils/url'45type Headers = Record<string, string>
1 unmodified line
78export type Env = Record<string, any>910export class Context<RequestParamKeyType extends string = string, E = Env> {13 req: HonoRequest<RequestParamKeyType>11 req: Request<RequestParamKeyType>12 env: E13 event: FetchEvent | undefined14 executionCtx: ExecutionContext | undefined15 finalized: boolean
5 unmodified lines
21 private _headers: Record<string, string> | undefined22 private _res: Response | undefined23 private notFoundHandler: NotFoundHandler2427 render: (content: string, params?: object, options?: object) => Response | Promise<Response>2825 constructor(30 req: HonoRequest | Request,26 req: Request,27 env: E | undefined = undefined,28 eventOrExecutionCtx: FetchEvent | ExecutionContext | undefined = undefined,29 notFoundHandler: NotFoundHandler = () => new Response()30 ) {35 if (req instanceof Request) {36 this.req = extendHonoRequest(req as HonoRequest)37 } else {38 this.req = req39 }31 this.req = req32 this.env = env ? env : ({} as E)3341 if (env) {42 this.env = env43 }4445 this.executionCtx = eventOrExecutionCtx4634 if (eventOrExecutionCtx && 'respondWith' in eventOrExecutionCtx) {35 this.event = eventOrExecutionCtx36 } else {37 this.executionCtx = eventOrExecutionCtx38 }3940 this.notFoundHandler = notFoundHandler41 this.finalized = false
90 unmodified lines
modified:src/hono.ts+6 added-12 removed
1import { compose } from './compose'2import { Context } from './context'3import type { Env } from './context'4import type { HonoRequest } from './request'5import { extendHonoRequest } from './request'4import { extendRequestPrototype } from './request'5import type { Router } from './router'6import { METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE } from './router'7import { TrieRouter } from './router/trie-router' // Default Router8import { getPathFromURL, mergePath } from './utils/url'
64 unmodified lines
7374 constructor(init: Partial<Pick<Hono, 'router' | 'strict'>> = {}) {75 super()7677 extendRequestPrototype()7879 const allMethods = [...methods, METHOD_NAME_ALL_LOWERCASE]80 allMethods.map((method) => {81 this[method] = <Path extends string = ''>(82 args1: Path | Handler<ParamKeys<Path>, E>,
78 unmodified lines
161 }162163 private async dispatch(164 request: Request,164 executionCtx?: ExecutionContext,165 eventOrExecutionCtx?: ExecutionContext | FetchEvent,166 env?: E167 ): Promise<Response> {167 request = extendHonoRequest(request as HonoRequest)168 const path = getPathFromURL(request.url, this.strict)169 const method = request.method170171 const result = this.matchRoute(method, path)172 ;(request as HonoRequest).paramData = result?.params172 request.paramData = result?.params173174 const handlers = result ? result.handlers : [this.notFoundHandler]175176 const c = new Context<string, E>(request, env, executionCtx, this.notFoundHandler)176 const c = new Context<string, E>(request, env, eventOrExecutionCtx, this.notFoundHandler)177178 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)179 let context: Context180 try {
24 unmodified lines
205 request(input: RequestInfo, requestInit?: RequestInit): Promise<Response> {206 const req = input instanceof Request ? input : new Request(input, requestInit)207 return this.dispatch(req)208 }209210 fire(): void {211 addEventListener('fetch', (event: FetchEvent): void => {212 event.respondWith(this.handleEvent(event))213 })214 }209}
modified:src/index.ts+15 added-1 removed
1// eslint-disable-next-line @typescript-eslint/triple-slash-reference2/// <reference path="./request.ts" /> Import "declare global" for the Request interface.34export { Hono } from './hono'4import { Hono } from './hono'5export type { Handler, Next } from './hono'6export { Context } from './context'7export type { Env } from './context'89declare module './hono' {10 interface Hono {11 fire(): void12 }13}1415Hono.prototype.fire = function () {16 addEventListener('fetch', (event: FetchEvent): void => {17 void event.respondWith(this.handleEvent(event))18 })19}2021export { Hono }
modified:src/request.ts+33 added-28 removed
1declare global {1export class HonoRequest<ParamKeyType extends string = string> extends Request {2 param: {3 (key: ParamKeyType): string4 (): Record<ParamKeyType, string>2 interface Request<ParamKeyType extends string = string> {3 param: {4 (key: ParamKeyType): string5 (): Record<ParamKeyType, string>6 }7 paramData?: Record<ParamKeyType, string>8 query: {9 (key: string): string10 (): Record<string, string>11 }12 queries: {13 (key: string): string[]14 (): Record<string, string[]>15 }16 header: {17 (name: string): string18 (): Record<string, string>19 }20 }6 paramData?: Record<ParamKeyType, string>7 query: {8 (key: string): string9 (): Record<string, string>10 }11 queries: {12 (key: string): string[]13 (): Record<string, string[]>14 }15 header: {16 (name: string): string17 (): Record<string, string>18 }21}2221export function extendHonoRequest(request: HonoRequest): HonoRequest {22 request.param = function (this: HonoRequest, key?: string) {23export function extendRequestPrototype() {24 if (!!Request.prototype.param as boolean) {25 // already extended26 return27 }2829 Request.prototype.param = function (this: Request, key?: string) {30 if (this.paramData) {31 if (key) {32 return this.paramData[key]33 } else {34 return this.paramData35 }36 }37 return null31 } as InstanceType<typeof HonoRequest>['param']38 } as InstanceType<typeof Request>['param']3933 request.header = function (this: HonoRequest, name?: string) {40 Request.prototype.header = function (this: Request, name?: string) {41 if (name) {42 return this.headers.get(name)43 } else {44 const result: Record<string, string> = {}
1 unmodified line
46 result[key] = value47 }48 return result49 }43 } as InstanceType<typeof HonoRequest>['header']50 } as InstanceType<typeof Request>['header']5145 request.query = function (this: HonoRequest, key?: string) {52 Request.prototype.query = function (this: Request, key?: string) {53 const url = new URL(this.url)54 if (key) {55 return url.searchParams.get(key)56 } else {
2 unmodified lines
59 result[key] = url.searchParams.get(key) || ''60 }61 return result62 }56 } as InstanceType<typeof HonoRequest>['query']63 } as InstanceType<typeof Request>['query']6458 request.queries = function (this: HonoRequest, key?: string) {65 Request.prototype.queries = function (this: Request, key?: string) {66 const url = new URL(this.url)67 if (key) {68 return url.searchParams.getAll(key)69 } else {
2 unmodified lines
72 result[key] = url.searchParams.getAll(key)73 }74 return result75 }69 } as InstanceType<typeof HonoRequest>['queries']7071 return request76 } as InstanceType<typeof Request>['queries']77}
modified:tsconfig.json+1 added-1 removed
7 unmodified lines
8 "esModuleInterop": true,9 "forceConsistentCasingInFileNames": true,10 "strict": true,11 "skipLibCheck": true,12 "strictPropertyInitialization": false,12 "strictPropertyInitialization": true,13 "strictNullChecks": true,14 "noUnusedLocals": true,15 "noUnusedParameters": true,16 "types": [
14 unmodified lines