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

feat(wobe): add a copy method in WobeResponse to copy a Response #18

Merged
merged 1 commit into from
Jun 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
Binary file modified bun.lockb
Binary file not shown.
20 changes: 20 additions & 0 deletions packages/wobe/src/WobeResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import { describe, expect, it } from 'bun:test'
import { WobeResponse } from './WobeResponse'

describe('Wobe Response', () => {
it('should clone a Response into a WobeResponse instance', () => {
const wobeResponse = new WobeResponse(
new Request('http://localhost:3000/test'),
)

wobeResponse.headers.set('X-Tata', 'tata')
wobeResponse.setCookie('cookieName', 'cookieValue')

const response = new Response()
response.headers.set('X-Test', 'test')

const clonedWobeResponse = wobeResponse.copy(response)

expect(clonedWobeResponse.headers.get('X-Test')).toBe('test')
expect(clonedWobeResponse.headers.get('X-Tata')).toBe('tata')
expect(clonedWobeResponse.headers.get('Set-Cookie')).toBe(
'cookieName=cookieValue;',
)
})

it('should set an empty header value', () => {
const wobeResponse = new WobeResponse(
new Request('http://localhost:3000/test'),
Expand Down
19 changes: 19 additions & 0 deletions packages/wobe/src/WobeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ export class WobeResponse {
this.request = request
}

/**
* Copy a response into an existing wobe instance response
* @param response The response to copy
* @returns A new wobe instance response
*/
copy(response: Response) {
const wobeResponse = new WobeResponse(this.request)

wobeResponse.headers = new Headers(response.headers)

for (const [key, value] of this.headers.entries())
wobeResponse.headers.set(key, value)

wobeResponse.status = response.status
wobeResponse.statusText = response.statusText

return wobeResponse
}

/**
* Set a cookie
* @param name The name of the cookie
Expand Down