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

Commit

Permalink
fix function templates from demo
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Apr 24, 2019
1 parent 3c71022 commit a02dbf7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/utils/serve-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function createCallback(response) {
if (err) {
return handleErr(err, response);
}
if (lambdaResponse === undefined) {
return handleErr(
"lambda response was undefined. check your function code again.",
response
);
}
if (!Number(lambdaResponse.statusCode)) {
console.log(
`${NETLIFYDEVERR} Your function response must have a numerical statusCode. You gave: $`,
Expand All @@ -52,6 +58,7 @@ function createCallback(response) {
for (const key in lambdaResponse.headers) {
response.setHeader(key, lambdaResponse.headers[key]);
}
console.log({ lambdaResponse });
response.write(
lambdaResponse.isBase64Encoded
? Buffer.from(lambdaResponse.body, "base64")
Expand All @@ -61,21 +68,6 @@ function createCallback(response) {
};
}

function promiseCallback(promise, callback) {
if (!promise) return;
if (typeof promise.then !== "function") return;
if (typeof callback !== "function") return;

promise.then(
function(data) {
callback(null, data);
},
function(err) {
callback(err, null);
}
);
}

// function getHandlerPath(functionPath) {
// if (functionPath.match(/\.js$/)) {
// return functionPath;
Expand Down Expand Up @@ -167,6 +159,11 @@ function createHandler(dir) {
try {
module.paths = [moduleDir];
handler = require(functionPath);
if (typeof handler.handler !== "function") {
throw new Error(
`function ${functionPath} must export a function named handler`
);
}
module.paths = before;
} catch (error) {
module.paths = before;
Expand Down Expand Up @@ -200,6 +197,22 @@ function createHandler(dir) {
};
}

function promiseCallback(promise, callback) {
if (!promise) return; // means no handler was written
if (typeof promise.then !== "function") return;
if (typeof callback !== "function") return;

promise.then(
function(data) {
console.log("hellooo");
callback(null, data);
},
function(err) {
callback(err, null);
}
);
}

async function serveFunctions(settings) {
const app = express();
const dir = settings.functionsDir;
Expand Down

0 comments on commit a02dbf7

Please sign in to comment.