Comparing version 1.0.1 to 1.0.2
@@ -12,6 +12,7 @@ import { Response } from "./Response"; | ||
export declare function postTo(path: string, handler: HttpHandler): ResourceRoutingHttpHandler; | ||
export interface Filter { | ||
} | ||
export declare class ResourceRoutingHttpHandler implements RoutingHttpHandler { | ||
server: Http4jsServer; | ||
private path; | ||
private handler; | ||
private handlers; | ||
@@ -21,3 +22,3 @@ private filters; | ||
withRoutes(routes: ResourceRoutingHttpHandler): ResourceRoutingHttpHandler; | ||
withFilter(filter: (HttpHandler) => HttpHandler): ResourceRoutingHttpHandler; | ||
withFilter(filter: Filter): ResourceRoutingHttpHandler; | ||
withHandler(path: string, method: string, handler: HttpHandler): ResourceRoutingHttpHandler; | ||
@@ -24,0 +25,0 @@ asServer(port: number): Http4jsServer; |
@@ -12,4 +12,2 @@ export declare class Uri { | ||
matches: object; | ||
private pathParamMatchingRegex; | ||
private pathParamCaptureTemplate; | ||
constructor(template: string); | ||
@@ -16,0 +14,0 @@ static of(uri: string): Uri; |
11
index.ts
@@ -20,3 +20,5 @@ import {Request} from "./src/main/core/Request"; | ||
let moreRoutes = routes("/bob/{id}", "POST", (req) => { return new Response(201, new Body("created a " + req.path))}); | ||
let moreRoutes = routes("/bob/{id}", "POST", (req) => { | ||
return new Response(201, new Body("created a " + req.path)) | ||
}); | ||
@@ -48,3 +50,10 @@ routes("/path", "GET", handler) | ||
export * from "./src/main/core/RoutingHttpHandler"; | ||
export * from "./src/main/core/Request"; | ||
export * from "./src/main/core/Response"; | ||
export * from "./src/main/core/Server"; | ||
export * from "./src/main/core/Client"; | ||
export * from "./src/main/core/Body"; | ||
export * from "./src/main/core/Uri"; | ||
{ | ||
"name": "http4js", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -27,6 +27,7 @@ ## Http4js | ||
- code: | ||
- daves advice | ||
- extract form data | ||
- ordering of filters | ||
- other client verbs | ||
- import lib to a new project as example | ||
- write docs | ||
- publish as library to npm |
@@ -7,2 +7,3 @@ import {Response} from "./Response"; | ||
import {Uri} from "./Uri"; | ||
import {Filter} from "../../../dist/main/core/RoutingHttpHandler"; | ||
@@ -31,3 +32,2 @@ export interface RoutingHttpHandler { | ||
private path: string; | ||
private handler: object; | ||
private handlers: object = {}; | ||
@@ -40,5 +40,3 @@ private filters: Array<any> = []; | ||
this.path = path; | ||
let verbToHandler = {verb: method, handler: handler}; | ||
this.handler = verbToHandler; | ||
this.handlers[path] = verbToHandler; | ||
this.handlers[path] = {verb: method, handler: handler}; | ||
} | ||
@@ -55,3 +53,3 @@ | ||
withFilter(filter: (HttpHandler) => HttpHandler): ResourceRoutingHttpHandler { | ||
withFilter(filter: Filter): ResourceRoutingHttpHandler { | ||
this.filters.push(filter); | ||
@@ -58,0 +56,0 @@ return this; |
let URI = require('url'); | ||
const pathParamMatchingRegex: RegExp = new RegExp(/\{(\w+)\}/g); | ||
const pathParamCaptureTemplate: string = "([\\w\\s]+)"; | ||
export class Uri { | ||
@@ -16,4 +19,2 @@ path: string; | ||
matches: object = {}; | ||
private pathParamMatchingRegex: RegExp = new RegExp(/\{(\w+)\}/g); | ||
private pathParamCaptureTemplate: string = "([\\w\\s]+)"; | ||
@@ -45,3 +46,3 @@ constructor(template: string) { | ||
let decodedUri = decodeURI(uri); | ||
let pathParamNames = this.template.match(this.pathParamMatchingRegex) | ||
let pathParamNames = this.template.match(pathParamMatchingRegex) | ||
.map(it => it.replace("{", "").replace("}", "")); | ||
@@ -60,7 +61,5 @@ let pathParams = this.uriTemplateToPathParamCapturingRegex().exec(decodedUri); | ||
withQuery(name: string, value: string): Uri { | ||
if (this.query && this.query.length > 0){ | ||
return Uri.of(this.href + `&${name}=${value}`) | ||
} else { | ||
return Uri.of(this.href + `?${name}=${value}`) | ||
} | ||
return this.query && this.query.length > 0 | ||
? Uri.of(this.href + `&${name}=${value}`) | ||
: Uri.of(this.href + `?${name}=${value}`); | ||
} | ||
@@ -70,6 +69,6 @@ | ||
return new RegExp(this.template.replace( | ||
this.pathParamMatchingRegex, | ||
this.pathParamCaptureTemplate) | ||
pathParamMatchingRegex, | ||
pathParamCaptureTemplate) | ||
); | ||
} | ||
} |
32118
878
33