From 023c57771ac94ffc550ec8514fd89075c9f8ae71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Nicol=C3=A1s=20Negro=20Caino?= Date: Fri, 3 May 2024 11:05:52 -0300 Subject: [PATCH] [Moments] Add MomentsClientFactory (#116) * Add getMomentsDefaultClient * PR Comment changes * bump versions --- packages/drops/package.json | 6 +- packages/moments/package.json | 6 +- .../MomentsClientFactory.ts | 52 +++++++++++++++++ .../options/GetMomentsDefaultClientOptions.ts | 58 +++++++++++++++++++ packages/moments/src/index.ts | 1 + packages/poaps/package.json | 6 +- packages/providers/package.json | 4 +- packages/utils/package.json | 2 +- yarn.lock | 18 +++--- 9 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 packages/moments/src/client/MomentsClientFactory/MomentsClientFactory.ts create mode 100644 packages/moments/src/client/MomentsClientFactory/options/GetMomentsDefaultClientOptions.ts diff --git a/packages/drops/package.json b/packages/drops/package.json index a4fba4af..4b8e744b 100644 --- a/packages/drops/package.json +++ b/packages/drops/package.json @@ -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", @@ -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" } } diff --git a/packages/moments/package.json b/packages/moments/package.json index 0b4ff9d2..3ccc864d 100644 --- a/packages/moments/package.json +++ b/packages/moments/package.json @@ -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", @@ -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": { diff --git a/packages/moments/src/client/MomentsClientFactory/MomentsClientFactory.ts b/packages/moments/src/client/MomentsClientFactory/MomentsClientFactory.ts new file mode 100644 index 00000000..fba9a7ef --- /dev/null +++ b/packages/moments/src/client/MomentsClientFactory/MomentsClientFactory.ts @@ -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, + ); + } +} diff --git a/packages/moments/src/client/MomentsClientFactory/options/GetMomentsDefaultClientOptions.ts b/packages/moments/src/client/MomentsClientFactory/options/GetMomentsDefaultClientOptions.ts new file mode 100644 index 00000000..7065727b --- /dev/null +++ b/packages/moments/src/client/MomentsClientFactory/options/GetMomentsDefaultClientOptions.ts @@ -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; + }; +} diff --git a/packages/moments/src/index.ts b/packages/moments/src/index.ts index 4358aa03..8ae5db7b 100644 --- a/packages/moments/src/index.ts +++ b/packages/moments/src/index.ts @@ -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'; diff --git a/packages/poaps/package.json b/packages/poaps/package.json index 3645181d..26e09c91 100644 --- a/packages/poaps/package.json +++ b/packages/poaps/package.json @@ -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", @@ -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" diff --git a/packages/providers/package.json b/packages/providers/package.json index 1d7351c9..37aff9a1 100644 --- a/packages/providers/package.json +++ b/packages/providers/package.json @@ -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", @@ -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" }, diff --git a/packages/utils/package.json b/packages/utils/package.json index 0f0050b8..d1ff7554 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index e060ab4c..5dff79bc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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 @@ -912,16 +912,16 @@ __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/providers@0.2.5, @poap-xyz/providers@workspace:packages/providers": +"@poap-xyz/providers@*, @poap-xyz/providers@0.2.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 @@ -929,7 +929,7 @@ __metadata: languageName: unknown linkType: soft -"@poap-xyz/utils@*, @poap-xyz/utils@0.2.5, @poap-xyz/utils@workspace:packages/utils": +"@poap-xyz/utils@*, @poap-xyz/utils@0.2.6, @poap-xyz/utils@workspace:packages/utils": version: 0.0.0-use.local resolution: "@poap-xyz/utils@workspace:packages/utils" languageName: unknown