Skip to content

Commit

Permalink
Bump TypeScript to 4.9.5 and fix types (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastahdocent authored Jun 15, 2023
1 parent 8ebdd25 commit db8a409
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dist/universal-router-sync.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/universal-router-sync.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/universal-router.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/universal-router.min.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@rollup/plugin-node-resolve": "13.0.0",
"@rollup/plugin-typescript": "8.2.1",
"@types/jest": "26.0.23",
"@types/node": "18.11.19",
"@typescript-eslint/eslint-plugin": "4.28.0",
"@typescript-eslint/parser": "4.28.0",
"babel-jest": "27.0.5",
Expand All @@ -48,7 +49,7 @@
"rollup-plugin-sourcemaps": "0.6.3",
"rollup-plugin-uglify": "6.0.4",
"ts-jest": "27.0.3",
"typescript": "4.3.4"
"typescript": "4.9.5"
},
"scripts": {
"lint": "eslint . --ext .ts,.js",
Expand Down
27 changes: 14 additions & 13 deletions src/UniversalRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import UniversalRouter, { Route } from './UniversalRouter'
import type { RouteError } from './UniversalRouter'

test('requires routes', () => {
// @ts-expect-error missing argument
Expand Down Expand Up @@ -79,11 +80,11 @@ test('throws when route not found', async () => {
try {
await router.resolve('/')
} catch (e) {
err = e
err = e as RouteError
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
})

test("executes the matching route's action method and return its result", async () => {
Expand Down Expand Up @@ -128,11 +129,11 @@ test('skips action methods of routes that do not match the URL path', async () =
try {
await router.resolve('/b')
} catch (e) {
err = e
err = e as RouteError
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
expect(action.mock.calls.length).toBe(0)
})

Expand Down Expand Up @@ -294,7 +295,7 @@ test('supports next() across multiple routes', async () => {
],
},
],
async action({ next }): Promise<unknown> {
async action({ next }) {
log.push(1)
const result = await next()
log.push(10)
Expand Down Expand Up @@ -520,12 +521,12 @@ test('respects baseUrl', async () => {
try {
await router.resolve('/a/b/c')
} catch (e) {
err = e
err = e as RouteError
}
expect(action.mock.calls.length).toBe(1)
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
})

test('matches routes with trailing slashes', async () => {
Expand Down Expand Up @@ -599,11 +600,11 @@ test('handles route not found error correctly', async () => {
try {
await router.resolve('/404')
} catch (e) {
err = e
err = e as RouteError
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
})

test('handles malformed URI params', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/UniversalRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function decode(val: string): string {
}
}

function matchRoute<R, C>(
function matchRoute<R, C extends RouterContext>(
route: Route<R, C>,
baseUrl: string,
options: RouterOptions<R, C>,
Expand Down
31 changes: 16 additions & 15 deletions src/UniversalRouterSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import UniversalRouter, { Route } from './UniversalRouterSync'
import type { RouteError } from './UniversalRouterSync'

test('requires routes', () => {
// @ts-expect-error missing argument
Expand Down Expand Up @@ -79,11 +80,11 @@ test('throws when route not found', () => {
try {
router.resolve('/')
} catch (e) {
err = e
err = e as RouteError
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
})

test("executes the matching route's action method and return its result", () => {
Expand Down Expand Up @@ -128,11 +129,11 @@ test('skips action methods of routes that do not match the URL path', () => {
try {
router.resolve('/b')
} catch (e) {
err = e
err = e as RouteError
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
expect(action.mock.calls.length).toBe(0)
})

Expand Down Expand Up @@ -253,7 +254,7 @@ test('supports next() across multiple routes', () => {
children: [
{
path: '',
action({ next }): Promise<void> {
action({ next }) {
log.push(3)
const result = next()
log.push(6)
Expand All @@ -262,7 +263,7 @@ test('supports next() across multiple routes', () => {
children: [
{
path: '',
action({ next }): Promise<void> {
action({ next }) {
log.push(4)
const result = next()
log.push(5)
Expand Down Expand Up @@ -294,7 +295,7 @@ test('supports next() across multiple routes', () => {
],
},
],
action({ next }): unknown {
action({ next }) {
log.push(1)
const result = next()
log.push(10)
Expand Down Expand Up @@ -517,12 +518,12 @@ test('respects baseUrl', () => {
try {
router.resolve('/a/b/c')
} catch (e) {
err = e
err = e as RouteError
}
expect(action.mock.calls.length).toBe(1)
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
})

test('matches routes with trailing slashes', () => {
Expand Down Expand Up @@ -596,11 +597,11 @@ test('handles route not found error correctly', () => {
try {
router.resolve('/404')
} catch (e) {
err = e
err = e as RouteError
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(err?.message).toBe('Route not found')
expect(err?.status).toBe(404)
})

test('handles malformed URI params', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/UniversalRouterSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function decode(val: string): string {
}
}

function matchRoute<R, C>(
function matchRoute<R, C extends RouterContext>(
route: Route<R, C>,
baseUrl: string,
options: RouterOptions<R, C>,
Expand Down Expand Up @@ -324,7 +324,7 @@ class UniversalRouterSync<R = any, C extends RouterContext = RouterContext> {
return next(true, this.root)
} catch (error) {
if (this.options.errorHandler) {
return this.options.errorHandler(error, currentContext)
return this.options.errorHandler(error as RouteError, currentContext)
}
throw error
}
Expand Down

0 comments on commit db8a409

Please sign in to comment.