c2-http 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/i18n.d.ts +12 -0
- package/dist/i18n.js +36 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +32 -1
- package/dist/model/HttpError.d.ts +6 -0
- package/dist/model/HttpError.js +16 -0
- package/package.json +5 -1
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare let i18n: any;
|
|
2
|
+
export interface Ii18nMessageConfig {
|
|
3
|
+
defaultLocale: string;
|
|
4
|
+
warn: boolean;
|
|
5
|
+
allowFallback: boolean;
|
|
6
|
+
messages: {
|
|
7
|
+
[key: string]: object;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export declare const initializei18n: (config: Ii18nMessageConfig) => void;
|
|
11
|
+
export declare const getMessage: (message: string, ...parameters: string[]) => string;
|
|
12
|
+
export default i18n;
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMessage = exports.initializei18n = void 0;
|
|
7
|
+
const c2_util_1 = require("c2-util");
|
|
8
|
+
const express_http_context_1 = __importDefault(require("express-http-context"));
|
|
9
|
+
const i18nCreate = require("express-rest-i18n");
|
|
10
|
+
let i18n = i18nCreate({
|
|
11
|
+
defaultLocale: "pt-br"
|
|
12
|
+
});
|
|
13
|
+
const initializei18n = (config) => {
|
|
14
|
+
i18n = i18nCreate(config);
|
|
15
|
+
};
|
|
16
|
+
exports.initializei18n = initializei18n;
|
|
17
|
+
const getMessage = (message, ...parameters) => {
|
|
18
|
+
const headers = express_http_context_1.default.get("headers");
|
|
19
|
+
let language = [];
|
|
20
|
+
if (typeof headers !== "undefined") {
|
|
21
|
+
if (headers["accept-language"] !== undefined) {
|
|
22
|
+
language = headers["accept-language"].split(",");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
let t = i18n.t(message);
|
|
26
|
+
if ((0, c2_util_1.isNotEmpty)(language)) {
|
|
27
|
+
t = i18n.t(message, language);
|
|
28
|
+
}
|
|
29
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
30
|
+
const param = parameters[i];
|
|
31
|
+
t = t.replaceAll(`{${i}}`, param);
|
|
32
|
+
}
|
|
33
|
+
return t;
|
|
34
|
+
};
|
|
35
|
+
exports.getMessage = getMessage;
|
|
36
|
+
exports.default = i18n;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { getMessage, initializei18n } from "./i18n";
|
|
1
2
|
import { Controller, HttpDispatchDownload, HttpDispatchHandling } from "./model/Controller";
|
|
2
3
|
import { CrudController } from "./model/CrudController";
|
|
4
|
+
import { HttpError } from "./model/HttpError";
|
|
3
5
|
import { IControllerOptions } from "./model/IControllerOptions";
|
|
4
6
|
import { ICrudControllerOptions } from "./model/ICrudControllerOptions";
|
|
5
|
-
export
|
|
7
|
+
export declare const convertErrorToHttpError: (error: any) => HttpError;
|
|
8
|
+
export { Controller, CrudController, IControllerOptions, ICrudControllerOptions, HttpDispatchDownload, HttpDispatchHandling, getMessage, initializei18n };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
6
|
+
exports.initializei18n = exports.getMessage = exports.HttpDispatchHandling = exports.HttpDispatchDownload = exports.CrudController = exports.Controller = exports.convertErrorToHttpError = void 0;
|
|
7
|
+
const express_http_context_1 = __importDefault(require("express-http-context"));
|
|
8
|
+
const http_status_1 = require("http-status");
|
|
9
|
+
const mongodb_1 = require("mongodb");
|
|
10
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
11
|
+
const i18n_1 = require("./i18n");
|
|
12
|
+
Object.defineProperty(exports, "getMessage", { enumerable: true, get: function () { return i18n_1.getMessage; } });
|
|
13
|
+
Object.defineProperty(exports, "initializei18n", { enumerable: true, get: function () { return i18n_1.initializei18n; } });
|
|
4
14
|
const Controller_1 = require("./model/Controller");
|
|
5
15
|
Object.defineProperty(exports, "Controller", { enumerable: true, get: function () { return Controller_1.Controller; } });
|
|
6
16
|
Object.defineProperty(exports, "HttpDispatchDownload", { enumerable: true, get: function () { return Controller_1.HttpDispatchDownload; } });
|
|
7
17
|
Object.defineProperty(exports, "HttpDispatchHandling", { enumerable: true, get: function () { return Controller_1.HttpDispatchHandling; } });
|
|
8
18
|
const CrudController_1 = require("./model/CrudController");
|
|
9
19
|
Object.defineProperty(exports, "CrudController", { enumerable: true, get: function () { return CrudController_1.CrudController; } });
|
|
20
|
+
const HttpError_1 = require("./model/HttpError");
|
|
21
|
+
const convertErrorToHttpError = (error) => {
|
|
22
|
+
if (error instanceof mongodb_1.MongoServerError) {
|
|
23
|
+
if (error.code === 11000) {
|
|
24
|
+
return new HttpError_1.HttpError(http_status_1.CONFLICT, (0, i18n_1.getMessage)("message.duplicate"), error.keyValue);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (error instanceof mongoose_1.default.Error.ValidationError) {
|
|
28
|
+
error = error;
|
|
29
|
+
const fields = Object.keys(error.errors);
|
|
30
|
+
return new HttpError_1.HttpError(http_status_1.NOT_ACCEPTABLE, (0, i18n_1.getMessage)("message.fieldIsRequired", fields.join(", ")), error.errors);
|
|
31
|
+
}
|
|
32
|
+
if (error instanceof HttpError_1.HttpError) {
|
|
33
|
+
return error;
|
|
34
|
+
}
|
|
35
|
+
return new HttpError_1.HttpError(http_status_1.INTERNAL_SERVER_ERROR, error.message, {
|
|
36
|
+
requestId: express_http_context_1.default.get("rid"),
|
|
37
|
+
error
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
exports.convertErrorToHttpError = convertErrorToHttpError;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpError = void 0;
|
|
4
|
+
class HttpError extends Error {
|
|
5
|
+
status;
|
|
6
|
+
detail;
|
|
7
|
+
message;
|
|
8
|
+
constructor(status, message, detail = undefined) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.detail = detail;
|
|
12
|
+
this.message = message;
|
|
13
|
+
Object.setPrototypeOf(this, HttpError.prototype);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.HttpError = HttpError;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Biblioteca Typescript para API NodeJS",
|
|
5
5
|
"repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
|
|
6
6
|
"author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",
|
|
@@ -21,9 +21,13 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@types/express": "^4.17.21",
|
|
23
23
|
"@types/http-status": "^1.1.2",
|
|
24
|
+
"@types/mongoose": "^5.11.97",
|
|
24
25
|
"c2-util": "^1.0.13",
|
|
25
26
|
"express": "^4.19.2",
|
|
27
|
+
"express-http-context": "^1.2.4",
|
|
28
|
+
"express-rest-i18n": "^1.0.1",
|
|
26
29
|
"http-status": "^1.7.4",
|
|
30
|
+
"mongoose": "^8.3.4",
|
|
27
31
|
"ts-node": "^10.8.1",
|
|
28
32
|
"typescript": "^4.7.4"
|
|
29
33
|
},
|