Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESLint and TypeScript #458

Merged
merged 12 commits into from Aug 4, 2022
10 changes: 6 additions & 4 deletions .github/workflows/ci.yaml
Expand Up @@ -11,9 +11,11 @@ jobs:
node-version:
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- name: Install dependencies
run: yarn
- name: Run tests
run: yarn run test
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -8,3 +8,6 @@ yarn-error.log
# coverage
coverage
.nyc_output

# build
dist/
2 changes: 2 additions & 0 deletions .npmrc
@@ -0,0 +1,2 @@
save-exact = true
strict-peer-dependencies=false
17 changes: 5 additions & 12 deletions package.json
@@ -1,24 +1,17 @@
{
"private": true,
"workspaces": [
"packages/*"
"packages/*",
"test"
],
"scripts": {
"test": "NODE_ENV=test nyc --check-coverage --statements 100 --branches 100 --functions 100 --lines 100 ava",
"prepublish": "lerna run prepublish",
"publish-canary": "lerna version prerelease --preid canary --force-publish && release --pre",
"publish-stable": "lerna version --force-publish"
"publish-stable": "lerna version --force-publish",
"test": "cd test && yarn run test"
},
"license": "MIT",
"devDependencies": {
"ava": "0.23.0",
"lerna": "^3.4.0",
"node-fetch": "2.6.0",
"nyc": "11.3.0",
"resumer": "0.0.0",
"rewire": "3.0.2",
"sinon": "4.4.3",
"test-listen": "1.0.2",
"then-sleep": "1.0.1"
"lerna": "^3.4.0"
}
}
12 changes: 12 additions & 0 deletions packages/micro/.eslintrc.js
@@ -0,0 +1,12 @@
module.exports = {
root: true,
extends: [
require.resolve('@vercel/style-guide/eslint/node'),
require.resolve('@vercel/style-guide/eslint/typescript'),
],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
ignorePatterns: ['dist/**', 'types/**'],
};
34 changes: 18 additions & 16 deletions packages/micro/README.md
Expand Up @@ -214,21 +214,23 @@ module.exports = async (req, res) => {
You can use Micro programmatically by requiring Micro directly:

```js
const micro = require('micro')
const sleep = require('then-sleep')
const http = require('http');
const { serve } = require('micro');
const sleep = require('then-sleep');

const server = micro(async (req, res) => {
await sleep(500)
return 'Hello world'
})
const server = new http.Server(
serve(async (req, res) => {
await sleep(500);
return 'Hello world';
}),
);

server.listen(3000)
server.listen(3000);
```

##### micro(fn)
##### serve(fn)

- This function is exposed as the `default` export.
- Use `require('micro')`.
- Use `require('micro').serve`.
- Returns a function with the `(req, res) => void` signature. That uses the provided `function` as the request handler.
- The supplied function is run with `await`. So it can be `async`

Expand Down Expand Up @@ -320,22 +322,22 @@ module.exports = handleErrors(async (req, res) => {
## Testing

Micro makes tests compact and a pleasure to read and write.
We recommend [ava](https://github.com/sindresorhus/ava), a highly parallel Micro test framework with built-in support for async tests:
We recommend [Node TAP](https://node-tap.org/) or [AVA](https://github.com/avajs/ava), a highly parallel test framework with built-in support for async tests:

```js
const http = require('http');
const micro = require('micro');
const { send, serve } = require('micro');
const test = require('ava');
const listen = require('test-listen');
const fetch = require('node-fetch');

test('my endpoint', async (t) => {
const service = new http.Server(
micro(async (req, res) => {
micro.send(res, 200, {
serve(async (req, res) => {
send(res, 200, {
test: 'woot',
});
})
}),
);

const url = await listen(service);
Expand All @@ -356,7 +358,7 @@ function that returns a URL with an ephemeral port every time it's called.
2. Link the package to the global module directory: `npm link`
3. Within the module you want to test your local development instance of Micro, just link it to the dependencies: `npm link micro`. Instead of the default one from npm, node will now use your clone of Micro!

You can run the [AVA](https://github.com/sindresorhus/ava) tests using: `npm test`
You can run the tests using: `npm test`.

## Credits

Expand Down
233 changes: 0 additions & 233 deletions packages/micro/bin/micro.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/micro/lib/error.js

This file was deleted.