Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the aws integration for the BikeTag API! #234

Open
wants to merge 1 commit into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,856 changes: 3,916 additions & 1,940 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"convert": "tsoa spec-and-routes"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.654.0",
"@sanity/client": "2.25.1-feature-image-file-input-refactor.150",
"axios": "^0.21.1",
"axios-cache-adapter": "^2.7.3",
Expand Down
49 changes: 49 additions & 0 deletions src/aws/getTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
GetObjectCommand,
ListObjectsCommand,

Check warning on line 3 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

'ListObjectsCommand' is defined but never used
S3Client,
} from '@aws-sdk/client-s3'
import { getTagsPayload } from '../common/payloads'
import { BikeTagApiResponse } from '../common/types'
import { Tag } from '../common/schema'
import { AvailableApis, HttpStatusCode } from '../common/enums'
import TinyCache from 'tinycache'

export async function getTags(
client: S3Client,
payload: getTagsPayload,
cache?: typeof TinyCache

Check warning on line 15 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

'cache' is defined but never used
): Promise<BikeTagApiResponse<Tag[]>> {
let error
let success = true
const tags = []

if (payload.tagnumbers?.length) {
payload.tagnumbers.forEach(async (tagNumber: number) => {
client.send(
new GetObjectCommand({
Bucket: 'biketag-portland',
Key: `biketag-portland-${tagNumber}`,
})
)
})
} else if (payload.slugs?.length) {
const imagePromises: Promise<Tag>[] = []

Check warning on line 31 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

'imagePromises' is assigned a value but never used
success = true
} else {
if (albumInfo.success) {

Check failure on line 34 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

Cannot find name 'albumInfo'.
albumImages = albumInfo.data?.images ?? []

Check failure on line 35 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

Cannot find name 'albumImages'.

Check failure on line 35 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

Cannot find name 'albumInfo'.
} else {
success = false
error = albumInfo.data

Check failure on line 38 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

Cannot find name 'albumInfo'.
}
}

return {
data: sortTags(tags, payload.sort, payload.limit, payload.time),

Check failure on line 43 in src/aws/getTags.ts

View workflow job for this annotation

GitHub Actions / Test on node 18.x

Cannot find name 'sortTags'.
success,
error,
source: AvailableApis[AvailableApis.imgur],
status: success ? HttpStatusCode.Ok : HttpStatusCode.BadRequest,
}
}
1 change: 1 addition & 0 deletions src/aws/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getTags } from './getTags'
29 changes: 29 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
Credentials,
BikeTagApiResponse,
ImgurCredentials,
AWSCredentials,
SanityCredentials,
RequireAtLeastOne,
BikeTagCredentials,
Expand Down Expand Up @@ -49,12 +50,15 @@ import {
assignBikeTagConfiguration,
isImgurCredentials,
isSanityCredentials,
isAWSCredentials,
isBikeTagCredentials,
isBikeTagApiReady,
isAWSApiReady,
isSanityApiReady,
isImgurApiReady,
createBikeTagCredentials,
createImgurCredentials,
createAWSCredentials,
createSanityCredentials,
} from './common/methods'
import {
Expand All @@ -65,10 +69,12 @@ import {

import * as BikeTagExpressions from './common/expressions'
import * as BikeTagGetters from './common/getters'
import * as awsApi from './aws'
import * as sanityApi from './sanity'
import * as imgurApi from './imgur'
import * as biketagApi from './biketag'

import { S3Client } from '@aws-sdk/client-s3'
import { ImgurClient } from 'imgur'
import sanityClient, { SanityClient } from '@sanity/client'

Expand Down Expand Up @@ -96,6 +102,8 @@ export class BikeTagClient extends EventEmitter {

protected imgurClient?: ImgurClient
protected sanityClient?: SanityClient
protected awsClient?: S3Client
protected awsConfig?: AWSCredentials
protected sanityConfig?: SanityCredentials
protected imgurConfig?: ImgurCredentials
protected biketagConfig?: BikeTagCredentials
Expand Down Expand Up @@ -333,6 +341,10 @@ export class BikeTagClient extends EventEmitter {
client = this.sanityClient
api = sanityApi
break
case AvailableApis.aws:
client = this.awsClient
api = awsApi
break
case AvailableApis.imgur:
client = this.imgurClient
api = imgurApi
Expand Down Expand Up @@ -366,6 +378,12 @@ export class BikeTagClient extends EventEmitter {
(!method || !!sanityApi[method])
) {
return AvailableApis.sanity
} else if (
this.awsConfig &&
this.awsClient &&
(!method || !!awsApi[method])
) {
return AvailableApis.aws
} else if (
this.biketagConfig &&
isBikeTagCredentials(this.biketagConfig) &&
Expand Down Expand Up @@ -431,6 +449,13 @@ export class BikeTagClient extends EventEmitter {
) {
this.imgurClient = new ImgurClient(config.imgur)
}
if (
config.aws &&
isAWSCredentials(config.aws) &&
isAWSApiReady(config.aws)
) {
this.awsClient = new S3Client(config.aws)
}
if (
config.sanity &&
isSanityCredentials(config.sanity) &&
Expand Down Expand Up @@ -481,6 +506,9 @@ export class BikeTagClient extends EventEmitter {
case AvailableApis.imgur:
createCredentialsMethod = createImgurCredentials
break
case AvailableApis.aws:
createCredentialsMethod = createAWSCredentials
break
case AvailableApis.sanity:
createCredentialsMethod = createSanityCredentials
break
Expand Down Expand Up @@ -510,6 +538,7 @@ export class BikeTagClient extends EventEmitter {
if (reInitialize) {
const initializeConfig: BikeTagConfiguration = {
biketag: undefined,
aws: undefined,
imgur: undefined,
sanity: undefined,
}
Expand Down
1 change: 1 addition & 0 deletions src/common/enums.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// **************************** BikeTag Enums ************************************** ///
export enum AvailableApis {
biketag = 'biketag',
aws = 'aws',
imgur = 'imgur',
sanity = 'sanity',
}
Expand Down
40 changes: 40 additions & 0 deletions src/common/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AccessToken,
ClientKey,
ImgurCredentials,
AWSCredentials,
SanityCredentials,
Credentials,
BikeTagCredentials,
Expand Down Expand Up @@ -101,6 +102,18 @@ export const isSanityCredentials = (
return credentials?.projectId !== undefined
}

export const isAWSCredentials = (credentials: AWSCredentials): boolean => {
return credentials?.region !== undefined
}

export const isAWSApiReady = (credentials: AWSCredentials): ApiAvailability => {
if (credentials.region !== undefined) {
return credentials.region !== undefined ? 3 : 1
}

return 0
}

export const isSanityApiReady = (
credentials: SanityCredentials
): ApiAvailability => {
Expand Down Expand Up @@ -229,6 +242,26 @@ export const assignSanityCredentials = (
return sanityCredentials as SanityCredentials
}

export const createAWSCredentials = (
credentials: Partial<AWSCredentials>,
defaults: Partial<AWSCredentials> = {}
): AWSCredentials => {
return {
region: credentials.region?.length ? credentials.region : defaults.region,
}
}

export const assignAWSCredentials = (
credentials: AWSCredentials,
defaults?: Partial<AWSCredentials>
): AWSCredentials => {
const awsCredentials = isAWSCredentials(credentials as AWSCredentials)
? createAWSCredentials(credentials, defaults)
: defaults

return awsCredentials as AWSCredentials
}

export const createBikeTagCredentials = (
credentials: Partial<BikeTagCredentials>,
defaults: Partial<BikeTagCredentials> = {}
Expand Down Expand Up @@ -280,6 +313,10 @@ export const assignBikeTagConfiguration = (
config as unknown as SanityCredentials,
defaults?.sanity
),
aws: assignAWSCredentials(
config as unknown as AWSCredentials,
defaults?.aws
),
imgur: assignImgurCredentials(
config as unknown as ImgurCredentials,
defaults?.imgur
Expand All @@ -290,6 +327,9 @@ export const assignBikeTagConfiguration = (
configuration.biketag = config.biketag
? { ...parsedConfig.biketag, ...createBikeTagCredentials(config.biketag) }
: parsedConfig.biketag
configuration.aws = config.aws
? { ...parsedConfig.aws, ...createAWSCredentials(config.aws) }
: parsedConfig.aws
configuration.sanity = config.sanity
? { ...parsedConfig.sanity, ...createSanityCredentials(config.sanity) }
: parsedConfig.sanity
Expand Down
15 changes: 13 additions & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export interface SanityCredentials extends SanityAccessToken, SanityProjectId {
password: string
}

export interface AWSRegion {
region: string
}

export interface AWSCredentials {
region: string
}

/// **************************** BikeTag Credential Objects ************************* ///
export interface CommonData {
game: string
Expand All @@ -57,6 +65,7 @@ export interface BikeTagCredentials

export type Credentials = Partial<BikeTagCredentials> &
Partial<SanityCredentials> &
Partial<AWSCredentials> &
Partial<ImgurCredentials>

/// **************************** BikeTag API Objects ******************************** ///
Expand Down Expand Up @@ -105,14 +114,16 @@ export type geopoint = {
/// **************************** BikeTag Configurations ***************************** ///
export type BikeTagConfiguration = {
biketag: BikeTagCredentials
sanity: SanityCredentials
aws: AWSCredentials
imgur: ImgurCredentials
sanity: SanityCredentials
}

export type PartialBikeTagConfiguration = RequireAtLeastOne<{
biketag: Partial<BikeTagCredentials>
sanity: Partial<SanityCredentials>
aws: Partial<AWSCredentials>
imgur: Partial<ImgurCredentials>
sanity: Partial<SanityCredentials>
}>

/// **************************** Gun Data State ************************************* ///
Expand Down
Loading