1+ const { LambdaClient, InvokeCommand } = require ( '@aws-sdk/client-lambda' ) ;
2+ const { SchemasClient, DescribeSchemaCommand, UpdateSchemaCommand, CreateSchemaCommand, CreateRegistryCommand } = require ( '@aws-sdk/client-schemas' ) ;
3+ const { fromSSO } = require ( "@aws-sdk/credential-provider-sso" ) ;
4+ const fs = require ( 'fs' ) ;
5+ const inputUtil = require ( '../../shared/inputUtil' ) ;
6+
7+ async function invoke ( cmd , resourceName ) {
8+ const lambdaClient = new LambdaClient ( { credentials : await fromSSO ( { profile : cmd . profile } ) } ) ;
9+ const schemasClient = new SchemasClient ( { credentials : await fromSSO ( { profile : cmd . profile } ) } ) ;
10+ if ( ! cmd . payload ) {
11+ const payloadSource = await inputUtil . list ( "Select a payload source" , [ "Local JSON file" , "Shared test event" , "Input JSON" ] ) ;
12+ if ( payloadSource === "Local JSON file" ) {
13+ cmd . payload = await inputUtil . file ( "Select file(s) to use as payload" , "json" ) ;
14+ } else if ( payloadSource === "Shared test event" ) {
15+ try {
16+ const sharedEvents = await schemasClient . send ( new DescribeSchemaCommand ( { RegistryName : "lambda-testevent-schemas" , SchemaName : `_${ resourceName } -schema` } ) ) ;
17+ const schema = JSON . parse ( sharedEvents . Content ) ;
18+ const savedEvents = Object . keys ( schema . components . examples ) ;
19+ const event = await inputUtil . autocomplete ( "Select an event" , savedEvents ) ;
20+ cmd . payload = JSON . stringify ( schema . components . examples [ event ] . value ) ;
21+ } catch ( e ) {
22+ console . log ( "Failed to fetch shared test events" , e . message ) ;
23+ process . exit ( 1 ) ;
24+ }
25+ } else if ( payloadSource === "Input JSON" ) {
26+ do {
27+ cmd . payload = await inputUtil . text ( "Enter payload JSON" ) ;
28+ } while ( ! isValidJson ( cmd . payload , true ) ) ;
29+ const save = await inputUtil . prompt ( "Save as shared test event?" , "No" ) ;
30+ if ( save ) {
31+ const name = await inputUtil . text ( "Enter a name for the event" ) ;
32+ try {
33+ try {
34+ await schemasClient . send ( new CreateRegistryCommand ( { RegistryName : registryName } ) ) ;
35+ } catch ( e ) {
36+ // do nothing
37+ }
38+
39+ const schema = await schemasClient . send ( new DescribeSchemaCommand ( { RegistryName : "lambda-testevent-schemas" , SchemaName : `_${ resourceName } -schema` } ) ) ;
40+ const schemaContent = JSON . parse ( schema . Content ) ;
41+ schemaContent . components . examples [ name ] = { value : JSON . parse ( cmd . payload ) } ;
42+ await schemasClient . send ( new UpdateSchemaCommand ( { RegistryName : "lambda-testevent-schemas" , SchemaName : `_${ resourceName } -schema` , Type : "OpenApi3" , Content : JSON . stringify ( schemaContent ) } ) ) ;
43+ } catch ( e ) {
44+ if ( e . message . includes ( "does not exist" ) ) {
45+ console . log ( "Creating new schema" ) ;
46+ const schemaContent = {
47+ openapi : "3.0.0" ,
48+ info : {
49+ title : `Event` ,
50+ version : "1.0.0"
51+ } ,
52+ paths : { } ,
53+ components : {
54+ examples : {
55+ [ name ] : {
56+ value : JSON . parse ( cmd . payload )
57+ }
58+ }
59+ }
60+ } ;
61+ await schemasClient . send ( new CreateSchemaCommand ( { RegistryName : "lambda-testevent-schemas" , SchemaName : `_${ resourceName } -schema` , Type : "OpenApi3" , Content : JSON . stringify ( schemaContent ) } ) ) ;
62+ } else {
63+
64+ console . log ( "Failed to save shared test event" , e . message ) ;
65+ process . exit ( 1 ) ;
66+ }
67+ }
68+ console . log ( `Saved event '${ name } '` ) ;
69+ }
70+ }
71+ }
72+
73+ if ( isFilePath ( cmd . payload ) ) {
74+ cmd . payload = fs . readFileSync ( cmd . payload ) . toString ( ) ;
75+ }
76+
77+ if ( ! isValidJson ( cmd . payload ) ) {
78+ try {
79+ const sharedEvents = await schemasClient . send ( new DescribeSchemaCommand ( { RegistryName : "lambda-testevent-schemas" , SchemaName : `_${ resourceName } -schema` } ) ) ;
80+ const schema = JSON . parse ( sharedEvents . Content ) ;
81+ cmd . payload = JSON . stringify ( schema . components . examples [ cmd . payload ] . value ) ;
82+ } catch ( e ) {
83+ console . log ( "Failed to fetch shared test events" , e . message ) ;
84+ process . exit ( 1 ) ;
85+ }
86+ }
87+
88+ if ( isValidJson ( cmd . payload ) ) {
89+ const params = new InvokeCommand ( {
90+ FunctionName : resourceName ,
91+ Payload : cmd . payload
92+ } ) ;
93+ try {
94+ console . log ( "Invoking function with payload:" , concatenateAndAddDots ( cmd . payload , 100 ) )
95+ const data = await lambdaClient . send ( params ) ;
96+ const response = JSON . parse ( Buffer . from ( data . Payload ) . toString ( ) ) ;
97+ try {
98+ console . log ( "Response:" , JSON . stringify ( JSON . parse ( response ) , null , 2 ) ) ;
99+ } catch ( e ) {
100+ console . log ( "Response:" , response ) ;
101+ }
102+ }
103+ catch ( err ) {
104+ console . log ( "Error" , err ) ;
105+ }
106+ } else {
107+ console . log ( "Invalid JSON, please try again" ) ;
108+ }
109+ }
110+
111+ function concatenateAndAddDots ( str , maxLength ) {
112+ if ( str . length <= maxLength ) {
113+ return str ;
114+ }
115+ return str . substring ( 0 , maxLength - 3 ) + "..." ;
116+ }
117+
118+ function isFilePath ( str ) {
119+ return str . startsWith ( "./" ) || str . startsWith ( "../" ) || str . startsWith ( "/" ) || str . startsWith ( "~" ) || str . startsWith ( "file://" )
120+ && fs . existsSync ( str ) ;
121+ }
122+
123+ function isValidJson ( str , logInfo ) {
124+ try {
125+ JSON . parse ( str ) ;
126+ } catch ( e ) {
127+ if ( logInfo )
128+ console . log ( "Invalid JSON, please try again" ) ;
129+ return false ;
130+ }
131+ return true ;
132+ }
133+
134+ exports . invoke = invoke ;
0 commit comments