Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
fix(netlify): Support Netlify Functions
Browse files Browse the repository at this point in the history
Support Netlify Functions
  • Loading branch information
nahtnam authored May 25, 2019
2 parents c64766e + 7af4967 commit d0ef678
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
39 changes: 24 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "light",
"version": "0.0.0-development",
"name": "light-test-netlify",
"version": "0.0.10",
"description": "a modern framework",
"scripts": {
"test": "npm run lint && npm run jest:ci",
Expand Down Expand Up @@ -47,7 +47,7 @@
"micro-http-router-query-params": "^1.4.0",
"micromatch": "^4.0.2",
"pino-http": "^4.2.0",
"pino-pretty": "^3.0.0",
"pino-pretty": "^3.0.1",
"signale": "^1.4.0",
"yargs": "^13.2.4",
"youch": "^2.0.10",
Expand All @@ -71,7 +71,7 @@
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-jest": "^22.6.4",
"jest": "^24.8.0",
"node-fetch": "^2.6.0",
Expand Down
37 changes: 23 additions & 14 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import pino from 'pino-http';
import { handleErrors } from 'micro-boom';
import bytes from 'bytes';

const isProd = process.env.NODE_ENV === 'production';
const { LIGHT_ENVIRONMENT, NODE_ENV } = process.env;
const isProd = NODE_ENV === 'production';
const isNetlify = LIGHT_ENVIRONMENT === 'netlify';
const isAWS = LIGHT_ENVIRONMENT === 'aws';
const isRunKit = LIGHT_ENVIRONMENT === 'runkit';

let Youch: any;
let forTerminal: any;

/* istanbul ignore next */
if (!isProd) {
Youch = require('youch'); // eslint-disable-line
forTerminal = require('youch-terminal'); // eslint-disable-line
Expand Down Expand Up @@ -106,11 +112,12 @@ export default (route: Route): Handler => {

const plugins = route.plugins || [];

if (fn.log !== false) {
if ((fn as any).log !== false) {
plugins.unshift(logger);
fn.log = false;
(fn as any).log = false;
}

/* istanbul ignore next */
if (!isProd) {
plugins.unshift(youchErrors);
}
Expand All @@ -121,27 +128,29 @@ export default (route: Route): Handler => {
exec = plugins.reverse().reduce((acc, val): any => val(acc), exec);
}

return run(Req, Res, exec);
return exec(Req, Res);
};


Object.keys(route).forEach((key): void => {
(fn as any)[key] = (route as any)[key];
});

fn.log = true;
fn.module = __dirname;
fn.handler = fn;

// TODO: Fix this
(fn as any).log = true;
(fn as any).module = __dirname;
(fn as any).handler = fn;
/* istanbul ignore next */
const { LIGHT_ENVIRONMENT } = process.env;
const isAWS: boolean = LIGHT_ENVIRONMENT === 'aws';
if (!isNetlify && !isAWS) {
(fn as any).handler = async (req: IM, res: SR): AP => run(req, res, fn);
}

/* istanbul ignore if */
if (isAWS) {
return AWSServerlessMicro(fn);
if (isNetlify || isAWS) {
return {
handler: AWSServerlessMicro(fn),
};
}

const isRunKit = !!(process.env.LIGHT_ENVIRONMENT && process.env.LIGHT_ENVIRONMENT.toLowerCase() === 'runkit');
if (isRunKit) {
return {
endpoint: fn,
Expand Down

0 comments on commit d0ef678

Please sign in to comment.