Skip to content

Commit

Permalink
chore: fix type-checking issues post TS upgrade - also bump eslint an…
Browse files Browse the repository at this point in the history
…d prettier
  • Loading branch information
JCMais committed Nov 5, 2023
1 parent f9b0da6 commit f48b553
Show file tree
Hide file tree
Showing 14 changed files with 1,167 additions and 653 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
'prettier/@typescript-eslint',
'prettier',
],
plugins: ['@typescript-eslint', 'prettier'],
rules: {
Expand All @@ -36,6 +36,8 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-non-null-assertion': 0,
'@typescript-eslint/ban-ts-ignore': 0,
'@typescript-eslint/no-duplicate-enum-values': 0,
'@typescript-eslint/no-unsafe-declaration-merging': 0,
'@typescript-eslint/ban-ts-comment': 0,
'@typescript-eslint/member-ordering': [
2,
Expand Down
4 changes: 2 additions & 2 deletions lib/Curl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Curl extends EventEmitter {
*
* See {@link enable | `enable`} and {@link disable | `disable`}
*/
protected features: CurlFeature = 0
protected features: CurlFeature = CurlFeature.Empty

// these are for stream handling
// the streams themselves
Expand Down Expand Up @@ -1014,7 +1014,7 @@ class Curl extends EventEmitter {
try {
handle.pause(CurlPause.RecvCont)
} catch (error) {
cb(error)
cb(error as Error)
return
}
}
Expand Down
25 changes: 15 additions & 10 deletions lib/curly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { CurlFeature } from './enum/CurlFeature'
*
* @public
*/
export interface CurlyResult<ResultData extends any = any> {
export interface CurlyResult<ResultData = any> {
/**
* Data will be the body of the requested URL
*/
Expand Down Expand Up @@ -79,7 +79,7 @@ const methods = [
'unsubscribe',
] as const

type HttpMethod = typeof methods[number]
type HttpMethod = (typeof methods)[number]

export type CurlyResponseBodyParser = (
data: Buffer,
Expand Down Expand Up @@ -216,9 +216,10 @@ interface CurlyHttpMethodCall {
*
* @typeParam ResultData You can use this to specify the type of the `data` property returned from this call.
*/
<ResultData extends any = any>(url: string, options?: CurlyOptions): Promise<
CurlyResult<ResultData>
>
<ResultData = any>(
url: string,
options?: CurlyOptions,
): Promise<CurlyResult<ResultData>>
}

// type HttpMethodCalls = { readonly [K in HttpMethod]: CurlyHttpMethodCall }
Expand All @@ -238,9 +239,10 @@ export interface CurlyFunction extends HttpMethodCalls {
* ```
* @typeParam ResultData You can use this to specify the type of the `data` property returned from this call.
*/
<ResultData extends any = any>(url: string, options?: CurlyOptions): Promise<
CurlyResult<ResultData>
>
<ResultData = any>(
url: string,
options?: CurlyOptions,
): Promise<CurlyResult<ResultData>>

/**
* **EXPERIMENTAL** This API can change between minor releases
Expand All @@ -262,7 +264,7 @@ export interface CurlyFunction extends HttpMethodCalls {
}

const create = (defaultOptions: CurlyOptions = {}): CurlyFunction => {
function curly<ResultData extends any>(
function curly<ResultData>(
url: string,
options: CurlyOptions = {},
): Promise<CurlyResult<ResultData>> {
Expand Down Expand Up @@ -339,6 +341,7 @@ const create = (defaultOptions: CurlyOptions = {}): CurlyFunction => {
const entries = Object.entries(headersReq)
for (const [headerKey, headerValue] of entries) {
delete headersReq[headerKey]
// @ts-expect-error ignoring this for now
headersReq[headerKey.toLowerCase()] = headerValue
}
}
Expand Down Expand Up @@ -381,7 +384,9 @@ const create = (defaultOptions: CurlyOptions = {}): CurlyFunction => {
headers[headers.length - 1],
).find(([k]) => k.toLowerCase() === 'content-type')

let contentType = contentTypeEntry ? contentTypeEntry[1] : ''
let contentType = (
contentTypeEntry ? contentTypeEntry[1] : ''
) as string

// remove the metadata of the content-type, like charset
// See https://tools.ietf.org/html/rfc7231#section-3.1.1.5
Expand Down
5 changes: 5 additions & 0 deletions lib/enum/CurlFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
* @public
*/
export enum CurlFeature {
/**
* Initial state
*/
Empty = 0,

/**
* Data received is passed as a Buffer to the end event.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/types/EasyNativeBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { SocketState } from '../enum/SocketState'

import { FileInfo, HttpPostField } from './'

export interface GetInfoReturn<DataType extends any = number | string | null> {
export interface GetInfoReturn<DataType = number | string | null> {
data: DataType
code: CurlCode
}
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@
"@types/mocha": "^7.0.2",
"@types/node": "^20.8.10",
"@types/should": "^13.0.0",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"cheerio": "^1.0.0-rc.3",
"clang-format": "^1.8.0",
"cookie-parser": "^1.4.6",
"electron-mocha": "^11.0.2",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.12.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-formatter-friendly": "^7.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.0.1",
"express": "4.18.2",
"formidable": "^1.2.2",
"http-auth": "^4.2.0",
Expand All @@ -115,7 +115,7 @@
"np": "^7.6.2",
"nyc": "^15.1.0",
"octonode": "^0.9.5",
"prettier": "^2.7.1",
"prettier": "^3.0.3",
"progress": "^2.0.3",
"should": "^13.2.3",
"sort-package-json": "^1.46.0",
Expand Down
4 changes: 2 additions & 2 deletions scripts/utils/createConstantsFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const createConstantsFile = async ({
(option) =>
`
${getDescriptionCommentForOption(option)}readonly ${
option.constantName
}: "${option.constantName}",
option.constantName
}: "${option.constantName}",
`,
),
`}`,
Expand Down
11 changes: 4 additions & 7 deletions test/curl/http2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ describe('HTTP2', () => {
})
})

after((done) => {
after(function (done) {
this.timeout(5000)
serverHttp2.removeListener('error', onError)
serverHttp2.removeListener('session', onSession)
serverHttp2.removeListener('stream', onStream)
session
? session.close(() => {
session.destroy()
serverHttp2.close(done)
})
: serverHttp2.close(done)
session.destroy()
serverHttp2.close(done)
})

it('should work with https2 site', (done) => {
Expand Down
10 changes: 3 additions & 7 deletions test/curl/optionHttpAuth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ describe('Option HTTPAUTH', () => {
curl.close()

app._router.stack.pop()
app._router.stack.pop()
})

before((done) => {
Expand All @@ -70,8 +69,7 @@ describe('Option HTTPAUTH', () => {
})

it('should authenticate using basic auth', (done) => {
app.use(httpAuthConnect(basic))
app.get('/', (req, res) => {
app.get('/', httpAuthConnect(basic), (req, res) => {
// @ts-ignore
res.send(req.user)
})
Expand Down Expand Up @@ -105,8 +103,7 @@ describe('Option HTTPAUTH', () => {
user = `${realmDigest}/${username}`
}

app.use(httpAuthConnect(digest))
app.get('/', (req, res) => {
app.get('/', httpAuthConnect(digest), (req, res) => {
// @ts-ignore
res.send(req.user)
})
Expand All @@ -131,8 +128,7 @@ describe('Option HTTPAUTH', () => {
})

it('should not authenticate using basic', (done) => {
app.use(httpAuthConnect(basic))
app.get('/', (req, res) => {
app.get('/', httpAuthConnect(basic), (req, res) => {
// @ts-ignore
res.send(req.user)
})
Expand Down
10 changes: 6 additions & 4 deletions test/curl/streams.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const getReadableStreamForBuffer = (
wantsMore = false
}
} catch (error) {
stream.destroy(error)
stream.destroy(error as Error)
}
}

Expand Down Expand Up @@ -132,9 +132,11 @@ describe('streams', () => {
},
})

const { statusCode, data: downloadStream, headers } = await curly.put<
Readable
>(`${url}/all?type=put-upload`, {
const {
statusCode,
data: downloadStream,
headers,
} = await curly.put<Readable>(`${url}/all?type=put-upload`, {
...getUploadOptions(curlyStreamUpload),
...getDownloadOptions(),
curlyProgressCallback() {
Expand Down
6 changes: 3 additions & 3 deletions test/helper/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import path from 'path'
import { Socket } from 'net'

import express from 'express'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'

const file = path.resolve.bind(this, __dirname)
Expand Down Expand Up @@ -49,8 +48,9 @@ export const serverHttp2 = http2.createSecureServer({
})

app
.use(bodyParser.urlencoded({ extended: true }))
.use(bodyParser.raw({ limit: '100MB', type: 'application/node-libcurl.raw' }))
.use(express.urlencoded({ extended: true }))
.use(express.raw({ limit: '100MB', type: 'application/node-libcurl.raw' }))
// @ts-expect-error - no time for fixing this right now
.use(cookieParser())

app.disable('etag')
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"allowSyntheticDefaultImports": true,
"typeRoots": ["node_modules/@types", "types/external"],
"noUnusedLocals": true,
"importHelpers": true
"importHelpers": true,
"skipLibCheck": true
},
"include": ["lib/**/*"],
"exclude": ["node_modules"],
Expand Down
11 changes: 3 additions & 8 deletions types/external/http-auth-connect/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as express from 'express-serve-static-core'
import * as express from 'express'
import 'express-serve-static-core'
import { HttpAuthFunction } from 'http-auth'

declare module 'express-serve-static-core' {
Expand All @@ -10,10 +11,4 @@ declare module 'express-serve-static-core' {
}
}

export default function connect(
func: HttpAuthFunction,
): (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => void
export default function connect(func: HttpAuthFunction): express.RequestHandler
Loading

0 comments on commit f48b553

Please sign in to comment.