Skip to content

Commit 282e0f6

Browse files
authored
Add importValueMap option and key-value mock notation (#26)
1 parent b09f398 commit 282e0f6

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Put options under `custom.appsync-simulator` in your `serverless.yml` file
5555
| lambda.loadLocalEnv | false | If `true`, all environment variables (`$ env`) will be accessible from the resolver function. Read more in section [Environment variables](#environment-variables). |
5656
| refMap | {} | A mapping of [resource resolutions](#resource-cloudformation-functions-resolution) for the `Ref` function |
5757
| getAttMap | {} | A mapping of [resource resolutions](#resource-cloudformation-functions-resolution) for the `GetAtt` function |
58+
| importValueMap | {} | A mapping of [resource resolutions](#resource-cloudformation-functions-resolution) for the `ImportValue` function |
5859
| dynamoDb.endpoint | http://localhost:8000 | Dynamodb endpoint. Specify it if you're not using serverless-dynamodb-local. Otherwise, port is taken from dynamodb-local conf |
5960
| dynamoDb.region | localhost | Dynamodb region. Specify it if you're connecting to a remote Dynamodb intance. |
6061
| dynamoDb.accessKeyId | DEFAULT_ACCESS_KEY | AWS Access Key ID to access DynamoDB |
@@ -73,7 +74,7 @@ custom:
7374

7475
# Resource CloudFormation functions resolution
7576

76-
This plugin supports *some* resources resolution from the `Ref` and `Fn::GetAtt` functions
77+
This plugin supports *some* resources resolution from the `Ref`, `Fn::GetAtt` and `Fn::ImportValue` functions
7778
in your yaml file. It also supports *some* other Cfn functions such as `Fn::Join`, `Fb::Sub`, etc.
7879

7980
**Note:** Under the hood, this features relies on the [cfn-resolver-lib](https://github.com/robessog/cfn-resolver-lib) package. For more info on supported cfn functions, refer to [the documentation](https://github.com/robessog/cfn-resolver-lib/blob/master/README.md)
@@ -115,10 +116,11 @@ dataSources:
115116

116117
Sometimes, some references **cannot** be resolved, as they come from an *Output* from Cloudformation; or you might want to use mocked values in your local environment.
117118

118-
In those cases, you can define (or override) those values using the `refMap` and `getAttMap` options.
119+
In those cases, you can define (or override) those values using the `refMap`, `getAttMap` and `importValueMap` options.
119120

120121
- `refMap` takes a mapping of *resource name* to *value* pairs
121122
- `getAttMap` takes a mapping of *resource name* to *attribute/values* pairs
123+
- `importValueMap` takes a mapping of *import name* to *values* pairs
122124

123125
Example:
124126

@@ -132,6 +134,8 @@ custom:
132134
# define ElasticSearchInstance DomainName
133135
ElasticSearchInstance:
134136
DomainEndpoint: "localhost:9200"
137+
importValueMap:
138+
other-service-api-url: "https://other.api.url.com/graphql"
135139

136140
# in your appsync config
137141
dataSources:
@@ -148,6 +152,26 @@ dataSources:
148152
- DomainEndpoint
149153
````
150154

155+
### Key-value mock notation
156+
157+
In some special cases you will need to use key-value mock nottation.
158+
Good example can be case when you need to include serverless stage value (`${self:provider.stage}`) in the import name.
159+
160+
*This notation can be used with all mocks - `refMap`, `getAttMap` and `importValueMap`*
161+
162+
```yaml
163+
provider:
164+
environment:
165+
FINISH_ACTIVITY_FUNCTION_ARN:
166+
Fn::ImportValue: other-service-api-${self:provider.stage}-url
167+
168+
custom:
169+
serverless-appsync-simulator:
170+
importValueMap:
171+
- key: other-service-api-${self:provider.stage}-url
172+
value: "https://other.api.url.com/graphql"
173+
```
174+
151175
## Environment variables
152176

153177
```yaml

src/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,19 @@ class ServerlessAppSyncSimulator {
116116
{},
117117
);
118118

119+
const keyValueArrayToObject = (mapping) => {
120+
if (Array.isArray(mapping)) {
121+
return mapping.reduce((acc, { key, value }) => (
122+
{ ...acc, [key]: value }
123+
), {});
124+
}
125+
return mapping;
126+
};
127+
119128
this.resourceResolvers = {
120-
RefResolvers: { ...refResolvers, ...this.options.refMap },
121-
'Fn::GetAttResolvers': this.options.getAttMap,
129+
RefResolvers: { ...refResolvers, ...keyValueArrayToObject(this.options.refMap) },
130+
'Fn::GetAttResolvers': keyValueArrayToObject(this.options.getAttMap),
131+
'Fn::ImportValueResolvers': keyValueArrayToObject(this.options.importValueMap),
122132
};
123133
}
124134

@@ -134,6 +144,7 @@ class ServerlessAppSyncSimulator {
134144
},
135145
refMap: {},
136146
getAttMap: {},
147+
importValueMap: {},
137148
dynamoDb: {
138149
endpoint: `http://localhost:${get(
139150
this.serverless.service,

0 commit comments

Comments
 (0)