Skip to content

Commit 2fce876

Browse files
committed
remove the word basic from basic router
1 parent 50b96f0 commit 2fce876

17 files changed

+58
-165
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Roads is a simple web framework. It's similar to Express.js, but has some very i
4242
- [Parsing request bodies](#parsing-request-bodies)
4343
- [Parse Body Context](#parse-body-context)
4444
- [Remove trailing slash](#remove-trailing-slash)
45-
- [Basic router](#basic-router)
45+
- [Router](#router)
4646
- [applyMiddleware(road: *Road*)](#applymiddlewareroad-road)
4747
- [addRoute(method: *string*, path: *string*, fn: *function*)](#addroutemethod-string-path-string-fn-function)
4848
- [addRouteFile(filePath: *string*, prefix?: *string*)](#addroutefilefilepath-string-prefix-string)
@@ -547,8 +547,8 @@ var road = new Road();
547547
road.use(RemoveTrailingSlashMiddleware.middleware);
548548
```
549549
550-
## Basic router
551-
This is a basic router middleware for roads. It allows you to easily attach functionality to HTTP methods and paths.
550+
## Router
551+
This is a Router middleware for roads. It allows you to easily attach functionality to HTTP methods and paths.
552552
553553
Here's how you use it.
554554

example/ts/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
public/*
1+
public/*
2+
types/*

example/ts/src/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* This file is an example of using roads router in the client
77
*/
88

9-
import { Road, RoadsPJAX, ParseBodyMiddleware, BasicRouterMiddleware, CookieMiddleware, Request } from 'roads';
9+
import { Road, RoadsPJAX, ParseBodyMiddleware, RouterMiddleware, CookieMiddleware, Request } from 'roads';
1010
import applyPublicRotues from './routes/applyPublicRoutes';
1111
import emptyTo404 from './middleware/emptyTo404';
1212

@@ -25,7 +25,7 @@ road.use(ParseBodyMiddleware.middleware);
2525
road.use(CookieMiddleware.buildClientMiddleware(document));
2626
pjax.register();
2727
pjax.registerAdditionalElement(document.getElementById('home') as HTMLAnchorElement);
28-
const router = new BasicRouterMiddleware.BasicRouter(road);
28+
const router = new RouterMiddleware.Router(road);
2929
applyPublicRotues(router);
3030

3131
const testRequest = new Request(false, 'localhost', 8081);

example/ts/src/routes/applyPrivateRoutes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import * as fs from 'fs';
1010
import { CookieContext } from 'roads/types/middleware/cookieMiddleware';
1111
import { StoreValsContext } from 'roads/types/middleware/storeVals';
12-
import { BasicRouterMiddleware, Response } from 'roads';
12+
import { RouterMiddleware, Response } from 'roads';
1313
const TITLE_KEY = 'page-title';
1414

1515
/**
@@ -19,7 +19,7 @@ const TITLE_KEY = 'page-title';
1919
*
2020
* @param {SimpleRouter} router - The router that the routes will be added to
2121
*/
22-
export default function applyPrivateRotues(router: BasicRouterMiddleware.BasicRouter<StoreValsContext>): void {
22+
export default function applyPrivateRotues(router: RouterMiddleware.Router<StoreValsContext>): void {
2323
router.addRoute<CookieContext>('GET', '/private', async function () {
2424
this.storeVal(TITLE_KEY, 'Private Resource');
2525
this.setCookie('private_cookie', 'foo', {

example/ts/src/routes/applyPublicRoutes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* This file is an example of how to assign some public routes to a roads server
77
*/
88

9-
import { Response, BasicRouterMiddleware } from 'roads';
9+
import { Response, RouterMiddleware } from 'roads';
1010
const TITLE_KEY = 'page-title';
1111

1212
import { ParseBodyContext } from 'roads/types/middleware/parseBody';
@@ -24,7 +24,7 @@ interface ExampleRequestBody {
2424
*
2525
* @param {SimpleRouter} router - The router that the routes will be added to
2626
*/
27-
export default function applyPublicRotues(router: BasicRouterMiddleware.BasicRouter<StoreValsContext>): void {
27+
export default function applyPublicRotues(router: RouterMiddleware.Router<StoreValsContext>): void {
2828
router.addRoute('GET', '/', async function () {
2929
this.storeVal(TITLE_KEY, 'Root Resource');
3030

example/ts/src/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Road, Response, RemoveTrailingSlashMiddleware, CookieMiddleware,
10-
StoreValsMiddleware, ParseBodyMiddleware, BasicRouterMiddleware } from 'roads';
10+
StoreValsMiddleware, ParseBodyMiddleware, RouterMiddleware } from 'roads';
1111

1212
import { Server } from 'roads-server';
1313
import addLayout from './middleware/addLayout';
@@ -28,7 +28,7 @@ road.use(StoreValsMiddleware.middleware);
2828
road.use(addLayout);
2929
road.use(ParseBodyMiddleware.middleware);
3030

31-
const router = new BasicRouterMiddleware.BasicRouter(road);
31+
const router = new RouterMiddleware.Router(road);
3232
applyPublicRotues(router);
3333
applyPrivateRoutes(router);
3434
road.use(emptyTo404);

example/ts/types/build.d.ts

-8
This file was deleted.

example/ts/types/buildClient.d.ts

-9
This file was deleted.

example/ts/types/client.d.ts

-8
This file was deleted.

example/ts/types/middleware/addLayout.d.ts

-22
This file was deleted.

example/ts/types/middleware/emptyTo404.d.ts

-19
This file was deleted.

example/ts/types/routes/applyPrivateRoutes.d.ts

-17
This file was deleted.

example/ts/types/routes/applyPublicRoutes.d.ts

-17
This file was deleted.

example/ts/types/server.d.ts

-8
This file was deleted.

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export * as ParseBodyMiddleware from './middleware/parseBody';
1111
export * as RerouteMiddleware from './middleware/reroute';
1212
export * as StoreValsMiddleware from './middleware/storeVals';
1313
export * as ModifiedSinceMiddleware from './middleware/modifiedSince';
14-
export * as BasicRouterMiddleware from './middleware/basicRouter';
14+
export * as RouterMiddleware from './middleware/router';

src/middleware/basicRouter.ts src/middleware/router.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* basicRouter.ts
2+
* router.ts
33
* Copyright(c) 2021 Aaron Hedges <[email protected]>
44
* MIT Licensed
55
*
6-
* This is a basic router middleware for roads.
6+
* This is a router middleware for roads.
77
* It allows you to easily attach functionality to HTTP methods and paths.
88
*/
99

@@ -15,7 +15,7 @@ import { NextCallback, RequestChain } from '../core/requestChain';
1515

1616

1717
export interface Route<ContextType extends Context> {
18-
(this: ContextType, method: string, path: BasicRouterURL, body: string,
18+
(this: ContextType, method: string, path: RouterURL, body: string,
1919
headers: IncomingHeaders, next: NextCallback): Promise<Response>
2020
}
2121

@@ -25,14 +25,14 @@ interface RouteDetails {
2525
method: string
2626
}
2727

28-
export interface BasicRouterURL extends ReturnType<typeof parse> {
28+
export interface RouterURL extends ReturnType<typeof parse> {
2929
args?: Record<string, string | number>
3030
}
3131
/**
32-
* This is a basic router middleware for roads.
33-
* You can assign functions to url paths, and those paths can have some very basic variable templating
32+
* This is a router middleware for roads.
33+
* You can assign functions to url paths, and those paths can have variable templating
3434
*
35-
* Templating is basic. Each URI is considered to be a series of "path parts" separated by slashes.
35+
* There are only a couple of template options. Each URI is considered to be a series of "path parts" separated by slashes.
3636
* If a path part starts with a #, it is assumed to be a numeric variable. Non-numbers will not match this route
3737
* If a path part starts with a $, it is considered to be an alphanumeric variabe.
3838
* All non-slash values will match this route.
@@ -43,13 +43,13 @@ export interface BasicRouterURL extends ReturnType<typeof parse> {
4343
* /users/#user_id will match /users/12345, not /users/abcde. If a request is made to /users/12345
4444
* the route's requestUrl object will contain { args: {user_id: 12345}} along with all other url object values
4545
*
46-
* @name BasicRouter
46+
* @name Router
4747
*/
48-
export class BasicRouter<RouterContextType extends Context> {
48+
export class Router<RouterContextType extends Context> {
4949
protected _routes: RouteDetails[];
5050

5151
/**
52-
* @param {Road} [road] - The road that will receive the BasicRouter middleware
52+
* @param {Road} [road] - The road that will receive the Router middleware
5353
*/
5454
constructor (road?: Road) {
5555
this._routes = [];
@@ -63,15 +63,15 @@ export class BasicRouter<RouterContextType extends Context> {
6363
* If you don't provide a road to the SimpleRouter constructor, your routes will not be executed.
6464
* If you have reason not to assign the road off the bat, you can assign it later with this function.
6565
*
66-
* @param {Road} road - The road that will receive the BasicRouter middleware
66+
* @param {Road} road - The road that will receive the Router middleware
6767
*/
6868
applyMiddleware (road: Road): void {
6969
// We need to alias because "this" for the middleware function must
70-
// be the this applied by road.use, not the BasicRouter
70+
// be the this applied by road.use, not the Router
7171
// eslint-disable-next-line @typescript-eslint/no-this-alias
7272
const _self = this;
7373

74-
// We do this to ensure we have access to the BasicRouter once we lose this due to road's context
74+
// We do this to ensure we have access to the Router once we lose this due to road's context
7575
road.use((function (request_method, request_url, request_body, request_headers, next) {
7676
return _self._middleware.call(this, _self._routes, request_method, request_url,
7777
request_body, request_headers, next);
@@ -82,7 +82,7 @@ export class BasicRouter<RouterContextType extends Context> {
8282
* This is where you want to write the majority of your webservice. The `fn` parameter should contain
8383
* the actions you want to perform when a certain `path` and HTTP `method` are accessed via the `road` object.
8484
*
85-
* The path supports a very basic templating system. The values inbetween each slash can be interpreted
85+
* The path supports a templating system. The values inbetween each slash can be interpreted
8686
* in one of three ways
8787
* - If a path part starts with a #, it is assumed to be a numeric variable. Non-numbers will not match this route
8888
* - If a path part starts with a $, it is considered to be an alphanumeric variabe. All non-slash values
@@ -203,7 +203,7 @@ export class BasicRouter<RouterContextType extends Context> {
203203
/**
204204
* Checks to see if the route matches the request, and if true assigns any applicable url variables and returns the route
205205
*
206-
* @param {object} route - Route object from this basic router class
206+
* @param {object} route - Route object from this router class
207207
* @param {object} route.method - HTTP method associated with this route
208208
* @param {object} route.path - HTTP path associated with this route
209209
* @param {object} request_url - Parsed HTTP request url
@@ -242,14 +242,14 @@ function compareRouteAndApplyArgs (route: {method: string, path: string}, reques
242242
}
243243

244244
// TODO: get rid of this `as`
245-
applyArg(request_url as BasicRouterURL, template_part.substring(1), Number(actual_part));
245+
applyArg(request_url as RouterURL, template_part.substring(1), Number(actual_part));
246246
continue;
247247
}
248248

249249
if (template_part[0] === '$') {
250250
// $ templates accept any non-slash alphanumeric character
251251
// TODO: get rid of this `as`
252-
applyArg(request_url as BasicRouterURL, template_part.substring(1), String(actual_part));
252+
applyArg(request_url as RouterURL, template_part.substring(1), String(actual_part));
253253
// Continue so that
254254
continue;
255255
}
@@ -272,7 +272,7 @@ function compareRouteAndApplyArgs (route: {method: string, path: string}, reques
272272
* @param {string} template_part - The template variable
273273
* @param {*} actual_part - The url value
274274
*/
275-
function applyArg(request_url: BasicRouterURL, template_part: string, actual_part: string | number): void {
275+
function applyArg(request_url: RouterURL, template_part: string, actual_part: string | number): void {
276276
if (typeof(request_url.args) === 'undefined') {
277277
request_url.args = {};
278278
}

0 commit comments

Comments
 (0)