Skip to content

Commit

Permalink
add feed queries
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Aug 11, 2023
1 parent f2a5dfb commit 12afc11
Show file tree
Hide file tree
Showing 8 changed files with 564 additions and 9 deletions.
5 changes: 2 additions & 3 deletions packages/client/src/explore/Explore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Authentication } from '../authentication';
import type { LensConfig } from '../consts/config';
import { FetchGraphQLClient } from '../graphql/FetchGraphQLClient';
import type { ProfileFragment } from '../graphql/fragments.generated';
import type { PublicationFragment } from '../graphql/types';
import type { PostFragment, ProfileFragment, QuoteFragment } from '../graphql/fragments.generated';
import type { ExploreProfilesRequest, ExplorePublicationRequest } from '../graphql/types.generated';
import {
buildImageTransformsFromConfig,
Expand Down Expand Up @@ -34,7 +33,7 @@ export class Explore {
async publications(
request: ExplorePublicationRequest,
observerId?: string,
): Promise<PaginatedResult<PublicationFragment>> {
): Promise<PaginatedResult<PostFragment | QuoteFragment>> {
return provideAuthHeaders(this.authentication, async (headers) => {
return buildPaginatedQueryResult(async (currRequest) => {
const result = await this.sdk.ExplorePublications(
Expand Down
108 changes: 108 additions & 0 deletions packages/client/src/feed/Feed.ts
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);
});
}
}
Loading

0 comments on commit 12afc11

Please sign in to comment.