seishiro 0.1.6-untest
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/LICENSE +661 -0
- package/README.md +124 -0
- package/helper/format-key.d.ts +1 -0
- package/helper/format-key.js +9 -0
- package/index.d.ts +4 -0
- package/index.js +14 -0
- package/lib/actions.d.ts +135 -0
- package/lib/actions.js +175 -0
- package/lib/message.d.ts +41 -0
- package/lib/message.js +122 -0
- package/lib/policy.d.ts +33 -0
- package/lib/policy.js +73 -0
- package/lib/registry.d.ts +29 -0
- package/lib/registry.js +72 -0
- package/package.json +21 -0
- package/types/message.type.d.ts +13 -0
- package/types/message.type.js +3 -0
- package/types/policy.type.d.ts +10 -0
- package/types/policy.type.js +2 -0
- package/types/registry.type.d.ts +16 -0
- package/types/registry.type.js +2 -0
- package/var/message.d.ts +1 -0
- package/var/message.js +4 -0
- package/var/registry.d.ts +3 -0
- package/var/registry.js +6 -0
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Seishiro API
|
|
2
|
+
|
|
3
|
+
Seishiro eliminates the complexity of routing folder structures and replaces them with a single control center. Just use one endpoint, manage it through the Registry, and control your entire application data flow (Web, Mobile, and SSR) with consistent standards.
|
|
4
|
+
|
|
5
|
+
## Tasks
|
|
6
|
+
|
|
7
|
+
- [x] Registry Controllers
|
|
8
|
+
- [x] Message Builder Error
|
|
9
|
+
- [x] UnRegistry Base System
|
|
10
|
+
- [x] Multi Language Respon
|
|
11
|
+
- [x] Protocol Response
|
|
12
|
+
- [x] Middleware Runner
|
|
13
|
+
- [x] Action Executed
|
|
14
|
+
- [ ] Versioning Header
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
1. Manual Library Installation
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npm install seishiro
|
|
22
|
+
# OR
|
|
23
|
+
bun add seishiro
|
|
24
|
+
yarn add seishiro
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
2. Copy the file `./example/api/action.(js/ts)` and then copy some of its structure for the sample. You can manage this system to be much more complex.
|
|
28
|
+
3. Set up and customize it as you like.
|
|
29
|
+
4. Add auth-cookie to put the default placeholder and client version.
|
|
30
|
+
5. Boom! Done!
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import dotenv from "dotenv"
|
|
36
|
+
import { UserLogin, UserProfile } from "@/controllers"
|
|
37
|
+
import { MiddlewareSystem } from "@/middlewares"
|
|
38
|
+
import { RegistryBuilder, MessageBuilder, PolicyBuilder, Actions } from "seishiro"
|
|
39
|
+
|
|
40
|
+
// Init ENV
|
|
41
|
+
dotenv.config()
|
|
42
|
+
|
|
43
|
+
// Registry
|
|
44
|
+
const registry = new RegistryBuilder()
|
|
45
|
+
registry.set("user:login", UserLogin)
|
|
46
|
+
registry.set("user:profile", UserProfile, MiddlewareSystem) // Using Middleware
|
|
47
|
+
|
|
48
|
+
// Message Error
|
|
49
|
+
const message = new MessageBuilder("en")
|
|
50
|
+
// Default Variable Message
|
|
51
|
+
message.set("no-response-sending", "Server not response!");
|
|
52
|
+
message.set("no-registry", "Registry not found!");
|
|
53
|
+
message.set("internal-server-error", "Internal server error!");
|
|
54
|
+
// Costum Variable Message
|
|
55
|
+
message.set("user-not-login", "{{username}} has not login!")
|
|
56
|
+
message.set("user-not-found", "{{username}} not found!")
|
|
57
|
+
|
|
58
|
+
// Rules System
|
|
59
|
+
const policy = new PolicyBuilder({
|
|
60
|
+
passkey: process.env.SEISHIRO_PASSKEY, // (Require!) For Book Registry Access
|
|
61
|
+
version_now: "1.4.5", // (Require!) Only support number (0-9.)
|
|
62
|
+
version_min: "1.4.0", // (Require!) Minimum version
|
|
63
|
+
version_forceupdate: true, // (Require!) If client on minimum version, client need to force update
|
|
64
|
+
})
|
|
65
|
+
policy.noaction("user:login", ["server-action"]) // block from server action
|
|
66
|
+
policy.noaction("user:login", ["api-action"]) // block from api action
|
|
67
|
+
|
|
68
|
+
// Action Setup
|
|
69
|
+
const action = new Actions({
|
|
70
|
+
registry: registry,
|
|
71
|
+
message: message,
|
|
72
|
+
policy: policy,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
// System Action - For Server Side Rendering
|
|
76
|
+
const sysaction = await action.SystemAction({
|
|
77
|
+
system: {
|
|
78
|
+
headers: {...}, // Header KV (Key, Value)
|
|
79
|
+
cookies: {...}, // Cookie KV (Key, Value)
|
|
80
|
+
ip: "103.214.146.116", // IP Address (IP Client - Example)
|
|
81
|
+
location: "JKT, Jakarta, Jakarta Pusat", // Location (Location Client By IP & PeerDB - Example)
|
|
82
|
+
},
|
|
83
|
+
type: "user:login", // Type Action
|
|
84
|
+
data: {
|
|
85
|
+
email: "testing@localhost.id",
|
|
86
|
+
password: "testing1234"
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
console.log(sysaction.response) // Response Data
|
|
90
|
+
|
|
91
|
+
// Server Action - For Server Action (Next.js)
|
|
92
|
+
const serveraction = await action.ServerAction({
|
|
93
|
+
system: {
|
|
94
|
+
headers: {...}, // Header KV (Key, Value)
|
|
95
|
+
cookies: {...}, // Cookie KV (Key, Value)
|
|
96
|
+
ip: "103.214.146.116", // IP Address (IP Client - Example)
|
|
97
|
+
location: "JKT, Jakarta, Jakarta Pusat", // Location (Location Client By IP & PeerDB - Example)
|
|
98
|
+
},
|
|
99
|
+
type: "user:profile", // Type Action
|
|
100
|
+
data: {
|
|
101
|
+
username: "testing1234"
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
console.log(serveraction.response) // Response Data
|
|
105
|
+
|
|
106
|
+
// API Action - For Rest API
|
|
107
|
+
const apiaction = action.APIAction({
|
|
108
|
+
system: {
|
|
109
|
+
headers: {...}, // Header KV (Key, Value)
|
|
110
|
+
cookies: {...}, // Cookie KV (Key, Value)
|
|
111
|
+
ip: "103.214.146.116", // IP Address (IP Client - Example)
|
|
112
|
+
location: "JKT, Jakarta, Jakarta Pusat", // Location (Location Client By IP & PeerDB - Example)
|
|
113
|
+
},
|
|
114
|
+
type: "user:profile", // Type Action
|
|
115
|
+
data: {
|
|
116
|
+
username: "testing1234"
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
console.log(apiaction.response) // Response Data
|
|
120
|
+
|
|
121
|
+
// Book Registry (List All Registry On Base Encrypted)
|
|
122
|
+
const bookRegistryJson = action.BookRegistry()
|
|
123
|
+
console.log(bookRegistryJson)
|
|
124
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function formatKey(strkey?: string): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = formatKey;
|
|
4
|
+
function formatKey(strkey = "") {
|
|
5
|
+
const keyStr = String(strkey || "")
|
|
6
|
+
.trim()
|
|
7
|
+
.replace(/[^a-zA-Z0-9-.:]/g, "");
|
|
8
|
+
return String(keyStr || "");
|
|
9
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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.Actions = exports.PolicyBuilder = exports.MessageBuilder = exports.RegistryBuilder = void 0;
|
|
7
|
+
var registry_1 = require("./lib/registry");
|
|
8
|
+
Object.defineProperty(exports, "RegistryBuilder", { enumerable: true, get: function () { return __importDefault(registry_1).default; } });
|
|
9
|
+
var message_1 = require("./lib/message");
|
|
10
|
+
Object.defineProperty(exports, "MessageBuilder", { enumerable: true, get: function () { return __importDefault(message_1).default; } });
|
|
11
|
+
var policy_1 = require("./lib/policy");
|
|
12
|
+
Object.defineProperty(exports, "PolicyBuilder", { enumerable: true, get: function () { return __importDefault(policy_1).default; } });
|
|
13
|
+
var actions_1 = require("./lib/actions");
|
|
14
|
+
Object.defineProperty(exports, "Actions", { enumerable: true, get: function () { return __importDefault(actions_1).default; } });
|
package/lib/actions.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { RegistryParams } from "../types/registry.type";
|
|
2
|
+
import RegistryBuilder from "./registry";
|
|
3
|
+
import MessageBuilder from "./message";
|
|
4
|
+
import PolicyBuilder from "./policy";
|
|
5
|
+
/**
|
|
6
|
+
* @name Actions
|
|
7
|
+
*/
|
|
8
|
+
export default class Actions {
|
|
9
|
+
private registry;
|
|
10
|
+
private message;
|
|
11
|
+
private policy;
|
|
12
|
+
constructor({ registry, message, policy, }: {
|
|
13
|
+
registry: RegistryBuilder;
|
|
14
|
+
message: MessageBuilder;
|
|
15
|
+
policy: PolicyBuilder;
|
|
16
|
+
});
|
|
17
|
+
BookRegistry(): {
|
|
18
|
+
iv_length: number;
|
|
19
|
+
type_base: string;
|
|
20
|
+
iv: string;
|
|
21
|
+
data: string;
|
|
22
|
+
};
|
|
23
|
+
private ResponseBuilder;
|
|
24
|
+
SystemAction({ system, middleware, type, data }: RegistryParams): Promise<{
|
|
25
|
+
header: {
|
|
26
|
+
key: string;
|
|
27
|
+
value: unknown;
|
|
28
|
+
}[];
|
|
29
|
+
set_cookie: any;
|
|
30
|
+
rm_cookie: any;
|
|
31
|
+
status: any;
|
|
32
|
+
redirect: any;
|
|
33
|
+
error: any;
|
|
34
|
+
response: {
|
|
35
|
+
status: any;
|
|
36
|
+
message: string;
|
|
37
|
+
protocol: string;
|
|
38
|
+
context: string[];
|
|
39
|
+
params: object[];
|
|
40
|
+
data?: undefined;
|
|
41
|
+
};
|
|
42
|
+
} | {
|
|
43
|
+
header: {
|
|
44
|
+
key: string;
|
|
45
|
+
value: unknown;
|
|
46
|
+
}[];
|
|
47
|
+
set_cookie: any;
|
|
48
|
+
rm_cookie: any;
|
|
49
|
+
status: any;
|
|
50
|
+
redirect: any;
|
|
51
|
+
error: null;
|
|
52
|
+
response: {
|
|
53
|
+
status: any;
|
|
54
|
+
data: any;
|
|
55
|
+
message?: undefined;
|
|
56
|
+
protocol?: undefined;
|
|
57
|
+
context?: undefined;
|
|
58
|
+
params?: undefined;
|
|
59
|
+
};
|
|
60
|
+
}>;
|
|
61
|
+
ServerAction({ system, middleware, type, data }: RegistryParams): Promise<{
|
|
62
|
+
header: {
|
|
63
|
+
key: string;
|
|
64
|
+
value: unknown;
|
|
65
|
+
}[];
|
|
66
|
+
set_cookie: any;
|
|
67
|
+
rm_cookie: any;
|
|
68
|
+
status: any;
|
|
69
|
+
redirect: any;
|
|
70
|
+
error: any;
|
|
71
|
+
response: {
|
|
72
|
+
status: any;
|
|
73
|
+
message: string;
|
|
74
|
+
protocol: string;
|
|
75
|
+
context: string[];
|
|
76
|
+
params: object[];
|
|
77
|
+
data?: undefined;
|
|
78
|
+
};
|
|
79
|
+
} | {
|
|
80
|
+
header: {
|
|
81
|
+
key: string;
|
|
82
|
+
value: unknown;
|
|
83
|
+
}[];
|
|
84
|
+
set_cookie: any;
|
|
85
|
+
rm_cookie: any;
|
|
86
|
+
status: any;
|
|
87
|
+
redirect: any;
|
|
88
|
+
error: null;
|
|
89
|
+
response: {
|
|
90
|
+
status: any;
|
|
91
|
+
data: any;
|
|
92
|
+
message?: undefined;
|
|
93
|
+
protocol?: undefined;
|
|
94
|
+
context?: undefined;
|
|
95
|
+
params?: undefined;
|
|
96
|
+
};
|
|
97
|
+
}>;
|
|
98
|
+
APIAction({ system, middleware, type, data }: RegistryParams): Promise<{
|
|
99
|
+
header: {
|
|
100
|
+
key: string;
|
|
101
|
+
value: unknown;
|
|
102
|
+
}[];
|
|
103
|
+
set_cookie: any;
|
|
104
|
+
rm_cookie: any;
|
|
105
|
+
status: any;
|
|
106
|
+
redirect: any;
|
|
107
|
+
error: any;
|
|
108
|
+
response: {
|
|
109
|
+
status: any;
|
|
110
|
+
message: string;
|
|
111
|
+
protocol: string;
|
|
112
|
+
context: string[];
|
|
113
|
+
params: object[];
|
|
114
|
+
data?: undefined;
|
|
115
|
+
};
|
|
116
|
+
} | {
|
|
117
|
+
header: {
|
|
118
|
+
key: string;
|
|
119
|
+
value: unknown;
|
|
120
|
+
}[];
|
|
121
|
+
set_cookie: any;
|
|
122
|
+
rm_cookie: any;
|
|
123
|
+
status: any;
|
|
124
|
+
redirect: any;
|
|
125
|
+
error: null;
|
|
126
|
+
response: {
|
|
127
|
+
status: any;
|
|
128
|
+
data: any;
|
|
129
|
+
message?: undefined;
|
|
130
|
+
protocol?: undefined;
|
|
131
|
+
context?: undefined;
|
|
132
|
+
params?: undefined;
|
|
133
|
+
};
|
|
134
|
+
}>;
|
|
135
|
+
}
|
package/lib/actions.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
16
|
+
/**
|
|
17
|
+
* @name Actions
|
|
18
|
+
*/
|
|
19
|
+
class Actions {
|
|
20
|
+
constructor({ registry, message, policy, }) {
|
|
21
|
+
this.registry = registry;
|
|
22
|
+
this.message = message;
|
|
23
|
+
this.policy = policy;
|
|
24
|
+
}
|
|
25
|
+
BookRegistry() {
|
|
26
|
+
// Default Configuration
|
|
27
|
+
const algorithm = "aes-256-ctr";
|
|
28
|
+
const type_base = "hex";
|
|
29
|
+
const ivLength = 16;
|
|
30
|
+
const ivKey = crypto_1.default.randomBytes(ivLength);
|
|
31
|
+
// Dynamic Info
|
|
32
|
+
const policyInfo = this.policy.apply();
|
|
33
|
+
const registry = this.registry.apply();
|
|
34
|
+
const keyencrypt = crypto_1.default
|
|
35
|
+
.createHash("sha256")
|
|
36
|
+
.update(String(policyInfo.passkey || "").trim())
|
|
37
|
+
.digest();
|
|
38
|
+
// Data List
|
|
39
|
+
const buildRegistry = {
|
|
40
|
+
listkey: Object.keys(registry).filter((a) => !policyInfo.noaction_api.includes(a)),
|
|
41
|
+
version_now: policyInfo.version_now,
|
|
42
|
+
version_min: policyInfo.version_min,
|
|
43
|
+
version_forceupdate: policyInfo.version_forceupdate,
|
|
44
|
+
};
|
|
45
|
+
// Create Encrypted Data
|
|
46
|
+
const cipher = crypto_1.default.createCipheriv(algorithm, keyencrypt, ivKey);
|
|
47
|
+
const results = Buffer.concat([
|
|
48
|
+
ivKey,
|
|
49
|
+
cipher.update(JSON.stringify(buildRegistry), "utf-8"),
|
|
50
|
+
cipher.final(),
|
|
51
|
+
]);
|
|
52
|
+
return {
|
|
53
|
+
iv_length: ivLength,
|
|
54
|
+
type_base: type_base,
|
|
55
|
+
iv: ivKey.toString(type_base),
|
|
56
|
+
data: results.toString(type_base),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
ResponseBuilder(dataRes = {}, system) {
|
|
60
|
+
const responseStatus = dataRes.error
|
|
61
|
+
? dataRes.status || 400
|
|
62
|
+
: dataRes.status || 200;
|
|
63
|
+
const setHeaders = typeof dataRes.headers === "object" && !Array.isArray(dataRes.headers)
|
|
64
|
+
? Object.entries(dataRes.headers).map(([key, value]) => {
|
|
65
|
+
return {
|
|
66
|
+
key: key,
|
|
67
|
+
value: value,
|
|
68
|
+
};
|
|
69
|
+
})
|
|
70
|
+
: [];
|
|
71
|
+
const setCookie = Array.isArray(dataRes.set_cookie)
|
|
72
|
+
? (dataRes.set_cookie || []).filter((a) => typeof a === "object" && !Array.isArray(a) && a.key && a.value)
|
|
73
|
+
: [];
|
|
74
|
+
const rmCookie = Array.isArray(dataRes.rm_cookie)
|
|
75
|
+
? (dataRes.rm_cookie || []).filter((a) => typeof a === "object" && !Array.isArray(a) && a.key)
|
|
76
|
+
: [];
|
|
77
|
+
const redirect = dataRes.redirect || null;
|
|
78
|
+
if (dataRes.error || !dataRes || typeof dataRes !== "object") {
|
|
79
|
+
const buildingMessage = this.message.error(dataRes.error || "system:no-response-sending", []);
|
|
80
|
+
return {
|
|
81
|
+
header: setHeaders,
|
|
82
|
+
set_cookie: setCookie,
|
|
83
|
+
rm_cookie: rmCookie,
|
|
84
|
+
status: responseStatus,
|
|
85
|
+
redirect: redirect,
|
|
86
|
+
error: dataRes.error || "system:no-response-sending",
|
|
87
|
+
response: {
|
|
88
|
+
status: responseStatus,
|
|
89
|
+
message: buildingMessage.message,
|
|
90
|
+
protocol: buildingMessage.protocol,
|
|
91
|
+
context: buildingMessage.context,
|
|
92
|
+
params: buildingMessage.params,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
header: setHeaders,
|
|
98
|
+
set_cookie: setCookie,
|
|
99
|
+
rm_cookie: rmCookie,
|
|
100
|
+
status: responseStatus,
|
|
101
|
+
redirect: redirect,
|
|
102
|
+
error: null,
|
|
103
|
+
response: {
|
|
104
|
+
status: responseStatus,
|
|
105
|
+
data: dataRes.data || {},
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
SystemAction(_a) {
|
|
110
|
+
return __awaiter(this, arguments, void 0, function* ({ system, middleware = {}, type, data }) {
|
|
111
|
+
try {
|
|
112
|
+
const showregistry = this.registry;
|
|
113
|
+
const getregistry = showregistry.get(type || "");
|
|
114
|
+
if (!getregistry) {
|
|
115
|
+
return this.ResponseBuilder({
|
|
116
|
+
error: "system:no-registry",
|
|
117
|
+
status: 404,
|
|
118
|
+
}, system);
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(getregistry)) {
|
|
121
|
+
const middlewareExecuted = yield getregistry[0]({
|
|
122
|
+
system,
|
|
123
|
+
middleware,
|
|
124
|
+
type,
|
|
125
|
+
data,
|
|
126
|
+
});
|
|
127
|
+
const responsesMiddleware = this.ResponseBuilder(middlewareExecuted, system);
|
|
128
|
+
const executedResponse = yield getregistry[1]({
|
|
129
|
+
system,
|
|
130
|
+
middleware: responsesMiddleware,
|
|
131
|
+
type,
|
|
132
|
+
data,
|
|
133
|
+
});
|
|
134
|
+
return this.ResponseBuilder(executedResponse, system);
|
|
135
|
+
}
|
|
136
|
+
const executedResponse = yield getregistry({
|
|
137
|
+
system,
|
|
138
|
+
middleware,
|
|
139
|
+
type,
|
|
140
|
+
data,
|
|
141
|
+
});
|
|
142
|
+
return this.ResponseBuilder(executedResponse, system);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
return this.ResponseBuilder({
|
|
146
|
+
error: "system:internal-server-error",
|
|
147
|
+
status: 500,
|
|
148
|
+
}, system);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
ServerAction(_a) {
|
|
153
|
+
return __awaiter(this, arguments, void 0, function* ({ system, middleware, type, data }) {
|
|
154
|
+
if (this.policy.apply().noaction_server.includes(type || "")) {
|
|
155
|
+
return this.ResponseBuilder({
|
|
156
|
+
error: "system:no-registry",
|
|
157
|
+
status: 404,
|
|
158
|
+
}, system);
|
|
159
|
+
}
|
|
160
|
+
return this.SystemAction({ system, middleware, type, data });
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
APIAction(_a) {
|
|
164
|
+
return __awaiter(this, arguments, void 0, function* ({ system, middleware, type, data }) {
|
|
165
|
+
if (this.policy.apply().noaction_api.includes(type || "")) {
|
|
166
|
+
return this.ResponseBuilder({
|
|
167
|
+
error: "system:no-registry",
|
|
168
|
+
status: 404,
|
|
169
|
+
}, system);
|
|
170
|
+
}
|
|
171
|
+
return this.SystemAction({ system, middleware, type, data });
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.default = Actions;
|
package/lib/message.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { MessageKey, MessageValue, MessageLang, MessageLogic, MessageLogicLang, MessageErrorContext, MessageErrorContextOpt, MessageErrorContextSlug } from "../types/message.type";
|
|
2
|
+
/**
|
|
3
|
+
* @name MessageBuilder
|
|
4
|
+
*/
|
|
5
|
+
export default class MessageBuilder {
|
|
6
|
+
private message_build_lang;
|
|
7
|
+
private message_logic;
|
|
8
|
+
constructor(language?: MessageLang);
|
|
9
|
+
/**
|
|
10
|
+
* @name use
|
|
11
|
+
* @param string string
|
|
12
|
+
* @param value string
|
|
13
|
+
*/
|
|
14
|
+
set(key: MessageKey, value: MessageValue): void;
|
|
15
|
+
/**
|
|
16
|
+
* @name get
|
|
17
|
+
* @param key string
|
|
18
|
+
*/
|
|
19
|
+
get(key?: MessageKey, lang?: MessageLang): MessageValue;
|
|
20
|
+
errorMessage(errorSlug?: MessageErrorContextSlug, errorOpt?: MessageErrorContextOpt, lang?: MessageLang): string;
|
|
21
|
+
/**
|
|
22
|
+
* @name error
|
|
23
|
+
* @param errorStr string
|
|
24
|
+
* @param errorOpts object[]
|
|
25
|
+
*/
|
|
26
|
+
error(errorStr?: MessageErrorContext, errorOpts?: MessageErrorContextOpt[], lang?: MessageLang): {
|
|
27
|
+
protocol: string;
|
|
28
|
+
context: string[];
|
|
29
|
+
params: object[];
|
|
30
|
+
message: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* @name apply
|
|
34
|
+
*/
|
|
35
|
+
apply(): MessageLogicLang;
|
|
36
|
+
/**
|
|
37
|
+
* @name use
|
|
38
|
+
* @param input MessageBuilder | MessageLogic
|
|
39
|
+
*/
|
|
40
|
+
use(input: MessageBuilder | MessageLogic): void;
|
|
41
|
+
}
|
package/lib/message.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
const format_key_1 = __importDefault(require("../helper/format-key"));
|
|
7
|
+
const message_1 = require("../var/message");
|
|
8
|
+
/**
|
|
9
|
+
* @name MessageBuilder
|
|
10
|
+
*/
|
|
11
|
+
class MessageBuilder {
|
|
12
|
+
constructor(language = "en") {
|
|
13
|
+
this.message_build_lang = "";
|
|
14
|
+
this.message_logic = {};
|
|
15
|
+
this.message_build_lang = String(language || "en")
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.replace(/[^a-z]/g, "")
|
|
18
|
+
.slice(0, 2);
|
|
19
|
+
this.message_logic = {};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @name use
|
|
23
|
+
* @param string string
|
|
24
|
+
* @param value string
|
|
25
|
+
*/
|
|
26
|
+
set(key, value) {
|
|
27
|
+
const keyStr = (0, format_key_1.default)(key);
|
|
28
|
+
if (typeof value !== "string") {
|
|
29
|
+
throw new Error("Message value is only type string!");
|
|
30
|
+
}
|
|
31
|
+
if (typeof key !== "string") {
|
|
32
|
+
throw new Error("Message key is only type string!");
|
|
33
|
+
}
|
|
34
|
+
if (!this.message_logic[this.message_build_lang]) {
|
|
35
|
+
this.message_logic[this.message_build_lang] = {};
|
|
36
|
+
}
|
|
37
|
+
this.message_logic[this.message_build_lang][keyStr] = String(value).trim();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @name get
|
|
41
|
+
* @param key string
|
|
42
|
+
*/
|
|
43
|
+
get(key = "", lang = this.message_build_lang) {
|
|
44
|
+
const keyStr = (0, format_key_1.default)(key);
|
|
45
|
+
let langSupport = lang;
|
|
46
|
+
// Not Language On Logic
|
|
47
|
+
if (!this.message_logic[langSupport]) {
|
|
48
|
+
console.warn("[Debugging Message]: Language not found on logic, using default language!");
|
|
49
|
+
if (this.message_logic[message_1.DefaultLanguage]) {
|
|
50
|
+
langSupport = message_1.DefaultLanguage;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
langSupport = Object.keys(this.message_logic || {})[0];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!this.message_logic[langSupport]) {
|
|
57
|
+
console.warn("[Debugging Message]: Language or variable not found on logic!");
|
|
58
|
+
return String(`[!NoneVariable ${keyStr}]`);
|
|
59
|
+
}
|
|
60
|
+
// Return
|
|
61
|
+
return String(this.message_logic[langSupport][keyStr] || "").trim();
|
|
62
|
+
}
|
|
63
|
+
errorMessage(errorSlug = "", errorOpt = {}, lang = this.message_build_lang) {
|
|
64
|
+
let messageContext = this.get(errorSlug, lang);
|
|
65
|
+
Object.entries(errorOpt || {}).forEach(([keys, values]) => {
|
|
66
|
+
const pattern = new RegExp(`{{${keys}}}`, "g");
|
|
67
|
+
messageContext = messageContext.replace(pattern, values);
|
|
68
|
+
});
|
|
69
|
+
return messageContext;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @name error
|
|
73
|
+
* @param errorStr string
|
|
74
|
+
* @param errorOpts object[]
|
|
75
|
+
*/
|
|
76
|
+
error(errorStr = "", errorOpts = [], lang = this.message_build_lang) {
|
|
77
|
+
const [protocol, ...messages] = String(errorStr || "").split(":");
|
|
78
|
+
const messageContext = String(messages.join(":")).split("|");
|
|
79
|
+
const messageArrayCtx = messageContext
|
|
80
|
+
.map((errorSlug, key) => {
|
|
81
|
+
const getOpt = errorOpts[key];
|
|
82
|
+
return this.errorMessage(errorSlug, getOpt, lang);
|
|
83
|
+
})
|
|
84
|
+
.join(", ");
|
|
85
|
+
return {
|
|
86
|
+
protocol: (0, format_key_1.default)(protocol),
|
|
87
|
+
context: messageContext,
|
|
88
|
+
params: errorOpts,
|
|
89
|
+
message: messageArrayCtx,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @name apply
|
|
94
|
+
*/
|
|
95
|
+
apply() {
|
|
96
|
+
return this.message_logic;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @name use
|
|
100
|
+
* @param input MessageBuilder | MessageLogic
|
|
101
|
+
*/
|
|
102
|
+
use(input) {
|
|
103
|
+
if (input instanceof MessageBuilder) {
|
|
104
|
+
const otherLogic = input.apply();
|
|
105
|
+
Object.entries(otherLogic).forEach(([lang, messages]) => {
|
|
106
|
+
if (!this.message_logic[lang]) {
|
|
107
|
+
this.message_logic[lang] = {};
|
|
108
|
+
}
|
|
109
|
+
Object.assign(this.message_logic[lang], messages);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else if (typeof input === "object" && input !== null) {
|
|
113
|
+
Object.entries(input).forEach(([key, value]) => {
|
|
114
|
+
this.set(key, value);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
throw new Error('The "use" input must be a MessageBuilder or mapping object!');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.default = MessageBuilder;
|
package/lib/policy.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PolicyNoActionAPIAction, PolicyNoActionServerAction, PolicyNoActionKey, PolicyNoActionBase } from "../types/policy.type";
|
|
2
|
+
/**
|
|
3
|
+
* @name PolicyBuilder
|
|
4
|
+
*/
|
|
5
|
+
export default class PolicyBuilder {
|
|
6
|
+
private passkey;
|
|
7
|
+
private version_now;
|
|
8
|
+
private version_min;
|
|
9
|
+
private version_forceupdate;
|
|
10
|
+
private noaction_api;
|
|
11
|
+
private noaction_server;
|
|
12
|
+
constructor({ passkey, version_now, version_min, version_forceupdate, }: {
|
|
13
|
+
passkey: string;
|
|
14
|
+
version_now: string;
|
|
15
|
+
version_min: string;
|
|
16
|
+
version_forceupdate?: boolean;
|
|
17
|
+
});
|
|
18
|
+
noaction(type?: PolicyNoActionKey, action?: PolicyNoActionBase): void;
|
|
19
|
+
private compareVersions;
|
|
20
|
+
version_info(version?: string): {
|
|
21
|
+
info_upgrade: boolean;
|
|
22
|
+
is_version_min: boolean;
|
|
23
|
+
is_version_now: boolean;
|
|
24
|
+
};
|
|
25
|
+
apply(): {
|
|
26
|
+
passkey: string;
|
|
27
|
+
version_now: string;
|
|
28
|
+
version_min: string;
|
|
29
|
+
version_forceupdate: boolean;
|
|
30
|
+
noaction_api: PolicyNoActionAPIAction;
|
|
31
|
+
noaction_server: PolicyNoActionServerAction;
|
|
32
|
+
};
|
|
33
|
+
}
|