also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,86 @@ | ||
| 1 | import { describe, expect, test } from 'bun:test' | |
| 2 | import { createHandlerContext, createHandlerContextWithBody, defineHandler } from '../src' | |
| 3 | ||
| 4 | describe('defineHandler', () => { | |
| 5 | test('returns the handler config as-is', () => { | |
| 6 | const config = defineHandler({ | |
| 7 | handler: ctx => ({ message: 'hello' }), | |
| 8 | }) | |
| 9 | expect(config.handler).toBeDefined() | |
| 10 | }) | |
| 11 | ||
| 12 | test('preserves middleware and input config', () => { | |
| 13 | const config = defineHandler({ | |
| 14 | handler: ctx => null, | |
| 15 | middleware: ['auth'], | |
| 16 | input: { parse: data => data }, | |
| 17 | }) | |
| 18 | expect(config.middleware).toEqual(['auth']) | |
| 19 | expect(config.input).toBeDefined() | |
| 20 | }) | |
| 21 | }) | |
| 22 | ||
| 23 | describe('createHandlerContext', () => { | |
| 24 | test('extracts url from request', () => { | |
| 25 | const request = new Request('http://localhost:3000/api/posts?page=1') | |
| 26 | const ctx = createHandlerContext(request) | |
| 27 | expect(ctx.url.pathname).toBe('/api/posts') | |
| 28 | expect(ctx.url.searchParams.get('page')).toBe('1') | |
| 29 | }) | |
| 30 | ||
| 31 | test('extracts headers from request', () => { | |
| 32 | const request = new Request('http://localhost:3000/api/test', { | |
| 33 | headers: { 'X-Custom': 'value' }, | |
| 34 | }) | |
| 35 | const ctx = createHandlerContext(request) | |
| 36 | expect(ctx.headers.get('X-Custom')).toBe('value') | |
| 37 | }) | |
| 38 | ||
| 39 | test('includes provided params', () => { | |
| 40 | const request = new Request('http://localhost:3000/api/posts/42') | |
| 41 | const ctx = createHandlerContext(request, { id: '42' }) | |
| 42 | expect(ctx.params.id).toBe('42') | |
| 43 | }) | |
| 44 | ||
| 45 | test('defaults to empty params', () => { | |
| 46 | const request = new Request('http://localhost:3000/api/test') | |
| 47 | const ctx = createHandlerContext(request) | |
| 48 | expect(ctx.params).toEqual({}) | |
| 49 | }) | |
| 50 | }) | |
| 51 | ||
| 52 | describe('createHandlerContextWithBody', () => { | |
| 53 | test('parses JSON body', async () => { | |
| 54 | const request = new Request('http://localhost:3000/api/posts', { | |
| 55 | method: 'POST', | |
| 56 | headers: { 'Content-Type': 'application/json' }, | |
| 57 | body: JSON.stringify({ title: 'Hello' }), | |
| 58 | }) | |
| 59 | const ctx = await createHandlerContextWithBody(request) | |
| 60 | expect(ctx.body).toEqual({ title: 'Hello' }) | |
| 61 | }) | |
| 62 | ||
| 63 | test('body is null for non-json content', async () => { | |
| 64 | const request = new Request('http://localhost:3000/api/posts', { | |
| 65 | method: 'POST', | |
| 66 | body: 'plain text', | |
| 67 | }) | |
| 68 | const ctx = await createHandlerContextWithBody(request) | |
| 69 | expect(ctx.body).toBeNull() | |
| 70 | }) | |
| 71 | }) | |
| 72 | ||
| 73 | describe('handler execution', () => { | |
| 74 | test('handler receives context and returns data', async () => { | |
| 75 | const config = defineHandler({ | |
| 76 | handler: (ctx) => { | |
| 77 | return { id: ctx.params.id, method: ctx.request.method } | |
| 78 | }, | |
| 79 | }) | |
| 80 | ||
| 81 | const request = new Request('http://localhost:3000/api/posts/5', { method: 'GET' }) | |
| 82 | const ctx = createHandlerContext(request, { id: '5' }) | |
| 83 | const result = await config.handler(ctx) | |
| 84 | expect(result).toEqual({ id: '5', method: 'GET' }) | |
| 85 | }) | |
| 86 | }) | |