Skip to content

Commit

Permalink
feat: support value config
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaz committed Nov 13, 2020
1 parent c8e49c0 commit 0013bff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
26 changes: 26 additions & 0 deletions packages/sls-env/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,32 @@ describe('serverless environment', () => {
.start({}, {})
})

it('creates config from value', () => {
type AppConfig = {
message: string
}

return environment<Handler<EmptyEvent, EmptyContext, void>, AppConfig, never, void>()
.config({ message: 'hello' })
.app(({ config }) => {
expect(config.message).toBe('hello')
})
.start({}, {})
})

it('creates config from Promise', () => {
type AppConfig = {
message: string
}

return environment<Handler<EmptyEvent, EmptyContext, void>, AppConfig, never, void>()
.config(Promise.resolve({ message: 'hello' }))
.app(({ config }) => {
expect(config.message).toBe('hello')
})
.start({}, {})
})

it.todo('create config from object factory')
})

Expand Down
15 changes: 10 additions & 5 deletions packages/sls-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type PayloadConstructor<H extends Handler<any, any, any>, R> = (event: Parameter

type SuccessHandler<I, O> = (i: I) => O

type FunctionConstructor<C> = () => C | Promise<C>

type ConfigConstructor<C> = FunctionConstructor<C> | Promise<C> | C
// EventType & ContextType => LambdaResult - that needs to be just a handler
// D - dependencies
// C - config
Expand All @@ -87,9 +90,7 @@ export type SlsEnvironment<
...func: [Function] | [ObjectUnionDependencies<DependentyType>] | TupleUnionDependencies<DependentyType>
) => SlsEnvironment<H, ConfigType, DependentyType, PayloadType, R>
logger: (logger: Logger) => SlsEnvironment<H, ConfigType, DependentyType, PayloadType, R>
config: (
config: () => ConfigType | Promise<ConfigType>
) => SlsEnvironment<H, ConfigType, DependentyType, PayloadType, R>
config: (config: ConfigConstructor<ConfigType>) => SlsEnvironment<H, ConfigType, DependentyType, PayloadType, R>
payload: (
payloadConstructor: PayloadConstructor<H, PayloadType>
) => SlsEnvironment<H, ConfigType, DependentyType, PayloadType, R>
Expand Down Expand Up @@ -148,8 +149,12 @@ export const environment = <
successHandler() {
return this
},
config(appConfigConstructor) {
config = Promise.resolve(appConfigConstructor())
config(constructor) {
if (typeof constructor === 'function' && constructor instanceof Function) {
config = Promise.resolve(constructor())
} else {
config = Promise.resolve(constructor)
}
return this
},
// maybe it will be just easier to configure that through
Expand Down

0 comments on commit 0013bff

Please sign in to comment.