Skip to content

Commit

Permalink
Merge pull request #62 from lifeomic/fixLambdaHandler
Browse files Browse the repository at this point in the history
fix: lambda-handler flag wasn't working
  • Loading branch information
DavidTanner authored Jul 17, 2023
2 parents d6f90e0 + 80675e1 commit a9ff86d
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 40 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
"clean": "yarn tsc --build --clean; rm -rf tsconfig.build.tsbuildinfo tsconfig.tsbuildinfo"
},
"devDependencies": {
"@aws-sdk/types": "^3.110.0",
"@aws-sdk/types": "^3.370.0",
"@koa/router": "^12.0.0",
"@lifeomic/eslint-config-standards": "^3.0.0",
"@lifeomic/jest-config": "^1.1.3",
"@lifeomic/typescript-config": "^1.0.3",
"@swc/core": "^1.2.207",
"@swc/jest": "^0.2.21",
"@types/aws-lambda": "^8.10.119",
"@types/glob": "^7.2.0",
"@types/jest": "^28.1.3",
"@types/js-yaml": "^4.0.5",
Expand All @@ -51,8 +52,8 @@
"ulid": "^2.3.0"
},
"dependencies": {
"@aws-sdk/client-sts": "^3.121.0",
"@lifeomic/alpha": "^5.1.0",
"@aws-sdk/client-sts": "^3.370.0",
"@lifeomic/alpha": "^5.1.3",
"axios": "^0.27.2",
"dotenv": "^16.0.2",
"glob": "^8.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/alphaProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const alphaProxy = (baseConfig: AlphaCliConfig) => {
const requestConfig: AlphaCliConfig = {
...baseConfig,
method: method as AlphaCliConfig['method'],
url: `${baseConfig.url as string}${url as string}`,
url: `${baseConfig.url ?? ''}${url as string}`,
headers: {
...baseConfig.headers,
...req.headers as Record<string, string>,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/lambda-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (yargs: Argv) => {
await loadEnvFile(envFile);
}
const exported = await import(lambdaHandler);
config.lambda = exported.handler || exported.default.handler;
config.handler = exported.handler || exported.default.handler;
}
};
};
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Handler } from 'aws-lambda';
import { AlphaOptions, AlphaResponse } from '@lifeomic/alpha';

export interface AlphaCliConfig extends AlphaOptions<string> {
responsePostProcessors: ((data: AlphaResponse<string>) => AlphaResponse<string>)[];
proxied?: boolean;
proxyPort?: number;
handler?: Handler;
}

export interface AlphaCliArguments {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AlphaCliConfig } from './types';
import { Alpha } from '@lifeomic/alpha';

export const callAlpha = async (config: AlphaCliConfig) => {
const client = new Alpha();
export const callAlpha = async ({ handler, ...config }: AlphaCliConfig) => {
const client = handler ? new Alpha(handler) : new Alpha();
const response = await client.request<string>(config);

const processedResponse = config.responsePostProcessors.reduce((reduce, processor) => {
Expand Down
2 changes: 0 additions & 2 deletions test/lambdaHandlers/exportApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ router.get('/echo/:param', (ctx) => {
ctx.status = 200;
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
app.use(router.routes());
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
app.use(router.allowedMethods());

app.handler = serverless(app);
Expand Down
21 changes: 2 additions & 19 deletions test/lambdaHandlers/exportHandler.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
import Koa from 'koa';
import Router from '@koa/router';
import serverless from 'serverless-http';

const app = new Koa();

const router = new Router();

router.get('/echo/:param', (ctx) => {
ctx.body = ctx.params.param;
ctx.status = 200;
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
app.use(router.routes());
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
app.use(router.allowedMethods());

export const handler = serverless(app);
import app from './exportApp';
export const handler = app.handler;
2 changes: 1 addition & 1 deletion test/plugins/lambda-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test.each([
'lambda-handler': path.join(__dirname, '..', 'lambdaHandlers', lambdaHandlerFile),
};
await expect(pluginFunc(config, cliArgs)).resolves.toBeUndefined();
expect(config).toHaveProperty('lambda', expect.any(Function));
expect(config).toHaveProperty('handler', expect.any(Function));
});

test('will throw exception on unknown extension', async () => {
Expand Down
8 changes: 4 additions & 4 deletions test/sign-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import yargs from 'yargs';
import { Credentials, Provider } from '@aws-sdk/types';
import { AwsCredentialIdentity, Provider } from '@aws-sdk/types';
import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts';
import { mockClient } from 'aws-sdk-client-mock';

Expand All @@ -18,7 +18,7 @@ const getConfig = async (...args: string[]) => {

const mockSts = mockClient(STSClient);

const expectedCreds: Credentials = {
const expectedCreds: AwsCredentialIdentity = {
accessKeyId: randomUUID(),
secretAccessKey: randomUUID(),
sessionToken: randomUUID(),
Expand Down Expand Up @@ -53,7 +53,7 @@ test('will set up using sts to assume a role', async () => {
const role = 'some-stinky-role';
const config = await getConfig('--sign', '--role', role);
expect(config).toHaveProperty('signAwsV4', { credentials: expect.any(Function) });
const { credentials } = config.signAwsV4 as { credentials: Provider<Credentials>; };
const { credentials } = config.signAwsV4 as { credentials: Provider<AwsCredentialIdentity>; };

await expect(credentials()).resolves.toEqual(expectedCreds);
expect(mockSts.commandCalls(AssumeRoleCommand)).toHaveLength(1);
Expand All @@ -68,7 +68,7 @@ test('will throw exception if Credentials is empty', async () => {
const role = 'some-stinky-role';
const config = await getConfig('--sign', '--role', role);
expect(config).toHaveProperty('signAwsV4', { credentials: expect.any(Function) });
const { credentials } = config.signAwsV4 as { credentials: Provider<Credentials>; };
const { credentials } = config.signAwsV4 as { credentials: Provider<AwsCredentialIdentity>; };

await expect(credentials()).rejects.toThrowError(`Unable to assume role ${role}`);
});
6 changes: 2 additions & 4 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"compilerOptions": {
"noEmit": false
},
"exclude": [
"*.js",
"*.ts",
"test/**"
"include": [
"src/"
]
}
Loading

0 comments on commit a9ff86d

Please sign in to comment.