@logto/client 1.1.1 → 1.1.2

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.
@@ -0,0 +1,17 @@
1
+ import type { Requester } from '@logto/js';
2
+ import type { Nullable } from '@silverhand/essentials';
3
+ export type StorageKey = 'idToken' | 'refreshToken' | 'accessToken' | 'signInSession';
4
+ export type Storage = {
5
+ getItem(key: StorageKey): Promise<Nullable<string>>;
6
+ setItem(key: StorageKey, value: string): Promise<void>;
7
+ removeItem(key: StorageKey): Promise<void>;
8
+ };
9
+ export type Navigate = (url: string) => void;
10
+ export type ClientAdapter = {
11
+ requester: Requester;
12
+ storage: Storage;
13
+ navigate: Navigate;
14
+ generateState: () => string;
15
+ generateCodeVerifier: () => string;
16
+ generateCodeChallenge: (codeVerifier: string) => Promise<string>;
17
+ };
@@ -0,0 +1,16 @@
1
+ import type { NormalizeKeyPaths } from '@silverhand/essentials';
2
+ declare const logtoClientErrorCodes: Readonly<{
3
+ sign_in_session: {
4
+ invalid: string;
5
+ not_found: string;
6
+ };
7
+ not_authenticated: "Not authenticated.";
8
+ fetch_user_info_failed: "Unable to fetch user info. The access token may be invalid.";
9
+ }>;
10
+ export type LogtoClientErrorCode = NormalizeKeyPaths<typeof logtoClientErrorCodes>;
11
+ export declare class LogtoClientError extends Error {
12
+ code: LogtoClientErrorCode;
13
+ data: unknown;
14
+ constructor(code: LogtoClientErrorCode, data?: unknown);
15
+ }
16
+ export {};
package/lib/errors.js ADDED
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ var get = require('lodash.get');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var get__default = /*#__PURE__*/_interopDefault(get);
8
+
9
+ const logtoClientErrorCodes = Object.freeze({
10
+ sign_in_session: {
11
+ invalid: 'Invalid sign-in session.',
12
+ not_found: 'Sign-in session not found.',
13
+ },
14
+ not_authenticated: 'Not authenticated.',
15
+ fetch_user_info_failed: 'Unable to fetch user info. The access token may be invalid.',
16
+ });
17
+ const getMessageByErrorCode = (errorCode) => {
18
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
19
+ const message = get__default.default(logtoClientErrorCodes, errorCode);
20
+ if (typeof message === 'string') {
21
+ return message;
22
+ }
23
+ return errorCode;
24
+ };
25
+ class LogtoClientError extends Error {
26
+ constructor(code, data) {
27
+ super(getMessageByErrorCode(code));
28
+ this.code = code;
29
+ this.data = data;
30
+ }
31
+ }
32
+
33
+ exports.LogtoClientError = LogtoClientError;
package/lib/errors.mjs ADDED
@@ -0,0 +1,27 @@
1
+ import get from 'lodash.get';
2
+
3
+ const logtoClientErrorCodes = Object.freeze({
4
+ sign_in_session: {
5
+ invalid: 'Invalid sign-in session.',
6
+ not_found: 'Sign-in session not found.',
7
+ },
8
+ not_authenticated: 'Not authenticated.',
9
+ fetch_user_info_failed: 'Unable to fetch user info. The access token may be invalid.',
10
+ });
11
+ const getMessageByErrorCode = (errorCode) => {
12
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
13
+ const message = get(logtoClientErrorCodes, errorCode);
14
+ if (typeof message === 'string') {
15
+ return message;
16
+ }
17
+ return errorCode;
18
+ };
19
+ class LogtoClientError extends Error {
20
+ constructor(code, data) {
21
+ super(getMessageByErrorCode(code));
22
+ this.code = code;
23
+ this.data = data;
24
+ }
25
+ }
26
+
27
+ export { LogtoClientError };
package/lib/index.d.ts CHANGED
@@ -1,61 +1,25 @@
1
- import { Requester, Prompt, IdTokenClaims, UserInfoResponse, InteractionMode } from "@logto/js";
2
- import { Nullable, NormalizeKeyPaths } from "@silverhand/essentials";
3
- export type StorageKey = 'idToken' | 'refreshToken' | 'accessToken' | 'signInSession';
4
- export type Storage = {
5
- getItem(key: StorageKey): Promise<Nullable<string>>;
6
- setItem(key: StorageKey, value: string): Promise<void>;
7
- removeItem(key: StorageKey): Promise<void>;
8
- };
9
- type Navigate = (url: string) => void;
10
- export type ClientAdapter = {
11
- requester: Requester;
12
- storage: Storage;
13
- navigate: Navigate;
14
- generateState: () => string;
15
- generateCodeVerifier: () => string;
16
- generateCodeChallenge: (codeVerifier: string) => Promise<string>;
17
- };
18
- declare const logtoClientErrorCodes: Readonly<{
19
- sign_in_session: {
20
- invalid: string;
21
- not_found: string;
22
- };
23
- not_authenticated: "Not authenticated.";
24
- fetch_user_info_failed: "Unable to fetch user info. The access token may be invalid.";
25
- }>;
26
- export type LogtoClientErrorCode = NormalizeKeyPaths<typeof logtoClientErrorCodes>;
27
- export class LogtoClientError extends Error {
28
- code: LogtoClientErrorCode;
29
- data: unknown;
30
- constructor(code: LogtoClientErrorCode, data?: unknown);
31
- }
32
- export type LogtoConfig = {
33
- endpoint: string;
34
- appId: string;
35
- appSecret?: string;
36
- scopes?: string[];
37
- resources?: string[];
38
- prompt?: Prompt;
39
- };
40
- export type AccessToken = {
41
- token: string;
42
- scope: string;
43
- expiresAt: number;
44
- };
45
- export const isLogtoSignInSessionItem: (data: unknown) => data is LogtoSignInSessionItem;
46
- export const isLogtoAccessTokenMap: (data: unknown) => data is Record<string, AccessToken>;
47
- export type LogtoSignInSessionItem = {
48
- redirectUri: string;
49
- codeVerifier: string;
50
- state: string;
51
- };
52
- export const createRequester: (fetchFunction: typeof fetch) => Requester;
1
+ import type { IdTokenClaims, UserInfoResponse, InteractionMode } from '@logto/js';
2
+ import type { Nullable } from '@silverhand/essentials';
3
+ import type { ClientAdapter } from './adapter';
4
+ import type { AccessToken, LogtoConfig, LogtoSignInSessionItem } from './types';
53
5
  export type { IdTokenClaims, LogtoErrorCode, UserInfoResponse, InteractionMode } from '@logto/js';
54
6
  export { LogtoError, OidcError, Prompt, LogtoRequestError, ReservedScope, UserScope, } from '@logto/js';
7
+ export * from './errors';
8
+ export type { Storage, StorageKey, ClientAdapter } from './adapter';
9
+ export { createRequester } from './utils';
10
+ export * from './types';
55
11
  export default class LogtoClient {
56
12
  protected readonly logtoConfig: LogtoConfig;
57
- protected readonly getOidcConfig: () => Promise<import("@silverhand/essentials").KeysToCamelCase<import("@logto/js").OidcConfigSnakeCaseResponse>>;
58
- protected readonly getJwtVerifyGetKey: () => Promise<import("jose/dist/types/types").GetKeyFunction<import("jose").JWSHeaderParameters, import("jose").FlattenedJWSInput>>;
13
+ protected readonly getOidcConfig: () => Promise<import("@silverhand/essentials").KeysToCamelCase<{
14
+ authorization_endpoint: string;
15
+ token_endpoint: string;
16
+ userinfo_endpoint: string;
17
+ end_session_endpoint: string;
18
+ revocation_endpoint: string;
19
+ jwks_uri: string;
20
+ issuer: string;
21
+ }>>;
22
+ protected readonly getJwtVerifyGetKey: () => Promise<(protectedHeader?: import("jose").JWSHeaderParameters | undefined, token?: import("jose").FlattenedJWSInput | undefined) => Promise<import("jose").KeyLike>>;
59
23
  protected readonly adapter: ClientAdapter;
60
24
  protected readonly accessTokenMap: Map<string, AccessToken>;
61
25
  constructor(logtoConfig: LogtoConfig, adapter: ClientAdapter);
@@ -71,6 +35,13 @@ export default class LogtoClient {
71
35
  signOut(postLogoutRedirectUri?: string): Promise<void>;
72
36
  protected getSignInSession(): Promise<Nullable<LogtoSignInSessionItem>>;
73
37
  protected setSignInSession(logtoSignInSessionItem: Nullable<LogtoSignInSessionItem>): Promise<void>;
38
+ private setIdToken;
39
+ private setRefreshToken;
40
+ private getAccessTokenByRefreshToken;
41
+ private _getOidcConfig;
42
+ private _getJwtVerifyGetKey;
43
+ private verifyIdToken;
44
+ private saveCodeToken;
45
+ private saveAccessTokenMap;
46
+ private loadAccessTokenMap;
74
47
  }
75
-
76
- //# sourceMappingURL=index.d.ts.map