mvc-middleware
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -15,3 +15,3 @@ import { Request, Response } from "express"; | ||
permanentRedirect(url: string): void; | ||
redirect(status: number, url: string): void; | ||
redirect(statusCode: number, url: string): void; | ||
badRequest(model?: any): void; | ||
@@ -18,0 +18,0 @@ unauthorized(model?: any): void; |
@@ -54,4 +54,7 @@ "use strict"; | ||
} | ||
redirect(status, url) { | ||
this.response.redirect(status, url); | ||
redirect(statusCode, url) { | ||
if (statusCode < 300 || statusCode > 308) { | ||
throw new Error(`Invalid argument: statusCode (${statusCode}). Status code should be in interval 300 - 308`); | ||
} | ||
this.response.redirect(statusCode, url); | ||
} | ||
@@ -80,5 +83,14 @@ badRequest(model) { | ||
} | ||
this.response.setHeader('Content-Type', 'application/json'); | ||
return JSON.stringify(model); | ||
} | ||
sendResponse(model, statusCode = 200) { | ||
if (statusCode < 100 || | ||
statusCode > 103 && statusCode < 200 || | ||
statusCode > 226 && statusCode < 300 || | ||
statusCode > 308 && statusCode < 400 || | ||
statusCode > 449 && statusCode < 500 || | ||
statusCode > 526) { | ||
throw new Error(`Invalid argument: statusCode (${statusCode}).`); | ||
} | ||
const result = this.toJson(model); | ||
@@ -85,0 +97,0 @@ this.response.status(statusCode).send(result); |
{ | ||
"name": "mvc-middleware", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Mvc middleware for express like .Net Mvc", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -214,4 +214,21 @@ # mvc-middleware | ||
## Controller API | ||
- `ok` method of `MvcController` returns data with 200 status code. You can pass plain text or object that will be transformed to json. | ||
- `view` returns html view by name (using of `getViewPath` method) | ||
- TBD. | ||
| Method name | Response status code | Response type | Arguments | Description | | ||
|:-------------------:|:--------------------:|:-------------:|:---------------------------------:|------------------------------------------------------------| | ||
| `view` | 200 | html | `viewName: string` | returns html view by name (using of `getViewPath` method) | | ||
| `ok` | 200 | text or json | `model?: any` | returns 200 status code with data | | ||
| `created` | 201 | text or json | `model?: any` | returns 201 status code with data | | ||
| `accepted` | 202 | text or json | `model?: any` | returns 202 status code with data | | ||
| `noContent` | 204 | - | - | returns 204 status code | | ||
| | | | | | | ||
| `found` | 302 | text | `url: string` | returns 302 status code | | ||
| `permanentRedirect` | 308 | text | `url: string` | returns 308 status code | | ||
| `redirect` | 300 - 308 | text | `statusCode: number, url: string` | returns redirection status code | | ||
| | | | | | | ||
| `badRequest` | 400 | text or json | `model?: any` | returns 400 status code with data | | ||
| `unauthorized` | 401 | text or json | `model?: any` | returns 401 status code with data | | ||
| `forbid` | 403 | - | `model?: any` | returns 403 status code | | ||
| `notFound` | 404 | text or json | `model?: any` | returns 404 status code with data | | ||
| `conflict` | 409 | text or json | `model?: any` | returns 409 status code with data | | ||
| | | | | | | ||
| `serverError` | 500 | text | `message?: any` | returns 500 status code with error message | | ||
| `sendResponse` | any http status code | text | `model: any, statusCode?: number` | returns status code with data. Default status code is 200 | |
Sorry, the diff of this file is not supported yet
28932
281
234