This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(global): set up global config (#54)
fix(global): set up global config
- Loading branch information
Showing
9 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
}; | ||
}); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
global: { | ||
hello: 'world', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { route, global } from '../../../../../src/index'; | ||
|
||
const { handler } = route(); | ||
const glob = global(); | ||
|
||
module.exports = handler(() => ({ | ||
hello: glob, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = { | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { route, global } from '../../../../../src/index'; | ||
|
||
const { handler } = route(); | ||
const glob = global(); | ||
|
||
module.exports = handler(() => ({ | ||
hello: glob, | ||
})); |