Skip to content

Commit 301d7dc

Browse files
author
v1rtl
committed
feat: improve types
1 parent 576f02f commit 301d7dc

43 files changed

Lines changed: 590 additions & 559 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ examples/dev-server/public/sosseDevSocketClient/main.umd.js
66
benchmark
77
site/static/js/index.js
88
examples/aws/.build
9-
examples/firebase-functions/lib
9+
examples/firebase-functions/lib
10+
build

.eslintrc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
},
99
"env": {
1010
"node": true,
11-
"es6": true
11+
"es6": true,
12+
"browser": true
1213
},
1314
"rules": {
14-
"@typescript-eslint/explicit-function-return-type": "off",
15-
"@typescript-eslint/no-explicit-any": "off",
16-
"@typescript-eslint/no-unused-vars": "off",
17-
"@typescript-eslint/explicit-module-boundary-types": "off",
18-
"@typescript-eslint/ban-ts-comment": "off",
19-
"no-unused-vars": "off",
20-
"no-console": "off",
21-
"no-undef": "off"
15+
"@typescript-eslint/ban-ts-comment": "warn"
2216
}
2317
}

examples/deta/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ app.put('/todos', async (req, res) => {
3636
})
3737

3838
app.delete('/todos', async (req, res) => {
39-
const { key } = req.body
39+
const { key, task } = req.body
4040
await db.delete(key)
4141
res.send(`Task ${task} has been updated!`)
4242
})

examples/react-ssr/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-undef */
12
import htm from 'https://unpkg.com/htm?module'
23
const html = htm.bind(React.createElement)
34

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@
1414
"@changesets/cli": "2.17.0",
1515
"@commitlint/cli": "13.1.0",
1616
"@commitlint/config-conventional": "13.1.0",
17-
"@jest/globals": "27.1.1",
17+
"@jest/globals": "27.2.1",
1818
"@rollup/plugin-typescript": "8.2.5",
19-
"@types/jest": "27.0.1",
20-
"@types/node": "16.9.1",
21-
"@typescript-eslint/eslint-plugin": "4.31.0",
22-
"@typescript-eslint/parser": "4.31.0",
23-
"colorette": "1.4.0",
19+
"@types/jest": "27.0.2",
20+
"@types/node": "16.9.6",
21+
"@typescript-eslint/eslint-plugin": "4.31.2",
22+
"@typescript-eslint/parser": "4.31.2",
23+
"colorette": "2.0.8",
2424
"decache": "4.6.0",
2525
"dirname-filename-esm": "1.1.1",
26-
"enhanced-resolve": "5.8.2",
26+
"enhanced-resolve": "5.8.3",
2727
"eslint": "7.32.0",
2828
"eslint-config-prettier": "8.3.0",
2929
"eslint-plugin-prettier": "4.0.0",
3030
"eta": "1.12.3",
3131
"husky": "7.0.2",
32-
"jest": "27.1.1",
32+
"jest": "27.2.1",
3333
"jsonwebtoken": "8.5.1",
34-
"prettier": "2.4.0",
34+
"prettier": "2.4.1",
3535
"range-parser": "1.2.1",
3636
"regexparam": "2.0.0",
37-
"rollup": "2.56.3",
37+
"rollup": "2.57.0",
3838
"supertest-fetch": "1.4.3",
3939
"ts-jest": "27.0.5",
4040
"tslib": "2.3.1",
41-
"typescript": "4.3.5"
41+
"typescript": "4.4.3"
4242
},
4343
"scripts": {
4444
"prerelease": "pnpm lint && pnpm build && pnpm test",

packages/app/src/app.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class App<
114114
* @param setting setting name
115115
* @param value setting value
116116
*/
117-
set(setting: string, value: any) {
117+
set(setting: string, value: any): this {
118118
this.settings[setting] = value
119119

120120
return this
@@ -124,7 +124,7 @@ export class App<
124124
* Enable app setting
125125
* @param setting Setting name
126126
*/
127-
enable(setting: string) {
127+
enable(setting: string): this {
128128
this.settings[setting] = true
129129

130130
return this
@@ -134,7 +134,7 @@ export class App<
134134
* Disable app setting
135135
* @param setting
136136
*/
137-
disable(setting: string) {
137+
disable(setting: string): this {
138138
this.settings[setting] = false
139139

140140
return this
@@ -152,7 +152,7 @@ export class App<
152152
data: Record<string, any> = {},
153153
cb: (err: unknown, html: unknown) => void,
154154
options: TemplateEngineOptions<RenderOptions> = {}
155-
) {
155+
): this {
156156
options.viewsFolder = options.viewsFolder || `${process.cwd()}/views`
157157
options.ext = options.ext || file.slice(file.lastIndexOf('.') + 1) || 'ejs'
158158

@@ -172,7 +172,7 @@ export class App<
172172

173173
return this
174174
}
175-
use(...args: UseMethodParams<Req, Res, App>) {
175+
use(...args: UseMethodParams<Req, Res, App>): this {
176176
const base = args[0]
177177

178178
const fns = args.slice(1).flat()
@@ -233,12 +233,12 @@ export class App<
233233
})
234234
}
235235

236-
return this // chainable
236+
return this
237237
}
238238
/**
239239
* Register a template engine with extension
240240
*/
241-
engine(ext: string, fn: TemplateFunc<RenderOptions>) {
241+
engine(ext: string, fn: TemplateFunc<RenderOptions>): this {
242242
this.engines[ext] = fn
243243

244244
return this
@@ -252,7 +252,7 @@ export class App<
252252
return app
253253
}
254254

255-
find(url: string) {
255+
find(url: string): Middleware<Req, Res>[] {
256256
return this.middleware.filter((m) => {
257257
m.regex = m.regex || rg(m.path, m.type === 'mw')
258258

@@ -271,7 +271,7 @@ export class App<
271271
* @param req Req object
272272
* @param res Res object
273273
*/
274-
handler(req: Req, res: Res, next?: NextFunction) {
274+
handler(req: Req, res: Res, next?: NextFunction): void {
275275
/* Set X-Powered-By header */
276276
const { xPoweredBy } = this.settings
277277
if (xPoweredBy) res.setHeader('X-Powered-By', typeof xPoweredBy === 'string' ? xPoweredBy : 'tinyhttp')
@@ -314,7 +314,7 @@ export class App<
314314
})
315315

316316
const handle = (mw: Middleware) => async (req: Req, res: Res, next?: NextFunction) => {
317-
const { path, handler, type, regex } = mw
317+
const { path, handler, regex } = mw
318318

319319
const params = regex ? getURLParams(regex, pathname) : {}
320320

packages/content-disposition/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const getlatin1 = (val: unknown) => {
2929

3030
export class ContentDisposition {
3131
type: string
32-
parameters: Record<any, any>
33-
constructor(type: string, parameters: Record<any, any>) {
32+
parameters: Record<string, any>
33+
constructor(type: string, parameters: Record<string, any>) {
3434
this.type = type
3535
this.parameters = parameters
3636
}
@@ -55,7 +55,7 @@ function format({
5555
parameters,
5656
type
5757
}: Partial<{
58-
parameters: Record<any, any>
58+
parameters: Record<string, any>
5959
type: string | boolean | undefined
6060
}>) {
6161
if (!type || typeof type !== 'string' || !TOKEN_REGEXP.test(type)) throw new TypeError('invalid type')
@@ -81,7 +81,7 @@ function createParams(filename?: string, fallback?: string | boolean) {
8181
if (filename === undefined) return
8282

8383
const params: Partial<
84-
Record<string, any> & {
84+
Record<string, string> & {
8585
filename: string
8686
}
8787
> = {}
@@ -126,7 +126,7 @@ export function contentDisposition(
126126
type: string
127127
fallback: string | boolean
128128
}> = {}
129-
) {
129+
): string {
130130
// format into string
131131
return format(new ContentDisposition(options.type || 'attachment', createParams(filename, options.fallback)))
132132
}
@@ -161,7 +161,7 @@ function decodefield(str: string) {
161161
* Parse Content-Disposition header string.
162162
* @param string string
163163
*/
164-
export function parse(string: string) {
164+
export function parse(string: string): ContentDisposition {
165165
let match = DISPOSITION_TYPE_REGEXP.exec(string)
166166

167167
if (!match) throw new TypeError('invalid type format')

packages/cookie-signature/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { createHmac, timingSafeEqual } from 'crypto'
33
/**
44
* Sign the given `val` with `secret`.
55
*/
6-
export const sign = (val: string, secret: string) =>
6+
export const sign = (val: string, secret: string): string =>
77
`${val}.${createHmac('sha256', secret).update(val).digest('base64').replace(/=+$/, '')}`
88

99
/**
1010
* Unsign and decode the given `val` with `secret`,
1111
* returning `false` if the signature is invalid.
1212
*/
13-
export const unsign = (val: string, secret: string) => {
13+
export const unsign = (val: string, secret: string): string | boolean => {
1414
const str = val.slice(0, val.lastIndexOf('.')),
1515
mac = sign(str, secret),
1616
macBuffer = Buffer.from(mac),

packages/cookie/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export type SerializeOptions = Partial<{
6767
expires: Date
6868
}>
6969

70-
export function serialize(name: string, val: string, opt: SerializeOptions = {}) {
70+
export function serialize(name: string, val: string, opt: SerializeOptions = {}): string {
7171
if (!opt.encode) opt.encode = encodeURIComponent
7272

7373
if (!fieldContentRegExp.test(name)) throw new TypeError('argument name is invalid')

packages/etag/src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { createHash } from 'crypto'
44
import { Stats } from 'fs'
55

6-
const entityTag = (entity: string | Buffer) => {
6+
const entityTag = (entity: string | Buffer): string => {
77
if (entity.length === 0) {
88
// fast-path empty
99
return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"'
@@ -20,14 +20,12 @@ const entityTag = (entity: string | Buffer) => {
2020
}
2121
}
2222

23-
const statTag = ({ mtime, size }: Stats) => {
23+
const statTag = ({ mtime, size }: Stats): string => {
2424
return '"' + mtime.getTime().toString(16) + '-' + size.toString(16) + '"'
2525
}
2626

27-
export const eTag = (entity: string | Buffer | Stats, options?: { weak: boolean }) => {
28-
if (entity == null) {
29-
throw new TypeError('argument entity is required')
30-
}
27+
export const eTag = (entity: string | Buffer | Stats, options?: { weak: boolean }): string => {
28+
if (entity == null) throw new TypeError('argument entity is required')
3129

3230
const weak = options?.weak || entity instanceof Stats
3331

0 commit comments

Comments
 (0)