Comparing version 0.0.12 to 0.0.13
/// <reference types="node" /> | ||
import { ClientRequest, ServerResponse } from 'http'; | ||
import { IGetResponse } from './body/create-get-response'; | ||
import { ConfigInput, IKequapp } from './main'; | ||
import { IKequapp } from './main'; | ||
import { ConfigInput } from './utils/setup-config'; | ||
declare type OptionsInput = { | ||
@@ -6,0 +7,0 @@ method?: string; |
@@ -10,6 +10,6 @@ "use strict"; | ||
const create_get_response_1 = __importDefault(require("./body/create-get-response")); | ||
const validate_1 = require("./utils/validate"); | ||
const setup_config_1 = require("./utils/setup-config"); | ||
function inject(app, override, options) { | ||
if (override) | ||
(0, validate_1.validateCreateAppConfig)(override); | ||
(0, setup_config_1.validateConfig)(override); | ||
const _options = Object.assign({}, options); | ||
@@ -16,0 +16,0 @@ if (_options.search) { |
@@ -9,2 +9,3 @@ /// <reference types="node" /> | ||
import Ex from './utils/ex'; | ||
import { ConfigInput, Logger } from './utils/setup-config'; | ||
export interface IKequapp extends RequestListener, Router { | ||
@@ -36,27 +37,3 @@ (req: IncomingMessage, res: ServerResponse, override?: ConfigInput): void; | ||
}; | ||
export declare type Config = { | ||
logger: Logger; | ||
renderers: ConfigRenderers; | ||
errorHandler: ConfigErrorHandler; | ||
maxPayloadSize?: number; | ||
}; | ||
export declare type ConfigInput = { | ||
logger?: Logger; | ||
renderers?: ConfigRenderers; | ||
errorHandler?: ConfigErrorHandler; | ||
maxPayloadSize?: number; | ||
}; | ||
export declare type ConfigRenderers = { | ||
[k: string]: Renderer; | ||
}; | ||
export declare type ConfigErrorHandler = (error: unknown, bundle: Bundle) => unknown; | ||
export declare type Logger = { | ||
log(...params: unknown[]): void; | ||
error(...params: unknown[]): void; | ||
warn(...params: unknown[]): void; | ||
debug(...params: unknown[]): void; | ||
info(...params: unknown[]): void; | ||
}; | ||
export declare type Renderer = (payload: unknown, bundle: Bundle) => Promise<void> | void; | ||
declare function createApp(options?: ConfigInput): IKequapp; | ||
export { Ex, createApp, sendFile, staticFiles }; |
@@ -13,3 +13,2 @@ "use strict"; | ||
const create_get_body_1 = __importDefault(require("./body/create-get-body")); | ||
const error_handler_1 = __importDefault(require("./built-in/error-handler")); | ||
const create_router_1 = __importDefault(require("./router/create-router")); | ||
@@ -20,13 +19,6 @@ const list_routes_1 = __importDefault(require("./router/list-routes")); | ||
exports.Ex = ex_1.default; | ||
const validate_1 = require("./utils/validate"); | ||
const DEFAULT_OPTIONS = { | ||
logger: console, | ||
renderers: {}, | ||
errorHandler: error_handler_1.default, | ||
maxPayloadSize: undefined | ||
}; | ||
const setup_config_1 = __importDefault(require("./utils/setup-config")); | ||
function createApp(options = {}) { | ||
(0, validate_1.validateCreateAppConfig)(options); | ||
const _routes = []; | ||
const _config = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options); | ||
const _config = (0, setup_config_1.default)(options); | ||
function app(req, res, _override = {}) { | ||
@@ -33,0 +25,0 @@ const config = Object.assign(Object.assign({}, _config), _override); |
@@ -5,3 +5,2 @@ import { Bundle } from '../main'; | ||
branch: IRouterBranch; | ||
middleware: IRouterMiddleware; | ||
}; | ||
@@ -26,6 +25,3 @@ export declare type RouteBuilder = { | ||
} | ||
export interface IRouterMiddleware { | ||
(...handles: Handle[]): Router; | ||
} | ||
declare function createRouter(routes: Route[], parent: RouteBuilder): Router; | ||
export default createRouter; |
@@ -11,7 +11,5 @@ "use strict"; | ||
branch: undefined, | ||
middleware: undefined | ||
}; | ||
scope.route = buildRoute(routes, parent, scope); | ||
scope.branch = buildBranch(routes, parent); | ||
scope.middleware = buildMiddleware(parent, scope); | ||
return scope; | ||
@@ -34,15 +32,2 @@ } | ||
} | ||
function buildMiddleware(parent, scope) { | ||
return function middleware(...params) { | ||
const handles = params.flat(Infinity); | ||
if (handles.find(handle => typeof handle !== 'function')) { | ||
throw new Error('Handle must be a function'); | ||
} | ||
Object.assign(parent, routeMerge(parent, { | ||
pathname: '/', | ||
handles | ||
})); | ||
return scope; | ||
}; | ||
} | ||
function buildRoute(routes, parent, scope) { | ||
@@ -49,0 +34,0 @@ return function route(...params) { |
@@ -1,3 +0,4 @@ | ||
import { Bundle, Config } from '../main'; | ||
import { Bundle } from '../main'; | ||
import { Config } from '../utils/setup-config'; | ||
declare function render(config: Config, payload: unknown, bundle: Bundle): Promise<void>; | ||
export default render; |
import { Route } from './create-router'; | ||
import { Bundle, Config } from '../main'; | ||
import { Bundle } from '../main'; | ||
import { Config } from '../utils/setup-config'; | ||
declare function requestProcessor(config: Config, routes: Route[], bundle: Bundle): Promise<void>; | ||
export default requestProcessor; |
{ | ||
"name": "kequapp", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "Versatile, non-intrusive, tiny webapp framework", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -34,4 +34,2 @@ # Kequapp | ||
Handlers are added to the current branch using `middleware()`. Provide any number of handlers that will affect siblings. This is most useful on the `app` instance to catch all requests. | ||
```javascript | ||
@@ -53,4 +51,3 @@ const { Ex } = require('kequapp'); | ||
app.branch('/user') | ||
.middleware(json) | ||
app.branch('/user', json) | ||
.route(() => { | ||
@@ -246,12 +243,12 @@ return { result: [] }; | ||
```javascript | ||
app.middleware(({ req, context }) => { | ||
function withCookies ({ req, context }) { | ||
const cookies = cookie.parse(req.headers.cookie); | ||
// cookies ~= { myCookie: 'hello' } | ||
context.cookies = cookies; | ||
}); | ||
} | ||
const cookiesBranch = app.branch(withCookies); | ||
app.route('/login', ({ res }) => { | ||
res.setHeader('Set-Cookie', [ | ||
cookie.serialize('myCookie', 'hello') | ||
]); | ||
res.setHeader('Set-Cookie', [cookie.serialize('myCookie', 'hello')]); | ||
}); | ||
@@ -379,3 +376,5 @@ ``` | ||
}); | ||
``` | ||
```javascript | ||
it('reads the body of a request', async function () { | ||
@@ -382,0 +381,0 @@ const { getResponse, req, res } = inject(app, { logger }, { |
67044
1462
393