@pmate/account-sdk 0.5.4 → 0.6.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.
Files changed (76) hide show
  1. package/package.json +18 -20
  2. package/src/api/AccountService.ts +97 -0
  3. package/src/api/Api.ts +99 -0
  4. package/src/api/AppService.ts +71 -0
  5. package/src/api/EntityService.ts +41 -0
  6. package/src/api/ProfileService.ts +133 -0
  7. package/src/api/cacheInMem.ts +73 -0
  8. package/src/api/index.ts +5 -0
  9. package/src/atoms/accountAtom.ts +18 -0
  10. package/src/atoms/accountProfileAtom.ts +13 -0
  11. package/src/atoms/atomWithLoadable.ts +48 -0
  12. package/src/atoms/createProfileAtom.ts +27 -0
  13. package/src/atoms/index.ts +16 -0
  14. package/src/atoms/learningLangAtom.ts +8 -0
  15. package/src/atoms/localStorageAtom.ts +39 -0
  16. package/src/atoms/loginAtom.ts +8 -0
  17. package/src/atoms/motherTongueAtom.ts +8 -0
  18. package/src/atoms/profileAtom.ts +24 -0
  19. package/src/atoms/profileDraftAtom.ts +7 -0
  20. package/src/atoms/profilesAtom.ts +20 -0
  21. package/src/atoms/sessionCheckAtom.ts +10 -0
  22. package/src/atoms/switchProfileAtom.ts +9 -0
  23. package/src/atoms/updateProfileAtom.ts +35 -0
  24. package/src/atoms/uploadAvatarAtom.ts +49 -0
  25. package/src/atoms/userLogoutAtom.ts +8 -0
  26. package/src/atoms/userSettingsAtom.ts +58 -0
  27. package/src/components/AuthProviderV2.tsx +315 -0
  28. package/src/components/Button.tsx +39 -0
  29. package/src/components/Drawer.tsx +80 -0
  30. package/src/components/index.ts +1 -0
  31. package/src/constants.ts +1 -0
  32. package/src/hooks/index.ts +5 -0
  33. package/src/hooks/useAppBackgroundStyle.ts +25 -0
  34. package/src/hooks/useAppConfig.ts +44 -0
  35. package/src/hooks/useAuthApp.ts +165 -0
  36. package/src/hooks/useAuthSnapshot.ts +84 -0
  37. package/src/hooks/useIsAuthenticated.ts +19 -0
  38. package/src/hooks/useProfileStepFlow.ts +59 -0
  39. package/src/i18n/index.ts +59 -0
  40. package/src/index.ts +9 -0
  41. package/src/locales/ar-SA.json +183 -0
  42. package/src/locales/de-DE.json +183 -0
  43. package/src/locales/el-GR.json +183 -0
  44. package/src/locales/en.json +183 -0
  45. package/src/locales/es-ES.json +183 -0
  46. package/src/locales/fi-FI.json +183 -0
  47. package/src/locales/fil-PH.json +183 -0
  48. package/src/locales/fr-FR.json +183 -0
  49. package/src/locales/hi-IN.json +183 -0
  50. package/src/locales/ja-JP.json +183 -0
  51. package/src/locales/ko-KR.json +183 -0
  52. package/src/locales/pt-BR.json +183 -0
  53. package/src/locales/pt-PT.json +183 -0
  54. package/src/locales/ru-RU.json +183 -0
  55. package/src/locales/ta-IN.json +183 -0
  56. package/src/locales/uk-UA.json +183 -0
  57. package/src/locales/zh-CN.json +183 -0
  58. package/src/locales/zh-TW.json +183 -0
  59. package/src/types/account.types.ts +28 -0
  60. package/src/types/app.ts +22 -0
  61. package/src/types/profile.ts +6 -0
  62. package/src/utils/AccountManagerV2.ts +349 -0
  63. package/src/utils/Redirect.ts +17 -0
  64. package/src/utils/accountStorage.ts +46 -0
  65. package/src/utils/errors.ts +1 -0
  66. package/src/utils/index.ts +11 -0
  67. package/src/utils/location.ts +34 -0
  68. package/src/utils/profileStep.ts +26 -0
  69. package/src/utils/resolveAppId.ts +13 -0
  70. package/src/utils/selectedProfileStorage.ts +46 -0
  71. package/src/utils/tokenStorage.ts +47 -0
  72. package/dist/index.cjs.js +0 -22
  73. package/dist/index.cjs.js.map +0 -1
  74. package/dist/index.d.ts +0 -302
  75. package/dist/index.es.js +0 -8884
  76. package/dist/index.es.js.map +0 -1
package/dist/index.d.ts DELETED
@@ -1,302 +0,0 @@
1
- import * as jotai from 'jotai';
2
- import { WritableAtom } from 'jotai';
3
- import * as _pmate_meta from '@pmate/meta';
4
- import { AccountState, Profile, UserSettings, LocalProfileState, RoomPeerInfo, LangShort, VCodeIssueRequest, VCodeIssueResult, AuthRequest, AuthLoginResponse, AuthSession, CreateProfileRequest, UpdateProfileRequest, ProfileScope, GroupInfo } from '@pmate/meta';
5
- import { AtomFamily } from 'jotai-family';
6
- import * as _pmate_utils from '@pmate/utils';
7
- import { Emitter, Maybe } from '@pmate/utils';
8
- import * as react_jsx_runtime from 'react/jsx-runtime';
9
- import * as react from 'react';
10
- import { PropsWithChildren } from 'react';
11
- import * as i18next from 'i18next';
12
- import * as node_modules_i18next from 'node_modules/i18next';
13
- export { getLangFull, isNicknameValid, legacyLangMap, normalizeLang } from '@pmate/lang';
14
- export { I18nextProvider } from 'react-i18next';
15
-
16
- type UploadAvatarParams = {
17
- file: File;
18
- userId: string;
19
- };
20
- declare const uploadAvatarAtom: jotai.WritableAtom<null, [UploadAvatarParams], Promise<string>> & {
21
- init: null;
22
- };
23
-
24
- declare enum AccountLifecycleState {
25
- Idle = "idle",
26
- Checking = "checking",
27
- Authenticated = "authenticated",
28
- Unauthenticated = "unauthenticated",
29
- ProfileUninitialized = "profile-uninitialized",
30
- ProfileInitializing = "profile-initializing",
31
- ProfileReady = "profile-ready",
32
- LoggingIn = "logging-in",
33
- LoggingOut = "logging-out",
34
- Error = "error"
35
- }
36
- type AccountSnapshot$1 = {
37
- state: AccountLifecycleState;
38
- account: AccountState | null;
39
- accountId: string | null;
40
- error: Error | null;
41
- profiles: Profile[];
42
- profile: Profile | null;
43
- };
44
-
45
- type AccountSnapshot = AccountSnapshot$1;
46
- declare const accountAtom: jotai.PrimitiveAtom<AccountSnapshot$1> & {
47
- init: AccountSnapshot$1;
48
- };
49
-
50
- declare const accountStateAtom: jotai.Atom<AccountState | undefined>;
51
- declare const profileAtom: jotai.Atom<Profile | undefined>;
52
-
53
- declare const switchProfileAtom: jotai.WritableAtom<null, [profile: Profile], void> & {
54
- init: null;
55
- };
56
-
57
- type CreateProfileParams = {
58
- nickName: string;
59
- };
60
- declare const createProfileAtom: jotai.WritableAtom<null, [CreateProfileParams], Promise<_pmate_meta.Profile>> & {
61
- init: null;
62
- };
63
-
64
- type ProfileDraft = Partial<Profile> & {
65
- isAdult?: boolean;
66
- age?: number;
67
- };
68
-
69
- /**
70
- * Temporary profile info during registration flow
71
- */
72
- declare const profileDraftAtom: jotai.PrimitiveAtom<ProfileDraft> & {
73
- init: ProfileDraft;
74
- };
75
-
76
- type ProfileByIdAtomFamily = AtomFamily<string, WritableAtom<Promise<Profile | null>, [], void>>;
77
- declare const profileByIdAtom: ProfileByIdAtomFamily;
78
-
79
- declare const profilesAtom: jotai.Atom<_pmate_utils.Loadable<Profile[]>>;
80
-
81
- declare const updateProfileAtom: jotai.WritableAtom<null, [profileId: string, updates: Record<string, any>], Promise<void>> & {
82
- init: null;
83
- };
84
-
85
- declare const loginAtom: jotai.WritableAtom<null, [], Promise<_pmate_meta.AccountState>> & {
86
- init: null;
87
- };
88
-
89
- declare const sessionCheckAtom: jotai.WritableAtom<null, [], Promise<_pmate_meta.AuthSession | null>> & {
90
- init: null;
91
- };
92
-
93
- declare const userLogoutAtom: jotai.WritableAtom<null, [], Promise<void>> & {
94
- init: null;
95
- };
96
-
97
- declare const motherTongueAtom: jotai.Atom<"en" | "zh-CN" | "zh-TW" | "ko-KR" | "es-ES" | "fr-FR" | "ja-JP" | "de-DE" | "ar-SA" | "el-GR" | "fi-FI" | "fil-PH" | "hi-IN" | "pt-BR" | "pt-PT" | "ru-RU" | "ta-IN" | "uk-UA">;
98
-
99
- declare const learningLangAtom: jotai.Atom<"en" | "zh-CN" | "zh-TW" | "ko-KR" | "es-ES" | "fr-FR" | "ja-JP" | "de-DE" | "ar-SA" | "el-GR" | "fi-FI" | "fil-PH" | "hi-IN" | "pt-BR" | "pt-PT" | "ru-RU" | "ta-IN" | "uk-UA">;
100
-
101
- declare const userSettingsAtom: <T extends keyof UserSettings>(key: T) => WritableAtom<UserSettings[T], [value: UserSettings[T]], void>;
102
-
103
- declare enum LSKEYS {
104
- USER_SETTINGS = "user-settings"
105
- }
106
- type LocalStorageJsonAtomFamily = AtomFamily<LSKEYS, WritableAtom<any, [any], void>>;
107
- declare const localStorageJsonAtom: LocalStorageJsonAtomFamily;
108
-
109
- type AuthRoute = string | {
110
- path: string;
111
- behavior?: "redirect" | "prompt";
112
- };
113
- type AuthProviderV2Props = PropsWithChildren<{
114
- app: string;
115
- authRoutes?: AuthRoute[];
116
- onLoginSuccess?: () => void | Promise<void>;
117
- rtcProvider?: React.ComponentType<{
118
- children: React.ReactNode;
119
- }>;
120
- pathname?: string;
121
- navigate?: (to: string, options?: {
122
- replace?: boolean;
123
- }) => void;
124
- }>;
125
- declare const AuthProviderV2: ({ app, authRoutes, rtcProvider: RtcProvider, pathname: pathnameProp, navigate: navigateProp, children, }: AuthProviderV2Props) => string | number | boolean | Iterable<react.ReactNode> | react_jsx_runtime.JSX.Element | null | undefined;
126
-
127
- declare const i18n: node_modules_i18next.i18n;
128
-
129
- declare const useTranslation: () => i18next.TFunction<"translation", undefined>;
130
-
131
- declare const DEFAULT_APP_ID = "@pmate/chat";
132
- interface ProfileStep$1 {
133
- type: "nickname" | "learning-language" | "mother-tongue" | "gender" | "is-adult" | "age" | "Identity";
134
- title: string;
135
- required: boolean;
136
- }
137
- interface AppConfig {
138
- id: string;
139
- name: string;
140
- icon: string;
141
- background: string;
142
- themeColor?: string;
143
- welcomeText: string;
144
- profiles: ProfileStep$1[];
145
- }
146
- declare const getAppConfig: (appId: string | null) => AppConfig;
147
-
148
- type ProfileStepType = "nickname" | "learning-language" | "mother-tongue" | "gender" | "is-adult" | "age";
149
- declare const isProfileStepType: (value: string) => value is ProfileStepType;
150
- interface ProfileStep {
151
- type: ProfileStepType;
152
- title: string;
153
- required: boolean;
154
- }
155
-
156
- type UseProfileStepFlowParams = {
157
- params: URLSearchParams;
158
- };
159
- declare const useProfileStepFlow: ({ params, }: UseProfileStepFlowParams) => {
160
- activeStep: ProfileStepType;
161
- appProfileSteps: ProfileStepType[];
162
- buildStepUrl: (next: ProfileStepType) => string;
163
- createSteps: ProfileStepType[];
164
- isCreateFlowStep: boolean;
165
- nextStep: ProfileStepType | undefined;
166
- };
167
-
168
- type UseAuthAppOptions = {
169
- app?: string;
170
- redirect?: string;
171
- };
172
- type AuthAppRedirectOptions = {
173
- app?: string;
174
- redirect?: string;
175
- };
176
- type AuthProfileRedirectOptions = AuthAppRedirectOptions & {
177
- step?: ProfileStepType;
178
- };
179
- declare const useAuthApp: (options?: UseAuthAppOptions) => {
180
- app: string;
181
- login: (overrides?: AuthAppRedirectOptions) => void;
182
- logout: (overrides?: AuthAppRedirectOptions) => void;
183
- selectProfile: (overrides?: AuthAppRedirectOptions) => void;
184
- createProfile: (overrides?: AuthProfileRedirectOptions) => void;
185
- updateProfile: (overrides?: AuthProfileRedirectOptions) => void;
186
- };
187
-
188
- declare const useAppBackgroundStyle: () => {
189
- background: string;
190
- };
191
-
192
- declare enum AccountManagerEvent {
193
- StateChange = "stateChange"
194
- }
195
- declare class AccountManagerV2 extends Emitter<AccountManagerEvent> {
196
- private readonly app;
197
- private static instances;
198
- constructor(app: string);
199
- static get(app?: string): AccountManagerV2;
200
- session(): Promise<_pmate_meta.AuthSession | null>;
201
- login(): Promise<AccountState>;
202
- hasUrlSession(): boolean;
203
- /**
204
- * When a URL contains a sessionId parameter, this method logs in the user.
205
- * If URL does not contain a sessionId parameter, this method logs in from existing session.
206
- */
207
- loginUrlSessionOverride(): Promise<AccountState>;
208
- logout(): Promise<void>;
209
- getSnapshot(): Promise<AccountSnapshot$1>;
210
- transition(next: AccountLifecycleState, error?: Error | null): void;
211
- getAccountState(): Promise<AccountState | null>;
212
- setAccountState(state: AccountState): void;
213
- clearAccountState(): void;
214
- getSelectedProfile(profiles?: Profile[]): Promise<Profile | null>;
215
- setSelectedProfile(profile: Profile | LocalProfileState | string): void;
216
- clearSelectedProfile(): void;
217
- getProfiles(): Promise<Profile[]>;
218
- clearProfiles(): void;
219
- clearSession(): void;
220
- getAuthToken(): string | null;
221
- setAuthToken(token: string): void;
222
- clearAuthToken(): void;
223
- getLocalProfile(): Promise<LocalProfileState | null>;
224
- getCurrentPeer(): Promise<RoomPeerInfo | undefined>;
225
- createProfile(payload: {
226
- nickName: string;
227
- learningTargetLang?: LangShort;
228
- }): Promise<Profile>;
229
- private prepareProfiles;
230
- }
231
-
232
- declare const resolveAppId: (fallbackApp?: string) => string;
233
-
234
- declare class NotAuthenticatedError extends Error {
235
- }
236
-
237
- declare class Redirect {
238
- static toLogin(app: string, redirect?: string): void;
239
- static toCreateProfile(app: string, redirect?: string): void;
240
- }
241
-
242
- declare class Api {
243
- static get<T>(url: string): Promise<T | null>;
244
- static post<T>(url: string, body: unknown): Promise<T>;
245
- static put<T>(url: string, body: unknown): Promise<Maybe<T>>;
246
- static getFile<T>(url: string): Promise<T | null>;
247
- static getFile<T>(url: string, type: "json"): Promise<T | null>;
248
- static getFile(url: string, type: "text"): Promise<string | null>;
249
- static getFile<T>(url: string, type: "log"): Promise<Maybe<T[]>>;
250
- static exists(url: string | Maybe<string>): Promise<boolean>;
251
- }
252
-
253
- declare class AccountService {
254
- private static sessionCached;
255
- static vcode(payload: VCodeIssueRequest): Promise<VCodeIssueResult>;
256
- static login(request: AuthRequest): Promise<AuthLoginResponse>;
257
- static logout(token?: string, app?: string): Promise<{
258
- success: boolean;
259
- }>;
260
- static session(token?: string, app?: string): Promise<AuthSession | null>;
261
- private static authRequest;
262
- }
263
-
264
- declare class ProfileService {
265
- static createProfile(req: CreateProfileRequest): Promise<Profile>;
266
- static updateProfile(req: UpdateProfileRequest): Promise<void>;
267
- static getProfiles(account: AccountState): Promise<Profile[]>;
268
- static getProfilesByScope(scope: ProfileScope): Promise<Profile[]>;
269
- static updateAvatar(req: {
270
- user: string;
271
- base64: string;
272
- filename: string;
273
- }): Promise<string>;
274
- static uploadMsgImage(req: {
275
- user: string;
276
- base64: string;
277
- filename: string;
278
- }): Promise<string>;
279
- static updateMyVoice(req: {
280
- user: string;
281
- base64: string;
282
- text: string;
283
- }): Promise<string>;
284
- static uploadUserFile(req: {
285
- user: string;
286
- base64: string;
287
- filename: string;
288
- }): Promise<string>;
289
- private static uploadToUploaderService;
290
- }
291
-
292
- declare class EntityService {
293
- static entity<T>(id: string): Promise<T | null>;
294
- static getProfile(id: string): Promise<Profile | null>;
295
- static getGroup(id: string): Promise<GroupInfo | null>;
296
- static entities<T extends {
297
- id?: string;
298
- }>(ids: string[]): Promise<Record<string, T>>;
299
- }
300
-
301
- export { AccountManagerEvent, AccountManagerV2, AccountService, Api, AuthProviderV2, DEFAULT_APP_ID, EntityService, LSKEYS, NotAuthenticatedError, ProfileService, Redirect, accountAtom, accountStateAtom, createProfileAtom, getAppConfig, i18n, isProfileStepType, learningLangAtom, localStorageJsonAtom, loginAtom, motherTongueAtom, profileAtom, profileByIdAtom, profileDraftAtom, profilesAtom, resolveAppId, sessionCheckAtom, switchProfileAtom, updateProfileAtom, uploadAvatarAtom, useAppBackgroundStyle, useAuthApp, useProfileStepFlow, useTranslation, userLogoutAtom, userSettingsAtom };
302
- export type { AccountSnapshot, AppConfig, AuthRoute, ProfileDraft, ProfileStep, ProfileStepType };