Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nickforddev committed Oct 4, 2017
0 parents commit 7da2b3c
Show file tree
Hide file tree
Showing 32 changed files with 12,243 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .babelrc
@@ -0,0 +1,18 @@
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
}
}],
"stage-2"
],
"plugins": ["transform-runtime"],
"env": {
"test": {
"presets": ["env", "stage-2"],
"plugins": ["istanbul"]
}
}
}
1 change: 1 addition & 0 deletions .coveralls.yml
@@ -0,0 +1 @@
repo_token: DQiQCSLbENvvOLn0NXP8nu8KBKXnPdRqk
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
build/*.js
config/*.js
30 changes: 30 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,30 @@
// http://eslint.org/docs/user-guide/configuring

module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
sourceType: 'module'
},
env: {
browser: true,
jest: true
},
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
extends: 'standard',
// required to lint *.vue files
plugins: [
'html'
],
// add your custom rules here
'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,
'camelcase': 0,
'space-before-function-paren': 0
}
}
14 changes: 14 additions & 0 deletions .gitignore
@@ -0,0 +1,14 @@
.DS_Store
node_modules/
dist/
/coverage/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
8 changes: 8 additions & 0 deletions .postcssrc.js
@@ -0,0 +1,8 @@
// https://github.com/michael-ciniawsky/postcss-load-config

module.exports = {
"plugins": {
// to edit target browsers: use "browserslist" field in package.json
"autoprefixer": {}
}
}
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "7"
152 changes: 152 additions & 0 deletions README.md
@@ -0,0 +1,152 @@
# vue-requests

[![Build](https://travis-ci.org/nickforddesign/vue-requests.svg?branch=master)](#)
[![Coverage Status](https://coveralls.io/repos/github/nickforddesign/vue-requests/badge.svg?branch=master)](https://coveralls.io/github/nickforddesign/vue-requests?branch=master)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

> A modern fetch plugin for Vue.js with easy config config and hooks
## Installation

``` bash
npm install vue-requests
```

## Setup

```js
import Vue from 'vue'
import VueRequests from 'vue-requests'

Vue.use(VueRequests, options)
```

## Options

### headers [Object]
Headers can contain any custom headers you'd like to include in your requests. The values can be properties or methods (ie. their values can either be functions, literals, or references). Use functions for values that may change over time, such as Vuex getters.

```js
import store from './store'

const Identity = {
$oid: '4234c0a877ccf94401baz'
}

const options = {
headers: {
Authorization() {
return store.getters.auth_token
},
Refresh: 'example_refresh_token',
Identity
}
}

Vue.use(VueRequests, options)
```

### before [Function]
Before hook to fire before each request. The hook uses async/await, so asynchronous hooks will complete before the actual request is made. This is particularly useful for checking if you have expired tokens and refreshing them before a request.

Here's an example of a before hook checking for expired tokens and attempting to refresh before the original request:

```js
import moment from 'moment'
import store from './store'

const token = {
id: '1234567890',
expires: '2017-09-30T01:44:19.273Z'
}
const options = {
async before() {
if (moment.utc(token.expires) >= moment.utc()) {
store.dispatch('refresh_tokens')
}
}
}

Vue.use(VueRequests, options)
```

### timeout [Function]
Timeout hook to fire in the event of a timeout.

```js
const options = {
timeout() {
alert('The request timed out.')
}
}

Vue.use(VueRequests, options)
```

### timeout_duration [Number]
Duration in ms for fetch timeout limit.

```js
const options = {
timeout_duration: 25000
}

Vue.use(VueRequests, options)
```

## Usage

```js
import Vue from 'vue'
export default {
mounted() {
this.fetch()
},
methods: {
async fetch() {
const response = await this.$request('/data')
this.$store.dispatch('set_data', response)
},
save() {
const response = await this.$request('/data', {
method: 'put',
body: {
foo: 'bar'
}
})
this.$store.dispatch('saved_data', response)
}
}
}
```

## Requests
The request function accepts the following parameters:

### url [String]
The base url to make relative requests from. If an absolute url is passed to the request function, this will be overriden.

### options [Object]
The options parameter accepts the following parameters:
#### method [String]
The method of the request (get (default), put, post, delete, options)
#### body [Object|String]
The body of the request
### headers [Headers]
Custom headers to add to the request


## Build Setup

``` bash
# install dependencies
npm install

# serve demo at localhost:8080
npm start

# run tests with jest
npm test
```

For detailed explanation on how things work, checkout the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader).
40 changes: 40 additions & 0 deletions build/build.js
@@ -0,0 +1,40 @@
require('./check-versions')()

process.env.NODE_ENV = 'production'

var ora = require('ora')
var rm = require('rimraf')
var path = require('path')
var chalk = require('chalk')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')

var spinner = ora('building for production...')
spinner.start()

rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
if (err) throw err
webpack(webpackConfig, function (err, stats) {
spinner.stop()
if (err) throw err
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')

if (stats.hasErrors()) {
console.log(chalk.red(' Build failed with errors.\n'))
process.exit(1)
}

console.log(chalk.cyan(' Build complete.\n'))
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
))
})
})
48 changes: 48 additions & 0 deletions build/check-versions.js
@@ -0,0 +1,48 @@
var chalk = require('chalk')
var semver = require('semver')
var packageConfig = require('../package.json')
var shell = require('shelljs')
function exec (cmd) {
return require('child_process').execSync(cmd).toString().trim()
}

var versionRequirements = [
{
name: 'node',
currentVersion: semver.clean(process.version),
versionRequirement: packageConfig.engines.node
}
]

if (shell.which('npm')) {
versionRequirements.push({
name: 'npm',
currentVersion: exec('npm --version'),
versionRequirement: packageConfig.engines.npm
})
}

module.exports = function () {
var warnings = []
for (var i = 0; i < versionRequirements.length; i++) {
var mod = versionRequirements[i]
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
warnings.push(mod.name + ': ' +
chalk.red(mod.currentVersion) + ' should be ' +
chalk.green(mod.versionRequirement)
)
}
}

if (warnings.length) {
console.log('')
console.log(chalk.yellow('To use this template, you must update following to modules:'))
console.log()
for (var i = 0; i < warnings.length; i++) {
var warning = warnings[i]
console.log(' ' + warning)
}
console.log()
process.exit(1)
}
}
9 changes: 9 additions & 0 deletions build/dev-client.js
@@ -0,0 +1,9 @@
/* eslint-disable */
require('eventsource-polyfill')
var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')

hotClient.subscribe(function (event) {
if (event.action === 'reload') {
window.location.reload()
}
})

0 comments on commit 7da2b3c

Please sign in to comment.