Skip to content

Commit

Permalink
refactor(module): update module (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored and pi0 committed Feb 16, 2019
1 parent 1f2092e commit 0bfced3
Show file tree
Hide file tree
Showing 16 changed files with 2,430 additions and 2,602 deletions.
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Common
node_modules
dist
.nuxt
coverage

# Plugin
lib/plugin.js
28 changes: 4 additions & 24 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint',
sourceType: 'module'
},
env: {
browser: true,
node: true,
jest: true
},
extends: 'standard',
plugins: [
'jest',
'vue'
],
rules: {
// Allow paren-less arrow functions
'arrow-parens': 0,
// Allow async-await
'generator-star-spacing': 0,
// Allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
// Do not allow console.logs etc...
'no-console': 2
},
globals: {
'jest/globals': true,
jasmine: true
}
extends: [
'@nuxtjs'
]
}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ node_modules
*.log*
.nuxt
.vscode
.DS_STORE
.DS_Store
coverage
dist
dist
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Nuxt Community - Pooya Parsa <pooya@pi0.ir>
Copyright (c) Nuxt Community - Pooya Parsa <pooya@pi0.ir>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
49 changes: 39 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Proxy Module

[![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/proxy/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/proxy)
[![npm](https://img.shields.io/npm/dt/@nuxtjs/proxy.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/proxy)
[![CircleCI](https://img.shields.io/circleci/project/github/nuxt-community/proxy-module.svg?style=flat-square)](https://circleci.com/gh/nuxt-community/proxy-module)
[![Codecov](https://img.shields.io/codecov/c/github/nuxt-community/proxy-module.svg?style=flat-square)](https://codecov.io/gh/nuxt-community/proxy-module)
[![Dependencies](https://david-dm.org/nuxt-community/proxy-module/status.svg?style=flat-square)](https://david-dm.org/nuxt-community/proxy-module)
[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Circle CI][circle-ci-src]][circle-ci-href]
[![Codecov][codecov-src]][codecov-href]
[![Dependencies][david-dm-src]][david-dm-href]
[![Standard JS][standard-js-src]][standard-js-href]

> The one-liner node.js http-proxy middleware solution for Nuxt.js using
[http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware)
> The one-liner node.js http-proxy middleware solution for Nuxt.js using [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware).
[📖 **Release Notes**](./CHANGELOG.md)

Expand All @@ -31,8 +30,10 @@
⚠ Does not work in generated/static mode!

## Setup
- Add `@nuxtjs/proxy` dependency using yarn or npm to your project
- Add `@nuxtjs/proxy` to `modules` section of `nuxt.config.js`

1. Add `@nuxtjs/proxy` dependency with `yarn` or `npm` into your project
2. Add `@nuxtjs/proxy` to `modules` section of `nuxt.config.js`
3. Configure it:

```js
{
Expand All @@ -49,14 +50,17 @@
- Define as many as proxy middleware you want in `proxy` section of `nuxt.config.js` (See [proxy](#proxy) section below)

## Options

- `changeOrigin` and `ws` options are enabled by default.

[optional] You can provide default options to all proxy targets by passing options to module options.

## `proxy`

You can provide proxy config using either object or array.

### Array mode

You can use magic [shorthands](https://github.com/chimurai/http-proxy-middleware#shorthand)

```js
Expand All @@ -75,6 +79,7 @@ You can use magic [shorthands](https://github.com/chimurai/http-proxy-middleware
```

### Object mode

Keys are [context](https://github.com/chimurai/http-proxy-middleware#context-matching)

```js
Expand All @@ -89,9 +94,33 @@ Keys are [context](https://github.com/chimurai/http-proxy-middleware#context-mat
}
```

## Development

1. Clone this repository
2. Install dependencies using `yarn install` or `npm install`
3. Start development server using `npm run dev`

## License

[MIT License](./LICENSE)

Copyright (c) Nuxt Community - Pooya Parsa <pooya@pi0.ir>

<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/dt/@nuxtjs/proxy.svg?style=flat-square
[npm-version-href]: https://npmjs.com/package/@nuxtjs/proxy

[npm-downloads-src]: https://img.shields.io/npm/v/@nuxtjs/proxy/latest.svg?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/proxy

[circle-ci-src]: https://img.shields.io/circleci/project/github/nuxt-community/proxy-module.svg?style=flat-square
[circle-ci-href]: https://circleci.com/gh/nuxt-community/proxy-module

[codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/proxy-module.svg?style=flat-square
[codecov-href]: https://codecov.io/gh/nuxt-community/proxy-module

[david-dm-src]: https://david-dm.org/nuxt-community/proxy-module/status.svg?style=flat-square
[david-dm-href]: https://david-dm.org/nuxt-community/proxy-module

[standard-js-src]: https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square
[standard-js-href]: https://standardjs.com
11 changes: 11 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
presets: [
[
'@babel/preset-env', {
targets: {
esmodules: true
}
}
]
]
}
5 changes: 5 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
extends: [
'@commitlint/config-conventional'
]
}
7 changes: 7 additions & 0 deletions husky.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
hooks: {
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
'pre-commit': 'yarn lint',
'pre-push': 'yarn lint'
}
}
17 changes: 17 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: [
'lib/**/*.js',
'!lib/plugin.js'
],
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/lib/$1',
'^~~$': '<rootDir>',
'^@@$': '<rootDir>',
'^@/(.*)$': '<rootDir>/lib/$1'
},
transform: {
'^.+\\.js$': 'babel-jest'
}
}
17 changes: 9 additions & 8 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const Proxy = require('http-proxy-middleware')
const consola = require('consola')

const logProvider = consola.withScope('nuxt:proxy')
const logger = consola.withScope('nuxt:proxy')

// Redirect info ~> debug
logProvider.info = logProvider.debug
logger.info = logger.debug

module.exports = function nuxtProxy (options) {
function proxyModule(options) {
if (!this.options.proxy) {
// No proxy defined
return
}

this.nuxt.hook('generate:before', () => {
consola.warn('This module does not work in generated mode')
logger.warn('The module `@nuxtjs/proxy` does not work in generated mode.')
})

// Defaults
Expand All @@ -24,7 +24,7 @@ module.exports = function nuxtProxy (options) {
// Proxy webSockets
ws: true,
// Use consola as default logger
logProvider: () => logProvider
logProvider: () => logger
},
options
)
Expand All @@ -38,7 +38,7 @@ module.exports = function nuxtProxy (options) {
const proxy = []
if (Array.isArray(this.options.proxy)) {
// Array mode
this.options.proxy.forEach(p => {
this.options.proxy.forEach((p) => {
if (Array.isArray(p)) {
proxy.push([p[0], applyDefaults(normalizeTarget(p[1]))])
} else {
Expand All @@ -47,7 +47,7 @@ module.exports = function nuxtProxy (options) {
})
} else {
// Object mode
Object.keys(this.options.proxy).forEach(context => {
Object.keys(this.options.proxy).forEach((context) => {
proxy.push([
context,
applyDefaults(normalizeTarget(this.options.proxy[context]))
Expand All @@ -56,12 +56,13 @@ module.exports = function nuxtProxy (options) {
}

// Register middleware
proxy.forEach(args => {
proxy.forEach((args) => {
// https://github.com/chimurai/http-proxy-middleware
const middleware = Proxy.apply(undefined, args)
middleware.prefix = false // Don't add router base
this.options.serverMiddleware.push(middleware)
})
}

module.exports = proxyModule
module.exports.meta = require('../package.json')
49 changes: 25 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "@nuxtjs/proxy",
"version": "1.3.1",
"description": "The one-liner node.js http-proxy middleware solution for Nuxt.js using http-proxy-middleware",
"description": "The one-liner node.js http-proxy middleware solution for Nuxt.js using http-proxy-middleware.",
"license": "MIT",
"contributors": [
{
"name": "Pooya Parsa <pooya@pi0.ir>"
}
"Pooya Parsa <pooya@pi0.ir>"
],
"main": "lib/module.js",
"repository": "https://github.com/nuxt-community/proxy-module",
Expand All @@ -16,36 +14,39 @@
"scripts": {
"dev": "nuxt test/fixture",
"lint": "eslint lib test",
"test": "npm run lint && jest",
"release": "standard-version && git push --follow-tags && npm publish"
"test": "yarn lint && jest",
"release": "yarn test && standard-version && git push --follow-tags && npm publish"
},
"eslintIgnore": [
"lib/templates/*.*"
],
"files": [
"lib"
],
"jest": {
"testEnvironment": "node",
"collectCoverage": true
},
"dependencies": {
"consola": "^2.4.0",
"consola": "^2.4.1",
"http-proxy-middleware": "^0.19.1"
},
"devDependencies": {
"@babel/core": "latest",
"@babel/preset-env": "latest",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@nuxtjs/eslint-config": "latest",
"babel-eslint": "latest",
"babel-jest": "latest",
"codecov": "latest",
"eslint": "^5.13.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^21.27.2",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint": "latest",
"eslint-config-standard": "latest",
"eslint-plugin-import": "latest",
"eslint-plugin-jest": "latest",
"eslint-plugin-node": "latest",
"eslint-plugin-promise": "latest",
"eslint-plugin-standard": "latest",
"eslint-plugin-vue": "^4.7.1",
"jest": "^23.6.0",
"jsdom": "^13.2.0",
"nuxt-edge": "2.3.0-25706271.cca799c",
"eslint-plugin-vue": "latest",
"get-port": "latest",
"husky": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"request": "latest",
"request-promise-native": "latest",
"standard-version": "latest"
}
}
2 changes: 1 addition & 1 deletion test/fixture/middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
path: '/api',
handler (req, res) {
handler(req, res) {
res.end('url:' + req.url)
}
}
6 changes: 4 additions & 2 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const { resolve } = require('path')

module.exports = {
rootDir: resolve(__dirname, '../..'),
buildDir: resolve(__dirname, '.nuxt'),
srcDir: __dirname,
dev: false,
render: {
resourceHints: false
},
modules: ['@@'],
modules: [
{ handler: require('../../') }
],
serverMiddleware: ['~/middleware.js'],
proxy: [
'http://icanhazip.com'
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
<div>
Works!
</div>
</div>
</template>

<script>
Expand Down
Loading

0 comments on commit 0bfced3

Please sign in to comment.