-
-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(account-elements): add initial account api (#6684)
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { type UserProfileResponse } from '@logto/schemas'; | ||
import ky, { type KyInstance } from 'ky'; | ||
|
||
export type AccessTokenFetcher = () => Promise<string>; | ||
|
||
/** | ||
* The API client for the Logto account elements | ||
* | ||
* Used internally to interact with Account-related backend APIs, including the Profile API. | ||
*/ | ||
export class LogtoAccountApi { | ||
private readonly ky: KyInstance; | ||
|
||
constructor( | ||
/** | ||
* The endpoint of the Logto instance. | ||
*/ | ||
logtoEndpoint: string, | ||
/** | ||
* The function to fetch the account access token. | ||
*/ | ||
accessTokenFetcher: AccessTokenFetcher | ||
) { | ||
this.ky = ky.create({ | ||
prefixUrl: logtoEndpoint, | ||
hooks: { | ||
beforeRequest: [ | ||
async (request) => { | ||
request.headers.set('Authorization', `Bearer ${await accessTokenFetcher()}`); | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
|
||
async fetchUserProfile() { | ||
return this.ky.get('api/profile').json<UserProfileResponse>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export type { AccessTokenFetcher } from './api/index.js'; | ||
|
||
export * from './providers/logto-account-provider.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters