auth-mn 0.2.0 → 1.0.0

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/index.d.ts CHANGED
@@ -1,374 +1,114 @@
1
- export declare const jsonToQueryString: (params: Record<string, any>) => string;
2
- export declare const ObjectId: () => string;
1
+ /**
2
+ * auth-mn — Auth.mn API client (Node.js)
3
+ *
4
+ * import authMn, { setAuth, setHost } from "auth-mn";
5
+ * setHost("prod"); // эсвэл setHost({ MAIN: "https://..." })
6
+ * setAuth({ client_id: "cl_...", client_secret: "sk_..." });
7
+ * const v = await authMn.user.verify(userTokenFromFrontend); // → v.data.user
8
+ *
9
+ * Token-ийг lib өөрөө авч, хугацаа дуусахаас өмнө refresh хийнэ.
10
+ * Client-д олгосон permission-ээс гадуур API дуудвал 403 + message буцна.
11
+ *
12
+ * Хамрах хүрээ: хэрэглэгч (verify / find), мэдэгдэл (push / sms / mail),
13
+ * socket (toast / status / orderStatus / emit). Merchant/admin зэрэг дотоод
14
+ * бүртгэлүүд энэ санд байхгүй.
15
+ */
16
+ import auth from "./auth";
17
+ import user from "./user";
18
+ import notification from "./notification";
19
+ import socket from "./socket";
20
+ export type Env = "prod" | "staging";
3
21
  export type Config = {
4
- token: string;
5
- env: "prod" | "staging";
22
+ env: Env;
6
23
  hosts: {
7
24
  MAIN: string;
8
25
  WALLET: string;
9
26
  };
10
27
  auth: {
11
- username: string;
12
- password: string;
28
+ client_id: string;
29
+ client_secret: string;
13
30
  };
14
31
  logger: boolean;
32
+ /** Хүсэлтийн timeout (ms), default 20000 */
33
+ timeout: number;
15
34
  };
16
35
  export declare const config: Config;
17
- export declare const setHost: ({ MAIN, WALLET }: {
36
+ /** Host тохируулна: setHost("staging") эсвэл setHost({ MAIN, WALLET }, "prod") */
37
+ export declare const setHost: (hosts: Env | {
18
38
  MAIN: string;
19
- WALLET: string;
20
- }, ENV: "staging" | "prod") => void;
39
+ WALLET?: string;
40
+ }, env?: Env) => void;
41
+ /** client_id + client_secret (Admin panel → Апп-ууд дээрээс авна) */
42
+ export declare const setAuth: (auth: {
43
+ client_id: string;
44
+ client_secret: string;
45
+ }) => void;
21
46
  export declare const setLogger: (status: boolean) => void;
47
+ export declare const jsonToQueryString: (params: Record<string, any>) => string;
48
+ export declare const ObjectId: () => string;
49
+ import { authState } from "./auth";
50
+ export { auth, user, notification, socket, authState };
51
+ export * from "./types";
22
52
  declare const _default: {
23
53
  auth: {
24
- TOKEN: (auth: {
54
+ getToken: () => Promise<string>;
55
+ login: () => Promise<import("./types").Result<import("./types").TokenData>>;
56
+ refresh: () => Promise<import("./types").Result<import("./types").TokenData>>;
57
+ revoke: () => Promise<import("./types").Result<null>>;
58
+ me: () => Promise<import("./types").Result<import("./types").ClientInfo>>;
59
+ permissions: () => Promise<import("./types").Result<import("./types").PermissionGroup[]>>;
60
+ hasPermission: (perm: string) => boolean;
61
+ TOKEN: (_legacy?: {
25
62
  username: string;
26
63
  password: string;
27
64
  }) => Promise<string>;
65
+ state: import("./auth").AuthState;
28
66
  };
29
- ObjectId: () => string;
30
- notification: {
31
- SinglePushNoti: (body: {
32
- user_id: number;
33
- target_app: "AVDAR-STORE" | "AUTH-APP" | "E-SIM" | "SELLER-PANDA";
34
- body: {
35
- title: string;
36
- body: string;
37
- icon: string;
38
- image?: string;
39
- button?: {
40
- title: string;
41
- url: string;
42
- } | null;
43
- };
44
- }) => Promise<{
45
- success: boolean;
46
- data: null;
47
- message: string;
48
- }>;
49
- SMS: (body: {
50
- text: string;
51
- to: string;
52
- from: string;
53
- }) => Promise<{
54
- success: boolean;
55
- data: null;
56
- message: string;
57
- }>;
58
- MAIL: (body: {
59
- type: "notification" | "otp" | "custom";
60
- form?: {
61
- name?: string;
62
- email?: string;
63
- };
64
- to: string[];
65
- subject: string;
66
- body: {
67
- name: string;
68
- title?: string;
69
- desc?: string;
70
- otp?: string | number;
71
- button?: {
72
- text: string;
73
- url: string | null;
74
- };
75
- footer_text?: string;
67
+ user: {
68
+ verify: (token: string) => Promise<import("./types").Result<import("./types").VerifiedUser>>;
69
+ find: (query: {
70
+ user_id?: number | string;
71
+ phone_number?: string;
72
+ email?: string;
73
+ }) => Promise<import("./types").Result<import("./types").UserT>>;
74
+ middleware: (opts?: import("./user").MiddlewareOptions) => (req: {
75
+ [k: string]: any;
76
+ headers: Record<string, any>;
77
+ query?: any;
78
+ body?: any;
79
+ }, res: {
80
+ status: (code: number) => {
81
+ json: (b: any) => any;
76
82
  };
77
- }) => Promise<{
78
- success: boolean;
79
- data: null;
80
- message: string;
81
- }>;
82
- _3RD: {
83
- TOKI: (body: {
84
- accountId: string;
85
- title: string;
86
- body: string;
87
- url: string;
88
- buttonName: string;
89
- imageURL?: string;
90
- icon: string;
91
- }) => Promise<{
92
- success: boolean;
93
- data: null;
94
- message: string;
95
- }>;
96
- HIPAY: (body: {
97
- customerId: string;
98
- message: string;
99
- }) => Promise<{
100
- success: boolean;
101
- data: null;
102
- message: string;
103
- }>;
104
- };
83
+ }, next: (err?: any) => void) => Promise<any>;
105
84
  };
106
- socket: {
107
- ShowToast: (body: {
108
- user_id: number;
109
- type: "info" | "warning";
110
- text: string;
111
- }) => Promise<{
112
- success: boolean;
113
- data: null;
114
- message: string;
115
- }>;
116
- ShowStatus: (body: {
117
- user_id: number;
118
- id: string;
119
- type?: "Loading" | "Success" | "Failure";
120
- title?: string;
121
- amount?: number | string;
122
- desc?: string;
123
- footer?: {
124
- text: string;
125
- buttons: {
126
- text: string;
127
- fun: "href" | "close" | "exit";
128
- href?: string | null;
129
- }[];
130
- };
131
- show?: boolean;
132
- expired_date?: Date;
133
- start_date?: Date;
134
- }) => Promise<{
135
- success: boolean;
136
- data: null;
137
- message: string;
138
- }>;
139
- ShowOrderStatus: (body: {
140
- user_id: number;
141
- order_id: string;
142
- }) => Promise<{
143
- success: boolean;
144
- data: null;
145
- message: string;
146
- }>;
147
- EMIT: (body: {
148
- user_id: number;
149
- emit_name: string;
150
- data: any;
151
- }[]) => Promise<{
152
- success: boolean;
153
- data: null;
154
- message: string;
155
- }>;
85
+ notification: {
86
+ push: (body: import("./types").PushNotiT) => Promise<import("./types").Result<null>>;
87
+ sms: (body: import("./types").SmsT) => Promise<import("./types").Result<null>>;
88
+ mail: (body: import("./types").MailT) => Promise<import("./types").Result<null>>;
89
+ SinglePushNoti: (body: import("./types").PushNotiT) => Promise<import("./types").Result<null>>;
90
+ SMS: (body: import("./types").SmsT) => Promise<import("./types").Result<null>>;
91
+ MAIL: (body: import("./types").MailT) => Promise<import("./types").Result<null>>;
156
92
  };
157
- find: {
158
- USER: (body: {
159
- user_id: number;
160
- user_name: string;
161
- nick_name: string;
162
- exp: number;
163
- primary_email?: string | null;
164
- primary_phone?: string | null;
165
- emails: import("./find").UserEmailItem[];
166
- phone_numbers: import("./find").UserPhoneItem[];
167
- auth_profiles: import("./find").UserAuthProfiles;
168
- profile_image_url: string;
169
- date_of_birth: Date | null;
170
- gender?: import("./find").Gender;
171
- sign_up_date: Date | null;
172
- last_login: Date | null;
173
- wallets: import("./find").UserWalletItem[];
174
- preferences: import("./find").UserPreferences;
175
- status: import("./find").UserStatus;
176
- security: import("./find").UserSecurity;
177
- verify_info: import("./find").UserVerifyInfo;
178
- dynamic_info: Record<string, any>;
179
- apps: import("./find").UserAppItem[];
180
- search_string?: string;
181
- delFlg?: boolean;
182
- order_index?: number;
183
- }) => Promise<{
184
- success: boolean;
185
- data: {
186
- user_id: number;
187
- user_name: string;
188
- nick_name: string;
189
- exp: number;
190
- primary_email?: string | null;
191
- primary_phone?: string | null;
192
- emails: import("./find").UserEmailItem[];
193
- phone_numbers: import("./find").UserPhoneItem[];
194
- auth_profiles: import("./find").UserAuthProfiles;
195
- profile_image_url: string;
196
- date_of_birth: Date | null;
197
- gender?: import("./find").Gender;
198
- sign_up_date: Date | null;
199
- last_login: Date | null;
200
- wallets: import("./find").UserWalletItem[];
201
- preferences: import("./find").UserPreferences;
202
- status: import("./find").UserStatus;
203
- security: import("./find").UserSecurity;
204
- verify_info: import("./find").UserVerifyInfo;
205
- dynamic_info: Record<string, any>;
206
- apps: import("./find").UserAppItem[];
207
- search_string?: string;
208
- delFlg?: boolean;
209
- order_index?: number;
210
- }[];
211
- message: string;
212
- }>;
213
- MERCHANT: (body: {
214
- _id?: string;
215
- user_id: number;
216
- user_name?: string;
217
- phone_number?: string;
218
- user_role?: "merchant" | "sub-merchant";
219
- avatar?: string;
220
- authority?: string[];
221
- user_email?: string;
222
- password?: string;
223
- show_router?: string[];
224
- show_id?: string[];
225
- merchant: {
226
- toki_id: string;
227
- hipay: string;
228
- };
229
- wallet_id?: string;
230
- wallet_key?: string;
231
- delFlg?: boolean;
232
- createdAt?: Date;
233
- updatedAt?: Date;
234
- }) => Promise<{
235
- success: boolean;
236
- data: {
237
- _id?: string;
238
- user_id: number;
239
- user_name?: string;
240
- phone_number?: string;
241
- user_role?: "merchant" | "sub-merchant";
242
- avatar?: string;
243
- authority?: string[];
244
- user_email?: string;
245
- password?: string;
246
- show_router?: string[];
247
- show_id?: string[];
248
- merchant: {
249
- toki_id: string;
250
- hipay: string;
251
- };
252
- wallet_id?: string;
253
- wallet_key?: string;
254
- delFlg?: boolean;
255
- createdAt?: Date;
256
- updatedAt?: Date;
257
- }[];
258
- message: string;
259
- }>;
260
- ADMIN: (body: {
261
- _id?: string;
262
- user_id?: number;
263
- user_name?: string;
264
- phone_number?: number;
265
- user_role?: "admin" | "sub-admin" | "super-admin";
266
- avatar?: string;
267
- authority?: string[];
268
- user_email?: string;
269
- password?: string;
270
- show_router?: string[];
271
- show_id?: string[];
272
- delFlg?: boolean;
273
- createdAt?: Date;
274
- updatedAt?: Date;
275
- }) => Promise<{
276
- success: boolean;
277
- data: {
278
- _id?: string;
279
- user_id?: number;
280
- user_name?: string;
281
- phone_number?: number;
282
- user_role?: "admin" | "sub-admin" | "super-admin";
283
- avatar?: string;
284
- authority?: string[];
285
- user_email?: string;
286
- password?: string;
287
- show_router?: string[];
288
- show_id?: string[];
289
- delFlg?: boolean;
290
- createdAt?: Date;
291
- updatedAt?: Date;
292
- }[];
293
- message: string;
294
- }>;
295
- CLIENT: (body: {
296
- _id?: string;
297
- user_id?: number;
298
- user_name?: string;
299
- phone_number?: number;
300
- user_role?: "crm" | "sub-crm";
301
- avatar?: string;
302
- authority?: string[];
303
- user_email?: string;
304
- password?: string;
305
- show_router?: string[];
306
- show_id?: string[];
307
- RefundFun?: boolean;
308
- delFlg?: boolean;
309
- createdAt?: Date;
310
- updatedAt?: Date;
311
- }) => Promise<{
312
- success: boolean;
313
- data: {
314
- _id?: string;
315
- user_id?: number;
316
- user_name?: string;
317
- phone_number?: number;
318
- user_role?: "crm" | "sub-crm";
319
- avatar?: string;
320
- authority?: string[];
321
- user_email?: string;
322
- password?: string;
323
- show_router?: string[];
324
- show_id?: string[];
325
- RefundFun?: boolean;
326
- delFlg?: boolean;
327
- createdAt?: Date;
328
- updatedAt?: Date;
329
- }[];
330
- message: string;
331
- }>;
332
- CRM: (body: {
333
- _id?: string;
334
- user_id?: number;
335
- user_name?: string;
336
- phone_number?: number;
337
- user_role?: "crm" | "sub-crm";
338
- avatar?: string;
339
- authority?: string[];
340
- user_email?: string;
341
- password?: string;
342
- show_router?: string[];
343
- show_id?: string[];
344
- RefundFun?: boolean;
345
- delFlg?: boolean;
346
- createdAt?: Date;
347
- updatedAt?: Date;
348
- }) => Promise<{
349
- success: boolean;
350
- data: {
351
- _id?: string;
352
- user_id?: number;
353
- user_name?: string;
354
- phone_number?: number;
355
- user_role?: "crm" | "sub-crm";
356
- avatar?: string;
357
- authority?: string[];
358
- user_email?: string;
359
- password?: string;
360
- show_router?: string[];
361
- show_id?: string[];
362
- RefundFun?: boolean;
363
- delFlg?: boolean;
364
- createdAt?: Date;
365
- updatedAt?: Date;
366
- }[];
367
- message: string;
368
- }>;
93
+ socket: {
94
+ toast: (body: import("./types").ToastT) => Promise<import("./types").Result<null>>;
95
+ status: (body: import("./types").StatusT) => Promise<import("./types").Result<null>>;
96
+ orderStatus: (body: import("./types").OrderStatusT) => Promise<import("./types").Result<null>>;
97
+ emit: (body: import("./types").EmitT[]) => Promise<import("./types").Result<null>>;
98
+ ShowToast: (body: import("./types").ToastT) => Promise<import("./types").Result<null>>;
99
+ ShowStatus: (body: import("./types").StatusT) => Promise<import("./types").Result<null>>;
100
+ ShowOrderStatus: (body: import("./types").OrderStatusT) => Promise<import("./types").Result<null>>;
101
+ EMIT: (body: import("./types").EmitT[]) => Promise<import("./types").Result<null>>;
369
102
  };
103
+ setAuth: (auth: {
104
+ client_id: string;
105
+ client_secret: string;
106
+ }) => void;
107
+ setHost: (hosts: Env | {
108
+ MAIN: string;
109
+ WALLET?: string;
110
+ }, env?: Env) => void;
111
+ setLogger: (status: boolean) => void;
112
+ ObjectId: () => string;
370
113
  };
371
114
  export default _default;
372
- export { default as notification } from "./notification";
373
- export { default as socket } from "./socket";
374
- export { default as find } from "./find";
package/dist/index.js CHANGED
@@ -1,13 +1,86 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.find = exports.socket = exports.notification = exports.setLogger = exports.setHost = exports.config = exports.ObjectId = exports.jsonToQueryString = void 0;
17
+ exports.authState = exports.socket = exports.notification = exports.user = exports.auth = exports.ObjectId = exports.jsonToQueryString = exports.setLogger = exports.setAuth = exports.setHost = exports.config = void 0;
18
+ /**
19
+ * auth-mn — Auth.mn API client (Node.js)
20
+ *
21
+ * import authMn, { setAuth, setHost } from "auth-mn";
22
+ * setHost("prod"); // эсвэл setHost({ MAIN: "https://..." })
23
+ * setAuth({ client_id: "cl_...", client_secret: "sk_..." });
24
+ * const v = await authMn.user.verify(userTokenFromFrontend); // → v.data.user
25
+ *
26
+ * Token-ийг lib өөрөө авч, хугацаа дуусахаас өмнө refresh хийнэ.
27
+ * Client-д олгосон permission-ээс гадуур API дуудвал 403 + message буцна.
28
+ *
29
+ * Хамрах хүрээ: хэрэглэгч (verify / find), мэдэгдэл (push / sms / mail),
30
+ * socket (toast / status / orderStatus / emit). Merchant/admin зэрэг дотоод
31
+ * бүртгэлүүд энэ санд байхгүй.
32
+ */
4
33
  const auth_1 = require("./auth");
34
+ exports.auth = auth_1.default;
35
+ const user_1 = require("./user");
36
+ exports.user = user_1.default;
5
37
  const notification_1 = require("./notification");
38
+ exports.notification = notification_1.default;
6
39
  const socket_1 = require("./socket");
7
- const find_1 = require("./find");
40
+ exports.socket = socket_1.default;
41
+ const HOSTS = {
42
+ prod: { MAIN: "https://api.auth.mn", WALLET: "https://api.auth.mn" },
43
+ staging: { MAIN: "https://staging-api.auth.mn", WALLET: "https://staging-api.auth.mn" }
44
+ };
45
+ exports.config = {
46
+ env: "prod",
47
+ hosts: { ...HOSTS.prod },
48
+ auth: { client_id: "", client_secret: "" },
49
+ logger: false,
50
+ timeout: 20000
51
+ };
52
+ /** Host тохируулна: setHost("staging") эсвэл setHost({ MAIN, WALLET }, "prod") */
53
+ const setHost = (hosts, env) => {
54
+ if (typeof hosts === "string") {
55
+ exports.config.env = hosts;
56
+ exports.config.hosts = { ...HOSTS[hosts] };
57
+ }
58
+ else {
59
+ exports.config.hosts.MAIN = hosts.MAIN.replace(/\/$/, "");
60
+ exports.config.hosts.WALLET = (hosts.WALLET || hosts.MAIN).replace(/\/$/, "");
61
+ if (env)
62
+ exports.config.env = env;
63
+ }
64
+ if (exports.config.logger)
65
+ console.log("[auth-mn] hosts:", exports.config.hosts, "env:", exports.config.env);
66
+ };
67
+ exports.setHost = setHost;
68
+ /** client_id + client_secret (Admin panel → Апп-ууд дээрээс авна) */
69
+ const setAuth = (auth) => {
70
+ exports.config.auth.client_id = auth.client_id;
71
+ exports.config.auth.client_secret = auth.client_secret;
72
+ // шинэ credential → хуучин token-ийг хаяна
73
+ auth_2.authState.clear();
74
+ };
75
+ exports.setAuth = setAuth;
76
+ const setLogger = (status) => {
77
+ exports.config.logger = status;
78
+ };
79
+ exports.setLogger = setLogger;
8
80
  const jsonToQueryString = (params) => {
9
81
  const query = Object.entries(params)
10
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
82
+ .filter(([, v]) => v !== undefined && v !== null)
83
+ .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
11
84
  .join("&");
12
85
  return query ? `?${query}` : "";
13
86
  };
@@ -15,49 +88,10 @@ exports.jsonToQueryString = jsonToQueryString;
15
88
  const ObjectId = () => {
16
89
  const timestamp = ((Date.now() / 1000) | 0).toString(16);
17
90
  return (timestamp +
18
- "xxxxxxxxxxxxxxxx"
19
- .replace(/[x]/g, () => ((Math.random() * 16) | 0).toString(16))
20
- .toLowerCase());
91
+ "xxxxxxxxxxxxxxxx".replace(/[x]/g, () => ((Math.random() * 16) | 0).toString(16)).toLowerCase());
21
92
  };
22
93
  exports.ObjectId = ObjectId;
23
- exports.config = {
24
- token: "",
25
- env: "staging",
26
- hosts: {
27
- MAIN: "https://staging-api.auth.mn",
28
- WALLET: "https://staging-api.auth.mn"
29
- },
30
- auth: {
31
- username: "",
32
- password: ""
33
- },
34
- logger: false
35
- };
36
- // Function to set the host URLs for API requests
37
- const setHost = ({ MAIN, WALLET }, ENV) => {
38
- exports.config.hosts.MAIN = MAIN;
39
- exports.config.hosts.WALLET = WALLET;
40
- exports.config.env = ENV;
41
- console.log("Auth.mn Hosts set to:", exports.config.hosts);
42
- console.log("Auth.mn Environment set to:", ENV);
43
- };
44
- exports.setHost = setHost;
45
- // Function to enable or disable logging
46
- const setLogger = (status) => {
47
- exports.config.logger = status;
48
- console.log("Logger status set to:", status);
49
- };
50
- exports.setLogger = setLogger;
51
- exports.default = {
52
- auth: auth_1.default,
53
- ObjectId: exports.ObjectId,
54
- notification: notification_1.default,
55
- socket: socket_1.default,
56
- find: find_1.default
57
- };
58
- var notification_2 = require("./notification");
59
- Object.defineProperty(exports, "notification", { enumerable: true, get: function () { return notification_2.default; } });
60
- var socket_2 = require("./socket");
61
- Object.defineProperty(exports, "socket", { enumerable: true, get: function () { return socket_2.default; } });
62
- var find_2 = require("./find");
63
- Object.defineProperty(exports, "find", { enumerable: true, get: function () { return find_2.default; } });
94
+ const auth_2 = require("./auth");
95
+ Object.defineProperty(exports, "authState", { enumerable: true, get: function () { return auth_2.authState; } });
96
+ __exportStar(require("./types"), exports);
97
+ exports.default = { auth: auth_1.default, user: user_1.default, notification: notification_1.default, socket: socket_1.default, setAuth: exports.setAuth, setHost: exports.setHost, setLogger: exports.setLogger, ObjectId: exports.ObjectId };