@yangsaiyam/helper 1.11.3 → 1.11.5
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/addressess/index.d.ts +137 -0
- package/dist/addressess/index.js +24 -0
- package/dist/addressess/index.test.d.ts +1 -0
- package/dist/addressess/index.test.js +16 -0
- package/dist/assessment-question/index.d.ts +19 -0
- package/dist/assessment-question/index.js +22 -0
- package/dist/assessment-question/index.test.d.ts +1 -0
- package/dist/assessment-question/index.test.js +27 -0
- package/dist/career/index.d.ts +132 -0
- package/dist/career/index.js +40 -0
- package/dist/contact-form/index.d.ts +13 -0
- package/dist/contact-form/index.js +13 -0
- package/dist/email-template/index.d.ts +13 -0
- package/dist/email-template/index.js +5 -0
- package/dist/files/index.d.ts +18 -0
- package/dist/files/index.js +8 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/job-applications/index.d.ts +33 -0
- package/dist/job-applications/index.js +10 -0
- package/dist/locale/index.d.ts +9 -0
- package/dist/locale/index.js +4 -0
- package/dist/roles/index.d.ts +61 -0
- package/dist/roles/index.js +170 -0
- package/dist/roles/index.test.d.ts +1 -0
- package/dist/roles/index.test.js +80 -0
- package/dist/routes/index.d.ts +80 -0
- package/dist/routes/index.js +106 -0
- package/dist/routes/index.test.d.ts +1 -0
- package/dist/routes/index.test.js +92 -0
- package/dist/shared/helpers.d.ts +5 -0
- package/dist/shared/helpers.js +15 -0
- package/dist/shared/types.d.ts +5 -0
- package/dist/shared/types.js +1 -0
- package/dist/src/addressess/index.d.ts +137 -0
- package/dist/src/addressess/index.js +24 -0
- package/dist/src/addressess/index.test.d.ts +1 -0
- package/dist/src/addressess/index.test.js +16 -0
- package/dist/src/assessment-question/index.d.ts +19 -0
- package/dist/src/assessment-question/index.js +22 -0
- package/dist/src/assessment-question/index.test.d.ts +1 -0
- package/dist/src/assessment-question/index.test.js +27 -0
- package/dist/src/career/index.d.ts +132 -0
- package/dist/src/career/index.js +40 -0
- package/dist/src/contact-form/index.d.ts +13 -0
- package/dist/src/contact-form/index.js +13 -0
- package/dist/src/email-template/index.d.ts +13 -0
- package/dist/src/email-template/index.js +5 -0
- package/dist/src/files/index.d.ts +18 -0
- package/dist/src/files/index.js +8 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +16 -0
- package/dist/src/job-applications/index.d.ts +33 -0
- package/dist/src/job-applications/index.js +10 -0
- package/dist/src/locale/index.d.ts +9 -0
- package/dist/src/locale/index.js +4 -0
- package/dist/src/roles/index.d.ts +61 -0
- package/dist/src/roles/index.js +170 -0
- package/dist/src/roles/index.test.d.ts +1 -0
- package/dist/src/roles/index.test.js +80 -0
- package/dist/src/routes/index.d.ts +80 -0
- package/dist/src/routes/index.js +106 -0
- package/dist/src/routes/index.test.d.ts +1 -0
- package/dist/src/routes/index.test.js +92 -0
- package/dist/src/shared/helpers.d.ts +5 -0
- package/dist/src/shared/helpers.js +15 -0
- package/dist/src/shared/types.d.ts +5 -0
- package/dist/src/shared/types.js +1 -0
- package/dist/src/time/index.d.ts +73 -0
- package/dist/src/time/index.js +161 -0
- package/dist/src/time/index.test.d.ts +1 -0
- package/dist/src/time/index.test.js +35 -0
- package/dist/src/track/index.d.ts +17 -0
- package/dist/src/track/index.js +14 -0
- package/dist/src/track/index.test.d.ts +1 -0
- package/dist/src/track/index.test.js +7 -0
- package/dist/src/url-prefix/index.d.ts +8 -0
- package/dist/src/url-prefix/index.js +9 -0
- package/dist/src/website/index.d.ts +16 -0
- package/dist/src/website/index.js +17 -0
- package/dist/time/index.d.ts +73 -0
- package/dist/time/index.js +161 -0
- package/dist/time/index.test.d.ts +1 -0
- package/dist/time/index.test.js +35 -0
- package/dist/track/index.d.ts +17 -0
- package/dist/track/index.js +14 -0
- package/dist/track/index.test.d.ts +1 -0
- package/dist/track/index.test.js +7 -0
- package/dist/url-prefix/index.d.ts +8 -0
- package/dist/url-prefix/index.js +9 -0
- package/dist/website/index.d.ts +16 -0
- package/dist/website/index.js +17 -0
- package/package.json +2 -1
- package/src/addressess/index.test.ts +1 -1
- package/src/addressess/index.ts +1 -1
- package/src/assessment-question/index.test.ts +2 -2
- package/src/assessment-question/index.ts +1 -1
- package/src/career/index.ts +2 -2
- package/src/contact-form/index.ts +1 -1
- package/src/files/index.ts +1 -1
- package/src/index.ts +16 -17
- package/src/locale/index.ts +1 -1
- package/src/roles/index.test.ts +1 -1
- package/src/roles/index.ts +1 -1
- package/src/routes/index.test.ts +1 -1
- package/src/shared/helpers.ts +1 -1
- package/src/time/index.test.ts +1 -1
- package/src/time/index.ts +1 -1
- package/src/track/index.test.ts +1 -1
- package/src/track/index.ts +1 -1
- package/src/url-prefix/index.ts +1 -1
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
export const ROLE_MAP = {
|
|
2
|
+
0: "super-admin",
|
|
3
|
+
1: "admin",
|
|
4
|
+
2: "staff",
|
|
5
|
+
3: "hr",
|
|
6
|
+
32766: "visitor",
|
|
7
|
+
32767: "none",
|
|
8
|
+
};
|
|
9
|
+
export const ROLE = [
|
|
10
|
+
{ key: 0, value: "super-admin", i18nKey: "users.roleSuperAdmin" },
|
|
11
|
+
{ key: 1, value: "admin", i18nKey: "users.roleAdmin" },
|
|
12
|
+
{ key: 2, value: "staff", i18nKey: "users.roleStaff" },
|
|
13
|
+
{ key: 3, value: "hr", i18nKey: "users.roleHr" },
|
|
14
|
+
{ key: 32766, value: "visitor", i18nKey: "users.roleVisitor" },
|
|
15
|
+
{ key: 32767, value: "none", i18nKey: "users.roleNone" },
|
|
16
|
+
];
|
|
17
|
+
export function normalizeRole(role) {
|
|
18
|
+
if (typeof role === "number") {
|
|
19
|
+
return ROLE_MAP[role];
|
|
20
|
+
}
|
|
21
|
+
switch (role) {
|
|
22
|
+
case "super-admin":
|
|
23
|
+
return "super-admin";
|
|
24
|
+
case "admin":
|
|
25
|
+
return "admin";
|
|
26
|
+
case "staff":
|
|
27
|
+
return "staff";
|
|
28
|
+
case "hr":
|
|
29
|
+
return "hr";
|
|
30
|
+
case "visitor":
|
|
31
|
+
return "visitor";
|
|
32
|
+
case "none":
|
|
33
|
+
return "none";
|
|
34
|
+
default:
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function roleLabelKey(role) {
|
|
39
|
+
switch (normalizeRole(role)) {
|
|
40
|
+
case "super-admin":
|
|
41
|
+
return "users.roleSuperAdmin";
|
|
42
|
+
case "admin":
|
|
43
|
+
return "users.roleAdmin";
|
|
44
|
+
case "hr":
|
|
45
|
+
return "users.roleHr";
|
|
46
|
+
case "staff":
|
|
47
|
+
return "users.roleStaff";
|
|
48
|
+
case "visitor":
|
|
49
|
+
return "users.roleVisitor";
|
|
50
|
+
case "none":
|
|
51
|
+
return "users.roleNone";
|
|
52
|
+
default:
|
|
53
|
+
return "users.roleStaff";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function roleToId(role) {
|
|
57
|
+
const normalizedRole = normalizeRole(role);
|
|
58
|
+
return ROLE.find((option) => option.value === normalizedRole)?.key;
|
|
59
|
+
}
|
|
60
|
+
export const Permission = {
|
|
61
|
+
ALLOWED_EMAILS_READ: "allowed-emails:read",
|
|
62
|
+
ALLOWED_EMAILS_WRITE: "allowed-emails:write",
|
|
63
|
+
USERS_READ: "users:read",
|
|
64
|
+
USERS_WRITE: "users:write",
|
|
65
|
+
USERS_DELETE: "users:delete",
|
|
66
|
+
CAREERS_READ: "careers:read",
|
|
67
|
+
CAREERS_WRITE: "careers:write",
|
|
68
|
+
CAREERS_DELETE: "careers:delete",
|
|
69
|
+
CONTACT_FORM_READ: "contact-form:read",
|
|
70
|
+
CONTACT_FORM_DELETE: "contact-form:delete",
|
|
71
|
+
JOB_APPLICATIONS_READ: "job-applications:read",
|
|
72
|
+
OTP_ATTEMPTS_READ: "otp-attempts:read",
|
|
73
|
+
OTP_ATTEMPTS_WRITE: "otp-attempts:write",
|
|
74
|
+
TWO_FACTOR_AUTH_READ: "two-factor-auth:read",
|
|
75
|
+
TWO_FACTOR_AUTH_WRITE: "two-factor-auth:write",
|
|
76
|
+
VIEWS_READ: "views:read",
|
|
77
|
+
LOGS_READ: "logs:read",
|
|
78
|
+
UTM_RECORDS_READ: "utm-records:read",
|
|
79
|
+
UTM_RECORDS_WRITE: "utm-records:write",
|
|
80
|
+
ORIGIN_READ: "origin:read",
|
|
81
|
+
ORIGIN_WRITE: "origin:write",
|
|
82
|
+
ORIGIN_DELETE: "origin:delete",
|
|
83
|
+
};
|
|
84
|
+
export const PERMISSION_I18N_KEY = {
|
|
85
|
+
[Permission.ALLOWED_EMAILS_READ]: "permissions.allowedEmailsRead",
|
|
86
|
+
[Permission.ALLOWED_EMAILS_WRITE]: "permissions.allowedEmailsWrite",
|
|
87
|
+
[Permission.USERS_READ]: "permissions.usersRead",
|
|
88
|
+
[Permission.USERS_WRITE]: "permissions.usersWrite",
|
|
89
|
+
[Permission.USERS_DELETE]: "permissions.usersDelete",
|
|
90
|
+
[Permission.CAREERS_READ]: "permissions.careersRead",
|
|
91
|
+
[Permission.CAREERS_WRITE]: "permissions.careersWrite",
|
|
92
|
+
[Permission.CAREERS_DELETE]: "permissions.careersDelete",
|
|
93
|
+
[Permission.CONTACT_FORM_READ]: "permissions.contactFormRead",
|
|
94
|
+
[Permission.CONTACT_FORM_DELETE]: "permissions.contactFormDelete",
|
|
95
|
+
[Permission.JOB_APPLICATIONS_READ]: "permissions.jobApplicationsRead",
|
|
96
|
+
[Permission.OTP_ATTEMPTS_READ]: "permissions.otpAttemptsRead",
|
|
97
|
+
[Permission.OTP_ATTEMPTS_WRITE]: "permissions.otpAttemptsWrite",
|
|
98
|
+
[Permission.TWO_FACTOR_AUTH_READ]: "permissions.twoFactorAuthRead",
|
|
99
|
+
[Permission.TWO_FACTOR_AUTH_WRITE]: "permissions.twoFactorAuthWrite",
|
|
100
|
+
[Permission.VIEWS_READ]: "permissions.viewsRead",
|
|
101
|
+
[Permission.LOGS_READ]: "permissions.logsRead",
|
|
102
|
+
[Permission.UTM_RECORDS_READ]: "permissions.utmRecordsRead",
|
|
103
|
+
[Permission.UTM_RECORDS_WRITE]: "permissions.utmRecordsWrite",
|
|
104
|
+
[Permission.ORIGIN_READ]: "permissions.originRead",
|
|
105
|
+
[Permission.ORIGIN_WRITE]: "permissions.originWrite",
|
|
106
|
+
[Permission.ORIGIN_DELETE]: "permissions.originDelete",
|
|
107
|
+
};
|
|
108
|
+
export const ROLE_PERMISSIONS = {
|
|
109
|
+
"super-admin": [
|
|
110
|
+
Permission.ALLOWED_EMAILS_READ,
|
|
111
|
+
Permission.ALLOWED_EMAILS_WRITE,
|
|
112
|
+
Permission.USERS_READ,
|
|
113
|
+
Permission.USERS_WRITE,
|
|
114
|
+
Permission.USERS_DELETE,
|
|
115
|
+
Permission.CAREERS_READ,
|
|
116
|
+
Permission.CAREERS_WRITE,
|
|
117
|
+
Permission.CAREERS_DELETE,
|
|
118
|
+
Permission.CONTACT_FORM_READ,
|
|
119
|
+
Permission.CONTACT_FORM_DELETE,
|
|
120
|
+
Permission.JOB_APPLICATIONS_READ,
|
|
121
|
+
Permission.OTP_ATTEMPTS_READ,
|
|
122
|
+
Permission.OTP_ATTEMPTS_WRITE,
|
|
123
|
+
Permission.TWO_FACTOR_AUTH_READ,
|
|
124
|
+
Permission.TWO_FACTOR_AUTH_WRITE,
|
|
125
|
+
Permission.VIEWS_READ,
|
|
126
|
+
Permission.LOGS_READ,
|
|
127
|
+
Permission.UTM_RECORDS_READ,
|
|
128
|
+
Permission.UTM_RECORDS_WRITE,
|
|
129
|
+
Permission.ORIGIN_READ,
|
|
130
|
+
Permission.ORIGIN_WRITE,
|
|
131
|
+
Permission.ORIGIN_DELETE,
|
|
132
|
+
],
|
|
133
|
+
admin: [
|
|
134
|
+
Permission.ALLOWED_EMAILS_READ,
|
|
135
|
+
Permission.ALLOWED_EMAILS_WRITE,
|
|
136
|
+
Permission.USERS_READ,
|
|
137
|
+
Permission.CAREERS_READ,
|
|
138
|
+
Permission.CAREERS_WRITE,
|
|
139
|
+
Permission.CAREERS_DELETE,
|
|
140
|
+
Permission.CONTACT_FORM_READ,
|
|
141
|
+
Permission.CONTACT_FORM_DELETE,
|
|
142
|
+
Permission.JOB_APPLICATIONS_READ,
|
|
143
|
+
Permission.VIEWS_READ,
|
|
144
|
+
Permission.LOGS_READ,
|
|
145
|
+
Permission.UTM_RECORDS_READ,
|
|
146
|
+
Permission.UTM_RECORDS_WRITE,
|
|
147
|
+
Permission.ORIGIN_READ,
|
|
148
|
+
Permission.ORIGIN_WRITE,
|
|
149
|
+
Permission.ORIGIN_DELETE,
|
|
150
|
+
],
|
|
151
|
+
hr: [
|
|
152
|
+
Permission.CAREERS_READ,
|
|
153
|
+
Permission.CAREERS_WRITE,
|
|
154
|
+
Permission.CAREERS_DELETE,
|
|
155
|
+
Permission.JOB_APPLICATIONS_READ,
|
|
156
|
+
],
|
|
157
|
+
staff: [Permission.CONTACT_FORM_READ],
|
|
158
|
+
visitor: [],
|
|
159
|
+
none: [],
|
|
160
|
+
};
|
|
161
|
+
export function hasPermission(role, permission) {
|
|
162
|
+
const normalizedRole = normalizeRole(role);
|
|
163
|
+
if (!normalizedRole) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
return ROLE_PERMISSIONS[normalizedRole].includes(permission);
|
|
167
|
+
}
|
|
168
|
+
export function permissionLabelKey(permission) {
|
|
169
|
+
return PERMISSION_I18N_KEY[permission];
|
|
170
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { PERMISSION_I18N_KEY, Permission, ROLE, ROLE_PERMISSIONS, hasPermission, normalizeRole, permissionLabelKey, roleLabelKey, roleToId, } from "./index.js";
|
|
3
|
+
assert.equal(normalizeRole(0), "super-admin");
|
|
4
|
+
assert.equal(normalizeRole("admin"), "admin");
|
|
5
|
+
assert.equal(normalizeRole(2), "staff");
|
|
6
|
+
assert.equal(normalizeRole("hr"), "hr");
|
|
7
|
+
assert.equal(normalizeRole(32766), "visitor");
|
|
8
|
+
assert.equal(normalizeRole("none"), "none");
|
|
9
|
+
assert.equal(normalizeRole("unknown"), undefined);
|
|
10
|
+
assert.equal(normalizeRole(undefined), undefined);
|
|
11
|
+
assert.equal(roleToId("super-admin"), 0);
|
|
12
|
+
assert.equal(roleToId("admin"), 1);
|
|
13
|
+
assert.equal(roleToId("staff"), 2);
|
|
14
|
+
assert.equal(roleToId("hr"), 3);
|
|
15
|
+
assert.equal(roleToId("visitor"), 32766);
|
|
16
|
+
assert.equal(roleToId("none"), 32767);
|
|
17
|
+
assert.equal(roleToId("unknown"), undefined);
|
|
18
|
+
assert.equal(roleLabelKey(0), "users.roleSuperAdmin");
|
|
19
|
+
assert.equal(roleLabelKey("admin"), "users.roleAdmin");
|
|
20
|
+
assert.equal(roleLabelKey(2), "users.roleStaff");
|
|
21
|
+
assert.equal(roleLabelKey("hr"), "users.roleHr");
|
|
22
|
+
assert.equal(roleLabelKey("visitor"), "users.roleVisitor");
|
|
23
|
+
assert.equal(roleLabelKey("none"), "users.roleNone");
|
|
24
|
+
assert.equal(roleLabelKey(undefined), "users.roleStaff");
|
|
25
|
+
assert.deepEqual(ROLE[0], {
|
|
26
|
+
key: 0,
|
|
27
|
+
value: "super-admin",
|
|
28
|
+
i18nKey: "users.roleSuperAdmin",
|
|
29
|
+
});
|
|
30
|
+
assert.equal(ROLE[3]?.i18nKey, "users.roleHr");
|
|
31
|
+
assert.deepEqual(ROLE[4], {
|
|
32
|
+
key: 32766,
|
|
33
|
+
value: "visitor",
|
|
34
|
+
i18nKey: "users.roleVisitor",
|
|
35
|
+
});
|
|
36
|
+
assert.deepEqual(ROLE[5], {
|
|
37
|
+
key: 32767,
|
|
38
|
+
value: "none",
|
|
39
|
+
i18nKey: "users.roleNone",
|
|
40
|
+
});
|
|
41
|
+
assert.equal(PERMISSION_I18N_KEY[Permission.ALLOWED_EMAILS_READ], "permissions.allowedEmailsRead");
|
|
42
|
+
assert.equal(permissionLabelKey(Permission.CONTACT_FORM_DELETE), "permissions.contactFormDelete");
|
|
43
|
+
assert.equal(permissionLabelKey(Permission.OTP_ATTEMPTS_READ), "permissions.otpAttemptsRead");
|
|
44
|
+
assert.equal(permissionLabelKey(Permission.TWO_FACTOR_AUTH_WRITE), "permissions.twoFactorAuthWrite");
|
|
45
|
+
assert.equal(permissionLabelKey(Permission.VIEWS_READ), "permissions.viewsRead");
|
|
46
|
+
assert.equal(permissionLabelKey(Permission.LOGS_READ), "permissions.logsRead");
|
|
47
|
+
assert.equal(permissionLabelKey(Permission.UTM_RECORDS_READ), "permissions.utmRecordsRead");
|
|
48
|
+
assert.equal(permissionLabelKey(Permission.UTM_RECORDS_WRITE), "permissions.utmRecordsWrite");
|
|
49
|
+
assert.equal(permissionLabelKey(Permission.ORIGIN_READ), "permissions.originRead");
|
|
50
|
+
assert.equal(permissionLabelKey(Permission.ORIGIN_WRITE), "permissions.originWrite");
|
|
51
|
+
assert.equal(permissionLabelKey(Permission.ORIGIN_DELETE), "permissions.originDelete");
|
|
52
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.USERS_DELETE));
|
|
53
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.OTP_ATTEMPTS_READ));
|
|
54
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.TWO_FACTOR_AUTH_WRITE));
|
|
55
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.VIEWS_READ));
|
|
56
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.LOGS_READ));
|
|
57
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.UTM_RECORDS_WRITE));
|
|
58
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.ORIGIN_DELETE));
|
|
59
|
+
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.CONTACT_FORM_DELETE));
|
|
60
|
+
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.VIEWS_READ));
|
|
61
|
+
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.LOGS_READ));
|
|
62
|
+
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.UTM_RECORDS_WRITE));
|
|
63
|
+
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.ORIGIN_DELETE));
|
|
64
|
+
assert.equal(ROLE_PERMISSIONS.admin.includes(Permission.USERS_WRITE), false);
|
|
65
|
+
assert.ok(ROLE_PERMISSIONS.hr.includes(Permission.JOB_APPLICATIONS_READ));
|
|
66
|
+
assert.deepEqual(ROLE_PERMISSIONS.staff, [Permission.CONTACT_FORM_READ]);
|
|
67
|
+
assert.deepEqual(ROLE_PERMISSIONS.visitor, []);
|
|
68
|
+
assert.deepEqual(ROLE_PERMISSIONS.none, []);
|
|
69
|
+
assert.equal(hasPermission("super-admin", Permission.USERS_WRITE), true);
|
|
70
|
+
assert.equal(hasPermission("super-admin", Permission.OTP_ATTEMPTS_WRITE), true);
|
|
71
|
+
assert.equal(hasPermission(1, Permission.USERS_WRITE), false);
|
|
72
|
+
assert.equal(hasPermission("hr", Permission.CAREERS_WRITE), true);
|
|
73
|
+
assert.equal(hasPermission("staff", Permission.CAREERS_WRITE), false);
|
|
74
|
+
assert.equal(hasPermission("staff", Permission.CONTACT_FORM_READ), true);
|
|
75
|
+
assert.equal(hasPermission("admin", Permission.VIEWS_READ), true);
|
|
76
|
+
assert.equal(hasPermission("admin", Permission.UTM_RECORDS_WRITE), true);
|
|
77
|
+
assert.equal(hasPermission("admin", Permission.ORIGIN_DELETE), true);
|
|
78
|
+
assert.equal(hasPermission("visitor", Permission.VIEWS_READ), false);
|
|
79
|
+
assert.equal(hasPermission(32767, Permission.LOGS_READ), false);
|
|
80
|
+
assert.equal(hasPermission(undefined, Permission.CAREERS_READ), false);
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export type RouteKey = "home" | "about" | "career" | "careerDetail" | "careerApply" | "careerAssessment" | "contact" | "services" | "servicesAdvertisingPlatform" | "servicesMcnBusiness" | "servicesSoftwareDevelopment";
|
|
2
|
+
export type RouteMapping = {
|
|
3
|
+
key: RouteKey;
|
|
4
|
+
path: string;
|
|
5
|
+
path_name: string;
|
|
6
|
+
path_key: string;
|
|
7
|
+
i18nKey: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const ROUTES: readonly [{
|
|
10
|
+
readonly key: "home";
|
|
11
|
+
readonly path: "/[locale]";
|
|
12
|
+
readonly path_name: "/";
|
|
13
|
+
readonly path_key: "/";
|
|
14
|
+
readonly i18nKey: "routes.home";
|
|
15
|
+
}, {
|
|
16
|
+
readonly key: "about";
|
|
17
|
+
readonly path: "/[locale]/about";
|
|
18
|
+
readonly path_name: "/about";
|
|
19
|
+
readonly path_key: "/about";
|
|
20
|
+
readonly i18nKey: "routes.about";
|
|
21
|
+
}, {
|
|
22
|
+
readonly key: "career";
|
|
23
|
+
readonly path: "/[locale]/career";
|
|
24
|
+
readonly path_name: "/career";
|
|
25
|
+
readonly path_key: "/career";
|
|
26
|
+
readonly i18nKey: "routes.career";
|
|
27
|
+
}, {
|
|
28
|
+
readonly key: "careerDetail";
|
|
29
|
+
readonly path: "/[locale]/career/[id]";
|
|
30
|
+
readonly path_name: "/career/[id]";
|
|
31
|
+
readonly path_key: "/career/[id]";
|
|
32
|
+
readonly i18nKey: "routes.careerDetail";
|
|
33
|
+
}, {
|
|
34
|
+
readonly key: "careerApply";
|
|
35
|
+
readonly path: "/[locale]/career/[id]/apply";
|
|
36
|
+
readonly path_name: "/career/[id]/apply";
|
|
37
|
+
readonly path_key: "/career/[id]/apply";
|
|
38
|
+
readonly i18nKey: "routes.careerApply";
|
|
39
|
+
}, {
|
|
40
|
+
readonly key: "careerAssessment";
|
|
41
|
+
readonly path: "/[locale]/career/[id]/apply/assessment";
|
|
42
|
+
readonly path_name: "/career/[id]/apply/assessment";
|
|
43
|
+
readonly path_key: "/career/[id]/apply/assessment";
|
|
44
|
+
readonly i18nKey: "routes.careerAssessment";
|
|
45
|
+
}, {
|
|
46
|
+
readonly key: "contact";
|
|
47
|
+
readonly path: "/[locale]/contact";
|
|
48
|
+
readonly path_name: "/contact";
|
|
49
|
+
readonly path_key: "/contact";
|
|
50
|
+
readonly i18nKey: "routes.contact";
|
|
51
|
+
}, {
|
|
52
|
+
readonly key: "services";
|
|
53
|
+
readonly path: "/[locale]/services";
|
|
54
|
+
readonly path_name: "/services";
|
|
55
|
+
readonly path_key: "/services";
|
|
56
|
+
readonly i18nKey: "routes.services";
|
|
57
|
+
}, {
|
|
58
|
+
readonly key: "servicesAdvertisingPlatform";
|
|
59
|
+
readonly path: "/[locale]/services/advertising-platform";
|
|
60
|
+
readonly path_name: "/services/advertising-platform";
|
|
61
|
+
readonly path_key: "/services/advertising-platform";
|
|
62
|
+
readonly i18nKey: "routes.servicesAdvertisingPlatform";
|
|
63
|
+
}, {
|
|
64
|
+
readonly key: "servicesMcnBusiness";
|
|
65
|
+
readonly path: "/[locale]/services/mcn-business";
|
|
66
|
+
readonly path_name: "/services/mcn-business";
|
|
67
|
+
readonly path_key: "/services/mcn-business";
|
|
68
|
+
readonly i18nKey: "routes.servicesMcnBusiness";
|
|
69
|
+
}, {
|
|
70
|
+
readonly key: "servicesSoftwareDevelopment";
|
|
71
|
+
readonly path: "/[locale]/services/software-development";
|
|
72
|
+
readonly path_name: "/services/software-development";
|
|
73
|
+
readonly path_key: "/services/software-development";
|
|
74
|
+
readonly i18nKey: "routes.servicesSoftwareDevelopment";
|
|
75
|
+
}];
|
|
76
|
+
export declare const ROUTE: Record<RouteKey, string>;
|
|
77
|
+
export declare function routeLabelKey(route: RouteKey): string;
|
|
78
|
+
export declare function routePathKey(route: RouteKey): string;
|
|
79
|
+
export declare function buildRoutePathName(route: RouteKey, params?: Partial<Record<"id", string | number>>): string;
|
|
80
|
+
export declare function buildLocalizedRoute(route: RouteKey, locale: string, params?: Partial<Record<"id", string | number>>): string;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export const ROUTES = [
|
|
2
|
+
{
|
|
3
|
+
key: "home",
|
|
4
|
+
path: "/[locale]",
|
|
5
|
+
path_name: "/",
|
|
6
|
+
path_key: "/",
|
|
7
|
+
i18nKey: "routes.home",
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
key: "about",
|
|
11
|
+
path: "/[locale]/about",
|
|
12
|
+
path_name: "/about",
|
|
13
|
+
path_key: "/about",
|
|
14
|
+
i18nKey: "routes.about",
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
key: "career",
|
|
18
|
+
path: "/[locale]/career",
|
|
19
|
+
path_name: "/career",
|
|
20
|
+
path_key: "/career",
|
|
21
|
+
i18nKey: "routes.career",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
key: "careerDetail",
|
|
25
|
+
path: "/[locale]/career/[id]",
|
|
26
|
+
path_name: "/career/[id]",
|
|
27
|
+
path_key: "/career/[id]",
|
|
28
|
+
i18nKey: "routes.careerDetail",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
key: "careerApply",
|
|
32
|
+
path: "/[locale]/career/[id]/apply",
|
|
33
|
+
path_name: "/career/[id]/apply",
|
|
34
|
+
path_key: "/career/[id]/apply",
|
|
35
|
+
i18nKey: "routes.careerApply",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
key: "careerAssessment",
|
|
39
|
+
path: "/[locale]/career/[id]/apply/assessment",
|
|
40
|
+
path_name: "/career/[id]/apply/assessment",
|
|
41
|
+
path_key: "/career/[id]/apply/assessment",
|
|
42
|
+
i18nKey: "routes.careerAssessment",
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
key: "contact",
|
|
46
|
+
path: "/[locale]/contact",
|
|
47
|
+
path_name: "/contact",
|
|
48
|
+
path_key: "/contact",
|
|
49
|
+
i18nKey: "routes.contact",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
key: "services",
|
|
53
|
+
path: "/[locale]/services",
|
|
54
|
+
path_name: "/services",
|
|
55
|
+
path_key: "/services",
|
|
56
|
+
i18nKey: "routes.services",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
key: "servicesAdvertisingPlatform",
|
|
60
|
+
path: "/[locale]/services/advertising-platform",
|
|
61
|
+
path_name: "/services/advertising-platform",
|
|
62
|
+
path_key: "/services/advertising-platform",
|
|
63
|
+
i18nKey: "routes.servicesAdvertisingPlatform",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
key: "servicesMcnBusiness",
|
|
67
|
+
path: "/[locale]/services/mcn-business",
|
|
68
|
+
path_name: "/services/mcn-business",
|
|
69
|
+
path_key: "/services/mcn-business",
|
|
70
|
+
i18nKey: "routes.servicesMcnBusiness",
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
key: "servicesSoftwareDevelopment",
|
|
74
|
+
path: "/[locale]/services/software-development",
|
|
75
|
+
path_name: "/services/software-development",
|
|
76
|
+
path_key: "/services/software-development",
|
|
77
|
+
i18nKey: "routes.servicesSoftwareDevelopment",
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
export const ROUTE = Object.fromEntries(ROUTES.map((route) => [route.key, route.path]));
|
|
81
|
+
export function routeLabelKey(route) {
|
|
82
|
+
return ROUTES.find((item) => item.key === route)?.i18nKey ?? "routes.home";
|
|
83
|
+
}
|
|
84
|
+
export function routePathKey(route) {
|
|
85
|
+
return ROUTES.find((item) => item.key === route)?.path_key ?? "/";
|
|
86
|
+
}
|
|
87
|
+
export function buildRoutePathName(route, params = {}) {
|
|
88
|
+
let pathName = ROUTES.find((item) => item.key === route)?.path_name ?? "/";
|
|
89
|
+
if (pathName.includes("[id]")) {
|
|
90
|
+
if (params.id === undefined || params.id === null) {
|
|
91
|
+
throw new Error(`Route "${route}" requires an id parameter.`);
|
|
92
|
+
}
|
|
93
|
+
pathName = pathName.replace("[id]", encodeURIComponent(String(params.id)));
|
|
94
|
+
}
|
|
95
|
+
return pathName;
|
|
96
|
+
}
|
|
97
|
+
export function buildLocalizedRoute(route, locale, params = {}) {
|
|
98
|
+
let path = ROUTE[route].replace("[locale]", encodeURIComponent(locale));
|
|
99
|
+
if (path.includes("[id]")) {
|
|
100
|
+
if (params.id === undefined || params.id === null) {
|
|
101
|
+
throw new Error(`Route "${route}" requires an id parameter.`);
|
|
102
|
+
}
|
|
103
|
+
path = path.replace("[id]", encodeURIComponent(String(params.id)));
|
|
104
|
+
}
|
|
105
|
+
return path;
|
|
106
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { ROUTE, ROUTES, buildRoutePathName, buildLocalizedRoute, routeLabelKey, routePathKey, } from "./index.js";
|
|
3
|
+
assert.deepEqual(ROUTES, [
|
|
4
|
+
{
|
|
5
|
+
key: "home",
|
|
6
|
+
path: "/[locale]",
|
|
7
|
+
path_name: "/",
|
|
8
|
+
path_key: "/",
|
|
9
|
+
i18nKey: "routes.home",
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
key: "about",
|
|
13
|
+
path: "/[locale]/about",
|
|
14
|
+
path_name: "/about",
|
|
15
|
+
path_key: "/about",
|
|
16
|
+
i18nKey: "routes.about",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
key: "career",
|
|
20
|
+
path: "/[locale]/career",
|
|
21
|
+
path_name: "/career",
|
|
22
|
+
path_key: "/career",
|
|
23
|
+
i18nKey: "routes.career",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
key: "careerDetail",
|
|
27
|
+
path: "/[locale]/career/[id]",
|
|
28
|
+
path_name: "/career/[id]",
|
|
29
|
+
path_key: "/career/[id]",
|
|
30
|
+
i18nKey: "routes.careerDetail",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
key: "careerApply",
|
|
34
|
+
path: "/[locale]/career/[id]/apply",
|
|
35
|
+
path_name: "/career/[id]/apply",
|
|
36
|
+
path_key: "/career/[id]/apply",
|
|
37
|
+
i18nKey: "routes.careerApply",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
key: "careerAssessment",
|
|
41
|
+
path: "/[locale]/career/[id]/apply/assessment",
|
|
42
|
+
path_name: "/career/[id]/apply/assessment",
|
|
43
|
+
path_key: "/career/[id]/apply/assessment",
|
|
44
|
+
i18nKey: "routes.careerAssessment",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
key: "contact",
|
|
48
|
+
path: "/[locale]/contact",
|
|
49
|
+
path_name: "/contact",
|
|
50
|
+
path_key: "/contact",
|
|
51
|
+
i18nKey: "routes.contact",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
key: "services",
|
|
55
|
+
path: "/[locale]/services",
|
|
56
|
+
path_name: "/services",
|
|
57
|
+
path_key: "/services",
|
|
58
|
+
i18nKey: "routes.services",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
key: "servicesAdvertisingPlatform",
|
|
62
|
+
path: "/[locale]/services/advertising-platform",
|
|
63
|
+
path_name: "/services/advertising-platform",
|
|
64
|
+
path_key: "/services/advertising-platform",
|
|
65
|
+
i18nKey: "routes.servicesAdvertisingPlatform",
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
key: "servicesMcnBusiness",
|
|
69
|
+
path: "/[locale]/services/mcn-business",
|
|
70
|
+
path_name: "/services/mcn-business",
|
|
71
|
+
path_key: "/services/mcn-business",
|
|
72
|
+
i18nKey: "routes.servicesMcnBusiness",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
key: "servicesSoftwareDevelopment",
|
|
76
|
+
path: "/[locale]/services/software-development",
|
|
77
|
+
path_name: "/services/software-development",
|
|
78
|
+
path_key: "/services/software-development",
|
|
79
|
+
i18nKey: "routes.servicesSoftwareDevelopment",
|
|
80
|
+
},
|
|
81
|
+
]);
|
|
82
|
+
assert.equal(ROUTE.home, "/[locale]");
|
|
83
|
+
assert.equal(ROUTE.careerAssessment, "/[locale]/career/[id]/apply/assessment");
|
|
84
|
+
assert.equal(buildLocalizedRoute("careerApply", "en", { id: "frontend-engineer" }), "/en/career/frontend-engineer/apply");
|
|
85
|
+
assert.equal(buildLocalizedRoute("servicesMcnBusiness", "zh"), "/zh/services/mcn-business");
|
|
86
|
+
assert.equal(buildRoutePathName("careerApply", { id: "frontend-engineer" }), "/career/frontend-engineer/apply");
|
|
87
|
+
assert.equal(routePathKey("home"), "/");
|
|
88
|
+
assert.equal(routePathKey("careerApply"), "/career/[id]/apply");
|
|
89
|
+
assert.throws(() => buildLocalizedRoute("careerDetail", "en"), {
|
|
90
|
+
message: 'Route "careerDetail" requires an id parameter.',
|
|
91
|
+
});
|
|
92
|
+
assert.equal(routeLabelKey("servicesSoftwareDevelopment"), "routes.servicesSoftwareDevelopment");
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { MappingOption } from "./types.js";
|
|
2
|
+
export declare function getOptionByKey<T extends MappingOption>(options: readonly T[], key?: number | null): T | undefined;
|
|
3
|
+
export declare function getLabelByKey<T extends MappingOption>(options: readonly T[], key?: number | null, fallback?: string): string;
|
|
4
|
+
export declare function getI18nKeyByKey<T extends MappingOption>(options: readonly T[], key?: number | null, fallback?: string): string;
|
|
5
|
+
export declare function toRecord<T extends MappingOption>(options: readonly T[]): Record<number, string>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function getOptionByKey(options, key) {
|
|
2
|
+
if (key === null || key === undefined) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
return options.find((option) => option.key === key);
|
|
6
|
+
}
|
|
7
|
+
export function getLabelByKey(options, key, fallback = "Unknown") {
|
|
8
|
+
return getOptionByKey(options, key)?.value ?? fallback;
|
|
9
|
+
}
|
|
10
|
+
export function getI18nKeyByKey(options, key, fallback = "unknown") {
|
|
11
|
+
return getOptionByKey(options, key)?.i18nKey ?? fallback;
|
|
12
|
+
}
|
|
13
|
+
export function toRecord(options) {
|
|
14
|
+
return Object.fromEntries(options.map((option) => [option.key, option.value]));
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|