Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlexLichter committed Apr 17, 2018
0 parents commit c8fe8b6
Show file tree
Hide file tree
Showing 16 changed files with 24,705 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: 2
jobs:
build:
working_directory: /usr/src/app
docker:
- image: banian/node
steps:
# Checkout repository
- checkout

# Restore cache
- restore_cache:
key: yarn-{{ checksum "yarn.lock" }}

# Install dependencies
- run:
name: Install Dependencies
command: NODE_ENV=dev yarn

# Keep cache
- save_cache:
key: yarn-{{ checksum "yarn.lock" }}
paths:
- "node_modules"

# Test
- run:
name: Tests
command: yarn test

# Coverage
- run:
name: Coverage
command: yarn codecov
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
30 changes: 30 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
root: true,
parserOptions: {
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
}
}
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
*.iml
.idea
*.log*
.nuxt
.vscode
.DS_STORE
coverage
dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Alexander Lichter <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Redirect Module 🔀 No more **cumbersome** redirects!
[![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/redirect-module/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/redirect-module)
[![npm](https://img.shields.io/npm/dt/@nuxtjs/redirect-module.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/redirect-module)
[![CircleCI](https://img.shields.io/circleci/project/github/nuxt-community/redirect-module.svg?style=flat-square)](https://circleci.com/gh/nuxt-community/redirect-module)
[![Codecov](https://img.shields.io/codecov/c/github/nuxt-community/redirect-module.svg?style=flat-square)](https://codecov.io/gh/nuxt-community/redirect-module)
[![Dependencies](https://david-dm.org/nuxt-community/redirect-module/status.svg?style=flat-square)](https://david-dm.org/nuxt-community/redirect-module)
[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)

>
[📖 **Release Notes**](./CHANGELOG.md)

## Features

Redirecting URLs is an often discussed topic, especially when it comes to
SEO. Previously it was hard to create a "real" redirect without performance
loss or incorrect handling. But this time is over!

With the Redirect Module setting up redirects will become easier than ever before!

## Setup
- Add `@nuxtjs/redirect-module` dependency using yarn or npm to your project
- Add `@nuxtjs/redirect-module` to `modules` section of `nuxt.config.js`
- Configure it:
```js
{
modules: [
// Simple usage
'@nuxtjs/redirect-module',

// With options
['@nuxtjs/redirect-module', []],
]
}
```

```js
{
modules: [
'@nuxtjs/redirect-module'
],
redirect: [
// Module options
]
}
```

## Usage

Simply add the links you want to redirect as objects to the module option array:

```js
redirect: [
{ from: '^/myoldurl', to: '/mynewurl' }
]
```

You can set up a custom status code as well. By default, it's *302*!

```js
redirect: [
{ from: '^/myoldurl', to: '/mynewurl', statusCode: 301 }
]
```

As you may have already noticed, we are leveraging the benefits of
*Regular Expressions*. Hence, you can fully customize your redirects.

```js
redirect: [
{ from: '^/myoldurl/(.*)$', to: '/comeallhere', } // Many urls to one
{ from: '^/anotherold/(.*)$', to: '/new/$1', } // One to one mapping
]
```

And if you really need more power... okay! You can also use a factory function
to generate your redirects:

```js
redirect: async () => {
const someThings = await axios.get("/myApi") // It'll wait!
return someThings.map(coolTransformFunction)
}
```

**ATTENTION**: The factory function **must** return an array with redirect
objects (as seen above).

## Development

- Clone this repository
- Install dependencies using `yarn install` or `npm install`
- Start development server using `npm run dev`

## License

[MIT License](./LICENSE)

Copyright (c) Alexander Lichter <[email protected]>
17 changes: 17 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Creates new middleware using provided options
function create (rules) {
return function redirectRoute (req, res, next) {
const foundRule = rules.find(o => o.from.test(req.url))

if (!foundRule) {
return next()
}
const toUrl = req.url.replace(foundRule.from, foundRule.to)

res.statusCode = foundRule.statusCode || 302
res.setHeader('Location', toUrl)
res.end()
}
}

module.exports = create
16 changes: 16 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = async function module (moduleOptions) {
if (typeof moduleOptions === 'function') {
moduleOptions = await moduleOptions()
}
if (typeof this.options.redirect === 'function') {
this.options.redirect = await this.options.redirect()
}

const initialRules = Object.assign([], this.options.redirect, 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 middleware = require('./middleware.js')(regExpRules)

this.addServerMiddleware(middleware)
}
Loading

0 comments on commit c8fe8b6

Please sign in to comment.