-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
185 additions
and
77 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Subscription } from 'rxjs'; | ||
import type { GraphQLAuthMode } from '@aws-amplify/core/internals/utils'; | ||
|
||
export interface SubscriptionObserver<T> { | ||
next(value: T): void; | ||
error(errorValue: any): void; | ||
} | ||
|
||
export interface EventsChannel { | ||
subscribe( | ||
observer: SubscriptionObserver<any>, | ||
subOptions?: EventsOptions, | ||
): Subscription; | ||
close(): void; | ||
} | ||
|
||
export type ResolvedGraphQLAuthModes = Exclude<GraphQLAuthMode, 'identityPool'>; | ||
|
||
export interface EventsOptions { | ||
authMode?: GraphQLAuthMode; | ||
authToken?: string; | ||
} | ||
|
||
export interface PublishedEvent { | ||
identifier: string; | ||
index: number; | ||
} | ||
|
||
export interface PublishResponse { | ||
failed: PublishedEvent[]; | ||
successful: PublishedEvent[]; | ||
} |
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,81 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { Amplify } from '@aws-amplify/core'; | ||
import { | ||
DocumentType, | ||
GraphQLAuthMode, | ||
} from '@aws-amplify/core/internals/utils'; | ||
|
||
import type { ResolvedGraphQLAuthModes } from './types'; | ||
|
||
export const normalizeAuth = ( | ||
explicitAuthMode: GraphQLAuthMode | undefined, | ||
defaultAuthMode: ResolvedGraphQLAuthModes, | ||
): ResolvedGraphQLAuthModes => { | ||
if (!explicitAuthMode) { | ||
return defaultAuthMode; | ||
} | ||
|
||
if (explicitAuthMode === 'identityPool') { | ||
return 'iam'; | ||
} | ||
|
||
return explicitAuthMode; | ||
}; | ||
|
||
export const configure = () => { | ||
const config = Amplify.getConfig() as any; | ||
|
||
// TODO - get this correct | ||
const eventsConfig = config.API?.GraphQL?.events ?? config.data?.events; | ||
|
||
if (!eventsConfig) { | ||
throw new Error( | ||
'Amplify configuration is missing. Have you called Amplify.configure()', | ||
); | ||
} | ||
|
||
const configAuthMode = normalizeAuth( | ||
eventsConfig.defaultAuthMode ?? eventsConfig.default_authorization_type, | ||
'apiKey', | ||
); | ||
|
||
const options = { | ||
appSyncGraphqlEndpoint: eventsConfig.url, | ||
region: eventsConfig.region ?? eventsConfig.aws_region, | ||
authenticationType: configAuthMode, | ||
apiKey: eventsConfig.apiKey ?? eventsConfig.api_key, | ||
}; | ||
|
||
return options; | ||
}; | ||
|
||
/** | ||
* Event API expects and array of JSON strings | ||
* | ||
* @param events - JSON-serializable value or an array of values | ||
* @returns array of JSON strings | ||
*/ | ||
export const serializeEvents = ( | ||
events: DocumentType | DocumentType[], | ||
): string[] => { | ||
if (Array.isArray(events)) { | ||
return events.map((ev, idx) => { | ||
const eventJson = JSON.stringify(ev); | ||
if (eventJson === undefined) { | ||
throw new Error( | ||
`Event must be a valid JSON value. Received ${ev} at index ${idx}`, | ||
); | ||
} | ||
|
||
return eventJson; | ||
}); | ||
} | ||
|
||
const eventJson = JSON.stringify(events); | ||
if (eventJson === undefined) { | ||
throw new Error(`Event must be a valid JSON value. Received ${events}`); | ||
} | ||
|
||
return [eventJson]; | ||
}; |
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