Skip to content

Commit

Permalink
chore(middleware): Fix some internal types, spelling etc (#10415)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Apr 4, 2024
1 parent 8d35096 commit 61ac910
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"peacock.color": "#b85833",
"cSpell.words": [
"autoplay",
"bazinga",
"corepack",
"execa",
"Fastify",
Expand Down
4 changes: 1 addition & 3 deletions packages/vite/src/middleware/CookieJar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export type CookieParams = {
options?: cookie.CookieSerializeOptions
}

/**
* Specialised cookie map, that lets you set cookies with options
*/
/** Specialized cookie map, that lets you set cookies with options */
export class CookieJar {
private map = new Map<string, CookieParams>()

Expand Down
20 changes: 11 additions & 9 deletions packages/vite/src/middleware/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { describe, expect, it, vitest } from 'vitest'

import { MiddlewareRequest } from './MiddlewareRequest'
import { MiddlewareResponse } from './MiddlewareResponse'
import type { MiddlewareReg } from './register'
import { addMiddlewareHandlers, chain, groupByRoutePatterns } from './register'
import type { Middleware, MiddlewareClass } from './types'
import type { Middleware, MiddlewareClass, MiddlewareReg } from './types'

const fakeMiddleware: Middleware = vitest.fn()

class FakeClassMw implements MiddlewareClass {
value: number

constructor(value: number) {
this.value = value
}
async invoke(req, res) {

async invoke(_req: MiddlewareRequest, res: MiddlewareResponse) {
res.body = 'MW initialized with ' + this.value
res.headers.set('class-mw-value', this.value.toString())
return res
Expand Down Expand Up @@ -57,7 +58,7 @@ describe('groupByRoutePatterns', () => {
})

it('Throws if not a function, instance or tuple', () => {
const badInput: MiddlewareReg = ['/badinput'] as any
const badInput: MiddlewareReg = ['/bad-input'] as any

expect(() => groupByRoutePatterns(badInput)).toThrow()
})
Expand All @@ -78,23 +79,23 @@ describe('groupByRoutePatterns', () => {
exampleRequest,
new MiddlewareResponse(),
)
expect(firstOutput?.body).toBe('MW initialized with 1')
expect((firstOutput || {}).body).toBe('MW initialized with 1')

const secondOutput = await output['/second-path'][0]?.(
exampleRequest,
new MiddlewareResponse(),
)
expect(secondOutput?.body).toBe('MW initialized with 2')
expect((secondOutput || {})?.body).toBe('MW initialized with 2')
})
})

describe('chain', () => {
const addHeaderMw: Middleware = (req, res) => {
const addHeaderMw: Middleware = (_req, res) => {
res.headers.append('add-header-mw', 'added')
return res
}

const addCookieMw: Middleware = (req, res) => {
const addCookieMw: Middleware = (_req, res) => {
res.cookies.set('add-cookie-mw', 'added')
return res
}
Expand Down Expand Up @@ -126,7 +127,8 @@ describe('chain', () => {
it('Routing with find-my-way', async () => {
const mwRouter = addMiddlewareHandlers(registerList)
const match = mwRouter.find('GET', '/bazinga')
// @ts-expect-error No way of customising find-my-way route type

// No way of customizing find-my-way route type
const output = await match?.handler(
exampleRequest,
new MiddlewareResponse(),
Expand Down

0 comments on commit 61ac910

Please sign in to comment.