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

Commit

Permalink
fix(global): set up global config (#54)
Browse files Browse the repository at this point in the history
fix(global): set up global config
  • Loading branch information
nahtnam committed Nov 12, 2019
2 parents 033432b + b649ad7 commit 74eb68a
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
39 changes: 39 additions & 0 deletions guides/global.md
@@ -0,0 +1,39 @@
---
title: global
subtitle: use the global function to share global values such as a logger
---

## Introduction

The global function is a way to share global values such as a logger, metrics, or even middleware and access them in all of your files without having to manually import them.

**This feature does not work in serverless mode!**

## Usage

Simply create a `light.config.js` file in the ROOT of your project. Light will search the `process.cwd()` and all parent folders, so make sure the file is somewhere along that path.

```javascript
module.exports = {
global: {
this: 'is global',
},
};
```

In any of your routes, just invoke the global function.

```javascript
const { route, global } = require('light');

const { logger } = global();
const { handler } = route();

module.exports = handler(() => {
logger.log('hello world!');
return {
hello: 'world',
};
});

```
1 change: 1 addition & 0 deletions guides/guides.json
Expand Up @@ -5,6 +5,7 @@
"Routing": "routing",
"Methods": "methods",
"Params": "params",
"Global": "global",
"Query": "query",
"Error Handling": "error-handling",
"Hot Module Reloading": "hot-module-reloading",
Expand Down
16 changes: 16 additions & 0 deletions src/global.ts
@@ -0,0 +1,16 @@
import { existsSync } from 'fs';
import { join } from 'path';

const findGlobal = (path: string): any => {
if (!path || path === '/') {
return {};
}
const file = join(path, 'light.config.js');
if (existsSync(file)) {
const conf = require(file); // eslint-disable-line
return conf.global || {};
}
return findGlobal(join(path, '../'));
};

export default (): any => findGlobal(process.cwd());
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -9,6 +9,7 @@ export { default as params } from './params';
export { default as query } from './query';
export { default as test } from './test';
export { default as route } from './route';
export { default as global } from './global';
export {
buffer,
text,
Expand Down
99 changes: 99 additions & 0 deletions tests/global.ts
@@ -0,0 +1,99 @@
import fetch from 'node-fetch';
import listen from 'test-listen';
import { join } from 'path';

import {
server,
} from '../src/index';

describe('global', () => {
beforeEach(() => {
jest.resetModules();
});

describe('with no light config', () => {
it('does not find a config', async () => {
expect.assertions(2);
const spy = jest.spyOn(process, 'cwd');
spy.mockReturnValue('/');

const app = server({
routes: join(__dirname, './seeds/global/with/routes'),
opts: { requestLogger: false },
});
const url = await listen(app.server);

const req = await fetch(url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: {} });
spy.mockRestore();

app.server.close();
});
});

describe('with light config', () => {
it('returns the config', async () => {
expect.assertions(2);
const spy = jest.spyOn(process, 'cwd');
spy.mockReturnValue(join(__dirname, './seeds/global/with'));

const app = server({
routes: join(__dirname, './seeds/global/with/routes'),
opts: { requestLogger: false },
});
const url = await listen(app.server);

const req = await fetch(url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: { hello: 'world' } });
spy.mockRestore();

app.server.close();
});

it('finds and returns the config', async () => {
expect.assertions(2);
const spy = jest.spyOn(process, 'cwd');
spy.mockReturnValue(join(__dirname, './seeds/global/with/routes'));

const app = server({
routes: join(__dirname, './seeds/global/with/routes'),
opts: { requestLogger: false },
});
const url = await listen(app.server);

const req = await fetch(url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: { hello: 'world' } });
spy.mockRestore();

app.server.close();
});
});

describe('without global property', () => {
it('returns an empty object', async () => {
expect.assertions(2);
const spy = jest.spyOn(process, 'cwd');
spy.mockReturnValue(join(__dirname, './seeds/global/without'));

const app = server({
routes: join(__dirname, './seeds/global/without/routes'),
opts: { requestLogger: false },
});
const url = await listen(app.server);

const req = await fetch(url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: {} });
spy.mockRestore();

app.server.close();
});
});
});
5 changes: 5 additions & 0 deletions tests/seeds/global/with/light.config.js
@@ -0,0 +1,5 @@
module.exports = {
global: {
hello: 'world',
},
};
8 changes: 8 additions & 0 deletions tests/seeds/global/with/routes/index.ts
@@ -0,0 +1,8 @@
import { route, global } from '../../../../../src/index';

const { handler } = route();
const glob = global();

module.exports = handler(() => ({
hello: glob,
}));
2 changes: 2 additions & 0 deletions tests/seeds/global/without/light.config.js
@@ -0,0 +1,2 @@
module.exports = {
};
8 changes: 8 additions & 0 deletions tests/seeds/global/without/routes/index.ts
@@ -0,0 +1,8 @@
import { route, global } from '../../../../../src/index';

const { handler } = route();
const glob = global();

module.exports = handler(() => ({
hello: glob,
}));

0 comments on commit 74eb68a

Please sign in to comment.