Skip to content

Commit

Permalink
Merge pull request #76 from hranum/keep-requestinit
Browse files Browse the repository at this point in the history
fix: keep RequestInit for treaty2 and add tests
  • Loading branch information
SaltyAom committed Apr 23, 2024
2 parents 899f458 + d655579 commit df6cff0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ const createProxy = (
)
}

const fetchOpts =
isGetOrHead && typeof body === 'object'
? body.fetch
: options?.fetch

fetchInit = {
...fetchInit,
...fetchOpts
}

if (isGetOrHead) delete fetchInit.body

if (onRequest) {
Expand Down Expand Up @@ -309,7 +319,6 @@ const createProxy = (
}

const url = domain + path + q

const response = await (elysia?.handle(
new Request(url, fetchInit)
) ?? fetcher!(url, fetchInit))
Expand Down
72 changes: 71 additions & 1 deletion test/treaty2.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Elysia, t } from 'elysia'
import { treaty } from '../src'

import { describe, expect, it, mock } from 'bun:test'
import { describe, expect, it, beforeAll, afterAll } from 'bun:test'

const app = new Elysia()
.get('/', 'a')
Expand Down Expand Up @@ -94,6 +94,19 @@ const app = new Elysia()
date: t.Date()
})
})
.get(
'/redirect',
({ set }) => (set.redirect = 'http://localhost:8083/true')
)
.post(
'/redirect',
({ set }) => (set.redirect = 'http://localhost:8083/true'),
{
body: t.Object({
username: t.String()
})
}
)

const client = treaty(app)

Expand Down Expand Up @@ -385,6 +398,63 @@ describe('Treaty2', () => {
expect(data).toBeInstanceOf(Date)
})

it('redirect should set location header', async () => {
const { headers, status } = await client['redirect'].get({
fetch: {
redirect: 'manual'
}
})
expect(status).toEqual(302)
expect(new Headers(headers).get('location')).toEqual(
'http://localhost:8083/true'
)
})
})

describe('Treaty2 - Using endpoint URL', () => {
const treatyApp = treaty<typeof app>('http://localhost:8083')

beforeAll(async () => {
await new Promise((resolve) => {
app.listen(8083, () => {
resolve(null)
})
})
})

afterAll(() => {
app.stop()
})

it('redirect should set location header', async () => {
const { headers, status } = await treatyApp.redirect.get({
fetch: {
redirect: 'manual'
}
})
expect(status).toEqual(302)
expect(new Headers(headers).get('location')).toEqual(
'http://localhost:8083/true'
)
})

it('redirect should set location header with post', async () => {
const { headers, status } = await treatyApp.redirect.post(
{
username: 'a'
},
{
fetch: {
redirect: 'manual'
}
}
)
expect(status).toEqual(302)
expect(new Headers(headers).get('location')).toEqual(
'http://localhost:8083/true'
)
})

it('doesn\'t encode if it doesn\'t need to', async () => {
const mockedFetch = mock(async () => new Response())

Check failure on line 459 in test/treaty2.test.ts

View workflow job for this annotation

GitHub Actions / Build and test code

ReferenceError: Can't find variable: mock

at /home/runner/work/eden/eden/test/treaty2.test.ts:459:29 at /home/runner/work/eden/eden/test/treaty2.test.ts:458:50
const client = treaty<typeof app>('', { fetcher: mockedFetch })
Expand Down

0 comments on commit df6cff0

Please sign in to comment.