Skip to content

Commit

Permalink
feat: Convert to TypeScript, add ESLint, drop Node.js 12 support and …
Browse files Browse the repository at this point in the history
…deprecated CLI options. (#458)

* chore: add .npmrc

* feat: configure TypeScript and ESLint

* feat!: rewrite modules/files using TypeScript

* chore: export types for the modules

* feat: change minimum node version to 14.5.0

BREAKING CHANGE: Switched to using ES2020 syntax and minimum Node engine to 14.5.0.

* feat!: removes the deprecated cli options

BREAKING CHANGE: Removes the deprecated options/arguments for starting the server. They are:

- --port (-p)
- --host (-h)
- --unix-socket (-s)

* test!: rewrite test using TypeScript & node-tap

* build: update ci script

* chore: update ESLint config

* ci: run the test script in the right directory

* ci: add test script to root package.json

* doc: update usage programmatically
  • Loading branch information
pmbanugo committed Aug 4, 2022
1 parent 406e6c0 commit 8d2ca07
Show file tree
Hide file tree
Showing 48 changed files with 4,582 additions and 3,339 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ yarn-error.log
# coverage
coverage
.nyc_output

# build
dist/
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
save-exact = true
strict-peer-dependencies=false
17 changes: 5 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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.

0 comments on commit 8d2ca07

Please sign in to comment.