-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
564 additions
and
9 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
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,108 @@ | ||
import type { PromiseResult } from '@lens-protocol/shared-kernel'; | ||
|
||
import type { Authentication } from '../authentication'; | ||
import type { LensConfig } from '../consts/config'; | ||
import type { CredentialsExpiredError, NotAuthenticatedError } from '../consts/errors'; | ||
import { FetchGraphQLClient } from '../graphql/FetchGraphQLClient'; | ||
import type { PostFragment, QuoteFragment } from '../graphql/fragments.generated'; | ||
import type { | ||
FeedHighlightsRequest, | ||
FeedRequest, | ||
PublicationForYouRequest, | ||
} from '../graphql/types.generated'; | ||
import { | ||
buildImageTransformsFromConfig, | ||
buildPaginatedQueryResult, | ||
PaginatedResult, | ||
requireAuthHeaders, | ||
} from '../helpers'; | ||
import { FeedItemFragment, getSdk, Sdk } from './graphql/feed.generated'; | ||
|
||
/** | ||
* Feed is one of the most fundamental element to create a successful social media site. | ||
* | ||
* @group LensClient Modules | ||
*/ | ||
export class Feed { | ||
private readonly authentication: Authentication | undefined; | ||
private readonly sdk: Sdk; | ||
|
||
constructor( | ||
private readonly config: LensConfig, | ||
authentication: Authentication, | ||
) { | ||
const client = new FetchGraphQLClient(config.environment.gqlEndpoint); | ||
|
||
this.sdk = getSdk(client); | ||
this.authentication = authentication; | ||
} | ||
|
||
async fetch( | ||
request: FeedRequest, | ||
observerId?: string, | ||
): PromiseResult< | ||
PaginatedResult<FeedItemFragment>, | ||
CredentialsExpiredError | NotAuthenticatedError | ||
> { | ||
return requireAuthHeaders(this.authentication, async (headers) => { | ||
return buildPaginatedQueryResult(async (currRequest) => { | ||
const result = await this.sdk.Feed( | ||
{ | ||
request: currRequest, | ||
observerId, | ||
...buildImageTransformsFromConfig(this.config.mediaTransforms), | ||
}, | ||
headers, | ||
); | ||
|
||
return result.data.result; | ||
}, request); | ||
}); | ||
} | ||
|
||
async highlights( | ||
request: FeedHighlightsRequest, | ||
observerId?: string, | ||
): PromiseResult< | ||
PaginatedResult<PostFragment | QuoteFragment>, | ||
CredentialsExpiredError | NotAuthenticatedError | ||
> { | ||
return requireAuthHeaders(this.authentication, async (headers) => { | ||
return buildPaginatedQueryResult(async (currRequest) => { | ||
const result = await this.sdk.FeedHighlights( | ||
{ | ||
request: currRequest, | ||
observerId, | ||
...buildImageTransformsFromConfig(this.config.mediaTransforms), | ||
}, | ||
headers, | ||
); | ||
|
||
return result.data.result; | ||
}, request); | ||
}); | ||
} | ||
|
||
async forYou( | ||
request: PublicationForYouRequest, | ||
observerId?: string, | ||
): PromiseResult< | ||
PaginatedResult<PostFragment | QuoteFragment>, | ||
CredentialsExpiredError | NotAuthenticatedError | ||
> { | ||
return requireAuthHeaders(this.authentication, async (headers) => { | ||
return buildPaginatedQueryResult(async (currRequest) => { | ||
const result = await this.sdk.ForYou( | ||
{ | ||
request: currRequest, | ||
observerId, | ||
...buildImageTransformsFromConfig(this.config.mediaTransforms), | ||
}, | ||
headers, | ||
); | ||
|
||
return result.data.result; | ||
}, request); | ||
}); | ||
} | ||
} |
Oops, something went wrong.