@spinajs/intl-http 2.0.179 → 2.0.181
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/lib/cjs/index.d.ts +23 -23
- package/lib/cjs/index.js +96 -96
- package/lib/cjs/index.js.map +1 -1
- package/lib/mjs/index.d.ts +23 -23
- package/lib/mjs/index.js +92 -92
- package/lib/mjs/index.js.map +1 -1
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +9 -9
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { ServerMiddleware, Request as sRequest, RouteArgs, IRouteCall, IRouteParameter } from '@spinajs/http';
|
|
2
|
-
import * as express from 'express';
|
|
3
|
-
declare module '@spinajs/http' {
|
|
4
|
-
interface IActionLocalStoregeContext {
|
|
5
|
-
language: string;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export declare function Lang(allowedLanguages?: string[]): (target: any, propertyKey?: string | symbol, indexOrDescriptor?: number | PropertyDescriptor) => void;
|
|
9
|
-
export declare class LangArgument extends RouteArgs {
|
|
10
|
-
protected LangQueryParameter: string;
|
|
11
|
-
protected defaultLocale: string;
|
|
12
|
-
get SupportedType(): string;
|
|
13
|
-
extract(callData: IRouteCall, param: IRouteParameter, req: sRequest): Promise<{
|
|
14
|
-
CallData: IRouteCall;
|
|
15
|
-
Args: any;
|
|
16
|
-
}>;
|
|
17
|
-
}
|
|
18
|
-
export declare class IntHttpMiddleware extends ServerMiddleware {
|
|
19
|
-
protected LangQueryParameter: string;
|
|
20
|
-
protected defaultLocale: string;
|
|
21
|
-
before(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
22
|
-
after(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
23
|
-
}
|
|
1
|
+
import { ServerMiddleware, Request as sRequest, RouteArgs, IRouteCall, IRouteParameter } from '@spinajs/http';
|
|
2
|
+
import * as express from 'express';
|
|
3
|
+
declare module '@spinajs/http' {
|
|
4
|
+
interface IActionLocalStoregeContext {
|
|
5
|
+
language: string;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare function Lang(allowedLanguages?: string[]): (target: any, propertyKey?: string | symbol, indexOrDescriptor?: number | PropertyDescriptor) => void;
|
|
9
|
+
export declare class LangArgument extends RouteArgs {
|
|
10
|
+
protected LangQueryParameter: string;
|
|
11
|
+
protected defaultLocale: string;
|
|
12
|
+
get SupportedType(): string;
|
|
13
|
+
extract(callData: IRouteCall, param: IRouteParameter, req: sRequest): Promise<{
|
|
14
|
+
CallData: IRouteCall;
|
|
15
|
+
Args: any;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export declare class IntHttpMiddleware extends ServerMiddleware {
|
|
19
|
+
protected LangQueryParameter: string;
|
|
20
|
+
protected defaultLocale: string;
|
|
21
|
+
before(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
22
|
+
after(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
23
|
+
}
|
|
24
24
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.IntHttpMiddleware = exports.LangArgument = exports.Lang = void 0;
|
|
13
|
-
const configuration_1 = require("@spinajs/configuration");
|
|
14
|
-
const di_1 = require("@spinajs/di");
|
|
15
|
-
const exceptions_1 = require("@spinajs/exceptions");
|
|
16
|
-
const http_1 = require("@spinajs/http");
|
|
17
|
-
function extractLanguageFromRequest(req, param) {
|
|
18
|
-
if (req.query[param]) {
|
|
19
|
-
return req.query[param];
|
|
20
|
-
}
|
|
21
|
-
else if (req.cookies[param]) {
|
|
22
|
-
return req.cookies[param];
|
|
23
|
-
}
|
|
24
|
-
else if (req.headers[`x-${param}`]) {
|
|
25
|
-
return req.headers[`x-${param}`];
|
|
26
|
-
}
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
function Lang(allowedLanguages) {
|
|
30
|
-
return (0, http_1.Route)((0, http_1.Parameter)('LangArgument', null, { allowedLanguages }));
|
|
31
|
-
}
|
|
32
|
-
exports.Lang = Lang;
|
|
33
|
-
function _validate(param) {
|
|
34
|
-
// lang param is optional
|
|
35
|
-
if (param === null || param === undefined)
|
|
36
|
-
return;
|
|
37
|
-
if (typeof param !== 'string')
|
|
38
|
-
throw new exceptions_1.InvalidArgument('lang parameter is not string');
|
|
39
|
-
if (param.length < 2 || param.length > 5)
|
|
40
|
-
throw new exceptions_1.InvalidArgument('lang parameter length is invalid');
|
|
41
|
-
}
|
|
42
|
-
let LangArgument = class LangArgument extends http_1.RouteArgs {
|
|
43
|
-
get SupportedType() {
|
|
44
|
-
return 'LangArgument';
|
|
45
|
-
}
|
|
46
|
-
async extract(callData, param, req) {
|
|
47
|
-
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
48
|
-
if (!lang) {
|
|
49
|
-
return { CallData: callData, Args: this.defaultLocale };
|
|
50
|
-
}
|
|
51
|
-
_validate(lang);
|
|
52
|
-
if (param.Options.allowedLanguages) {
|
|
53
|
-
if (param.Options.allowedLanguages.indexOf(lang) === -1) {
|
|
54
|
-
throw new exceptions_1.InvalidArgument(`Language not supported, allowed languages are ${param.Options.allowedLanguages.join(',')}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return { CallData: callData, Args: lang };
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
LangArgument
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
let IntHttpMiddleware = class IntHttpMiddleware extends http_1.ServerMiddleware {
|
|
73
|
-
before() {
|
|
74
|
-
return (req, _res, next) => {
|
|
75
|
-
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
76
|
-
_validate(lang);
|
|
77
|
-
req.storage.language = lang ?? this.defaultLocale;
|
|
78
|
-
next();
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
after() {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
IntHttpMiddleware
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.IntHttpMiddleware = exports.LangArgument = exports.Lang = void 0;
|
|
13
|
+
const configuration_1 = require("@spinajs/configuration");
|
|
14
|
+
const di_1 = require("@spinajs/di");
|
|
15
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
16
|
+
const http_1 = require("@spinajs/http");
|
|
17
|
+
function extractLanguageFromRequest(req, param) {
|
|
18
|
+
if (req.query[param]) {
|
|
19
|
+
return req.query[param];
|
|
20
|
+
}
|
|
21
|
+
else if (req.cookies[param]) {
|
|
22
|
+
return req.cookies[param];
|
|
23
|
+
}
|
|
24
|
+
else if (req.headers[`x-${param}`]) {
|
|
25
|
+
return req.headers[`x-${param}`];
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function Lang(allowedLanguages) {
|
|
30
|
+
return (0, http_1.Route)((0, http_1.Parameter)('LangArgument', null, { allowedLanguages }));
|
|
31
|
+
}
|
|
32
|
+
exports.Lang = Lang;
|
|
33
|
+
function _validate(param) {
|
|
34
|
+
// lang param is optional
|
|
35
|
+
if (param === null || param === undefined)
|
|
36
|
+
return;
|
|
37
|
+
if (typeof param !== 'string')
|
|
38
|
+
throw new exceptions_1.InvalidArgument('lang parameter is not string');
|
|
39
|
+
if (param.length < 2 || param.length > 5)
|
|
40
|
+
throw new exceptions_1.InvalidArgument('lang parameter length is invalid');
|
|
41
|
+
}
|
|
42
|
+
let LangArgument = class LangArgument extends http_1.RouteArgs {
|
|
43
|
+
get SupportedType() {
|
|
44
|
+
return 'LangArgument';
|
|
45
|
+
}
|
|
46
|
+
async extract(callData, param, req) {
|
|
47
|
+
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
48
|
+
if (!lang) {
|
|
49
|
+
return { CallData: callData, Args: this.defaultLocale };
|
|
50
|
+
}
|
|
51
|
+
_validate(lang);
|
|
52
|
+
if (param.Options.allowedLanguages) {
|
|
53
|
+
if (param.Options.allowedLanguages.indexOf(lang) === -1) {
|
|
54
|
+
throw new exceptions_1.InvalidArgument(`Language not supported, allowed languages are ${param.Options.allowedLanguages.join(',')}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { CallData: callData, Args: lang };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
exports.LangArgument = LangArgument;
|
|
61
|
+
__decorate([
|
|
62
|
+
(0, configuration_1.Config)('intl.queryParameter'),
|
|
63
|
+
__metadata("design:type", String)
|
|
64
|
+
], LangArgument.prototype, "LangQueryParameter", void 0);
|
|
65
|
+
__decorate([
|
|
66
|
+
(0, configuration_1.Config)('intl.defaultLocale'),
|
|
67
|
+
__metadata("design:type", String)
|
|
68
|
+
], LangArgument.prototype, "defaultLocale", void 0);
|
|
69
|
+
exports.LangArgument = LangArgument = __decorate([
|
|
70
|
+
(0, di_1.Injectable)()
|
|
71
|
+
], LangArgument);
|
|
72
|
+
let IntHttpMiddleware = class IntHttpMiddleware extends http_1.ServerMiddleware {
|
|
73
|
+
before() {
|
|
74
|
+
return (req, _res, next) => {
|
|
75
|
+
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
76
|
+
_validate(lang);
|
|
77
|
+
req.storage.language = lang ?? this.defaultLocale;
|
|
78
|
+
next();
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
after() {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
exports.IntHttpMiddleware = IntHttpMiddleware;
|
|
86
|
+
__decorate([
|
|
87
|
+
(0, configuration_1.Config)('intl.queryParameter'),
|
|
88
|
+
__metadata("design:type", String)
|
|
89
|
+
], IntHttpMiddleware.prototype, "LangQueryParameter", void 0);
|
|
90
|
+
__decorate([
|
|
91
|
+
(0, configuration_1.Config)('intl.defaultLocale'),
|
|
92
|
+
__metadata("design:type", String)
|
|
93
|
+
], IntHttpMiddleware.prototype, "defaultLocale", void 0);
|
|
94
|
+
exports.IntHttpMiddleware = IntHttpMiddleware = __decorate([
|
|
95
|
+
(0, di_1.Injectable)(http_1.ServerMiddleware)
|
|
96
|
+
], IntHttpMiddleware);
|
|
97
97
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0DAAgD;AAChD,oCAAyC;AACzC,oDAAsD;AACtD,wCAAgI;AAShI,SAAS,0BAA0B,CAAC,GAAa,EAAE,KAAa;IAC9D,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;QACpB,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAW,CAAC;KACnC;SAAM,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QAC7B,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;KAC3B;SAAM,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,KAAK,EAAE,CAAC,EAAE;QACpC,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,KAAK,EAAE,CAAW,CAAC;KAC5C;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,IAAI,CAAC,gBAA2B;IAC9C,OAAO,IAAA,YAAK,EAAC,IAAA,gBAAS,EAAC,cAAc,EAAE,IAAI,EAAE,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC;AACtE,CAAC;AAFD,oBAEC;AAED,SAAS,SAAS,CAAC,KAAc;IAE/B,yBAAyB;IACzB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO;IAElD,IAAI,OAAO,KAAK,KAAK,QAAQ;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7B"}
|
package/lib/mjs/index.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { ServerMiddleware, Request as sRequest, RouteArgs, IRouteCall, IRouteParameter } from '@spinajs/http';
|
|
2
|
-
import * as express from 'express';
|
|
3
|
-
declare module '@spinajs/http' {
|
|
4
|
-
interface IActionLocalStoregeContext {
|
|
5
|
-
language: string;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export declare function Lang(allowedLanguages?: string[]): (target: any, propertyKey?: string | symbol, indexOrDescriptor?: number | PropertyDescriptor) => void;
|
|
9
|
-
export declare class LangArgument extends RouteArgs {
|
|
10
|
-
protected LangQueryParameter: string;
|
|
11
|
-
protected defaultLocale: string;
|
|
12
|
-
get SupportedType(): string;
|
|
13
|
-
extract(callData: IRouteCall, param: IRouteParameter, req: sRequest): Promise<{
|
|
14
|
-
CallData: IRouteCall;
|
|
15
|
-
Args: any;
|
|
16
|
-
}>;
|
|
17
|
-
}
|
|
18
|
-
export declare class IntHttpMiddleware extends ServerMiddleware {
|
|
19
|
-
protected LangQueryParameter: string;
|
|
20
|
-
protected defaultLocale: string;
|
|
21
|
-
before(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
22
|
-
after(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
23
|
-
}
|
|
1
|
+
import { ServerMiddleware, Request as sRequest, RouteArgs, IRouteCall, IRouteParameter } from '@spinajs/http';
|
|
2
|
+
import * as express from 'express';
|
|
3
|
+
declare module '@spinajs/http' {
|
|
4
|
+
interface IActionLocalStoregeContext {
|
|
5
|
+
language: string;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare function Lang(allowedLanguages?: string[]): (target: any, propertyKey?: string | symbol, indexOrDescriptor?: number | PropertyDescriptor) => void;
|
|
9
|
+
export declare class LangArgument extends RouteArgs {
|
|
10
|
+
protected LangQueryParameter: string;
|
|
11
|
+
protected defaultLocale: string;
|
|
12
|
+
get SupportedType(): string;
|
|
13
|
+
extract(callData: IRouteCall, param: IRouteParameter, req: sRequest): Promise<{
|
|
14
|
+
CallData: IRouteCall;
|
|
15
|
+
Args: any;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export declare class IntHttpMiddleware extends ServerMiddleware {
|
|
19
|
+
protected LangQueryParameter: string;
|
|
20
|
+
protected defaultLocale: string;
|
|
21
|
+
before(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
22
|
+
after(): (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
23
|
+
}
|
|
24
24
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/mjs/index.js
CHANGED
|
@@ -1,93 +1,93 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { Config } from '@spinajs/configuration';
|
|
11
|
-
import { Injectable } from '@spinajs/di';
|
|
12
|
-
import { InvalidArgument } from '@spinajs/exceptions';
|
|
13
|
-
import { ServerMiddleware, Route, Parameter, RouteArgs } from '@spinajs/http';
|
|
14
|
-
function extractLanguageFromRequest(req, param) {
|
|
15
|
-
if (req.query[param]) {
|
|
16
|
-
return req.query[param];
|
|
17
|
-
}
|
|
18
|
-
else if (req.cookies[param]) {
|
|
19
|
-
return req.cookies[param];
|
|
20
|
-
}
|
|
21
|
-
else if (req.headers[`x-${param}`]) {
|
|
22
|
-
return req.headers[`x-${param}`];
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
export function Lang(allowedLanguages) {
|
|
27
|
-
return Route(Parameter('LangArgument', null, { allowedLanguages }));
|
|
28
|
-
}
|
|
29
|
-
function _validate(param) {
|
|
30
|
-
// lang param is optional
|
|
31
|
-
if (param === null || param === undefined)
|
|
32
|
-
return;
|
|
33
|
-
if (typeof param !== 'string')
|
|
34
|
-
throw new InvalidArgument('lang parameter is not string');
|
|
35
|
-
if (param.length < 2 || param.length > 5)
|
|
36
|
-
throw new InvalidArgument('lang parameter length is invalid');
|
|
37
|
-
}
|
|
38
|
-
let LangArgument = class LangArgument extends RouteArgs {
|
|
39
|
-
get SupportedType() {
|
|
40
|
-
return 'LangArgument';
|
|
41
|
-
}
|
|
42
|
-
async extract(callData, param, req) {
|
|
43
|
-
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
44
|
-
if (!lang) {
|
|
45
|
-
return { CallData: callData, Args: this.defaultLocale };
|
|
46
|
-
}
|
|
47
|
-
_validate(lang);
|
|
48
|
-
if (param.Options.allowedLanguages) {
|
|
49
|
-
if (param.Options.allowedLanguages.indexOf(lang) === -1) {
|
|
50
|
-
throw new InvalidArgument(`Language not supported, allowed languages are ${param.Options.allowedLanguages.join(',')}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return { CallData: callData, Args: lang };
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
__decorate([
|
|
57
|
-
Config('intl.queryParameter'),
|
|
58
|
-
__metadata("design:type", String)
|
|
59
|
-
], LangArgument.prototype, "LangQueryParameter", void 0);
|
|
60
|
-
__decorate([
|
|
61
|
-
Config('intl.defaultLocale'),
|
|
62
|
-
__metadata("design:type", String)
|
|
63
|
-
], LangArgument.prototype, "defaultLocale", void 0);
|
|
64
|
-
LangArgument = __decorate([
|
|
65
|
-
Injectable()
|
|
66
|
-
], LangArgument);
|
|
67
|
-
export { LangArgument };
|
|
68
|
-
let IntHttpMiddleware = class IntHttpMiddleware extends ServerMiddleware {
|
|
69
|
-
before() {
|
|
70
|
-
return (req, _res, next) => {
|
|
71
|
-
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
72
|
-
_validate(lang);
|
|
73
|
-
req.storage.language = lang ?? this.defaultLocale;
|
|
74
|
-
next();
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
after() {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
__decorate([
|
|
82
|
-
Config('intl.queryParameter'),
|
|
83
|
-
__metadata("design:type", String)
|
|
84
|
-
], IntHttpMiddleware.prototype, "LangQueryParameter", void 0);
|
|
85
|
-
__decorate([
|
|
86
|
-
Config('intl.defaultLocale'),
|
|
87
|
-
__metadata("design:type", String)
|
|
88
|
-
], IntHttpMiddleware.prototype, "defaultLocale", void 0);
|
|
89
|
-
IntHttpMiddleware = __decorate([
|
|
90
|
-
Injectable(ServerMiddleware)
|
|
91
|
-
], IntHttpMiddleware);
|
|
92
|
-
export { IntHttpMiddleware };
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Config } from '@spinajs/configuration';
|
|
11
|
+
import { Injectable } from '@spinajs/di';
|
|
12
|
+
import { InvalidArgument } from '@spinajs/exceptions';
|
|
13
|
+
import { ServerMiddleware, Route, Parameter, RouteArgs } from '@spinajs/http';
|
|
14
|
+
function extractLanguageFromRequest(req, param) {
|
|
15
|
+
if (req.query[param]) {
|
|
16
|
+
return req.query[param];
|
|
17
|
+
}
|
|
18
|
+
else if (req.cookies[param]) {
|
|
19
|
+
return req.cookies[param];
|
|
20
|
+
}
|
|
21
|
+
else if (req.headers[`x-${param}`]) {
|
|
22
|
+
return req.headers[`x-${param}`];
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
export function Lang(allowedLanguages) {
|
|
27
|
+
return Route(Parameter('LangArgument', null, { allowedLanguages }));
|
|
28
|
+
}
|
|
29
|
+
function _validate(param) {
|
|
30
|
+
// lang param is optional
|
|
31
|
+
if (param === null || param === undefined)
|
|
32
|
+
return;
|
|
33
|
+
if (typeof param !== 'string')
|
|
34
|
+
throw new InvalidArgument('lang parameter is not string');
|
|
35
|
+
if (param.length < 2 || param.length > 5)
|
|
36
|
+
throw new InvalidArgument('lang parameter length is invalid');
|
|
37
|
+
}
|
|
38
|
+
let LangArgument = class LangArgument extends RouteArgs {
|
|
39
|
+
get SupportedType() {
|
|
40
|
+
return 'LangArgument';
|
|
41
|
+
}
|
|
42
|
+
async extract(callData, param, req) {
|
|
43
|
+
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
44
|
+
if (!lang) {
|
|
45
|
+
return { CallData: callData, Args: this.defaultLocale };
|
|
46
|
+
}
|
|
47
|
+
_validate(lang);
|
|
48
|
+
if (param.Options.allowedLanguages) {
|
|
49
|
+
if (param.Options.allowedLanguages.indexOf(lang) === -1) {
|
|
50
|
+
throw new InvalidArgument(`Language not supported, allowed languages are ${param.Options.allowedLanguages.join(',')}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { CallData: callData, Args: lang };
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
__decorate([
|
|
57
|
+
Config('intl.queryParameter'),
|
|
58
|
+
__metadata("design:type", String)
|
|
59
|
+
], LangArgument.prototype, "LangQueryParameter", void 0);
|
|
60
|
+
__decorate([
|
|
61
|
+
Config('intl.defaultLocale'),
|
|
62
|
+
__metadata("design:type", String)
|
|
63
|
+
], LangArgument.prototype, "defaultLocale", void 0);
|
|
64
|
+
LangArgument = __decorate([
|
|
65
|
+
Injectable()
|
|
66
|
+
], LangArgument);
|
|
67
|
+
export { LangArgument };
|
|
68
|
+
let IntHttpMiddleware = class IntHttpMiddleware extends ServerMiddleware {
|
|
69
|
+
before() {
|
|
70
|
+
return (req, _res, next) => {
|
|
71
|
+
const lang = extractLanguageFromRequest(req, this.LangQueryParameter);
|
|
72
|
+
_validate(lang);
|
|
73
|
+
req.storage.language = lang ?? this.defaultLocale;
|
|
74
|
+
next();
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
after() {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
__decorate([
|
|
82
|
+
Config('intl.queryParameter'),
|
|
83
|
+
__metadata("design:type", String)
|
|
84
|
+
], IntHttpMiddleware.prototype, "LangQueryParameter", void 0);
|
|
85
|
+
__decorate([
|
|
86
|
+
Config('intl.defaultLocale'),
|
|
87
|
+
__metadata("design:type", String)
|
|
88
|
+
], IntHttpMiddleware.prototype, "defaultLocale", void 0);
|
|
89
|
+
IntHttpMiddleware = __decorate([
|
|
90
|
+
Injectable(ServerMiddleware)
|
|
91
|
+
], IntHttpMiddleware);
|
|
92
|
+
export { IntHttpMiddleware };
|
|
93
93
|
//# sourceMappingURL=index.js.map
|
package/lib/mjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,M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