QueryHandler: Added new handy query handler middleware
This commit is contained in:
parent
e5ca7e5d74
commit
89c96e2ad4
3 changed files with 36 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
import http from 'http'
|
import http from 'http'
|
||||||
|
import { URL } from 'url'
|
||||||
import stream from 'stream'
|
import stream from 'stream'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,6 +51,12 @@ function assertIsHandler(handler, name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function QueryHandler() {
|
||||||
|
return function(ctx) {
|
||||||
|
ctx.query = (new URL(ctx.req.url, 'http://localhost')).searchParams
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class FlaskaRouter {
|
export class FlaskaRouter {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.root = new Branch()
|
this.root = new Branch()
|
||||||
|
@ -379,6 +386,7 @@ export class Flaska {
|
||||||
search: search,
|
search: search,
|
||||||
state: {},
|
state: {},
|
||||||
status: 200,
|
status: 200,
|
||||||
|
query: new Map(),
|
||||||
body: null,
|
body: null,
|
||||||
type: null,
|
type: null,
|
||||||
length: null,
|
length: null,
|
||||||
|
|
|
@ -151,6 +151,10 @@ t.describe('#requestStart()', function() {
|
||||||
assert.strictEqual(ctx.type, null)
|
assert.strictEqual(ctx.type, null)
|
||||||
assert.strictEqual(ctx.length, null)
|
assert.strictEqual(ctx.length, null)
|
||||||
assert.strictEqual(ctx.log, assertLog)
|
assert.strictEqual(ctx.log, assertLog)
|
||||||
|
assert.ok(ctx.query)
|
||||||
|
assert.ok(ctx.query.get)
|
||||||
|
assert.ok(ctx.query.set)
|
||||||
|
assert.ok(ctx.query.delete)
|
||||||
cb()
|
cb()
|
||||||
} catch (err) { cb(err) }
|
} catch (err) { cb(err) }
|
||||||
}
|
}
|
||||||
|
|
24
test/middlewares.test.mjs
Normal file
24
test/middlewares.test.mjs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { Eltro as t, assert} from 'eltro'
|
||||||
|
import { QueryHandler } from '../flaska.mjs'
|
||||||
|
|
||||||
|
t.describe('#QueryHandler()', function() {
|
||||||
|
let queryHandler = QueryHandler()
|
||||||
|
|
||||||
|
t.test('should return a handler', function() {
|
||||||
|
assert.strictEqual(typeof(queryHandler), 'function')
|
||||||
|
})
|
||||||
|
|
||||||
|
t.test('should support separating query from request url', function() {
|
||||||
|
const assertItem1 = 'safdsfdsag'
|
||||||
|
const assertItem2 = 'hello%20world'
|
||||||
|
const ctx = {
|
||||||
|
req: {
|
||||||
|
url: `/some/path?item1=${assertItem1}&ITEM2=${assertItem2}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
queryHandler(ctx)
|
||||||
|
assert.strictEqual(ctx.query.get('item1'), assertItem1)
|
||||||
|
assert.strictEqual(ctx.query.get('ITEM2'), 'hello world')
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue