auth-mn 0.2.0 → 1.0.1

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,119 @@
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 = {
22
+ /** Одоогийн client access token (сан автоматаар шинэчилнэ; гараар Authorization header үүсгэхэд ашиглаж болно) */
4
23
  token: string;
5
- env: "prod" | "staging";
24
+ env: Env;
6
25
  hosts: {
7
26
  MAIN: string;
8
27
  WALLET: string;
9
28
  };
10
29
  auth: {
11
- username: string;
12
- password: string;
30
+ client_id: string;
31
+ client_secret: string;
13
32
  };
14
33
  logger: boolean;
34
+ /** Хүсэлтийн timeout (ms), default 20000 */
35
+ timeout: number;
15
36
  };
16
37
  export declare const config: Config;
17
- export declare const setHost: ({ MAIN, WALLET }: {
38
+ /** Host тохируулна: setHost("staging") эсвэл setHost({ MAIN, WALLET }, "prod") */
39
+ export declare const setHost: (hosts: Env | {
18
40
  MAIN: string;
19
- WALLET: string;
20
- }, ENV: "staging" | "prod") => void;
41
+ WALLET?: string;
42
+ }, env?: Env) => void;
43
+ /** client_id + client_secret (Admin panel → Апп-ууд дээрээс авна) */
44
+ export declare const setAuth: (auth: {
45
+ client_id: string;
46
+ client_secret: string;
47
+ }) => void;
21
48
  export declare const setLogger: (status: boolean) => void;
49
+ export declare const jsonToQueryString: (params: Record<string, any>) => string;
50
+ export declare const ObjectId: () => string;
51
+ import { authState } from "./auth";
52
+ export { auth, user, notification, socket, authState };
53
+ export * from "./types";
22
54
  declare const _default: {
23
55
  auth: {
24
- TOKEN: (auth: {
56
+ getToken: () => Promise<string>;
57
+ token: () => string;
58
+ login: () => Promise<import("./types").Result<import("./types").TokenData>>;
59
+ refresh: () => Promise<import("./types").Result<import("./types").TokenData>>;
60
+ revoke: () => Promise<import("./types").Result<null>>;
61
+ me: () => Promise<import("./types").Result<import("./types").ClientInfo>>;
62
+ permissions: () => Promise<import("./types").Result<import("./types").PermissionGroup[]>>;
63
+ hasPermission: (perm: string) => boolean;
64
+ TOKEN: (_legacy?: {
25
65
  username: string;
26
66
  password: string;
27
67
  }) => Promise<string>;
68
+ state: import("./auth").AuthState;
28
69
  };
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;
70
+ user: {
71
+ verify: (token: string) => Promise<import("./types").Result<import("./types").VerifiedUser>>;
72
+ find: (query: {
73
+ user_id?: number | string;
74
+ phone_number?: string;
75
+ email?: string;
76
+ }) => Promise<import("./types").Result<import("./types").UserT>>;
77
+ list: typeof import("./user").list;
78
+ listAll: typeof import("./user").listAll;
79
+ middleware: (opts?: import("./user").MiddlewareOptions) => (req: {
80
+ [k: string]: any;
81
+ headers: Record<string, any>;
82
+ query?: any;
83
+ body?: any;
84
+ }, res: {
85
+ status: (code: number) => {
86
+ json: (b: any) => any;
76
87
  };
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
- };
88
+ }, next: (err?: any) => void) => Promise<any>;
105
89
  };
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
- }>;
90
+ notification: {
91
+ push: (body: import("./types").PushNotiT) => Promise<import("./types").Result<null>>;
92
+ sms: (body: import("./types").SmsT) => Promise<import("./types").Result<null>>;
93
+ mail: (body: import("./types").MailT) => Promise<import("./types").Result<null>>;
94
+ SinglePushNoti: (body: import("./types").PushNotiT) => Promise<import("./types").Result<null>>;
95
+ SMS: (body: import("./types").SmsT) => Promise<import("./types").Result<null>>;
96
+ MAIL: (body: import("./types").MailT) => Promise<import("./types").Result<null>>;
156
97
  };
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
- }>;
98
+ socket: {
99
+ toast: (body: import("./types").ToastT) => Promise<import("./types").Result<null>>;
100
+ status: (body: import("./types").StatusT) => Promise<import("./types").Result<null>>;
101
+ orderStatus: (body: import("./types").OrderStatusT) => Promise<import("./types").Result<null>>;
102
+ emit: (body: import("./types").EmitT[]) => Promise<import("./types").Result<null>>;
103
+ ShowToast: (body: import("./types").ToastT) => Promise<import("./types").Result<null>>;
104
+ ShowStatus: (body: import("./types").StatusT) => Promise<import("./types").Result<null>>;
105
+ ShowOrderStatus: (body: import("./types").OrderStatusT) => Promise<import("./types").Result<null>>;
106
+ EMIT: (body: import("./types").EmitT[]) => Promise<import("./types").Result<null>>;
369
107
  };
108
+ setAuth: (auth: {
109
+ client_id: string;
110
+ client_secret: string;
111
+ }) => void;
112
+ setHost: (hosts: Env | {
113
+ MAIN: string;
114
+ WALLET?: string;
115
+ }, env?: Env) => void;
116
+ setLogger: (status: boolean) => void;
117
+ ObjectId: () => string;
370
118
  };
371
119
  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,87 @@
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
+ token: "",
47
+ env: "prod",
48
+ hosts: { ...HOSTS.prod },
49
+ auth: { client_id: "", client_secret: "" },
50
+ logger: false,
51
+ timeout: 20000
52
+ };
53
+ /** Host тохируулна: setHost("staging") эсвэл setHost({ MAIN, WALLET }, "prod") */
54
+ const setHost = (hosts, env) => {
55
+ if (typeof hosts === "string") {
56
+ exports.config.env = hosts;
57
+ exports.config.hosts = { ...HOSTS[hosts] };
58
+ }
59
+ else {
60
+ exports.config.hosts.MAIN = hosts.MAIN.replace(/\/$/, "");
61
+ exports.config.hosts.WALLET = (hosts.WALLET || hosts.MAIN).replace(/\/$/, "");
62
+ if (env)
63
+ exports.config.env = env;
64
+ }
65
+ if (exports.config.logger)
66
+ console.log("[auth-mn] hosts:", exports.config.hosts, "env:", exports.config.env);
67
+ };
68
+ exports.setHost = setHost;
69
+ /** client_id + client_secret (Admin panel → Апп-ууд дээрээс авна) */
70
+ const setAuth = (auth) => {
71
+ exports.config.auth.client_id = auth.client_id;
72
+ exports.config.auth.client_secret = auth.client_secret;
73
+ // шинэ credential → хуучин token-ийг хаяна
74
+ auth_2.authState.clear();
75
+ };
76
+ exports.setAuth = setAuth;
77
+ const setLogger = (status) => {
78
+ exports.config.logger = status;
79
+ };
80
+ exports.setLogger = setLogger;
8
81
  const jsonToQueryString = (params) => {
9
82
  const query = Object.entries(params)
10
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
83
+ .filter(([, v]) => v !== undefined && v !== null)
84
+ .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
11
85
  .join("&");
12
86
  return query ? `?${query}` : "";
13
87
  };
@@ -15,49 +89,10 @@ exports.jsonToQueryString = jsonToQueryString;
15
89
  const ObjectId = () => {
16
90
  const timestamp = ((Date.now() / 1000) | 0).toString(16);
17
91
  return (timestamp +
18
- "xxxxxxxxxxxxxxxx"
19
- .replace(/[x]/g, () => ((Math.random() * 16) | 0).toString(16))
20
- .toLowerCase());
92
+ "xxxxxxxxxxxxxxxx".replace(/[x]/g, () => ((Math.random() * 16) | 0).toString(16)).toLowerCase());
21
93
  };
22
94
  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; } });
95
+ const auth_2 = require("./auth");
96
+ Object.defineProperty(exports, "authState", { enumerable: true, get: function () { return auth_2.authState; } });
97
+ __exportStar(require("./types"), exports);
98
+ 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 };