Skip to content

Commit

Permalink
new mediasets
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Jul 28, 2023
1 parent e479dc2 commit 0de596f
Show file tree
Hide file tree
Showing 33 changed files with 403 additions and 203 deletions.
19 changes: 19 additions & 0 deletions packages/client/src/consts/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { IStorageProvider } from '@lens-protocol/storage';

import { Environment } from './environments';
import { MediaTransform } from './media';

/**
* The media transforms configuration.
*/
export type MediaTransformsConfig = {
publication?: MediaTransform;
profilePicture?: MediaTransform;
profileCover?: MediaTransform;
};

/**
* LensClient configuration
Expand All @@ -14,4 +24,13 @@ export type LensConfig = {
* The storage provider to use.
*/
storage?: IStorageProvider;

/**
* Media returned from the publication and profile queries can be transformed
* to sizes needed by the SDK consuming application.
* To overwrite default transformation values, provide a `mediaTransforms` object.
*
* @see {@link MediaTransformsConfig} for more information
*/
mediaTransforms?: MediaTransformsConfig;
};
7 changes: 0 additions & 7 deletions packages/client/src/consts/defaults.ts

This file was deleted.

34 changes: 34 additions & 0 deletions packages/client/src/consts/media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';

type Percentage = `100%` | `${Digit}${Digit}%` | `${Digit}%`;

type Pixel =
| `${Digit}${Digit}${Digit}${Digit}px`
| `${Digit}${Digit}${Digit}px`
| `${Digit}${Digit}px`;

export type ImageSizeTransform = Pixel | Percentage | 'auto';

export type MediaTransform = {
/**
* Set the transformed image's width. You can use specific size in
* pixels eg. 100px, a percentage eg. 50% or set as 'auto' to be set automatically.
*
* @defaultValue 'auto'
*/
width?: ImageSizeTransform;
/**
* Set the transformed image's height. You can use specific size in
* pixels eg. 100px, a percentage eg. 50% or set as 'auto' to be set automatically.
*
* @defaultValue 'auto'
*/
height?: ImageSizeTransform;
/**
* Set if you want to keep the image's original aspect ratio.
* If explicitly set to false, the image will stretch based on the width and height values.
*
* @defaultValue true
*/
keepAspectRatio?: boolean;
};
24 changes: 10 additions & 14 deletions packages/client/src/explore/Explore.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Authentication } from '../authentication';
import type { LensConfig } from '../consts/config';
import { defaultMediaTransformParams } from '../consts/defaults';
import { FetchGraphQLClient } from '../graphql/FetchGraphQLClient';
import type { ProfileFragment } from '../graphql/fragments.generated';
import type { PublicationFragment } from '../graphql/types';
import type {
ExploreProfilesRequest,
ExplorePublicationRequest,
MediaTransformParams,
} from '../graphql/types.generated';
import { buildPaginatedQueryResult, PaginatedResult, provideAuthHeaders } from '../helpers';
import type { ExploreProfilesRequest, ExplorePublicationRequest } from '../graphql/types.generated';
import {
buildMediaTransformsFromConfig,
buildPaginatedQueryResult,
PaginatedResult,
provideAuthHeaders,
} from '../helpers';
import { getSdk, Sdk } from './graphql/explore.generated';

/**
Expand All @@ -21,7 +21,7 @@ export class Explore {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(config: LensConfig, authentication?: Authentication) {
constructor(private readonly config: LensConfig, authentication?: Authentication) {
const client = new FetchGraphQLClient(config.environment.gqlEndpoint);

this.sdk = getSdk(client);
Expand All @@ -33,7 +33,6 @@ export class Explore {
*
* @param request - Request object for the query
* @param observerId - Optional id of a profile that is the observer for this request
* @param mediaTransformParams - Optional media transform params if you want to optimize media in the response
* @returns Array of {@link PublicationFragment} wrapped in {@link PaginatedResult}
*
* @example
Expand All @@ -48,15 +47,14 @@ export class Explore {
async publications(
request: ExplorePublicationRequest,
observerId?: string,
mediaTransformParams: MediaTransformParams = defaultMediaTransformParams,
): Promise<PaginatedResult<PublicationFragment>> {
return provideAuthHeaders(this.authentication, async (headers) => {
return buildPaginatedQueryResult(async (currRequest) => {
const result = await this.sdk.ExplorePublications(
{
request: currRequest,
observerId,
mediaTransformParams,
...buildMediaTransformsFromConfig(this.config.mediaTransforms),
},
headers,
);
Expand All @@ -71,7 +69,6 @@ export class Explore {
*
* @param request - Request object for the query
* @param observerId - Optional id of a profile that is the observer for this request
* @param mediaTransformParams - Optional media transform params if you want to optimize media in the response
* @returns Array of {@link ProfileFragment} wrapped in {@link PaginatedResult}
*
* @example
Expand All @@ -86,15 +83,14 @@ export class Explore {
async profiles(
request: ExploreProfilesRequest,
observerId?: string,
mediaTransformParams: MediaTransformParams = defaultMediaTransformParams,
): Promise<PaginatedResult<ProfileFragment>> {
return provideAuthHeaders(this.authentication, async (headers) => {
return buildPaginatedQueryResult(async (currRequest) => {
const result = await this.sdk.ExploreProfiles(
{
request: currRequest,
observerId,
mediaTransformParams,
...buildMediaTransformsFromConfig(this.config.mediaTransforms),
},
headers,
);
Expand Down
14 changes: 10 additions & 4 deletions packages/client/src/explore/graphql/explore.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
export type ExplorePublicationsQueryVariables = Types.Exact<{
request: Types.ExplorePublicationRequest;
observerId?: Types.InputMaybe<Types.Scalars['ProfileId']>;
mediaTransformParams: Types.MediaTransformParams;
mediaTransformPublication?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfilePicture?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfileCover?: Types.InputMaybe<Types.MediaTransformParams>;
}>;

export type ExplorePublicationsQuery = {
Expand All @@ -47,7 +49,8 @@ export type ExplorePublicationsQuery = {
export type ExploreProfilesQueryVariables = Types.Exact<{
request: Types.ExploreProfilesRequest;
observerId?: Types.InputMaybe<Types.Scalars['ProfileId']>;
mediaTransformParams: Types.MediaTransformParams;
mediaTransformProfilePicture?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfileCover?: Types.InputMaybe<Types.MediaTransformParams>;
}>;

export type ExploreProfilesQuery = {
Expand All @@ -58,7 +61,9 @@ export const ExplorePublicationsDocument = gql`
query ExplorePublications(
$request: ExplorePublicationRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformPublication: MediaTransformParams = {}
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: explorePublications(request: $request) {
items {
Expand Down Expand Up @@ -86,7 +91,8 @@ export const ExploreProfilesDocument = gql`
query ExploreProfiles(
$request: ExploreProfilesRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: exploreProfiles(request: $request) {
items {
Expand Down
7 changes: 5 additions & 2 deletions packages/client/src/explore/graphql/explore.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
query ExplorePublications(
$request: ExplorePublicationRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformPublication: MediaTransformParams = {}
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: explorePublications(request: $request) {
items {
Expand All @@ -26,7 +28,8 @@ query ExplorePublications(
query ExploreProfiles(
$request: ExploreProfilesRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: exploreProfiles(request: $request) {
items {
Expand Down
24 changes: 10 additions & 14 deletions packages/client/src/feed/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import type { PromiseResult } from '@lens-protocol/shared-kernel';

import type { Authentication } from '../authentication';
import type { LensConfig } from '../consts/config';
import { defaultMediaTransformParams } from '../consts/defaults';
import type { CredentialsExpiredError, NotAuthenticatedError } from '../consts/errors';
import { FetchGraphQLClient } from '../graphql/FetchGraphQLClient';
import type { PublicationFragment } from '../graphql/types';
import type {
FeedHighlightsRequest,
FeedRequest,
MediaTransformParams,
} from '../graphql/types.generated';
import { buildPaginatedQueryResult, PaginatedResult, requireAuthHeaders } from '../helpers';
import type { FeedHighlightsRequest, FeedRequest } from '../graphql/types.generated';
import {
buildMediaTransformsFromConfig,
buildPaginatedQueryResult,
PaginatedResult,
requireAuthHeaders,
} from '../helpers';
import { FeedItemFragment, getSdk, Sdk } from './graphql/feed.generated';

/**
Expand All @@ -23,7 +23,7 @@ export class Feed {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(config: LensConfig, authentication: Authentication) {
constructor(private readonly config: LensConfig, authentication: Authentication) {
const client = new FetchGraphQLClient(config.environment.gqlEndpoint);

this.sdk = getSdk(client);
Expand All @@ -37,7 +37,6 @@ export class Feed {
*
* @param request - Request object for the query
* @param observerId - Optional id of a profile that is the observer for this request
* @param mediaTransformParams - Optional media transform params if you want to optimize media in the response
* @returns {@link PromiseResult} with array of {@link FeedItemFragment} wrapped in {@link PaginatedResult}
*
* @example
Expand All @@ -50,7 +49,6 @@ export class Feed {
async fetch(
request: FeedRequest,
observerId?: string,
mediaTransformParams: MediaTransformParams = defaultMediaTransformParams,
): PromiseResult<
PaginatedResult<FeedItemFragment>,
CredentialsExpiredError | NotAuthenticatedError
Expand All @@ -61,7 +59,7 @@ export class Feed {
{
request: currRequest,
observerId,
mediaTransformParams,
...buildMediaTransformsFromConfig(this.config.mediaTransforms),
},
headers,
);
Expand All @@ -78,7 +76,6 @@ export class Feed {
*
* @param request - Request object for the query
* @param observerId - Optional id of a profile that is the observer for this request
* @param mediaTransformParams - Optional media transform params if you want to optimize media in the response
* @returns {@link PromiseResult} with array of {@link PublicationFragment} wrapped in {@link PaginatedResult}
*
* @example
Expand All @@ -91,7 +88,6 @@ export class Feed {
async fetchHighlights(
request: FeedHighlightsRequest,
observerId?: string,
mediaTransformParams: MediaTransformParams = defaultMediaTransformParams,
): PromiseResult<
PaginatedResult<PublicationFragment>,
CredentialsExpiredError | NotAuthenticatedError
Expand All @@ -102,7 +98,7 @@ export class Feed {
{
request: currRequest,
observerId,
mediaTransformParams,
...buildMediaTransformsFromConfig(this.config.mediaTransforms),
},
headers,
);
Expand Down
16 changes: 12 additions & 4 deletions packages/client/src/feed/graphql/feed.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export type FeedItemFragment = {
export type FeedQueryVariables = Types.Exact<{
request: Types.FeedRequest;
observerId?: Types.InputMaybe<Types.Scalars['ProfileId']>;
mediaTransformParams: Types.MediaTransformParams;
mediaTransformPublication?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfilePicture?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfileCover?: Types.InputMaybe<Types.MediaTransformParams>;
}>;

export type FeedQuery = {
Expand All @@ -78,7 +80,9 @@ export type FeedQuery = {
export type FeedHighlightsQueryVariables = Types.Exact<{
request: Types.FeedHighlightsRequest;
observerId?: Types.InputMaybe<Types.Scalars['ProfileId']>;
mediaTransformParams: Types.MediaTransformParams;
mediaTransformPublication?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfilePicture?: Types.InputMaybe<Types.MediaTransformParams>;
mediaTransformProfileCover?: Types.InputMaybe<Types.MediaTransformParams>;
}>;

export type FeedHighlightsQuery = {
Expand Down Expand Up @@ -168,7 +172,9 @@ export const FeedDocument = gql`
query Feed(
$request: FeedRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformPublication: MediaTransformParams = {}
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: feed(request: $request) {
items {
Expand All @@ -186,7 +192,9 @@ export const FeedHighlightsDocument = gql`
query FeedHighlights(
$request: FeedHighlightsRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformPublication: MediaTransformParams = {}
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: feedHighlights(request: $request) {
items {
Expand Down
8 changes: 6 additions & 2 deletions packages/client/src/feed/graphql/feed.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ fragment FeedItem on FeedItem {
query Feed(
$request: FeedRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformPublication: MediaTransformParams = {}
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: feed(request: $request) {
items {
Expand All @@ -78,7 +80,9 @@ query Feed(
query FeedHighlights(
$request: FeedHighlightsRequest!
$observerId: ProfileId
$mediaTransformParams: MediaTransformParams!
$mediaTransformPublication: MediaTransformParams = {}
$mediaTransformProfilePicture: MediaTransformParams = {}
$mediaTransformProfileCover: MediaTransformParams = {}
) {
result: feedHighlights(request: $request) {
items {
Expand Down
Loading

0 comments on commit 0de596f

Please sign in to comment.