Skip to content

Commit

Permalink
chore: update S3Client to support region from options
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Aug 18, 2024
1 parent 5537eb7 commit 8c2e7c6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 36 deletions.
12 changes: 8 additions & 4 deletions src/providers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { S3Client } from '@aws-sdk/client-s3'

import { InvalidConfigError } from '../errors'
import { Storage, custom } from '../schemas/input'
import { Storage, customOptions } from '../schemas/input'
import { sync, syncMetadata, syncTags } from '../storages/s3/buckets'
import {
ISyncCloudStorage,
Expand Down Expand Up @@ -33,7 +33,7 @@ export abstract class BaseProvider implements ISyncCloudStorage {
constructor(options: ProviderOptions, servicePath: string) {
this.options = options
this.servicePath = servicePath
const validatedConfig = custom.safeParse(options)
const validatedConfig = customOptions.safeParse(options)
const { success } = validatedConfig

if (!success) {
Expand Down Expand Up @@ -62,8 +62,12 @@ export abstract class BaseProvider implements ISyncCloudStorage {
const endpoint = this.options.syncCloudStorage.offline
? this.options.syncCloudStorage.endpoint
: undefined

return new S3Client({ endpoint })
const region =
this.options.syncCloudStorage.region ?? process.env.AWS_REGION
return new S3Client({
endpoint,
...(region ? { region } : {}),
})
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/providers/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ export class SyncCloudStorageServerless extends BaseProvider {
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention
_logging?: ServerlessPlugin.Logging
) {
const pluginOptions = serverless.service.custom.syncCloudStorage
super(

Check warning on line 18 in src/providers/serverless.ts

View check run for this annotation

Codecov / codecov/patch

src/providers/serverless.ts#L17-L18

Added lines #L17 - L18 were not covered by tests
{
syncCloudStorage: {
...serverless.service.custom.syncCloudStorage,
region:
serverless.service.custom.syncCloudStorage.region ||
options['region'],
...pluginOptions,
region: options['region'] || pluginOptions.region,
},
},
serverless.service.serverless.config.servicePath
Expand Down
8 changes: 4 additions & 4 deletions src/schemas/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const storage = z.object({

const storages = z.array(storage).min(1)

const custom = z.object({
const customOptions = z.object({
syncCloudStorage: z.object({
disabled: z.boolean().optional().default(false),
storages: storages,
Expand All @@ -43,9 +43,9 @@ const custom = z.object({
}),
})

type Custom = z.infer<typeof custom>
type CustomOptions = z.infer<typeof customOptions>
type Storage = z.infer<typeof storage>
type Tags = z.infer<typeof tags>

export type { Custom, Storage, Tags }
export { custom, tags, storage, storages, objectCannedACLs }
export type { CustomOptions, Storage, Tags }
export { customOptions, tags, storage, storages, objectCannedACLs }
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { DeletedObject, Tag, _Object } from '@aws-sdk/client-s3'
import { Construct } from 'constructs'
import Serverless from 'serverless'

import { Custom, Storage } from './schemas/input'
import { CustomOptions, Storage } from './schemas/input'

export type IServerlessProvider = ReturnType<
typeof Serverless.prototype.getProvider
>

export type IServerless = {
service: {
custom: Custom
custom: CustomOptions
serverless: {
config: {
servicePath: string
Expand Down Expand Up @@ -110,9 +110,9 @@ export interface ISyncCloudStorage {

export type Provider = IServerless | Construct

export type ServerlessOptions = Custom
export type CdkOptions = Custom['syncCloudStorage']
export type ProviderOptions = Custom
export type ServerlessOptions = CustomOptions
export type CdkOptions = CustomOptions['syncCloudStorage']
export type ProviderOptions = CustomOptions
export interface LogActivity {
name: string
status: 'fulfilled' | 'rejected'
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/serverless.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Custom } from '../../src/schemas/input'
import { CustomOptions } from '../../src/schemas/input'
import { IServerless } from '../../src/types'

const serverlessGetProviderSpy = jest.fn().mockReturnValue({
Expand All @@ -10,7 +10,7 @@ const serverlessGetProviderSpy = jest.fn().mockReturnValue({
})

export const getServerlessMock = (
inputCustom: Custom,
inputCustom: CustomOptions,
servicePath: string
): IServerless => ({
service: {
Expand Down
29 changes: 17 additions & 12 deletions test/schemas/input.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { faker } from '@faker-js/faker'

import { Custom, Storage, objectCannedACLs } from '../../src/schemas/input'
import {
CustomOptions,
Storage,
objectCannedACLs,
} from '../../src/schemas/input'
import { DeepPartial } from '../../src/types'

export const sampleStoragePatterns = {
Expand Down Expand Up @@ -33,7 +37,7 @@ const generateStorage = (overrides: Partial<Storage> = {}): Storage => ({
...overrides,
})

const createBaseInputFixture = (): Required<Custom> => ({
const createBaseInputFixture = (): Required<CustomOptions> => ({
syncCloudStorage: {
disabled: faker.datatype.boolean(),
storages: [generateStorage()],
Expand Down Expand Up @@ -62,7 +66,7 @@ const createCustomInputFixture = ({
region = process.env.AWS_REGION || '',
silent = false,
storageOverrides = {},
}: FixtureOptions = {}): Custom => ({
}: FixtureOptions = {}): CustomOptions => ({
syncCloudStorage: {
disabled: false,
endpoint,
Expand All @@ -81,17 +85,18 @@ const createCustomInputFixture = ({
},
})

export const createValidInputFixture = (options: FixtureOptions = {}): Custom =>
createCustomInputFixture(options)
export const createValidInputFixture = (
options: FixtureOptions = {}
): CustomOptions => createCustomInputFixture(options)

export const createValidCdkInputFixture = (
options: FixtureOptions = {}
): Custom['syncCloudStorage'] =>
): CustomOptions['syncCloudStorage'] =>
createValidInputFixture(options).syncCloudStorage

export const createValidInputFixtureWithACLBucketOwner = (
options: FixtureOptions = {}
): Required<Custom> =>
): Required<CustomOptions> =>
createCustomInputFixture({
...options,
storageOverrides: {
Expand All @@ -102,7 +107,7 @@ export const createValidInputFixtureWithACLBucketOwner = (

export const createValidInputFixtureWithTags = (
options: FixtureOptions = {}
): Required<Custom> =>
): Required<CustomOptions> =>
createCustomInputFixture({
...options,
storageOverrides: {
Expand All @@ -113,7 +118,7 @@ export const createValidInputFixtureWithTags = (

export const createValidInputFixtureWithMetadata = (
options: FixtureOptions = {}
): Required<Custom> =>
): Required<CustomOptions> =>
createCustomInputFixture({
...options,
storageOverrides: {
Expand All @@ -122,19 +127,19 @@ export const createValidInputFixtureWithMetadata = (
},
})

export const createValidDisabledInputFixture = (): Required<Custom> => ({
export const createValidDisabledInputFixture = (): Required<CustomOptions> => ({
...createBaseInputFixture(),
syncCloudStorage: {
...createBaseInputFixture().syncCloudStorage,
disabled: true,
},
})

export const createValidInputFileFixture = (): Required<Custom> =>
export const createValidInputFileFixture = (): Required<CustomOptions> =>
createBaseInputFixture()

export const createInvalidInputFixture = (
additionalProps: DeepPartial<Custom> = {}
additionalProps: DeepPartial<CustomOptions> = {}
) => ({
syncCloudStorage: {
disabled: 'true',
Expand Down
6 changes: 3 additions & 3 deletions test/schemas/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import {
createInvalidInputFixture,
createValidInputFileFixture,
} from './input.fixture'
import { custom } from '../../src/schemas/input'
import { customOptions } from '../../src/schemas/input'

describe('Input Custom Schema', () => {
it('should validate the generated fake data', () => {
const input = createValidInputFileFixture()
const result = custom.safeParse(input)
const result = customOptions.safeParse(input)

expect(result.success).toBe(true)
})

it('should throw an error if the data is invalid', () => {
const invalidInput = createInvalidInputFixture()
const result = custom.safeParse(invalidInput)
const result = customOptions.safeParse(invalidInput)

expect(result.success).toBe(false)
})
Expand Down
4 changes: 2 additions & 2 deletions test/storages/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Local File Provider', () => {
syncCloudStorage: {
storages: [storage],
},
} = createValidInputFixture(patterns)
} = createValidInputFixture({ patterns })
const expectedFiles = [
'test/assets/giraffe-multiple/README.md',
'test/assets/giraffe-multiple/sub/README.md',
Expand All @@ -29,7 +29,7 @@ describe('Local File Provider', () => {
syncCloudStorage: {
storages: [storage],
},
} = createValidInputFixture(patterns)
} = createValidInputFixture({ patterns })
const expectedFiles = ['test/assets/giraffe-multiple/sub/README.md']
const filesToUpload = await getLocalFiles(patterns, storage, process.cwd())
const filesToUploadKeys = filesToUpload.map((file) => file.key)
Expand Down

0 comments on commit 8c2e7c6

Please sign in to comment.