@nuxtjs/redirect-module
Advanced tools
Comparing version
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.2.0"></a> | ||
# [0.2.0](https://github.com/nuxt-community/redirect-module/compare/v0.1.0...v0.2.0) (2018-11-24) | ||
### Features | ||
* allow function as `to` value ([#7](https://github.com/nuxt-community/redirect-module/issues/7)) ([9ed5407](https://github.com/nuxt-community/redirect-module/commit/9ed5407)) | ||
<a name="0.1.0"></a> | ||
@@ -7,0 +17,0 @@ # [0.1.0](https://github.com/nuxt-community/redirect-module/compare/v0.0.2...v0.1.0) (2018-08-24) |
/* eslint-disable no-console */ | ||
// Creates new middleware using provided options | ||
function create (rules) { | ||
return function redirectRoute (req, res, next) { | ||
function create(rules) { | ||
return async function redirectRoute(req, res, next) { | ||
const decodedBaseUrl = decodeURI(req.url) | ||
@@ -12,4 +12,11 @@ const foundRule = rules.find(o => o.from.test(decodedBaseUrl)) | ||
} | ||
const toUrl = decodedBaseUrl.replace(foundRule.from, foundRule.to) | ||
// Expect rule 'to' to either a | ||
// 1) regex | ||
// 2) string | ||
// 3) function taking from & req (when from is regex, req might be more interesting) | ||
const toTarget = typeof foundRule.to === 'function' ? await foundRule.to(foundRule.from, req) : foundRule.to | ||
const toUrl = decodedBaseUrl.replace(foundRule.from, toTarget) | ||
res.statusCode = foundRule.statusCode || 302 | ||
@@ -16,0 +23,0 @@ res.setHeader('Location', toUrl) |
@@ -1,2 +0,2 @@ | ||
module.exports = async function module (moduleOptions) { | ||
export default async function nuxtRedirect(moduleOptions) { | ||
if (typeof moduleOptions === 'function') { | ||
@@ -12,3 +12,3 @@ moduleOptions = await moduleOptions() | ||
// Transform each "from" value to a RegExp for later test | ||
const regExpRules = initialRules.map(o => (Object.assign({}, o, { from: new RegExp(o.from) }))) | ||
const regExpRules = initialRules.map(o => Object.assign(o, { from: new RegExp(o.from) })) | ||
const middleware = require('./middleware.js')(regExpRules) | ||
@@ -15,0 +15,0 @@ |
{ | ||
"name": "@nuxtjs/redirect-module", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -20,3 +20,3 @@ "license": "MIT", | ||
"commitlint": "commitlint -e $GIT_PARAMS", | ||
"test": "npm run lint && jest", | ||
"test": "yarn lint && jest", | ||
"release": "standard-version && git push --follow-tags && npm publish" | ||
@@ -49,18 +49,20 @@ }, | ||
"devDependencies": { | ||
"@commitlint/cli": "^7.0.0", | ||
"@commitlint/config-conventional": "^7.0.1", | ||
"codecov": "latest", | ||
"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": "latest", | ||
"husky": "^1.0.0-rc.13", | ||
"jest": "latest", | ||
"jsdom": "latest", | ||
"nuxt": "latest", | ||
"standard-version": "latest" | ||
"@commitlint/cli": "^7.2.1", | ||
"@commitlint/config-conventional": "^7.1.2", | ||
"@nuxtjs/eslint-config": "^0.0.1", | ||
"babel-eslint": "^10.0.1", | ||
"codecov": "^3.1.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jest": "^22.0.0", | ||
"eslint-plugin-node": "^8.0.0", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"eslint-plugin-vue": "^4.7.1", | ||
"husky": "^1.2.0", | ||
"jest": "^23.6.0", | ||
"jsdom": "^13.0.0", | ||
"nuxt": "^2.3.2", | ||
"standard-version": "^4.4.0" | ||
}, | ||
@@ -67,0 +69,0 @@ "husky": { |
@@ -75,2 +75,18 @@ # Redirect Module 🔀 No more **cumbersome** redirects! | ||
Furthermoer you can use a function to create your `to` url as well :+1: | ||
The `from` rule and the `req` of the middleware will be provided as arguments. | ||
The function can also be *async*! | ||
```js | ||
redirect: [ | ||
{ | ||
from: '^/someUrlHere/(.*)$', | ||
to: (from, req) => { | ||
const param = req.url.match(/functionAsync\/(.*)$/)[1] | ||
return `/posts/${param}` | ||
} | ||
} | ||
] | ||
``` | ||
And if you really need more power... okay! You can also use a factory function | ||
@@ -77,0 +93,0 @@ to generate your redirects: |
8428
13.85%34
17.24%115
16.16%18
12.5%