Skip to content

Commit

Permalink
fix(typescript): ssm context is enriched correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Aug 25, 2023
1 parent 548ef2f commit f48f3e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export interface MiddlewareObj<
TEvent = unknown,
TResult = any,
TErr = Error,
TContext extends LambdaContext = LambdaContext
TContext extends LambdaContext = LambdaContext,
TInternal = {}
> {
before?: MiddlewareFn<TEvent, TResult, TErr, TContext>
after?: MiddlewareFn<TEvent, TResult, TErr, TContext>
Expand Down
4 changes: 3 additions & 1 deletion packages/ssm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface Options<AwsSSMClient = SSMClient>
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ExtractSingles<T> = T extends `/${infer _}` ? never : T

export type Internal<TOptions extends Options> = Record<ExtractSingles<keyof TOptions['fetchData']>, JsonValue>

export type Context<TOptions extends Options | undefined> = TOptions extends {
setToContext: true
}
Expand All @@ -23,6 +25,6 @@ export type Context<TOptions extends Options | undefined> = TOptions extends {

declare function ssm<TOptions extends Options> (
options?: TOptions
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>

export default ssm
23 changes: 23 additions & 0 deletions packages/ssm/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SSMClient } from '@aws-sdk/client-ssm'
import { captureAWSv3Client } from 'aws-xray-sdk'
import { expectType } from 'tsd'
import ssm, { Context } from '.'
import { JsonValue } from 'type-fest'

// use with default options
expectType<middy.MiddlewareObj<unknown, any, Error, Context<undefined>>>(ssm())
Expand All @@ -24,3 +25,25 @@ const options = {
expectType<middy.MiddlewareObj<unknown, any, Error, Context<typeof options>>>(
ssm(options)
)


// checks that values fetched are actually enriching the context correctly (#1084)
middy()
.use(
ssm({
fetchData: {
accessToken: '/dev/service_name/access_token', // single value
dbParams: '/dev/service_name/database/', // object of values, key for each path
defaults: '/dev/defaults'
},
setToContext: true
})
)
.before((request) => {
// checks that the context is correctly enriched in before
expectType<Record<"accessToken" | "dbParams" | "defaults", JsonValue>>(request.context)
})
.handler(async (req, context) => {
// checks that the context is correctly enriched in handler
expectType<Record<"accessToken" | "dbParams" | "defaults", JsonValue>>(context)
})

0 comments on commit f48f3e7

Please sign in to comment.