Skip to content

Commit

Permalink
[Moments] Add MomentsClientFactory (#116)
Browse files Browse the repository at this point in the history
* Add getMomentsDefaultClient

* PR Comment changes

* bump versions
  • Loading branch information
nacho9900 authored May 3, 2024
1 parent 684a5f0 commit 023c577
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packages/drops/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/drops",
"version": "0.2.5",
"version": "0.2.6",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -29,7 +29,7 @@
"node": ">=18"
},
"dependencies": {
"@poap-xyz/providers": "0.2.5",
"@poap-xyz/utils": "0.2.5"
"@poap-xyz/providers": "0.2.6",
"@poap-xyz/utils": "0.2.6"
}
}
6 changes: 3 additions & 3 deletions packages/moments/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/moments",
"version": "0.2.5",
"version": "0.2.6",
"description": "Moments module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.2.5",
"@poap-xyz/utils": "0.2.5",
"@poap-xyz/providers": "0.2.6",
"@poap-xyz/utils": "0.2.6",
"uuid": "^9.0.0"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MomentsClient } from 'moments';
import {
AuthenticationProvider,
AuthenticationProviderHttp,
PoapCompass,
PoapMomentsApi,
} from '@poap-xyz/providers';
import { GetMomentsDefaultClientOptions } from './options/GetMomentsDefaultClientOptions';

export class MomentsClientFactory {
private static readonly DEFAULT_OAUTH_SERVER = 'https://accounts.poap.tech';

public static getMomentsDefaultClient(
params: GetMomentsDefaultClientOptions,
): MomentsClient {
const compassProvider = new PoapCompass({
apiKey: params.compass.compassApiKey,
baseUrl: params.compass.compassBaseUrl,
});

const authenticationProvider =
MomentsClientFactory.getAuthenticationProvider(params);

const momentsAPI = new PoapMomentsApi({
baseUrl: params.moments.momentsBaseUrl,
authenticationProvider,
});

return new MomentsClient(momentsAPI, compassProvider);
}

private static getAuthenticationProvider(
params: GetMomentsDefaultClientOptions,
): AuthenticationProvider {
if ('authenticationProvider' in params.moments) {
return params.moments.authenticationProvider;
}

if (!params.moments.oAuthCredentials) {
throw new Error(
'Either an authentication provider or an OAuth authentication configuration must be provided for Moments.',
);
}

return new AuthenticationProviderHttp(
params.moments.oAuthCredentials.clientId,
params.moments.oAuthCredentials.clientSecret,
params.moments.oAuthCredentials.oAuthServerDomain ??
MomentsClientFactory.DEFAULT_OAUTH_SERVER,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AuthenticationProvider } from '@poap-xyz/providers';

interface MomentsBaseOptions {
/**
* The base URL for the Moments API.
* @default https://moments.poap.tech
*/
momentsBaseUrl?: string;
}

/**
* Options to create a Moments client with an authentication provider.
*/
export interface MomentsWithAuthenticationProvider extends MomentsBaseOptions {
authenticationProvider: AuthenticationProvider;
}

/**
* Options to create a Moments client with OAuth credentials.
*/
export interface MomentsWithOAuthCredentials extends MomentsBaseOptions {
oAuthCredentials: {
/**
* The client ID for the OAuth server.
*/
clientId: string;
/**
* The client secret for the OAuth server.
*/
clientSecret: string;
/**
* The domain of the OAuth server.
* @default https://accounts.poap.tech
*/
oAuthServerDomain?: string;
};
}

export interface GetMomentsDefaultClientOptions {
/**
* The options to create a Moments client.
*/
moments: MomentsWithAuthenticationProvider | MomentsWithOAuthCredentials;
/**
* The options to create a Compass provider.
*/
compass: {
/**
* The API key for the Compass provider.
*/
compassApiKey: string;
/**
* The base URL for the Compass provider.
* @default https://public.compass.poap.tech/v1/graphql
*/
compassBaseUrl?: string;
};
}
1 change: 1 addition & 0 deletions packages/moments/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { MomentsClient } from './client/MomentsClient';
export { CreateMomentInput } from './client/dtos/create/CreateInput';
export { CreateSteps } from './client/dtos/create/CreateSteps';
export { FetchMomentsInput } from './client/dtos/fetch/FetchMomentsInput';
export { MomentsClientFactory } from './client/MomentsClientFactory/MomentsClientFactory';
6 changes: 3 additions & 3 deletions packages/poaps/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poaps",
"version": "0.2.5",
"version": "0.2.6",
"description": "Poaps module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.2.5",
"@poap-xyz/utils": "0.2.5"
"@poap-xyz/providers": "0.2.6",
"@poap-xyz/utils": "0.2.6"
},
"engines": {
"node": ">=18"
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/providers",
"version": "0.2.5",
"version": "0.2.6",
"description": "Providers module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,7 +26,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/utils": "0.2.5",
"@poap-xyz/utils": "0.2.6",
"axios": "^1.3.5",
"lodash.chunk": "^4.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/utils",
"version": "0.2.5",
"version": "0.2.6",
"description": "Utils module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/drops@workspace:packages/drops"
dependencies:
"@poap-xyz/providers": 0.2.5
"@poap-xyz/utils": 0.2.5
"@poap-xyz/providers": 0.2.6
"@poap-xyz/utils": 0.2.6
languageName: unknown
linkType: soft

Expand All @@ -901,8 +901,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/moments@workspace:packages/moments"
dependencies:
"@poap-xyz/providers": 0.2.5
"@poap-xyz/utils": 0.2.5
"@poap-xyz/providers": 0.2.6
"@poap-xyz/utils": 0.2.6
"@types/uuid": ^9.0.2
uuid: ^9.0.0
languageName: unknown
Expand All @@ -912,24 +912,24 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/poaps@workspace:packages/poaps"
dependencies:
"@poap-xyz/providers": 0.2.5
"@poap-xyz/utils": 0.2.5
"@poap-xyz/providers": 0.2.6
"@poap-xyz/utils": 0.2.6
languageName: unknown
linkType: soft

"@poap-xyz/providers@*, @poap-xyz/[email protected].5, @poap-xyz/providers@workspace:packages/providers":
"@poap-xyz/providers@*, @poap-xyz/[email protected].6, @poap-xyz/providers@workspace:packages/providers":
version: 0.0.0-use.local
resolution: "@poap-xyz/providers@workspace:packages/providers"
dependencies:
"@poap-xyz/utils": 0.2.5
"@poap-xyz/utils": 0.2.6
axios: ^1.3.5
axios-mock-adapter: ^1.21.4
jest-fetch-mock: ^3.0.3
lodash.chunk: ^4.2.0
languageName: unknown
linkType: soft

"@poap-xyz/utils@*, @poap-xyz/[email protected].5, @poap-xyz/utils@workspace:packages/utils":
"@poap-xyz/utils@*, @poap-xyz/[email protected].6, @poap-xyz/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@poap-xyz/utils@workspace:packages/utils"
languageName: unknown
Expand Down

0 comments on commit 023c577

Please sign in to comment.