Skip to content

Commit

Permalink
fix: export method for validate credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
ivkan committed Aug 9, 2023
1 parent 63efe5f commit 998272b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
17 changes: 2 additions & 15 deletions src/client/user-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { DEFAULT_OPTIONS } from './client-options';
import { assertCredentials, assertNotEmptyString } from '../utils/assert';
import { TGLink } from './link';
import { localStorageAdapter } from '../utils/local-storage';
import { isValidCredentials } from '../utils/is-valid-credentials';

const storageStruct = object({
getItem : fn(),
Expand Down Expand Up @@ -213,7 +214,7 @@ export class TGUserApi

if (maybeSession !== null)
{
if (this.#isValidCredentials(maybeSession))
if (isValidCredentials(maybeSession))
{
await this.useCredentials(maybeSession);
}
Expand Down Expand Up @@ -309,18 +310,4 @@ export class TGUserApi
);
}
}

#isValidCredentials(
maybeSession: unknown,
): maybeSession is TGUserCredentials
{
return (
isObject(maybeSession) &&
'priv' in maybeSession &&
'epriv' in maybeSession &&
'alias' in maybeSession &&
'pub' in maybeSession &&
'epub' in maybeSession
);
}
}
25 changes: 13 additions & 12 deletions src/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,20 @@ export function assertNotEmptyString(value: unknown, msg?: string): string
return unwrap(actual);
}

const userCredentialsErrMes = arg => `Credentials must contain '${arg}' string property.`;
export const userCredentialsStruct = object(
{
alias: string(userCredentialsErrMes('alias')),
pub : string(userCredentialsErrMes('pub')),
priv : string(userCredentialsErrMes('priv')),
epriv: string(userCredentialsErrMes('epriv')),
epub : string(userCredentialsErrMes('priv')),
},
'Credentials invalid',
);

export function assertCredentials(value: unknown): TGUserCredentials
{
const errMes = arg => `Credentials must contain '${arg}' string property.`;
const struct = object(
{
alias: string(errMes('alias')),
pub : string(errMes('pub')),
priv : string(errMes('priv')),
epriv: string(errMes('epriv')),
epub : string(errMes('priv')),
},
'Credentials invalid',
);
const actual = struct(value);
const actual = userCredentialsStruct(value);
return unwrap(actual);
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './storage-helpers';
export * from './stringify-options-get';
export * from './uuidv4';
export * from './window-or-global';
export * from './is-valid-credentials';
7 changes: 7 additions & 0 deletions src/utils/is-valid-credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TGUserCredentials } from '../types';
import { userCredentialsStruct } from './assert';

export function isValidCredentials(maybeSession: unknown): maybeSession is TGUserCredentials
{
return userCredentialsStruct(maybeSession).ok;
}

0 comments on commit 998272b

Please sign in to comment.