Comparing version 2.1.5 to 2.1.6
{ | ||
"name": "true-di", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "Framework Agnostic, Zero Dependency, Isomorphic & Minimalistic Dependency Injection Container for TypeScript and JavaScript projects", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -86,27 +86,19 @@ # true-di | ||
```typescript | ||
import Express from 'express'; | ||
import { Request, Response, NextFunction as Next } from 'express'; | ||
import { IGetOrderById, IGetOrders } from './interfaces'; | ||
import { Injected } from './interfaces/IRequestInjected'; | ||
import { sendJson } from './utils/sendJson'; | ||
import { expectFound } from './utils/NotFoundError'; | ||
export const getOrders = ( | ||
{ injected: { ecommerceService } }: Injected<{ ecommerceService: IGetOrders }>, | ||
res: Express.Response, | ||
next: Express.NextFunction, | ||
) => | ||
ecommerceService | ||
.getOrders() | ||
.then(sendJson(res), next); | ||
export const getOrders = (req: Request, res: Response, next: Next) => | ||
({ ecommerceService }: { ecommerceService: IGetOrders }) => | ||
ecommerceService | ||
.getOrders() | ||
.then(sendJson(res), next); | ||
export const getOrderById = ( | ||
{ params, injected: { ecommerceService } }: | ||
{ params: { id: string } } & Injected<{ ecommerceService: IGetOrderById }>, | ||
res: Express.Response, | ||
next: Express.NextFunction, | ||
) => | ||
ecommerceService | ||
.getOrderById(params.id) | ||
.then(expectFound(`Order(${params.id})`)) | ||
.then(sendJson(res), next); | ||
export const getOrderById = ({ params }: Request<{ id: string }>, res: Response, next: Next) => | ||
({ ecommerceService }: { ecommerceService: IGetOrderById }) => | ||
ecommerceService | ||
.getOrderById(params.id) | ||
.then(expectFound(`Order(${params.id})`)) | ||
.then(sendJson(res), next); | ||
``` | ||
@@ -118,2 +110,3 @@ | ||
import express from 'express'; | ||
import createContext from 'express-async-context'; | ||
import container from './container'; | ||
@@ -124,21 +117,15 @@ import { getOrderById, getOrders } from './controller'; | ||
const app = express(); | ||
const Context = createContext(() => container); | ||
app.use((req, _, next) => { | ||
req.injected = container; | ||
next(); | ||
}); | ||
app.use(Context.provider); | ||
app.get('/orders', getOrders); | ||
app.get('/orders/:id', getOrderById); | ||
app.get('/orders', Context.consumer(getOrders)); | ||
app.get('/orders/:id', Context.consumer(getOrderById)); | ||
app.use(handleErrors); | ||
app.use(Context.consumer(handleErrors)); | ||
if (module.parent == null) { | ||
app.listen(8080, () => { | ||
console.log('Server is listening on port: 8080'); | ||
console.log('Follow: http://localhost:8080/orders'); | ||
}); | ||
} | ||
export default app; | ||
app.listen(8080, () => { | ||
console.log('Server is listening on port: 8080'); | ||
console.log('Follow: http://localhost:8080/orders'); | ||
}); | ||
``` | ||
@@ -145,0 +132,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59208
160