Skip to content

Commit

Permalink
feat(account-elements): add initial account api (#6684)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun authored Oct 16, 2024
1 parent 7925dfd commit 96d47db
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/account-elements/src/api/index.ts
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>();
}
}
2 changes: 2 additions & 0 deletions packages/account-elements/src/index.ts
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';
2 changes: 2 additions & 0 deletions packages/account-elements/src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createComponent } from '@lit/react';

import { LogtoAccountProvider } from './index.js';

export type { AccessTokenFetcher } from './api/index.js';

export const createReactComponents = (react: Parameters<typeof createComponent>[0]['react']) => {
return {
LogtoAccountProvider: createComponent({
Expand Down

0 comments on commit 96d47db

Please sign in to comment.