Skip to content

Commit

Permalink
chore: initial refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Dec 13, 2024
1 parent 1397934 commit cbb8287
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 81 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
CreateAccountWithUsernameResult,
EnableSignlessResult,
MuteRequest,
Paginated,
RecommendAccountRequest,
RemoveSignlessResult,
ReportAccountRequest,
Expand Down Expand Up @@ -52,7 +53,6 @@ import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient, SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Fetch an Account.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/actions/accountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
AddAccountManagerRequest,
AddAccountManagerResult,
HideManagedAccountRequest,
Paginated,
RemoveAccountManagerRequest,
RemoveAccountManagerResult,
UnhideManagedAccountRequest,
Expand All @@ -22,7 +23,6 @@ import type { ResultAsync } from '@lens-protocol/types';

import type { SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Fetch Account Managers.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/actions/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AuthenticatedSession,
AuthenticatedSessionsRequest,
Paginated,
RefreshRequest,
RefreshResult,
RevokeAuthenticationRequest,
Expand All @@ -20,7 +21,6 @@ import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient, SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Get the AuthenticatedSession associated with the authenticated Account.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/actions/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
CreateFollowRequest,
CreateUnfollowRequest,
FollowResult,
Paginated,
UnfollowResult,
} from '@lens-protocol/graphql';
import {
Expand All @@ -23,7 +24,6 @@ import type { FollowStatusRequest } from '@lens-protocol/graphql';
import type { FollowStatusResult } from '@lens-protocol/graphql';
import type { AnyClient, SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Follow an Account
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/actions/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Notification, NotificationsRequest } from '@lens-protocol/graphql';
import type { Notification, NotificationsRequest, Paginated } from '@lens-protocol/graphql';
import { NotificationsQuery } from '@lens-protocol/graphql';
import type { ResultAsync } from '@lens-protocol/types';

import type { SessionClient } from '../clients';
import type { UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Fetch notifications for the authenticated Account.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/actions/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AccountPostReaction,
ActionInfo,
AnyPost,
Paginated,
Post,
PostActionsRequest,
PostBookmarksRequest,
Expand All @@ -20,7 +21,6 @@ import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient, SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Fetch a Post.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/actions/timeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
Paginated,
Post,
TimelineHighlightsRequest,
TimelineItem,
Expand All @@ -9,7 +10,6 @@ import type { ResultAsync } from '@lens-protocol/types';

import type { AnyClient } from '../clients';
import type { UnexpectedError } from '../errors';
import type { Paginated } from '../types';

/**
* Fetch timeline from an account.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/actions/username.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
AssignUsernameToAccountResult,
CreateUsernameRequest,
CreateUsernameResult,
Paginated,
UnassignUsernameFromAccountRequest,
UnassignUsernameToAccountResult,
Username,
Expand All @@ -22,7 +23,6 @@ import type { SessionClient } from '../clients';
import type { UnauthenticatedError, UnexpectedError } from '../errors';

import type { AnyClient } from '../clients';
import type { Paginated } from '../types';

/**
* Create a username
Expand Down
49 changes: 29 additions & 20 deletions packages/client/src/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
AuthenticationChallenge,
ChallengeRequest,
SignedAuthChallenge,
StandardData,
} from '@lens-protocol/graphql';
import type { Credentials, IStorage } from '@lens-protocol/storage';
import { createCredentialsStorage } from '@lens-protocol/storage';
Expand Down Expand Up @@ -42,7 +43,6 @@ import {
hasExtensionCode,
} from './errors';
import { decodeIdToken } from './tokens';
import type { StandardData } from './types';
import { delay } from './utils';

function takeValue<T>({
Expand All @@ -59,14 +59,14 @@ export type LoginParams = ChallengeRequest & {
signMessage: SignMessage;
};

abstract class AbstractClient<TError> {
abstract class AbstractClient<TContext extends Context, TError> {
protected readonly urql: UrqlClient;

protected readonly logger: Logger;

protected readonly credentials: IStorage<Credentials>;

protected constructor(public readonly context: Context) {
protected constructor(public readonly context: TContext) {
this.credentials = createCredentialsStorage(context.storage, context.environment.name);

this.logger = getLogger(this.constructor.name);
Expand Down Expand Up @@ -99,12 +99,12 @@ abstract class AbstractClient<TError> {
/**
* Asserts that the client is a {@link PublicClient}.
*/
public abstract isPublicClient(): this is PublicClient;
public abstract isPublicClient(): this is PublicClient<TContext>;

/**
* that the client is a {@link SessionClient}.
*/
public abstract isSessionClient(): this is SessionClient;
public abstract isSessionClient(): this is SessionClient<TContext>;

public abstract query<TValue, TVariables extends AnyVariables>(
document: TypedDocumentNode<StandardData<TValue>, TVariables>,
Expand Down Expand Up @@ -144,13 +144,16 @@ abstract class AbstractClient<TError> {
/**
* A client to interact with the public access queries and mutations of the Lens GraphQL API.
*/
export class PublicClient extends AbstractClient<UnexpectedError> {
export class PublicClient<TContext extends Context = Context> extends AbstractClient<
TContext,
UnexpectedError
> {
/**
* The current session client.
*
* This could be the {@link PublicClient} itself if the user is not authenticated, or a {@link SessionClient} if the user is authenticated.
*/
public currentSession: PublicClient | SessionClient = this;
public currentSession: PublicClient<TContext> | SessionClient<TContext> = this;

/**
* Create a new instance of the {@link PublicClient}.
Expand All @@ -165,7 +168,7 @@ export class PublicClient extends AbstractClient<UnexpectedError> {
* @param options - The options to configure the client.
* @returns The new instance of the client.
*/
static create(options: ClientConfig): PublicClient {
static create(options: ClientConfig): PublicClient<Context> {
return new PublicClient(configureContext(options));
}

Expand All @@ -181,7 +184,7 @@ export class PublicClient extends AbstractClient<UnexpectedError> {
*/
authenticate(
request: SignedAuthChallenge,
): ResultAsync<SessionClient, AuthenticationError | UnexpectedError> {
): ResultAsync<SessionClient<TContext>, AuthenticationError | UnexpectedError> {
return this.mutation(AuthenticateMutation, { request })
.andThen((result) => {
if (result.__typename === 'AuthenticationTokens') {
Expand All @@ -205,7 +208,7 @@ export class PublicClient extends AbstractClient<UnexpectedError> {
signMessage,
...request
}: LoginParams): ResultAsync<
SessionClient,
SessionClient<TContext>,
AuthenticationError | SigningError | UnexpectedError
> {
return this.challenge(request)
Expand All @@ -232,7 +235,7 @@ export class PublicClient extends AbstractClient<UnexpectedError> {
*
* @returns The session client if available.
*/
resumeSession(): ResultAsync<SessionClient, UnauthenticatedError> {
resumeSession(): ResultAsync<SessionClient<TContext>, UnauthenticatedError> {
return ResultAsync.fromSafePromise(this.credentials.get()).andThen((credentials) => {
if (!credentials) {
return new UnauthenticatedError('No credentials found').asResultAsync();
Expand All @@ -244,14 +247,14 @@ export class PublicClient extends AbstractClient<UnexpectedError> {
/**
* {@inheritDoc AbstractClient.isPublicClient}
*/
public override isPublicClient(): this is PublicClient {
public override isPublicClient(): this is PublicClient<TContext> {
return true;
}

/**
* {@inheritDoc AbstractClient.isSessionClient}
*/
public override isSessionClient(): this is SessionClient {
public override isSessionClient(): this is SessionClient<TContext> {
return false;
}

Expand Down Expand Up @@ -289,12 +292,15 @@ export class PublicClient extends AbstractClient<UnexpectedError> {
*
* @privateRemarks Intentionally not exported.
*/
class SessionClient extends AbstractClient<UnauthenticatedError | UnexpectedError> {
public get parent(): PublicClient {
class SessionClient<TContext extends Context = Context> extends AbstractClient<
TContext,
UnauthenticatedError | UnexpectedError
> {
public get parent(): PublicClient<TContext> {
return this._parent;
}

constructor(private readonly _parent: PublicClient) {
constructor(private readonly _parent: PublicClient<TContext>) {
super(_parent.context);
_parent.currentSession = this;
}
Expand Down Expand Up @@ -328,14 +334,14 @@ class SessionClient extends AbstractClient<UnauthenticatedError | UnexpectedErro
/**
* {@inheritDoc AbstractClient.isPublicClient}
*/
public override isPublicClient(): this is PublicClient {
public override isPublicClient(): this is PublicClient<TContext> {
return false;
}

/**
* {@inheritDoc AbstractClient.isSessionClient}
*/
public override isSessionClient(): this is SessionClient {
public override isSessionClient(): this is SessionClient<TContext> {
return true;
}

Expand Down Expand Up @@ -431,7 +437,8 @@ class SessionClient extends AbstractClient<UnauthenticatedError | UnexpectedErro
...base,
headers: {
...base.headers,
Authorization: `Bearer ${credentials.accessToken}`,
'x-access-token': credentials.accessToken,
// Authorization: `Bearer ${this.tokens.accessToken}`,
},
};
}
Expand All @@ -456,4 +463,6 @@ export type { SessionClient };
/**
* Any client that can be used to interact with the Lens GraphQL API.
*/
export type AnyClient = PublicClient | SessionClient;
export type AnyClient<TContext extends Context = Context> =
| PublicClient<TContext>
| SessionClient<TContext>;
18 changes: 0 additions & 18 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { PaginatedResultInfo } from '@lens-protocol/graphql';

import type {
SelfFundedTransactionRequest,
SponsoredTransactionRequest,
Expand Down Expand Up @@ -56,19 +54,3 @@ export type DelegableOperationHandler<T extends string, E extends string> = (
export type OperationHandler<T extends string, E extends string> =
| RestrictedOperationHandler<E>
| DelegableOperationHandler<T, E>;

/**
* A standardized data object.
*
* All GQL operations should alias their results to `value` to ensure interoperability
* with this client interface.
*/
export type StandardData<T> = { value: T };

/**
* A paginated list of items.
*/
export type Paginated<T> = {
items: readonly T[];
pageInfo: PaginatedResultInfo;
};
17 changes: 17 additions & 0 deletions packages/graphql/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { PaginatedResultInfo } from './fragments';

/**
* A paginated list of items.
*/
export type Paginated<T> = {
items: readonly T[];
pageInfo: PaginatedResultInfo;
};

/**
* A standardized data object.
*
* All GQL operations should alias their results to `value` to ensure interoperability
* with this client interface.
*/
export type StandardData<T> = { value: T };
Loading

0 comments on commit cbb8287

Please sign in to comment.