arrow-express
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -15,6 +15,4 @@ // eslint-disable-next-line no-undef | ||
"semi": ["error"], | ||
"no-explicit-any": { | ||
ignoreRestArgs: true, | ||
} | ||
"no-explicit-any": 0 | ||
} | ||
}; | ||
}; |
@@ -16,3 +16,3 @@ { | ||
"dependencies": { | ||
"arrow-express": "../", | ||
"arrow-express": "0.3.1", | ||
"compression": "^1.7.4", | ||
@@ -19,0 +19,0 @@ "cors": "^2.8.5", |
# This is example application | ||
You can test out this application for example with Postman. | ||
It has no database connection. We recommend use for example typeorm for this purpose. | ||
It has no database connection. We recommend use for example prisma for this purpose. | ||
@@ -16,2 +16,2 @@ # Default settings | ||
### You can copy this folder as boilerplate for your application | ||
### You can copy this folder as boilerplate for your application |
import { User } from '../entities/user.entity'; | ||
/** | ||
* This is stub service we advise to use for example typeorm for database connection management. | ||
* This is stub service we advise to use for example prisma for database connection management. | ||
*/ | ||
@@ -27,2 +27,2 @@ let stubUser: User = { | ||
} | ||
} | ||
} |
@@ -29,2 +29,2 @@ // Express packages | ||
startServer(); | ||
startServer(); |
{ | ||
"name": "arrow-express", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Library to bootstrap express applications with zero configuration", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -110,4 +110,7 @@ # Arrow Express | ||
- `prefix` - register controller prefix which will be used by all routes | ||
- `registerRoute` - register route in controller | ||
- `prefix(prefix)` - register controller prefix which will be used by all routes | ||
- `registerRoute(route)` - register route in controller | ||
- `registerRoutes(...routes)` - register multiple routes in controller | ||
- `registerController(controller)` - register sub controller in controller | ||
- `registerControllers(...controllers)` - register multiple sub controllers in controller | ||
@@ -123,33 +126,21 @@ ### Route | ||
function CheckToken (): UserId { | ||
// here we check if user is logged by proper token | ||
return userId; | ||
} | ||
function getUserRoute() { | ||
return Route() | ||
.method('get') | ||
.path('myself') | ||
.contextGuard(CheckToken) | ||
.handler(async (req: Express.Request, res: Express.Response, userId: UserId) => { | ||
// here we can get user using UserId received from guard | ||
return user; | ||
}); | ||
} | ||
function LoginController () { | ||
return Controller() | ||
.prefix('user') | ||
.registerRoutes( | ||
getUserRoute() | ||
); | ||
} | ||
Application({port: 8080}) | ||
.registerController( | ||
LoginController(), | ||
Controller() | ||
.prefix('user') | ||
.registerRoutes( | ||
Route() | ||
.method('get') | ||
.path('myself') | ||
.handler(async (req: Express.Request, res: Express.Response) => { | ||
const user = {}; | ||
// Use some service to extract route | ||
return user; | ||
}) | ||
) | ||
) | ||
.start(); | ||
// Registered path will be: '/user/myself' | ||
// Registered path will be: GET '/user/myself' | ||
``` | ||
@@ -188,1 +179,41 @@ | ||
If route guard throw error route handler won't be called. | ||
### Advices | ||
Check out `example` folder for example code guidance. | ||
#### Use closures to structure services | ||
Good approach is to use function closures to organize code into chunks. | ||
Eg: create function which will return `Controller` and pass to it instance of service as argument instead of importing Singleton service. | ||
This way you will be able to test routes and controllers with ease without module mocking and you will avoid side effects. | ||
```ts | ||
// index.ts file | ||
async function startServer() { | ||
const expressApplication = Express(); | ||
const userService = new UserService(); | ||
expressApplication.use(cors()); | ||
expressApplication.use(Compression()); | ||
expressApplication.use(Express.json()); | ||
Application({ | ||
port: 3000, | ||
app: expressApplication | ||
}) | ||
.registerController(UserController(userService)) | ||
.start(); | ||
} | ||
// user.controller.ts file | ||
export function UserController(userService: UserService): ControllerConfiguration { | ||
return Controller() | ||
.prefix('users') | ||
.registerRoutes( | ||
GetUserById(userService), | ||
GetMyselfRoute(userService) | ||
); | ||
} | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
217
267948
1447
1