Skip to content

Commit

Permalink
chore: make Authentication required in all submodules - doesn't mean …
Browse files Browse the repository at this point in the history
…client is always authenticated (#805)
  • Loading branch information
krzysu authored Jan 24, 2024
1 parent 6a4df1b commit 1d169dd
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 89 deletions.
27 changes: 0 additions & 27 deletions packages/client/src/helpers/requireAuthHeaders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ const context: LensContext = {
};

describe(`Given the "${requireAuthHeaders.name}" helper`, () => {
describe(`when the ${Authentication.name} is not provided`, () => {
it(`should return failure with ${NotAuthenticatedError.name}`, async () => {
const result = await requireAuthHeaders(undefined, (header) => {
return Promise.resolve(header);
});

expect(result.isFailure()).toBeTruthy();
expect(() => result.unwrap()).toThrow(NotAuthenticatedError);
});
});

describe(`when the ${Authentication.name} is available but not authenticated`, () => {
it(`should return failure with ${NotAuthenticatedError.name}`, async () => {
const authentication = new Authentication(context);
Expand All @@ -35,20 +24,4 @@ describe(`Given the "${requireAuthHeaders.name}" helper`, () => {
expect(() => result.unwrap()).toThrow(NotAuthenticatedError);
});
});

// describeAuthenticatedScenario()((getTestSetup) => {
// describe(`when the ${Authentication.name} is available and authenticated`, () => {
// it(`should provide the authentication header`, async () => {
// const { authentication } = getTestSetup();
// const result = await requireAuthHeaders(authentication, (header) => {
// return Promise.resolve(header);
// });

// expect(result.isSuccess()).toBeTruthy();
// expect(result.unwrap()).toEqual({
// authorization: expect.any(String),
// });
// });
// });
// });
});
9 changes: 2 additions & 7 deletions packages/client/src/helpers/requireAuthHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { failure, PromiseResult, success } from '@lens-protocol/shared-kernel';
import { PromiseResult, success } from '@lens-protocol/shared-kernel';

import type { Authentication } from '../authentication';
import { CredentialsExpiredError, NotAuthenticatedError } from '../errors';

type Handler<Val> = (headers: Record<string, string>) => Promise<Val>;

export async function requireAuthHeaders<Val>(
authentication: Authentication | undefined,
authentication: Authentication,
handler: Handler<Val>,
): PromiseResult<Val, CredentialsExpiredError | NotAuthenticatedError> {
// TODO revisit this might be actually defined all the time
if (!authentication) {
return failure(new NotAuthenticatedError());
}

const result = await authentication.getRequestHeader();

if (result.isFailure()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/submodules/explore/Explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Explore {

constructor(
private readonly context: LensContext,
authentication?: Authentication,
authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);
this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
Expand Down
5 changes: 1 addition & 4 deletions packages/client/src/submodules/feed/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ import { FeedItemFragment, getSdk, Sdk } from './graphql/feed.generated';
* @group LensClient Modules
*/
export class Feed {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/client/src/submodules/invites/Invites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ import { InvitedResultFragment, Sdk, getSdk } from './graphql/invites.generated'
* @group LensClient Modules
*/
export class Invites {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication?: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/submodules/modules/Modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import {
* @group LensClient Modules
*/
export class Modules {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(context: LensContext, authentication: Authentication) {
constructor(
context: LensContext,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/submodules/momoka/Momoka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Momoka {

constructor(
private readonly context: LensContext,
authentication?: Authentication,
authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);
this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/submodules/nfts/Nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import {
* @group LensClient Modules
*/
export class Nfts {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(context: LensContext, authentication?: Authentication) {
constructor(
context: LensContext,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@ export type NotificationFragment =
* @group LensClient Modules
*/
export class Notifications {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/client/src/submodules/profile/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ import { FetchProfileOptions } from './types';
* @group LensClient Modules
*/
export class Profile {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication?: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/client/src/submodules/publication/Publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,14 @@ import { FetchPublicationOptions, RequestOverwrites } from './types';
* @group LensClient Modules
*/
export class Publication {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication?: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import {
* @group LensClient Modules
*/
export class Actions {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(context: LensContext, authentication?: Authentication) {
constructor(
context: LensContext,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ import { getSdk, Sdk } from './graphql/bookmarks.generated';
* @group LensClient Modules
*/
export class Bookmarks {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication?: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import { Sdk, getSdk } from './graphql/notInterested.generated';
* @group LensClient Modules
*/
export class NotInterested {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(context: LensContext, authentication?: Authentication) {
constructor(
context: LensContext,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ import { getSdk, ProfileWhoReactedResultFragment, Sdk } from './graphql/reaction
* @group LensClient Modules
*/
export class Reactions {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication?: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/submodules/revenue/Revenue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Revenue {

constructor(
private readonly context: LensContext,
authentication?: Authentication,
authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);
this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/submodules/search/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Search {

constructor(
private readonly context: LensContext,
authentication?: Authentication,
authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);
this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/submodules/transaction/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export class TransactionPollingError extends Error {
* @group LensClient Modules
*/
export class Transaction {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(context: LensContext, authentication: Authentication) {
constructor(
context: LensContext,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/client/src/submodules/wallet/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ import {
* @group LensClient Modules
*/
export class Wallet {
private readonly authentication: Authentication | undefined;
private readonly sdk: Sdk;

constructor(
private readonly context: LensContext,
authentication?: Authentication,
private readonly authentication: Authentication,
) {
const client = new FetchGraphQLClient(context);

this.sdk = getSdk(client, sdkAuthHeaderWrapper(authentication));
this.authentication = authentication;
}

/**
Expand Down

0 comments on commit 1d169dd

Please sign in to comment.