Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(middleware): Fix some internal types, spelling etc #10415

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, Thank you! 🤣

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's now an official Redwood word 😄

"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
Loading