forked from laardee/aws-app-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
151 lines (139 loc) · 4.18 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const AWS = require('aws-sdk')
const { clone, equals, find, isEmpty, isNil, map, merge, not, pick } = require('ramda')
const { listAll } = require('./lib')
/**
* Get AWS clients
* @param {object} credentials
* @param {string} region
* @returns {object} AWS clients
*/
const getClients = (credentials, region = 'us-east-1') => {
const appSync = new AWS.AppSync({ credentials, region })
return {
appSync
}
}
const authentication = (authenticationType) => {
switch (authenticationType) {
case 'AMAZON_COGNITO_USER_POOLS':
return 'userPoolConfig'
case 'OPENID_CONNECT':
return 'openIDConnectConfig'
}
}
const openIdConnectDefaults = (config) =>
merge(
{
clientId: null,
iatTTL: 0,
authTTL: 0
},
config
)
const userPoolDefaults = (config) =>
merge(
{
appIdClientRegex: null
},
config
)
const addDefaults = (inputs) => {
if (inputs.openIDConnectConfig) {
inputs.openIDConnectConfig = openIdConnectDefaults(inputs.openIDConnectConfig)
} else if (inputs.userPoolConfig) {
inputs.userPoolConfig = userPoolDefaults(inputs.userPoolConfig)
}
if (inputs.additionalAuthenticationProviders) {
inputs.additionalAuthenticationProviders = map((additionalAuthenticationProvider) => {
if (additionalAuthenticationProvider.openIDConnectConfig) {
additionalAuthenticationProvider.openIDConnectConfig = openIdConnectDefaults(
additionalAuthenticationProvider.openIDConnectConfig
)
} else if (additionalAuthenticationProvider.userPoolConfig) {
additionalAuthenticationProvider.userPoolConfig = userPoolDefaults(
additionalAuthenticationProvider.userPoolConfig
)
}
return additionalAuthenticationProvider
}, inputs.additionalAuthenticationProviders)
}
return inputs
}
/**
* Create or update graphql api
* @param {object} appSync
* @param {object} config
* @returns {object} - graphqlApi
*/
const createOrUpdateGraphqlApi = async (appSync, config, debug) => {
const inputFields = [
'name',
'authenticationType',
authentication(config.authenticationType),
'additionalAuthenticationProviders',
'logConfig'
]
const inputs = pick(inputFields, config)
let graphqlApi
if (config.apiId) {
debug(`Fetching graphql API by API id ${config.apiId}`)
try {
const response = await appSync.getGraphqlApi({ apiId: config.apiId }).promise()
// eslint-disable-next-line prefer-destructuring
graphqlApi = response.graphqlApi
} catch (error) {
if (not(equals('NotFoundException', error.code))) {
throw error
}
debug(`API id '${config.apiId}' not found`)
}
}
if (isNil(graphqlApi)) {
debug(`Fetching graphql API by API name ${config.name}`)
graphqlApi = find(
({ name }) => equals(name, config.name),
await listAll(appSync, 'listGraphqlApis', {}, 'graphqlApis')
)
if (not(isNil(graphqlApi))) {
config.apiId = graphqlApi.apiId
}
}
if (isNil(graphqlApi)) {
debug('Creating a new graphql API')
const response = await appSync.createGraphqlApi(inputs).promise()
// eslint-disable-next-line prefer-destructuring
graphqlApi = response.graphqlApi
} else if (
not(equals(addDefaults(clone(inputs)), pick(inputFields, graphqlApi))) &&
not(isEmpty(inputs))
) {
debug(`Updating graphql API ${config.apiId}`)
const parameters = merge(pick(inputFields, graphqlApi), merge(inputs, { apiId: config.apiId }))
const response = await appSync.updateGraphqlApi(parameters).promise()
// eslint-disable-next-line prefer-destructuring
graphqlApi = response.graphqlApi
}
return graphqlApi
}
const removeGraphqlApi = async (appSync, config) => {
if (not(isNil(config.apiId))) {
try {
await appSync.deleteGraphqlApi({ apiId: config.apiId }).promise()
} catch (error) {
if (not(equals(error.code, 'NotFoundException'))) {
throw error
}
}
}
}
module.exports = {
getClients,
createOrUpdateGraphqlApi,
removeGraphqlApi,
...require('./lib/datasources'),
...require('./lib/schema'),
...require('./lib/resolvers'),
...require('./lib/functions'),
...require('./lib/role'),
...require('./lib/apikeys')
}