-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add backend-function runtime behavior to get the data config
- Loading branch information
Showing
10 changed files
with
425 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/backend-function/src/function_data_config_generator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
packages/backend-function/src/function_data_config_generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.