Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic function implementation #87

Merged
merged 9 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/backend-function/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Be very careful editing this file. It is crafted to work around [this issue](https://github.com/npm/npm/issues/4479)

# First ignore everything
**/*

# Then add back in transpiled js and ts declaration files
!lib/**/*.js
!lib/**/*.d.ts

# Then ignore test js and ts declaration files
*.test.js
*.test.d.ts

# This leaves us with including only js and ts declaration files of functional code
40 changes: 40 additions & 0 deletions packages/backend-function/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## API Report File for "@aws-amplify/backend-function"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).

```ts

import { AmplifyFunction } from '@aws-amplify/function-construct';
import { AmplifyFunctionProps } from '@aws-amplify/function-construct';
import { ConstructFactory } from '@aws-amplify/plugin-types';
import { ConstructFactoryGetInstanceProps } from '@aws-amplify/plugin-types';

// @public
export class AmplifyFunctionFactory implements ConstructFactory<AmplifyFunction> {
static build(props: AmplifyFunctionFactoryBuildProps): Promise<AmplifyFunctionFactory>;
static fromDir(props: AmplifyFunctionFactoryFromDirProps): AmplifyFunctionFactory;
getInstance({ constructContainer, }: ConstructFactoryGetInstanceProps): AmplifyFunction;
Comment on lines +14 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory method to produce factory that produces something :-)

}

// @public (undocumented)
export type AmplifyFunctionFactoryBaseProps = {
name: string;
};

// @public (undocumented)
export type AmplifyFunctionFactoryBuildProps = AmplifyFunctionFactoryBaseProps & Omit<AmplifyFunctionProps, 'absoluteCodePath'> & {
buildCommand: string;
outDir: string;
};

// @public (undocumented)
export type AmplifyFunctionFactoryFromDirProps = AmplifyFunctionFactoryBaseProps & Omit<AmplifyFunctionProps, 'absoluteCodePath'> & {
codePath: string;
};

// @public
export const Func: typeof AmplifyFunctionFactory;

// (No @packageDocumentation comment for this package)

```
3 changes: 3 additions & 0 deletions packages/backend-function/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../api-extractor.base.json"
}
28 changes: 28 additions & 0 deletions packages/backend-function/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@aws-amplify/backend-function",
"version": "0.1.0",
"type": "module",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.js"
}
},
"types": "lib/index.d.ts",
"scripts": {
"update:api": "api-extractor run --local"
},
"dependencies": {
"@aws-amplify/function-construct": "^0.1.0",
"execa": "^7.1.1"
},
"devDependencies": {
"@aws-amplify/backend-engine": "^0.1.0",
"@aws-amplify/plugin-types": "^0.1.0"
},
"peerDependencies": {
"aws-cdk-lib": "~2.68.0",
"constructs": "^10.0.0"
}
}
74 changes: 74 additions & 0 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { beforeEach, describe, it, mock } from 'node:test';
import { Func } from './factory.js';
import { App, Stack } from 'aws-cdk-lib';
import {
NestedStackResolver,
SingletonConstructContainer,
StackMetadataBackendOutputStorageStrategy,
} from '@aws-amplify/backend-engine';
import {
BackendOutputEntry,
BackendOutputStorageStrategy,
ConstructContainer,
} from '@aws-amplify/plugin-types';
import assert from 'node:assert';
import { fileURLToPath } from 'url';

describe('AmplifyFunctionFactory', () => {
let constructContainer: ConstructContainer;
let outputStorageStrategy: BackendOutputStorageStrategy<BackendOutputEntry>;

beforeEach(() => {
const app = new App();
const stack = new Stack(app, 'testStack');

constructContainer = new SingletonConstructContainer(
new NestedStackResolver(stack)
);

outputStorageStrategy = new StackMetadataBackendOutputStorageStrategy(
stack
);
});

it('creates singleton function instance', () => {
const functionFactory = Func.fromDir({
name: 'testFunc',
codePath: '../test-assets/test-lambda',
});
const instance1 = functionFactory.getInstance({
constructContainer,
outputStorageStrategy,
});
const instance2 = functionFactory.getInstance({
constructContainer,
outputStorageStrategy,
});
assert.strictEqual(instance1, instance2);
});

it('executes build command from directory where constructor is used', async () => {
const commandExecutorMock = mock.fn();

// Casting to never is necessary because commandExecutor is a private method.
// TS yells that it's not a property on Func even though it is there
mock.method(Func, 'commandExecutor' as never, commandExecutorMock);

(
await Func.build({
name: 'testFunc',
outDir: '../test-assets/test-lambda',
buildCommand: 'test command',
})
).getInstance({ constructContainer, outputStorageStrategy });

assert.strictEqual(commandExecutorMock.mock.callCount(), 1);
assert.deepStrictEqual(commandExecutorMock.mock.calls[0].arguments, [
'test command',
{
cwd: fileURLToPath(new URL('../src', import.meta.url)),
stdio: 'inherit',
},
]);
});
});
Loading
Loading