-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Moments] Add MomentsClientFactory (#116)
* Add getMomentsDefaultClient * PR Comment changes * bump versions
- Loading branch information
Showing
9 changed files
with
132 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/moments/src/client/MomentsClientFactory/MomentsClientFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/moments/src/client/MomentsClientFactory/options/GetMomentsDefaultClientOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 | ||
|