Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-badin committed Jun 17, 2018
1 parent f766b35 commit 7862f99
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 86 deletions.
13 changes: 4 additions & 9 deletions dist/vue-october-requests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/vue-october-requests.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-october-requests",
"version": "0.0.6",
"version": "0.0.7",
"description": "October data attribute api for vuejs.",
"main": "dist/vue-october-requests.js",
"scripts": {
Expand Down Expand Up @@ -32,12 +32,12 @@
"babel-loader": "^7.1.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-env": "^1.7.0",
"babel-preset-flow": "^6.23.0",
"eslint": "^4.13.1",
"eslint-loader": "^1.9.0",
"eslint-plugin-flowtype": "^2.46.3",
"flow-bin": "^0.70.0",
"eslint-loader": "^2.0.0",
"eslint-plugin-flowtype": "^2.49.3",
"flow-bin": "^0.74.0",
"jest": "^22.4.3",
"vue": "^2.5.16",
"vue-template-compiler": "^2.5.16",
Expand Down
27 changes: 11 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
// @flow

import { request, type Props } from "./request";
import type { Axios } from "axios";
import type { Vue as VueType, VNodeDirective } from "vue";

type Options = {
axios: Axios
};
import { request } from "./request";
import type { AxiosInstance } from "axios";
import type { Vue as TVue, VNodeDirective } from "vue";
import type { PluginOptions, RequestProps } from "../types";

module.exports = {
install: (Vue: VueType, options: Options = {}) => {
install: (Vue: TVue, options: PluginOptions = {}) => {
if (!options.axios) {
throw new Error("Axios option is not specified.");
}

const instance = options.axios.create({
const instance: AxiosInstance = options.axios.create({
baseURL: window.location.href,
timeout: 1000,
timeout: options.timeout || 1000,
headers: { "X-Requested-With": "XMLHttpRequest" }
});

Vue.prototype.$october = {
request(props: Props & { formData: FormData } = {}) {
request(props: RequestProps = {}) {
return request({ ...props, instance });
}
};

Vue.directive("october", {
bind(el: HTMLFormElement, binding: VNodeDirective) {
if (binding.arg === "request") {
const { modifiers, value } = binding;

if (!(el instanceof HTMLFormElement)) {
throw new Error("The element is not instance of HTMLFormElement");
}

el.addEventListener("submit", (event: Event) => {
if (modifiers.prevent) {
if (binding.modifiers.prevent) {
event.preventDefault();
}

request({
...modifiers,
...value,
...binding.modifiers,
...binding.value,
instance,
formData: new FormData(event.target)
});
Expand Down
32 changes: 3 additions & 29 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
// @flow

import type { AxiosInstance } from "axios";

type SuccessResponse = {
X_OCTOBER_REDIRECT: string
};

type ErrorResponse = {
X_OCTOBER_ERROR_FIELDS: { [key: string]: string },
X_OCTOBER_ERROR_MESSAGE: string
};

type Modifiers = {
prevent: boolean
};

type Value = {
handler: string,
onError?: mixed => void,
onLoading?: boolean => void,
onSuccess?: mixed => void,
redirect?: string
};

export type Props = Value & Modifiers;

type RequestProps = Props & { instance: AxiosInstance, formData?: FormData };
import type { SuccessResponse, RequestProps } from "../types";

export function request({
formData,
Expand All @@ -44,7 +19,7 @@ export function request({
throw new Error("The formData is not instance of FormData");
}

function emit(name, data) {
function emit(name: string, data: mixed) {
const eventName = `on${name}`;
const func = bag[eventName];

Expand Down Expand Up @@ -73,8 +48,7 @@ export function request({
}
})
.catch(err => {
const response: { data: ErrorResponse } = err.response;
emit("Error", response.data);
emit("Error", err.response.data);
})
.finally(() => {
emit("Loading", false);
Expand Down
28 changes: 28 additions & 0 deletions types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @flow

import type { Axios, AxiosInstance } from "axios";

export type SuccessResponse = {
X_OCTOBER_REDIRECT: string
};

export type ErrorResponse = {
X_OCTOBER_ERROR_FIELDS: { [key: string]: string },
X_OCTOBER_ERROR_MESSAGE: string
};

export type RequestProps = {
formData: FormData,
handler: string,
instance: AxiosInstance,
onError?: mixed => void,
onLoading?: boolean => void,
onSuccess?: mixed => void,
prevent: boolean,
redirect?: string
};

export type PluginOptions = {
axios: Axios,
timeout?: number
};
52 changes: 26 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ babel-polyfill@^6.26.0:
core-js "^2.5.0"
regenerator-runtime "^0.10.5"

babel-preset-env@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/babel-preset-env/-/babel-preset-env-1.6.1.tgz#a18b564cc9b9afdf4aae57ae3c1b0d99188e6f48"
babel-preset-env@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/babel-preset-env/-/babel-preset-env-1.7.0.tgz#dea79fa4ebeb883cd35dab07e260c1c9c04df77a"
dependencies:
babel-plugin-check-es2015-constants "^6.22.0"
babel-plugin-syntax-trailing-function-commas "^6.22.0"
Expand Down Expand Up @@ -834,7 +834,7 @@ babel-preset-env@^1.6.1:
babel-plugin-transform-es2015-unicode-regex "^6.22.0"
babel-plugin-transform-exponentiation-operator "^6.22.0"
babel-plugin-transform-regenerator "^6.22.0"
browserslist "^2.1.2"
browserslist "^3.2.6"
invariant "^2.2.2"
semver "^5.3.0"

Expand Down Expand Up @@ -1057,12 +1057,12 @@ browserify-zlib@^0.2.0:
dependencies:
pako "~1.0.5"

browserslist@^2.1.2:
version "2.11.3"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-2.11.3.tgz#fe36167aed1bbcde4827ebfe71347a2cc70b99b2"
browserslist@^3.2.6:
version "3.2.8"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-3.2.8.tgz#b0005361d6471f0f5952797a76fc985f1f978fc6"
dependencies:
caniuse-lite "^1.0.30000792"
electron-to-chromium "^1.3.30"
caniuse-lite "^1.0.30000844"
electron-to-chromium "^1.3.47"

bser@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -1130,9 +1130,9 @@ camelcase@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"

caniuse-lite@^1.0.30000792:
version "1.0.30000836"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000836.tgz#c08f405b884d36dc44fa4c9a85c2c06cdab1dbb5"
caniuse-lite@^1.0.30000844:
version "1.0.30000856"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000856.tgz#ecc16978135a6f219b138991eb62009d25ee8daa"

capture-exit@^1.2.0:
version "1.2.0"
Expand Down Expand Up @@ -1589,9 +1589,9 @@ ecc-jsbn@~0.1.1:
dependencies:
jsbn "~0.1.0"

electron-to-chromium@^1.3.30:
version "1.3.45"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.45.tgz#458ac1b1c5c760ce8811a16d2bfbd97ec30bafb8"
electron-to-chromium@^1.3.47:
version "1.3.48"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.48.tgz#d3b0d8593814044e092ece2108fc3ac9aea4b900"

elliptic@^6.0.0:
version "6.4.0"
Expand Down Expand Up @@ -1725,21 +1725,21 @@ escope@^3.6.0:
esrecurse "^4.1.0"
estraverse "^4.1.1"

eslint-loader@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/eslint-loader/-/eslint-loader-1.9.0.tgz#7e1be9feddca328d3dcfaef1ad49d5beffe83a13"
eslint-loader@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/eslint-loader/-/eslint-loader-2.0.0.tgz#d136619b5c684e36531ffc28c60a56e404608f5d"
dependencies:
loader-fs-cache "^1.0.0"
loader-utils "^1.0.2"
object-assign "^4.0.1"
object-hash "^1.1.4"
rimraf "^2.6.1"

eslint-plugin-flowtype@^2.46.3:
version "2.46.3"
resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.46.3.tgz#7e84131d87ef18b496b1810448593374860b4e8e"
eslint-plugin-flowtype@^2.49.3:
version "2.49.3"
resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.49.3.tgz#ccca6ee5ba2027eb3ed36bc2ec8c9a842feee841"
dependencies:
lodash "^4.15.0"
lodash "^4.17.10"

eslint-scope@^3.7.1, eslint-scope@~3.7.1:
version "3.7.1"
Expand Down Expand Up @@ -2060,9 +2060,9 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"

flow-bin@^0.70.0:
version "0.70.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5"
flow-bin@^0.74.0:
version "0.74.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.74.0.tgz#8017bb00efb37cbe8d81fbb7f464038bde06adc9"

follow-redirects@^1.3.0:
version "1.4.1"
Expand Down Expand Up @@ -3231,7 +3231,7 @@ lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down

0 comments on commit 7862f99

Please sign in to comment.