@@ -80,9 +80,11 @@ Resources:
80
80
Variables :
81
81
API_KEY : !GetAtt ApiKey.ApiKey
82
82
GRAPHQL_URL : !GetAtt AppSyncApi.GraphQLUrl
83
- Runtime : nodejs18 .x
83
+ Runtime : nodejs14 .x
84
84
Handler : index.handler
85
85
InlineCode : |
86
+ const https = require("https");
87
+
86
88
exports.handler = async (_) => {
87
89
const queries = {
88
90
getNote: /* GraphQL */ `
@@ -103,23 +105,47 @@ Resources:
103
105
`,
104
106
};
105
107
108
+ const fetch = async (url, options) =>
109
+ new Promise((resolve, reject) => {
110
+ const req = https.request(url, options, (res) => {
111
+ const body = [];
112
+ res.on("data", (chunk) => body.push(chunk));
113
+ res.on("end", () => {
114
+ const resString = Buffer.concat(body).toString();
115
+ resolve(resString);
116
+ });
117
+ });
118
+
119
+ req.on("error", (err) => {
120
+ reject(err);
121
+ });
122
+
123
+ req.on("timeout", () => {
124
+ req.destroy();
125
+ reject(new Error("Request time out"));
126
+ });
127
+
128
+ req.write(options.body);
129
+ req.end();
130
+ });
131
+
106
132
const makeRequest = async (queryName) => {
107
133
const options = {
108
134
method: "POST",
109
135
headers: {
110
136
"x-api-key": process.env.API_KEY,
111
137
},
112
138
body: JSON.stringify({ query: queries[queryName] }),
139
+ timeout: 10000, // ms
113
140
};
114
141
115
142
let statusCode;
116
143
let body;
117
144
let response;
118
145
119
146
try {
120
- /*global fetch*/
121
147
response = await fetch(process.env.GRAPHQL_URL, options);
122
- body = await response.json( );
148
+ body = JSON.parse(response );
123
149
const data = body.data?.[queryName];
124
150
const hasNoErrors = body.errors === undefined;
125
151
const allFieldsAreSet =
@@ -172,5 +198,6 @@ Resources:
172
198
};
173
199
};
174
200
201
+
175
202
Metadata :
176
203
SamTransformTest : true
0 commit comments