diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d92da..02090c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 1.0.15 - 25 Apr 2024 +# 1.0.15 - 27 Apr 2024 Feature: - add `redirect` function to `Context` diff --git a/package.json b/package.json index 559ba6a..2c54d5c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "elysia", "description": "Ergonomic Framework for Human", - "version": "1.0.14", + "version": "1.0.15", "author": { "name": "saltyAom", "url": "https://github.com/SaltyAom", diff --git a/src/index.ts b/src/index.ts index 4757683..de43ec5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5343,6 +5343,7 @@ export { mergeHook, mergeObjectArray, getResponseSchemaValidator, + redirect, StatusMap, InvertedStatusMap } from './utils' diff --git a/src/utils.ts b/src/utils.ts index 7c8d196..b57a536 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -977,7 +977,7 @@ export const cloneInference = (inference: { */ export const redirect = ( url: string, - status: number = 302 + status: number = 301 ) => new Response(null, { status, diff --git a/test/response/redirect.test.ts b/test/response/redirect.test.ts new file mode 100644 index 0000000..eb6ffac --- /dev/null +++ b/test/response/redirect.test.ts @@ -0,0 +1,49 @@ +import { Elysia } from '../../src' + +import { describe, expect, it } from 'bun:test' +import { req } from '../utils' + +describe('Response Headers', () => { + it('handle redirect', async () => { + const app = new Elysia().get('/', ({ redirect }) => redirect('/skadi')) + + const { headers, status } = await app.handle(req('/')) + + expect(status).toBe(301) + // @ts-expect-error + expect(headers.toJSON()).toEqual({ + location: '/skadi' + }) + }) + + it('handle redirect status', async () => { + const app = new Elysia().get('/', ({ redirect }) => + redirect('/skadi', 302) + ) + + const { headers, status } = await app.handle(req('/')) + + expect(status).toBe(302) + // @ts-expect-error + expect(headers.toJSON()).toEqual({ + location: '/skadi' + }) + }) + + it('add set.headers to redirect', async () => { + const app = new Elysia().get('/', ({ redirect, set }) => { + set.headers.alias = 'Abyssal Hunter' + + return redirect('/skadi') + }) + + const { headers, status } = await app.handle(req('/')) + + expect(status).toBe(301) + // @ts-expect-error + expect(headers.toJSON()).toEqual({ + location: '/skadi', + alias: 'Abyssal Hunter' + }) + }) +})