|
| 1 | +import { AppConfigDataClient } from "@aws-sdk/client-appconfigdata"; |
| 2 | +import middy from "@middy/core"; |
| 3 | +import { getInternal } from "@middy/util"; |
| 4 | +import type { Context as LambdaContext } from "aws-lambda"; |
| 5 | +import { captureAWSv3Client } from "aws-xray-sdk"; |
| 6 | +import { expect, test } from "tstyche"; |
| 7 | +import appConfig, { appConfigReq } from "."; |
| 8 | + |
| 9 | +const options = { |
| 10 | + AwsClient: AppConfigDataClient, |
| 11 | + awsClientOptions: { |
| 12 | + credentials: { |
| 13 | + secretAccessKey: "secret", |
| 14 | + sessionToken: "token", |
| 15 | + accessKeyId: "key", |
| 16 | + }, |
| 17 | + }, |
| 18 | + awsClientAssumeRole: "some-role", |
| 19 | + awsClientCapture: captureAWSv3Client, |
| 20 | + disablePrefetch: true, |
| 21 | + cacheKey: "some-key", |
| 22 | + cacheExpiry: 60 * 60 * 5, |
| 23 | + setToContext: false, |
| 24 | +} as const; |
| 25 | + |
| 26 | +test("should use default options", () => { |
| 27 | + expect(appConfig()).type.toBe< |
| 28 | + middy.MiddlewareObj<unknown, any, Error, LambdaContext> |
| 29 | + >(); |
| 30 | +}); |
| 31 | + |
| 32 | +test("should use with setToContext: false", () => { |
| 33 | + expect( |
| 34 | + appConfig({ |
| 35 | + ...options, |
| 36 | + fetchData: { |
| 37 | + config: { |
| 38 | + ApplicationIdentifier: "app", |
| 39 | + ConfigurationProfileIdentifier: "configId", |
| 40 | + EnvironmentIdentifier: "development", |
| 41 | + }, |
| 42 | + }, |
| 43 | + setToContext: false, |
| 44 | + }), |
| 45 | + ).type.toBe< |
| 46 | + middy.MiddlewareObj< |
| 47 | + unknown, |
| 48 | + any, |
| 49 | + Error, |
| 50 | + LambdaContext, |
| 51 | + Record<"config", unknown> |
| 52 | + > |
| 53 | + >(); |
| 54 | +}); |
| 55 | + |
| 56 | +test("should use with setToContext: true", () => { |
| 57 | + expect( |
| 58 | + appConfig({ |
| 59 | + ...options, |
| 60 | + fetchData: { |
| 61 | + config: { |
| 62 | + ApplicationIdentifier: "app", |
| 63 | + ConfigurationProfileIdentifier: "configId", |
| 64 | + EnvironmentIdentifier: "development", |
| 65 | + }, |
| 66 | + }, |
| 67 | + setToContext: true, |
| 68 | + }), |
| 69 | + ).type.toBe< |
| 70 | + middy.MiddlewareObj< |
| 71 | + unknown, |
| 72 | + any, |
| 73 | + Error, |
| 74 | + LambdaContext & Record<"config", unknown>, |
| 75 | + Record<"config", unknown> |
| 76 | + > |
| 77 | + >(); |
| 78 | +}); |
| 79 | + |
| 80 | +// @ts-expect-error - fetchData must be an object |
| 81 | +appConfig({ ...options, fetchData: "not-an-object" }); |
| 82 | + |
| 83 | +appConfig({ |
| 84 | + ...options, |
| 85 | + fetchData: { |
| 86 | + config: { |
| 87 | + // @ts-expect-error - Application must be a string |
| 88 | + ApplicationIdentifier: 123, |
| 89 | + // @ts-expect-error - Configuration must be a string |
| 90 | + ConfigurationProfileIdentifier: 123, |
| 91 | + // @ts-expect-error - Environment must be a string |
| 92 | + EnvironmentIdentifier: 123, |
| 93 | + }, |
| 94 | + }, |
| 95 | +}); |
| 96 | + |
| 97 | +appConfig({ |
| 98 | + ...options, |
| 99 | + fetchData: { |
| 100 | + // @ts-expect-error - config must contain Application, ClientId, Configuration and Environment |
| 101 | + config: {}, |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +const handler = middy(async (event: {}, context: LambdaContext) => { |
| 106 | + return await Promise.resolve({}); |
| 107 | +}); |
| 108 | + |
| 109 | +test("should return the correct config", () => { |
| 110 | + handler |
| 111 | + .use( |
| 112 | + appConfig({ |
| 113 | + fetchData: { |
| 114 | + config: appConfigReq<{ |
| 115 | + config1: string; |
| 116 | + config2: string; |
| 117 | + config3: number; |
| 118 | + }>({ |
| 119 | + ApplicationIdentifier: "app", |
| 120 | + ConfigurationProfileIdentifier: "configId", |
| 121 | + EnvironmentIdentifier: "development", |
| 122 | + }), |
| 123 | + }, |
| 124 | + setToContext: true, |
| 125 | + }), |
| 126 | + ) |
| 127 | + .before(async (request) => { |
| 128 | + expect(request.context.config).type.toBe<{ |
| 129 | + config1: string; |
| 130 | + config2: string; |
| 131 | + config3: number; |
| 132 | + }>(); |
| 133 | + const data = await getInternal("config", request); |
| 134 | + expect(data.config.config1).type.toBe<string>(); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +test("should return the correct config using the getInternal method", () => { |
| 139 | + handler |
| 140 | + .use( |
| 141 | + appConfig({ |
| 142 | + fetchData: { |
| 143 | + config: appConfigReq<{ |
| 144 | + config1: string; |
| 145 | + config2: string; |
| 146 | + config3: number; |
| 147 | + }>({ |
| 148 | + ApplicationIdentifier: "app", |
| 149 | + ConfigurationProfileIdentifier: "configId", |
| 150 | + EnvironmentIdentifier: "development", |
| 151 | + }), |
| 152 | + }, |
| 153 | + setToContext: false, |
| 154 | + }), |
| 155 | + ) |
| 156 | + .before(async (request) => { |
| 157 | + const data = await getInternal("config", request); |
| 158 | + expect(data.config).type.toBe<{ |
| 159 | + config1: string; |
| 160 | + config2: string; |
| 161 | + config3: number; |
| 162 | + }>(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments