Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit a02dbf7

Browse files
author
sw-yx
committed
fix function templates from demo
1 parent 3c71022 commit a02dbf7

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/utils/serve-functions.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ function createCallback(response) {
3232
if (err) {
3333
return handleErr(err, response);
3434
}
35+
if (lambdaResponse === undefined) {
36+
return handleErr(
37+
"lambda response was undefined. check your function code again.",
38+
response
39+
);
40+
}
3541
if (!Number(lambdaResponse.statusCode)) {
3642
console.log(
3743
`${NETLIFYDEVERR} Your function response must have a numerical statusCode. You gave: $`,
@@ -52,6 +58,7 @@ function createCallback(response) {
5258
for (const key in lambdaResponse.headers) {
5359
response.setHeader(key, lambdaResponse.headers[key]);
5460
}
61+
console.log({ lambdaResponse });
5562
response.write(
5663
lambdaResponse.isBase64Encoded
5764
? Buffer.from(lambdaResponse.body, "base64")
@@ -61,21 +68,6 @@ function createCallback(response) {
6168
};
6269
}
6370

64-
function promiseCallback(promise, callback) {
65-
if (!promise) return;
66-
if (typeof promise.then !== "function") return;
67-
if (typeof callback !== "function") return;
68-
69-
promise.then(
70-
function(data) {
71-
callback(null, data);
72-
},
73-
function(err) {
74-
callback(err, null);
75-
}
76-
);
77-
}
78-
7971
// function getHandlerPath(functionPath) {
8072
// if (functionPath.match(/\.js$/)) {
8173
// return functionPath;
@@ -167,6 +159,11 @@ function createHandler(dir) {
167159
try {
168160
module.paths = [moduleDir];
169161
handler = require(functionPath);
162+
if (typeof handler.handler !== "function") {
163+
throw new Error(
164+
`function ${functionPath} must export a function named handler`
165+
);
166+
}
170167
module.paths = before;
171168
} catch (error) {
172169
module.paths = before;
@@ -200,6 +197,22 @@ function createHandler(dir) {
200197
};
201198
}
202199

200+
function promiseCallback(promise, callback) {
201+
if (!promise) return; // means no handler was written
202+
if (typeof promise.then !== "function") return;
203+
if (typeof callback !== "function") return;
204+
205+
promise.then(
206+
function(data) {
207+
console.log("hellooo");
208+
callback(null, data);
209+
},
210+
function(err) {
211+
callback(err, null);
212+
}
213+
);
214+
}
215+
203216
async function serveFunctions(settings) {
204217
const app = express();
205218
const dir = settings.functionsDir;

0 commit comments

Comments
 (0)