Skip to content

Commit

Permalink
feat: Add backend-function runtime behavior to get the data config
Browse files Browse the repository at this point in the history
  • Loading branch information
stocaaro committed Nov 14, 2024
1 parent 5d873a1 commit eb41f64
Show file tree
Hide file tree
Showing 10 changed files with 425 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/brave-cheetahs-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@aws-amplify/backend': patch
'@aws-amplify/backend-function': patch
---

feat: Add backend-function runtime behavior to get the data config
54 changes: 28 additions & 26 deletions package-lock.json

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

7 changes: 7 additions & 0 deletions packages/backend-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.js"
},
"./runtime": {
"types": "./lib/runtime/index.d.ts",
"import": "./lib/runtime/index.js",
"require": "./lib/runtime/index.js"
}
},
"main": "lib/index.js",
Expand All @@ -22,11 +27,13 @@
"@aws-amplify/backend-output-schemas": "^1.4.0",
"@aws-amplify/backend-output-storage": "^1.1.3",
"@aws-amplify/plugin-types": "^1.4.0",
"@aws-sdk/client-s3": "^3.624.0",
"execa": "^8.0.1"
},
"devDependencies": {
"@aws-amplify/backend-platform-test-stubs": "^0.3.6",
"@aws-amplify/platform-core": "^1.1.0",
"@aws-sdk/client-s3": "^3.624.0",
"@aws-sdk/client-ssm": "^3.624.0",
"aws-sdk": "^2.1550.0",
"uuid": "^9.0.1"
Expand Down
5 changes: 5 additions & 0 deletions packages/backend-function/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { FunctionEnvironmentTranslator } from './function_env_translator.js';
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';
import { FunctionLayerArnParser } from './layer_parser.js';
import { convertFunctionSchedulesToRuleSchedules } from './schedule_parser.js';
import { FunctionDataConfigGenerator } from './function_data_config_generator.js';

const functionStackType = 'function-Lambda';

Expand Down Expand Up @@ -425,6 +426,10 @@ class AmplifyFunction
// This will be overwritten with the typed file at the end of synthesis
functionEnvironmentTypeGenerator.generateProcessEnvShim();

const functionClientConfigGenerator = new FunctionDataConfigGenerator(id);

functionClientConfigGenerator.generateDataConfigShim();

let functionLambda: NodejsFunction;
try {
functionLambda = new NodejsFunction(scope, `${id}-lambda`, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, it, mock } from 'node:test';
import fs from 'fs';
import { FunctionDataConfigGenerator } from './function_data_config_generator.js';
import assert from 'assert';
import { pathToFileURL } from 'url';
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';

void describe('FunctionDataConfigGenerator', () => {
void it('generates a type definition file', () => {
const fsOpenSyncMock = mock.method(fs, 'openSync');
const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null);
fsOpenSyncMock.mock.mockImplementation(() => 0);
const functionDataConfigGenerator = new FunctionDataConfigGenerator(
'testFunction'
);
const configExport =
'export const { libraryOptions, resourceConfig } = await internalGetAmplifyClientConfiguration(env);';

functionDataConfigGenerator.generateDataConfigShim();

// assert type definition file path
assert.equal(
fsWriteFileSyncMock.mock.calls[0].arguments[0],
`${process.cwd()}/.amplify/generated/data-config/testFunction.ts`
);

// assert content
assert.ok(
fsWriteFileSyncMock.mock.calls[0].arguments[1]
?.toString()
.includes(configExport)
);

mock.restoreAll();
});

void it('generated data configuration file has valid syntax', async () => {
const functionDataConfigGenerator = new FunctionDataConfigGenerator(
'testFunction'
);
const filePath = `${process.cwd()}/.amplify/generated/data-config/testFunction.ts`;

functionDataConfigGenerator.generateDataConfigShim();

// The data config shim depends upon the env shim, so we need to build it for the config to be importable
const functionEnvironmentTypeGenerator =
new FunctionEnvironmentTypeGenerator('testFunction');
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim(['TEST_ENV']);

// import to validate syntax of data config file
await import(pathToFileURL(filePath).toString());
});
});
45 changes: 45 additions & 0 deletions packages/backend-function/src/function_data_config_generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import fs from 'fs';
import path from 'path';

const lambdaDataConfigTemplate = (
functionName: string
) => `// This file is auto-generated by Amplify. Edits will be overwritten.
import { internalGetAmplifyClientConfiguration } from "@aws-amplify/backend-function/runtime";
import { env } from "../env/${functionName}";
export const { libraryOptions, resourceConfig } = await internalGetAmplifyClientConfiguration(env);
`;

/**
* Generates the data configuration imports
*/
export class FunctionDataConfigGenerator {
private typeDefFilePath: string;

private indentation: string = ' ';

/**
* Initialize data configuration file name and location
*/
constructor(private readonly functionName: string) {
this.typeDefFilePath = `${process.cwd()}/.amplify/generated/data-config/${
this.functionName
}.ts`;
}

/**
* Generate data-config shim
*/
generateDataConfigShim = () => {
this.writeShimFile(lambdaDataConfigTemplate(this.functionName));
};

private writeShimFile = (content: string) => {
const typeDefFileDirname = path.dirname(this.typeDefFilePath);

if (!fs.existsSync(typeDefFileDirname)) {
fs.mkdirSync(typeDefFileDirname, { recursive: true });
}

fs.writeFileSync(this.typeDefFilePath, content);
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, mock } from 'node:test';
import fs from 'fs';
import fsp from 'fs/promises';
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';
import assert from 'assert';
import { pathToFileURL } from 'url';
Expand Down Expand Up @@ -57,7 +56,6 @@ void describe('FunctionEnvironmentTypeGenerator', () => {
});

void it('generated type definition file has valid syntax', async () => {
const targetDirectory = await fsp.mkdtemp('func_env_type_gen_test');
const functionEnvironmentTypeGenerator =
new FunctionEnvironmentTypeGenerator('testFunction');
const filePath = `${process.cwd()}/.amplify/generated/env/testFunction.ts`;
Expand All @@ -66,8 +64,6 @@ void describe('FunctionEnvironmentTypeGenerator', () => {

// import to validate syntax of type definition file
await import(pathToFileURL(filePath).toString());

await fsp.rm(targetDirectory, { recursive: true, force: true });
});

void it('does not generate duplicate environment variables', () => {
Expand Down
Loading

0 comments on commit eb41f64

Please sign in to comment.