Skip to content

Commit

Permalink
Merge 8866866 into 8b1f437
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffijoe committed Dec 20, 2018
2 parents 8b1f437 + 8866866 commit 71c40d7
Show file tree
Hide file tree
Showing 6 changed files with 4,055 additions and 1,457 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ notifications:

# Add additional versions here as appropriate.
node_js:
- "stable"
- "8"
- "7"
- "6"
- 'stable'
- '10'
- '8'
- '6'

# Lint errors should trigger a failure.
before_script:
Expand Down
41 changes: 20 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,30 @@
},
"homepage": "https://github.com/jeffijoe/awilix-router-core#readme",
"dependencies": {
"glob": "^7.1.2",
"tslib": "^1.8.1"
"glob": "^7.1.3",
"tslib": "^1.9.3"
},
"devDependencies": {
"@semantic-release/condition-codeship": "^1.1.0",
"@semantic-release/release-notes-generator": "^6.0.3",
"@types/glob": "^5.0.33",
"@types/jest": "^21.1.8",
"@types/node": "^8.0.57",
"@types/prettier": "^1.8.1",
"@semantic-release/release-notes-generator": "^7.1.4",
"@types/glob": "^7.1.1",
"@types/jest": "^23.3.10",
"@types/node": "^10.12.18",
"@types/prettier": "^1.15.2",
"@types/rimraf": "^2.0.2",
"coveralls": "^3.0.0",
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^6.0.0",
"prettier": "^1.9.1",
"coveralls": "^3.0.2",
"husky": "^1.3.0",
"jest": "^23.6.0",
"lint-staged": "^8.1.0",
"prettier": "^1.15.3",
"rimraf": "^2.6.1",
"semantic-release": "^8.2.0",
"semantic-release-conventional-commits": "^1.2.0",
"ts-jest": "^21.2.4",
"tslint": "^5.8.0",
"tslint-config-prettier": "^1.6.0",
"tslint-config-standard": "^7.0.0",
"typescript": "^2.6.2"
"semantic-release": "^15.13.1",
"semantic-release-conventional-commits": "^2.0.1",
"ts-jest": "^23.10.5",
"tslint": "^5.12.0",
"tslint-config-prettier": "^1.17.0",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.2.2"
},
"lint-staged": {
"*.ts": [
Expand All @@ -71,7 +71,6 @@
},
"jest": {
"testEnvironment": "node",
"mapCoverage": true,
"testRegex": "(/__tests__/.*\\.(test|spec))\\.(ts|tsx|js)$",
"collectCoverageFrom": [
"src/**/*.ts"
Expand All @@ -84,7 +83,7 @@
],
"coverageDirectory": "<rootDir>/coverage",
"transform": {
"\\.(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"\\.(ts|tsx)": "ts-jest"
},
"moduleFileExtensions": [
"ts",
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { getState, rollUpState } from '../state-util'
describe('createController', () => {
it('creates the correct state', () => {
const target = () => {
/**/
const noop = () => undefined
return { all: noop, get: noop, post: noop, put: noop }
}
const router = createController(target)
.prefix('/root1')
Expand Down
39 changes: 21 additions & 18 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
addBeforeMiddleware,
Constructor,
IRouterConfigState,
addAfterMiddleware
addAfterMiddleware,
ClassOrFunctionReturning
} from './state-util'
import { HttpVerbs, HttpVerb } from './http-verbs'
import { STATE, IS_CONTROLLER_BUILDER } from './symbols'
Expand All @@ -22,30 +23,32 @@ export interface IMethodBuilderOpts {
/**
* Verb builder function.
*/
export type VerbBuilderFunction = (
export type VerbBuilderFunction<T = any> = (
path: string,
method: string,
method: keyof T,
opts?: IMethodBuilderOpts
) => IAwilixControllerBuilder
) => IAwilixControllerBuilder<T>

/**
* Fluid router builder.
*/
export interface IAwilixControllerBuilder {
export interface IAwilixControllerBuilder<T = any> {
[STATE]: IRouterConfigState
[IS_CONTROLLER_BUILDER]: boolean
target: Constructor | Function
get: VerbBuilderFunction
post: VerbBuilderFunction
put: VerbBuilderFunction
patch: VerbBuilderFunction
delete: VerbBuilderFunction
head: VerbBuilderFunction
options: VerbBuilderFunction
connect: VerbBuilderFunction
all: VerbBuilderFunction
get: VerbBuilderFunction<T>
post: VerbBuilderFunction<T>
put: VerbBuilderFunction<T>
patch: VerbBuilderFunction<T>
delete: VerbBuilderFunction<T>
head: VerbBuilderFunction<T>
options: VerbBuilderFunction<T>
connect: VerbBuilderFunction<T>
all: VerbBuilderFunction<T>
verbs(
verbs: Array<HttpVerb>,
path: string,
method: string,
method: keyof T,
opts?: IMethodBuilderOpts
): this
prefix(path: string): this
Expand Down Expand Up @@ -73,9 +76,9 @@ export interface IAwilixControllerBuilder {
* before: [authenticate()]
* })
*/
export function createController(
ClassOrFunction: Constructor | Function
): IAwilixControllerBuilder {
export function createController<T = any>(
ClassOrFunction: ClassOrFunctionReturning<T>
): IAwilixControllerBuilder<T> {
return createControllerFromState(ClassOrFunction, createState())
}

Expand Down
44 changes: 33 additions & 11 deletions src/state-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@ import { IAwilixControllerBuilder } from './controller'
export type MiddlewareParameter = Array<any> | any

/**
* Basic constructor type.
* Function that returns T.
*/
export type Constructor = new (...args: Array<any>) => any
export type FunctionReturning<T = any> = (...args: Array<any>) => T

/**
* A class or function returning T.
*/
export type ClassOrFunctionReturning<T = any> =
| FunctionReturning<T>
| Constructor<T>

/**
* A class constructor. For example:
*
* class MyClass {}
*
* MyClass
* ^^^^^^^
*/
export type Constructor<T = any> = { new (...args: any[]): T }

/**
* Target to instantiate and it's router state.
Expand All @@ -38,7 +55,7 @@ export interface IRouterConfigState {
/**
* Method configs (method-level).
*/
methods: Map<string, IRouteConfig>
methods: Map<MethodName, IRouteConfig>
}

/**
Expand All @@ -63,6 +80,11 @@ export interface IRouteConfig {
verbs: Array<HttpVerb>
}

/**
* Method name type.
*/
export type MethodName = string | number | symbol | null

/**
* Rolls up state so paths are joined, middleware rolled into
* the correct order, etc.
Expand All @@ -71,8 +93,8 @@ export interface IRouteConfig {
*/
export function rollUpState(
state: IRouterConfigState
): Map<string, IRouteConfig> {
const result = new Map<string, IRouteConfig>()
): Map<MethodName, IRouteConfig> {
const result = new Map<MethodName, IRouteConfig>()
state.methods.forEach((method, key) => {
result.set(key, {
paths: concatPaths(state.root.paths, method.paths),
Expand Down Expand Up @@ -121,7 +143,7 @@ export function getStateAndTarget(src: any): IStateAndTarget | null {
*/
export function addRoute(
state: IRouterConfigState,
methodName: string | null,
methodName: MethodName,
path: string
) {
const config = getOrCreateConfig(state, methodName)
Expand All @@ -139,7 +161,7 @@ export function addRoute(
*/
export function addBeforeMiddleware(
state: IRouterConfigState,
methodName: string | null,
methodName: MethodName,
middleware: MiddlewareParameter
) {
const config = getOrCreateConfig(state, methodName)
Expand All @@ -157,7 +179,7 @@ export function addBeforeMiddleware(
*/
export function addAfterMiddleware(
state: IRouterConfigState,
methodName: string | null,
methodName: MethodName,
middleware: MiddlewareParameter
) {
const config = getOrCreateConfig(state, methodName)
Expand All @@ -175,7 +197,7 @@ export function addAfterMiddleware(
*/
export function addHttpVerbs(
state: IRouterConfigState,
methodName: string | null,
methodName: MethodName,
value: Array<HttpVerb>
) {
const config = getOrCreateConfig(state, methodName)
Expand All @@ -192,7 +214,7 @@ export function addHttpVerbs(
*/
export function getOrCreateConfig(
state: IRouterConfigState,
methodName: string | null
methodName: MethodName
) {
const config =
methodName === null ? state.root : state.methods.get(methodName)
Expand Down Expand Up @@ -276,7 +298,7 @@ export function createState(): IRouterConfigState {
*/
export function updateConfig(
state: IRouterConfigState,
methodName: string | null,
methodName: MethodName,
newConfig: Partial<IRouteConfig>
): IRouterConfigState {
const existing = getOrCreateConfig(state, methodName)
Expand Down

0 comments on commit 71c40d7

Please sign in to comment.