Skip to content

Commit 8d2ca07

Browse files
authored
feat: Convert to TypeScript, add ESLint, drop Node.js 12 support and 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
1 parent 406e6c0 commit 8d2ca07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4582
-3339
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ jobs:
1111
node-version:
1212
- 16
1313
steps:
14-
- uses: actions/checkout@v2
15-
- uses: actions/setup-node@v1
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-node@v3
1616
with:
1717
node-version: ${{ matrix.node-version }}
18-
- run: npm install
19-
- run: npm test
18+
- name: Install dependencies
19+
run: yarn
20+
- name: Run tests
21+
run: yarn run test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ yarn-error.log
88
# coverage
99
coverage
1010
.nyc_output
11+
12+
# build
13+
dist/

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
save-exact = true
2+
strict-peer-dependencies=false

package.json

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
{
22
"private": true,
33
"workspaces": [
4-
"packages/*"
4+
"packages/*",
5+
"test"
56
],
67
"scripts": {
7-
"test": "NODE_ENV=test nyc --check-coverage --statements 100 --branches 100 --functions 100 --lines 100 ava",
88
"prepublish": "lerna run prepublish",
99
"publish-canary": "lerna version prerelease --preid canary --force-publish && release --pre",
10-
"publish-stable": "lerna version --force-publish"
10+
"publish-stable": "lerna version --force-publish",
11+
"test": "cd test && yarn run test"
1112
},
1213
"license": "MIT",
1314
"devDependencies": {
14-
"ava": "0.23.0",
15-
"lerna": "^3.4.0",
16-
"node-fetch": "2.6.0",
17-
"nyc": "11.3.0",
18-
"resumer": "0.0.0",
19-
"rewire": "3.0.2",
20-
"sinon": "4.4.3",
21-
"test-listen": "1.0.2",
22-
"then-sleep": "1.0.1"
15+
"lerna": "^3.4.0"
2316
}
2417
}

packages/micro/.eslintrc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
root: true,
3+
extends: [
4+
require.resolve('@vercel/style-guide/eslint/node'),
5+
require.resolve('@vercel/style-guide/eslint/typescript'),
6+
],
7+
parserOptions: {
8+
tsconfigRootDir: __dirname,
9+
project: ['./tsconfig.json'],
10+
},
11+
ignorePatterns: ['dist/**', 'types/**'],
12+
};

packages/micro/README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,23 @@ module.exports = async (req, res) => {
214214
You can use Micro programmatically by requiring Micro directly:
215215

216216
```js
217-
const micro = require('micro')
218-
const sleep = require('then-sleep')
217+
const http = require('http');
218+
const { serve } = require('micro');
219+
const sleep = require('then-sleep');
219220

220-
const server = micro(async (req, res) => {
221-
await sleep(500)
222-
return 'Hello world'
223-
})
221+
const server = new http.Server(
222+
serve(async (req, res) => {
223+
await sleep(500);
224+
return 'Hello world';
225+
}),
226+
);
224227

225-
server.listen(3000)
228+
server.listen(3000);
226229
```
227230

228-
##### micro(fn)
231+
##### serve(fn)
229232

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

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

322324
Micro makes tests compact and a pleasure to read and write.
323-
We recommend [ava](https://github.com/sindresorhus/ava), a highly parallel Micro test framework with built-in support for async tests:
325+
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:
324326

325327
```js
326328
const http = require('http');
327-
const micro = require('micro');
329+
const { send, serve } = require('micro');
328330
const test = require('ava');
329331
const listen = require('test-listen');
330332
const fetch = require('node-fetch');
331333

332334
test('my endpoint', async (t) => {
333335
const service = new http.Server(
334-
micro(async (req, res) => {
335-
micro.send(res, 200, {
336+
serve(async (req, res) => {
337+
send(res, 200, {
336338
test: 'woot',
337339
});
338-
})
340+
}),
339341
);
340342

341343
const url = await listen(service);
@@ -356,7 +358,7 @@ function that returns a URL with an ephemeral port every time it's called.
356358
2. Link the package to the global module directory: `npm link`
357359
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!
358360

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

361363
## Credits
362364

packages/micro/bin/micro.js

Lines changed: 0 additions & 233 deletions
This file was deleted.

packages/micro/lib/error.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)