Skip to content

Commit

Permalink
fix: allows to define Origin header from RN integrations (#847)
Browse files Browse the repository at this point in the history
* fix: allows to define Origin header from RN integrations

* Fix types issue

* Address code review comment
  • Loading branch information
cesarenaldi committed Feb 19, 2024
1 parent 7cd6bad commit 87f6da5
Show file tree
Hide file tree
Showing 37 changed files with 127 additions and 105 deletions.
8 changes: 8 additions & 0 deletions .changeset/swift-readers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@lens-protocol/api-bindings": patch
"@lens-protocol/react-native": patch
"@lens-protocol/react-web": patch
"@lens-protocol/react": patch
---

**fix:** allows to define Origin header from React Native integrations
28 changes: 16 additions & 12 deletions packages/api-bindings/src/apollo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import { ILogger } from '@lens-protocol/shared-kernel';
import { LENS_API_MINIMAL_SUPPORTED_VERSION } from '../constants';
import { SafeApolloClient } from './SafeApolloClient';
import { createSnapshotCache } from './cache';
import { createLensCache, QueryParams } from './cache/createLensCache';
import { createLensLink, IAccessTokenStorage, createAuthLink, createSnapshotLink } from './links';
import { QueryParams, createLensCache } from './cache/createLensCache';
import {
AuthLinkArgs,
IAccessTokenStorage,
createAuthLink,
createLensLink,
createSnapshotLink,
} from './links';

export { demoSnapshotPoll, snapshotPoll } from './cache/utils/ContentInsight';
export type { ContentInsightMatcher } from './cache/utils/ContentInsight';
export { snapshotPoll, demoSnapshotPoll } from './cache/utils/ContentInsight';

export type ApolloClientConfig = {
accessTokenStorage: IAccessTokenStorage;
export type ApolloClientConfig = AuthLinkArgs & {
uri: string;
logger: ILogger;
pollingInterval: number;
Expand All @@ -20,12 +25,13 @@ export type ApolloClientConfig = {

export function createLensApolloClient({
accessTokenStorage,
origin,
uri,
logger,
pollingInterval,
queryParams,
}: ApolloClientConfig) {
const authLink = createAuthLink(accessTokenStorage);
const authLink = createAuthLink({ accessTokenStorage, origin });

const httpLink = createLensLink({
uri,
Expand Down Expand Up @@ -66,11 +72,9 @@ export function createSnapshotApolloClient({ uri }: SnapshotApolloClientConfig)
});
}

export type { IAccessTokenStorage };
export { defaultQueryParams } from './cache/createLensCache';
export type { QueryParams };
export type { IGraphQLClient } from './IGraphQLClient';
export * from './errors';
export * from './cache/transactions';
export { defaultQueryParams } from './cache/createLensCache';
export * from './cache/session';
export type { SafeApolloClient };
export * from './cache/transactions';
export * from './errors';
export type { IAccessTokenStorage, QueryParams, SafeApolloClient };
10 changes: 8 additions & 2 deletions packages/api-bindings/src/apollo/links/AuthLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ class RefreshTokensLink extends ApolloLink {
}
}

export function createAuthLink(accessTokenStorage: IAccessTokenStorage) {
export type AuthLinkArgs = {
accessTokenStorage: IAccessTokenStorage;
origin?: string;
};

export function createAuthLink({ accessTokenStorage, origin }: AuthLinkArgs) {
const tokenRefreshLink = new RefreshTokensLink(accessTokenStorage);

const authHeaderLink = setContext((_, prevContext) => {
Expand All @@ -126,7 +131,8 @@ export function createAuthLink(accessTokenStorage: IAccessTokenStorage) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
headers: {
...('headers' in prevContext && prevContext.headers),
authorization: `Bearer ${token}`,
Authorization: `Bearer ${token}`,
...(origin && { Origin: origin }),
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function setupTestScenario(mocks: ReadonlyArray<Response>) {
return new ApolloClient({
cache: new InMemoryCache(),
link: from([
createAuthLink(accessTokenStorage),
createAuthLink({ accessTokenStorage }),
createHttpLink({
fetch,
uri: 'http://localhost:4000/graphql',
Expand Down
53 changes: 28 additions & 25 deletions packages/react/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,44 @@ export { SupportedFiatType } from '@lens-protocol/api-bindings';

export interface IBindings extends ISignerBinding, IProviderBinding {}

/**
* Internal configuration
*
* @internal
*/
export type RequiredConfig = {
bindings: IBindings;

environment: EnvironmentConfig;

logger: ILogger;

debug: boolean;

storage: IStorageProvider | IObservableStorageProvider;

params: QueryParams;

origin?: string;
};

/**
* `<BaseProvider>` configuration
*
* @internal
*/
export type BaseConfig = {
/**
* Provides integration with the ethers.js Signer and Provider
*/
bindings: IBindings;
/**
* The environment to use. See {@link production} or {@link development}.
*/

environment: EnvironmentConfig;
/**
* The logger interface to use when something worth logging happens
*
* @defaultValue `ConsoleLogger`, an internal implementation of `ILogger` interface that logs to the console
*/

logger?: ILogger;
/**
* Enable debug mode. Disable gas estimation on self-funded transactions.
*
* @defaultValue `false`
*/

debug?: boolean;
/**
* The storage provider to use.
*
* If a implementation of {@link IObservableStorageProvider} is provided,
* the provider will be used to subscribe to changes in the storage.
*/

storage: IStorageProvider | IObservableStorageProvider;
/**
* The common query params allow you customize some aspect of the returned data.
*/

params?: QueryParams;

origin?: string;
};
7 changes: 4 additions & 3 deletions packages/react/src/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { CredentialsFactory } from './authentication/adapters/CredentialsFactory
import { CredentialsGateway } from './authentication/adapters/CredentialsGateway';
import { CredentialsStorage } from './authentication/adapters/CredentialsStorage';
import { LogoutPresenter } from './authentication/adapters/LogoutPresenter';
import { BaseConfig } from './config';
import { BaseConfig, RequiredConfig } from './config';
import { createInboxKeyStorage, DisableConversationsGateway } from './inbox';
import { IProfileCacheManager } from './profile/adapters/IProfileCacheManager';
import { ProfileCacheManager } from './profile/infrastructure/ProfileCacheManager';
Expand Down Expand Up @@ -62,7 +62,7 @@ export function createSharedDependencies(userConfig: BaseConfig): SharedDependen
logger: new ConsoleLogger(),
params: defaultQueryParams,
};
const config: Required<BaseConfig> = { ...defaultConfig, ...userConfig };
const config: RequiredConfig = { ...defaultConfig, ...userConfig };

// auth api
const anonymousApolloClient = createAuthApolloClient({
Expand All @@ -84,6 +84,7 @@ export function createSharedDependencies(userConfig: BaseConfig): SharedDependen
accessTokenStorage,
pollingInterval: config.environment.timings.pollingInterval,
logger: config.logger,
origin: config.origin,
});

// infrastructure
Expand Down Expand Up @@ -187,7 +188,7 @@ export type SharedDependencies = {
accessTokenStorage: AccessTokenStorage;
activeWallet: ActiveWallet;
apolloClient: SafeApolloClient;
config: BaseConfig;
config: RequiredConfig;
credentialsFactory: CredentialsFactory;
credentialsGateway: CredentialsGateway;
inboxKeyStorage: IStorage<string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Amount, ChainType, Data, EvmAddress } from '@lens-protocol/shared-kerne
import { BigNumberish, BytesLike } from 'ethers';
import { v4 } from 'uuid';

import { BaseConfig } from '../../config';
import { RequiredConfig } from '../../config';
import { ITransactionRequest } from '../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../wallet/adapters/IProviderFactory';
import { Eip1559GasPriceEstimator, TransactionExecutionSpeed } from './Eip1559GasPriceEstimator';
Expand Down Expand Up @@ -50,7 +50,7 @@ export abstract class AbstractContractCallGateway<TRequest extends AnyTransactio
implements IPaidTransactionGateway<TRequest>
{
constructor(
protected readonly config: BaseConfig,
protected readonly config: RequiredConfig,
private readonly providerFactory: IProviderFactory,
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { permissionlessCreator } from '@lens-protocol/blockchain-bindings';
import { CreateProfileRequest } from '@lens-protocol/domain/use-cases/profile';
import { Data, EvmAddress } from '@lens-protocol/shared-kernel';

import { BaseConfig } from '../../config';
import { RequiredConfig } from '../../config';
import { IProviderFactory } from '../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from './AbstractContractCallGateway';

export class CreateProfileTransactionGateway extends AbstractContractCallGateway<CreateProfileRequest> {
constructor(
private apolloClient: SafeApolloClient,
config: BaseConfig,
config: RequiredConfig,
providerFactory: IProviderFactory,
) {
super(config, providerFactory);
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/transactions/adapters/OpenActionGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { ChainType, Data, PromiseResult, success } from '@lens-protocol/shared-kernel';
import { v4 } from 'uuid';

import { BaseConfig } from '../../config';
import { RequiredConfig } from '../../config';
import { UnsignedProtocolCall } from '../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from './AbstractContractCallGateway';
Expand All @@ -50,7 +50,7 @@ export class OpenActionGateway
IDelegatedTransactionGateway<DelegableOpenActionRequest>
{
constructor(
config: BaseConfig,
config: RequiredConfig,
private readonly apolloClient: SafeApolloClient,
private readonly transactionFactory: ITransactionFactory<OpenActionRequest>,
providerFactory: IProviderFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { BigNumber, constants, providers, utils } from 'ethers';
import { mock } from 'jest-mock-extended';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { mockIProviderFactory } from '../../../wallet/adapters/__helpers__/mocks';
import { UnsignedContractCallTransaction } from '../AbstractContractCallGateway';
import { ApproveTransactionGateway } from '../ApproveTransactionGateway';
Expand All @@ -23,7 +23,7 @@ function setupApproveTransactionGateway({
request: TokenAllowanceRequest;
provider: providers.JsonRpcProvider;
}) {
const config = mock<BaseConfig>();
const config = mock<RequiredConfig>();
const providerFactory = mockIProviderFactory({
chainType: request.amount.asset.chainType,
provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { deployMockContract } from 'ethereum-waffle';
import { BigNumber, Wallet, utils } from 'ethers';
import { mock } from 'jest-mock-extended';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { staging } from '../../../environments';
import { mockIProviderFactory } from '../../../wallet/adapters/__helpers__/mocks';
import { UnsignedContractCallTransaction } from '../AbstractContractCallGateway';
Expand Down Expand Up @@ -49,7 +49,7 @@ async function setupTestScenario(mocks: MockedResponse[] = []) {
// eslint-disable-next-line @typescript-eslint/await-thenable
await mockContract.mock.getProfileWithHandleCreationPrice!.returns(utils.parseEther('10'));

const config = mock<BaseConfig>({ environment: staging });
const config = mock<RequiredConfig>({ environment: staging });

return new CreateProfileTransactionGateway(apolloClient, config, providerFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { mockEvmAddress } from '@lens-protocol/shared-kernel/mocks';
import { providers } from 'ethers';
import { mock } from 'jest-mock-extended';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { UnsignedProtocolCall } from '../../../wallet/adapters/ConcreteWallet';
import { mockIProviderFactory } from '../../../wallet/adapters/__helpers__/mocks';
import { UnsignedContractCallTransaction } from '../AbstractContractCallGateway';
Expand All @@ -54,7 +54,7 @@ function setupTestScenario({
apolloClient: SafeApolloClient;
provider?: providers.JsonRpcProvider;
}) {
const config = mock<BaseConfig>();
const config = mock<RequiredConfig>();
const transactionFactory = mockITransactionFactory();

const providerFactory = mockIProviderFactory({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { ChainType, Data, PromiseResult, success } from '@lens-protocol/shared-kernel';
import { v4 } from 'uuid';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { UnsignedProtocolCall } from '../../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from '../AbstractContractCallGateway';
Expand All @@ -35,7 +35,7 @@ export class BlockProfilesGateway
ISignedOnChainGateway<BlockProfilesRequest>
{
constructor(
config: BaseConfig,
config: RequiredConfig,
providerFactory: IProviderFactory,
private readonly apolloClient: SafeApolloClient,
private readonly transactionFactory: ITransactionFactory<BlockProfilesRequest>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import { ChainType, Data, PromiseResult, success } from '@lens-protocol/shared-kernel';
import { v4 } from 'uuid';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { UnsignedProtocolCall } from '../../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from '../AbstractContractCallGateway';
Expand Down Expand Up @@ -76,7 +76,7 @@ export class FollowProfileGateway
implements IDelegatedTransactionGateway<FreeFollowRequest>, ISignedOnChainGateway<FollowRequest>
{
constructor(
config: BaseConfig,
config: RequiredConfig,
providerFactory: IProviderFactory,
private readonly apolloClient: SafeApolloClient,
private readonly transactionFactory: ITransactionFactory<FreeFollowRequest>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { ChainType, Data, PromiseResult, success } from '@lens-protocol/shared-kernel';
import { v4 } from 'uuid';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { UnsignedProtocolCall } from '../../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from '../AbstractContractCallGateway';
Expand All @@ -35,7 +35,7 @@ export class LinkHandleGateway
ISignedOnChainGateway<LinkHandleRequest>
{
constructor(
config: BaseConfig,
config: RequiredConfig,
providerFactory: IProviderFactory,
private readonly apolloClient: SafeApolloClient,
private readonly transactionFactory: ITransactionFactory<LinkHandleRequest>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { ChainType, Data, PromiseResult, success } from '@lens-protocol/shared-kernel';
import { v4 } from 'uuid';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { UnsignedProtocolCall } from '../../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from '../AbstractContractCallGateway';
Expand All @@ -35,7 +35,7 @@ export class ProfileMetadataGateway
ISignedOnChainGateway<SetProfileMetadataRequest>
{
constructor(
config: BaseConfig,
config: RequiredConfig,
providerFactory: IProviderFactory,
private readonly apolloClient: SafeApolloClient,
private readonly transactionFactory: ITransactionFactory<SetProfileMetadataRequest>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { ChainType, Data, PromiseResult, failure, success } from '@lens-protocol/shared-kernel';
import { v4 } from 'uuid';

import { BaseConfig } from '../../../config';
import { RequiredConfig } from '../../../config';
import { UnsignedProtocolCall } from '../../../wallet/adapters/ConcreteWallet';
import { IProviderFactory } from '../../../wallet/adapters/IProviderFactory';
import { AbstractContractCallGateway, ContractCallDetails } from '../AbstractContractCallGateway';
Expand All @@ -35,7 +35,7 @@ export class UnblockProfilesGateway
ISignedOnChainGateway<UnblockProfilesRequest>
{
constructor(
config: BaseConfig,
config: RequiredConfig,
providerFactory: IProviderFactory,
private readonly apolloClient: SafeApolloClient,
private readonly transactionFactory: ITransactionFactory<UnblockProfilesRequest>,
Expand Down
Loading

0 comments on commit 87f6da5

Please sign in to comment.