Skip to content

Commit 834d962

Browse files
authored
feat: add logger middleware (#2)
* chore: change license to MIT * feat: add yab-fetch-logger * chore: update root readme of packages
1 parent 4807a42 commit 834d962

24 files changed

+6291
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ This repository is a monorepo that we manage using Lerna. That means that we act
7878
| -------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
7979
| [`yab-fetch`](/packages/yab-fetch) | [![npm](https://img.shields.io/npm/v/yab-fetch.svg?style=flat-square)](https://www.npmjs.com/package/yab-fetch) | The fetch library. |
8080
| [`yab-fetch-cache`](/packages/yab-fetch-cache) | [![npm](https://img.shields.io/npm/v/yab-fetch-cache.svg?style=flat-square)](https://www.npmjs.com/package/yab-fetch-cache) | A yab middleware, focus on cache response using IndexDB. |
81+
| [`yab-fetch-logger`](/packages/yab-fetch-logger) | [![npm](https://img.shields.io/npm/v/yab-fetch-logger.svg?style=flat-square)](https://www.npmjs.com/package/yab-fetch-logger) | A yab middleware, logger request/response of fetch action |
8182

8283
## Changelog
8384

packages/yab-fetch-cache/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.1-alpha.1",
44
"description": "",
55
"homepage": "https://github.com/mjolnirjs/yab/tree/master/packages/yab-fetch-cache#readme",
6-
"license": "ISC",
6+
"license": "MIT",
77
"repository": {
88
"type": "git",
99
"url": "git+https://github.com/mjolnirjs/yab.git"
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[*]
2+
3+
indent_style = space
4+
indent_size = 2
5+
charset = utf-8
6+
insert_final_newline = true
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
config
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
extends: [
3+
'airbnb-base',
4+
'plugin:import/typescript',
5+
'plugin:@typescript-eslint/recommended',
6+
'plugin:@typescript-eslint/eslint-recommended',
7+
'plugin:prettier/recommended',
8+
'prettier/@typescript-eslint'
9+
],
10+
rules: {
11+
'import/prefer-default-export': 'off',
12+
'no-underscore-dangle': 'off',
13+
'@typescript-eslint/explicit-function-return-type': 'off',
14+
'@typescript-eslint/interface-name-prefix': 'off'
15+
}
16+
};

packages/yab-fetch-logger/.gitignore

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env
67+
.env.test
68+
69+
# parcel-bundler cache (https://parceljs.org/)
70+
.cache
71+
72+
# next.js build output
73+
.next
74+
75+
# nuxt.js build output
76+
.nuxt
77+
78+
# vuepress build output
79+
.vuepress/dist
80+
81+
# Serverless directories
82+
.serverless/
83+
84+
# FuseBox cache
85+
.fusebox/
86+
87+
# DynamoDB Local files
88+
.dynamodb/
89+
90+
91+
# custom
92+
build
93+
dist
94+
lib
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**/*.md
2+
**/*.svg
3+
**/*.ejs
4+
**/*.html
5+
package.json
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
printWidth: 80,
3+
singleQuote: true,
4+
arrowParens: 'always'
5+
};

packages/yab-fetch-logger/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# yab-fetch-logger
2+
3+
## Install
4+
5+
`npm i yab-fetch-logger`
6+
7+
## Useage
8+
> **must be the last middleware in chain.**
9+
10+
11+
```ts
12+
import { createFetch } from 'yab-fetch';
13+
import { createLogger } from 'yab-fetch-logger';
14+
15+
const logger = createLogger({
16+
// options
17+
});
18+
19+
const request = createFetch({
20+
middleware: [logger],
21+
});
22+
```
23+
24+
## Example
25+
26+
![example](assets/log-example.png)
41.1 KB
Loading
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require('path');
2+
3+
// 拼接目录路径
4+
function resolve(dir) {
5+
return path.join(__dirname, '..', dir);
6+
}
7+
8+
module.exports = {
9+
resolve
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
2+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
3+
4+
const { resolve } = require('./utils');
5+
const projectName = 'yab-fetch-logger';
6+
7+
module.exports = {
8+
entry: {
9+
[`${projectName}.min`]: [resolve('src')] // same as resolve('src/index.js');
10+
},
11+
output: {
12+
filename: '[name].js',
13+
path: resolve('dist'),
14+
library: projectName,
15+
libraryTarget: 'umd'
16+
},
17+
resolve: {
18+
extensions: ['.ts', '.tsx', '.js', '.json']
19+
},
20+
module: {
21+
rules: [
22+
{
23+
enforce: 'pre',
24+
test: /\.(ts|tsx)?$/,
25+
exclude: /node_modules/,
26+
loader: 'eslint-loader'
27+
},
28+
{
29+
test: /\.(ts|tsx)?$/,
30+
exclude: /node_modules/,
31+
use: [{ loader: 'ts-loader', options: { transpileOnly: true } }]
32+
}
33+
]
34+
},
35+
plugins: [new CaseSensitivePathsPlugin(), new ForkTsCheckerWebpackPlugin()]
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const merge = require('webpack-merge');
2+
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
3+
const ForkTsCheckerNotifierWebpackPlugin = require('fork-ts-checker-notifier-webpack-plugin');
4+
5+
const baseConfig = require('./webpack.config.base');
6+
7+
const developmentConfig = merge(baseConfig, {
8+
mode: 'development',
9+
devServer: {
10+
// Enable history API fallback so HTML5 History API based
11+
// routing works. Good for complex setups.
12+
historyApiFallback: true,
13+
14+
// Display only errors to reduce the amount of output.
15+
stats: 'errors-only',
16+
17+
// Parse host and port from env to allow customization.
18+
//
19+
// If you use Docker, Vagrant or Cloud9, set
20+
// host: options.host || '0.0.0.0';
21+
//
22+
// 0.0.0.0 is available to all network devices
23+
// unlike default `localhost`.
24+
port: 8080, // Defaults to 8080
25+
// overlay: true is equivalent
26+
overlay: {
27+
errors: true,
28+
warnings: false
29+
},
30+
disableHostCheck: true,
31+
// 配合 FriendlyErrorsWebpackPlugin, 只展示 Friendly 处理后的
32+
quiet: true
33+
34+
// publicPath 设置的话, 是使得 bundle 的文件,在此路径下访问
35+
// publicPath: '/assets/',
36+
// proxy: {
37+
// '/api': {
38+
// target: 'http://localhost:8080',
39+
// pathRewrite: { '^/api': '' }
40+
// }
41+
// }
42+
},
43+
devtool: 'cheap-module-eval-source-map',
44+
plugins: [
45+
new FriendlyErrorsWebpackPlugin(),
46+
new ForkTsCheckerNotifierWebpackPlugin({ excludeWarnings: true })
47+
]
48+
});
49+
50+
module.exports = developmentConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const merge = require('webpack-merge');
2+
const webpack = require('webpack');
3+
const TerserPlugin = require('terser-webpack-plugin');
4+
5+
const baseConfig = require('./webpack.config.base');
6+
7+
const productionConfig = merge(baseConfig, {
8+
mode: 'production',
9+
plugins: [
10+
new webpack.DefinePlugin({
11+
'process.env': {
12+
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
13+
}
14+
})
15+
],
16+
optimization: {
17+
minimizer: [new TerserPlugin()]
18+
}
19+
});
20+
21+
module.exports = productionConfig;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Log Middleware</title>
8+
</head>
9+
<body>
10+
<script src="../../yab-fetch//dist/yab-fetch.min.js"></script>
11+
<script src="../dist/yab-fetch-logger.min.js"></script>
12+
</body>
13+
</html>
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
preset: 'ts-jest'
3+
};
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "yab-fetch-logger",
3+
"version": "0.0.1-alpha.1",
4+
"description": "",
5+
"homepage": "https://github.com/mjolnirjs/yab/tree/master/packages/yab-fetch-logger#readme",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/mjolnirjs/yab.git"
10+
},
11+
"scripts": {
12+
"clean": "rimraf dist",
13+
"build": "npm run clean && npm run build:es && npm run build:lib && npm run build:dist",
14+
"build:es": "tsc -p ./tsconfig.es.json",
15+
"build:lib": "tsc -p ./tsconfig.lib.json",
16+
"build:dist": "cross-env NODE_ENV=production webpack --config ./config/webpack.config.dist.js",
17+
"lint": "eslint src --ext ts"
18+
},
19+
"main": "./dist/lib/index.js",
20+
"module": "./dist/es/index.js",
21+
"types": "./dist/types/index.d.ts",
22+
"devDependencies": {
23+
"@typescript-eslint/eslint-plugin": "^1.9.0",
24+
"@typescript-eslint/parser": "^1.9.0",
25+
"cache-loader": "^3.0.1",
26+
"case-sensitive-paths-webpack-plugin": "^2.2.0",
27+
"cross-env": "^5.2.0",
28+
"eslint": "^5.16.0",
29+
"eslint-config-airbnb-base": "^13.1.0",
30+
"eslint-config-prettier": "^4.3.0",
31+
"eslint-loader": "^2.1.2",
32+
"eslint-plugin-import": "^2.17.3",
33+
"eslint-plugin-prettier": "^3.1.0",
34+
"fork-ts-checker-notifier-webpack-plugin": "^1.0.0",
35+
"fork-ts-checker-webpack-plugin": "^1.3.4",
36+
"friendly-errors-webpack-plugin": "^1.7.0",
37+
"jest": "^24.8.0",
38+
"prettier": "^1.17.1",
39+
"rimraf": "^2.6.3",
40+
"terser-webpack-plugin": "^1.3.0",
41+
"ts-jest": "^24.0.2",
42+
"ts-loader": "^6.0.3",
43+
"typescript": "^3.5.2",
44+
"webpack": "^4.32.2",
45+
"webpack-cli": "^3.3.2",
46+
"webpack-merge": "^4.2.1",
47+
"whatwg-fetch": "^3.0.0",
48+
"yab-fetch": "^0.0.1-alpha.1"
49+
},
50+
"files": [
51+
"dist"
52+
],
53+
"bugs": {
54+
"url": "https://github.com/mjolnirjs/yab/issues"
55+
}
56+
}

0 commit comments

Comments
 (0)