vue-resource-case-converter
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -5,2 +5,4 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
// | ||
@@ -27,2 +29,10 @@ // Plugin for vue-resource to convert request params to snake case | ||
function snakeCaseUnderscoreNumbers(string) { | ||
var find = /([A-Z0-9]+)/g; | ||
var convert = function convert(matches) { | ||
return '_' + matches.toLowerCase(); | ||
}; | ||
return string.replace(find, convert); | ||
} | ||
function getClass(obj) { | ||
@@ -55,20 +65,13 @@ // Workaround for detecting native classes. | ||
function mergeOptions(globalOptions, localOptions) { | ||
// Combines the value of requestUrlFilter/responseUrlFilter with the global options | ||
if (typeof localOptions === 'boolean') { | ||
return _extends({}, globalOptions, { convert: localOptions }); | ||
} | ||
return _extends({}, globalOptions, localOptions); | ||
} | ||
var VueResourceCaseConverter = { | ||
install: function install(Vue, options) { | ||
var requestUrlFilter = function requestUrlFilter() { | ||
return true; | ||
}; | ||
var responseUrlFilter = function responseUrlFilter() { | ||
return true; | ||
}; | ||
if (options != null && options.requestUrlFilter) { | ||
requestUrlFilter = options.requestUrlFilter; | ||
} | ||
if (options != null && options.responseUrlFilter) { | ||
responseUrlFilter = options.responseUrlFilter; | ||
} | ||
if (Vue.http == null) { | ||
@@ -79,15 +82,30 @@ undefined.$log('Please add http-resource plugin to your Vue instance'); | ||
var globalOptions = { | ||
convert: true, | ||
underscoreNumbers: false, | ||
requestUrlFilter: function requestUrlFilter() { | ||
return {}; | ||
}, | ||
responseUrlFilter: function responseUrlFilter() { | ||
return {}; | ||
} | ||
}; | ||
Object.keys(globalOptions).forEach(function (key) { | ||
if (options && options.hasOwnProperty(key)) { | ||
globalOptions[key] = options[key]; | ||
} | ||
}); | ||
Vue.http.interceptors.push(function (request, next) { | ||
if (requestUrlFilter(request.url)) { | ||
request.params = convertObjectKeys(request.params, snakeCase); | ||
request.body = convertObjectKeys(request.body, snakeCase); | ||
var options = mergeOptions(globalOptions, globalOptions.requestUrlFilter(request.url)); | ||
if (options.convert) { | ||
var keyConversionFun = options.underscoreNumbers ? snakeCaseUnderscoreNumbers : snakeCase; | ||
request.params = convertObjectKeys(request.params, keyConversionFun); | ||
request.body = convertObjectKeys(request.body, keyConversionFun); | ||
} | ||
next(function (response) { | ||
if (!responseUrlFilter(response.url)) { | ||
return response; | ||
var options = mergeOptions(globalOptions, globalOptions.responseUrlFilter(response.url)); | ||
if (options.convert) { | ||
response.body = convertObjectKeys(response.body, camelCase); | ||
} | ||
var convertedBody = convertObjectKeys(response.body, camelCase); | ||
response.body = convertedBody; | ||
return response; | ||
@@ -94,0 +112,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
"use strict";function camelCase(e){var t=/(\_\w)/g,r=function(e){return e[1].toUpperCase()};return e.replace(t,r)}function snakeCase(e){var t=/([A-Z])/g,r=function(e){return"_"+e.toLowerCase()};return e.replace(t,r)}function getClass(e){var t=Object.prototype.toString.call(e);return t.match(/\[object (.+)\]/)[1]}function convertObjectKeys(e,t){return"Object"!==getClass(e)&&"Array"!==getClass(e)?e:Object.keys(e).reduce(function(r,n){return r[t(n)]=convertObjectKeys(e[n],t),r},Array.isArray(e)?[]:{})}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},VueResourceCaseConverter={install:function(e,t){var r=function(){return!0},n=function(){return!0};return null!=t&&t.requestUrlFilter&&(r=t.requestUrlFilter),null!=t&&t.responseUrlFilter&&(n=t.responseUrlFilter),null==e.http?void(void 0).$log("Please add http-resource plugin to your Vue instance"):void e.http.interceptors.push(function(e,t){r(e.url)&&(e.params=convertObjectKeys(e.params,snakeCase),e.body=convertObjectKeys(e.body,snakeCase)),t(function(e){if(!n(e.url))return e;var t=convertObjectKeys(e.body,camelCase);return e.body=t,e})})}};"object"===("undefined"==typeof module?"undefined":_typeof(module))&&module.exports&&(module.exports=VueResourceCaseConverter); | ||
"use strict";function camelCase(e){var r=/(\_\w)/g,t=function(e){return e[1].toUpperCase()};return e.replace(r,t)}function snakeCase(e){var r=/([A-Z])/g,t=function(e){return"_"+e.toLowerCase()};return e.replace(r,t)}function snakeCaseUnderscoreNumbers(e){var r=/([A-Z0-9]+)/g,t=function(e){return"_"+e.toLowerCase()};return e.replace(r,t)}function getClass(e){var r=Object.prototype.toString.call(e);return r.match(/\[object (.+)\]/)[1]}function convertObjectKeys(e,r){return"Object"!==getClass(e)&&"Array"!==getClass(e)?e:Object.keys(e).reduce(function(t,n){return t[r(n)]=convertObjectKeys(e[n],r),t},Array.isArray(e)?[]:{})}function mergeOptions(e,r){return"boolean"==typeof r?_extends({},e,{convert:r}):_extends({},e,r)}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},_extends=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},VueResourceCaseConverter={install:function(e,r){if(null==e.http)return void(void 0).$log("Please add http-resource plugin to your Vue instance");var t={convert:!0,underscoreNumbers:!1,requestUrlFilter:function(){return{}},responseUrlFilter:function(){return{}}};Object.keys(t).forEach(function(e){r&&r.hasOwnProperty(e)&&(t[e]=r[e])}),e.http.interceptors.push(function(e,r){var n=mergeOptions(t,t.requestUrlFilter(e.url));if(n.convert){var o=n.underscoreNumbers?snakeCaseUnderscoreNumbers:snakeCase;e.params=convertObjectKeys(e.params,o),e.body=convertObjectKeys(e.body,o)}r(function(e){var r=mergeOptions(t,t.responseUrlFilter(e.url));return r.convert&&(e.body=convertObjectKeys(e.body,camelCase)),e})})}};"object"===("undefined"==typeof module?"undefined":_typeof(module))&&module.exports&&(module.exports=VueResourceCaseConverter); |
@@ -9,3 +9,4 @@ const gulp = require('gulp'); | ||
.pipe(babel({ | ||
presets: ['es2015'] | ||
presets: ['es2015'], | ||
plugins: ['transform-object-assign'], | ||
})) | ||
@@ -12,0 +13,0 @@ .pipe(rename('vue-resource-case-converter.js')) |
{ | ||
"name": "vue-resource-case-converter", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Vue resource plugin to convert request json keys to snake_case and response json keys to camelCase", | ||
@@ -31,2 +31,3 @@ "main": "dist/vue-resource-case-converter.js", | ||
"devDependencies": { | ||
"babel-plugin-transform-object-assign": "^6.22.0", | ||
"babel-preset-es2015": "^6.13.2", | ||
@@ -33,0 +34,0 @@ "gulp": "^3.9.1", |
@@ -35,17 +35,36 @@ # vue-resource-case-converter | ||
In order to restrict certain urls from converting, | ||
you can do it by providing `responseUrlFilter` or `requestUrlFilter` function to | ||
configuration object. These functions receive `url` and should return true for url | ||
to be processed by interceptor. | ||
A number of configuration options are supported, which can be set either globally or on a | ||
URL-by-URL basis. | ||
- __`convert` (boolean)__: Whether to convert JSON keys between cases. Defaults to `true`. | ||
- __`underscoreNumbers` (boolean)__: Whether to treat numbers as the start of a new word within | ||
the key. For example, if true, `fooBar2` converts to `foo_bar_2`; otherwise, `fooBar2` | ||
converts to `foo_bar2`. Defaults to `false`. | ||
- __`responseUrlFilter` (function)__: If provided, customizes behavior by URL. The function takes | ||
a URL as the first argument, and should return either a boolean or an object. A boolean | ||
indicates simply whether or not to make case conversions, and allows you to turn off case | ||
conversion for particular APIs or URLs. An object return value should mirror the global | ||
options and allows you to override `convert` and `underscoreNumbers` on a URL basis. | ||
Defaults to `null`. | ||
- __`requestUrlFilter` (function)__: Same as `responseUrlFilter`, but for resquests. | ||
For example: | ||
Vue.use(VueResourceCaseConverter, { | ||
convert: false, // e.g., to turn off by default, and enable by URL | ||
underscoreNumbers: false, | ||
responseUrlFilter(url) { | ||
// Your custom logic | ||
// For example: | ||
// return url.indexOf('api') >= 0; | ||
if (/api\/v2/.test(url)) { | ||
return { | ||
convert: true | ||
}; | ||
} | ||
}, | ||
requestUrlFilter(url) { | ||
// Your custom logic | ||
}, | ||
if (/api\/v2/.test(url)) { | ||
return { | ||
convert: true | ||
}; | ||
} | ||
} | ||
}); | ||
@@ -52,0 +71,0 @@ |
@@ -22,2 +22,10 @@ // | ||
function snakeCaseUnderscoreNumbers (string) { | ||
let find = /([A-Z0-9]+)/g; | ||
let convert = function(matches) { | ||
return '_' + matches.toLowerCase(); | ||
}; | ||
return string.replace(find, convert); | ||
} | ||
function getClass(obj) { | ||
@@ -50,20 +58,13 @@ // Workaround for detecting native classes. | ||
function mergeOptions(globalOptions, localOptions) { | ||
// Combines the value of requestUrlFilter/responseUrlFilter with the global options | ||
if (typeof(localOptions) === 'boolean') { | ||
return Object.assign({}, globalOptions, {convert: localOptions}); | ||
} | ||
return Object.assign({}, globalOptions, localOptions); | ||
} | ||
var VueResourceCaseConverter = { | ||
install: (Vue, options) => { | ||
let requestUrlFilter = function () { | ||
return true; | ||
}; | ||
let responseUrlFilter = function () { | ||
return true; | ||
}; | ||
if (options != null && options.requestUrlFilter) { | ||
requestUrlFilter = options.requestUrlFilter; | ||
} | ||
if (options != null && options.responseUrlFilter) { | ||
responseUrlFilter = options.responseUrlFilter; | ||
} | ||
if (Vue.http == null) { | ||
@@ -74,15 +75,30 @@ this.$log('Please add http-resource plugin to your Vue instance'); | ||
let globalOptions = { | ||
convert: true, | ||
underscoreNumbers: false, | ||
requestUrlFilter () { | ||
return {}; | ||
}, | ||
responseUrlFilter () { | ||
return {}; | ||
} | ||
}; | ||
Object.keys(globalOptions).forEach((key) => { | ||
if (options && options.hasOwnProperty(key)) { | ||
globalOptions[key] = options[key]; | ||
} | ||
}); | ||
Vue.http.interceptors.push((request, next) => { | ||
if (requestUrlFilter(request.url)) { | ||
request.params = convertObjectKeys(request.params, snakeCase); | ||
request.body = convertObjectKeys(request.body, snakeCase); | ||
let options = mergeOptions(globalOptions, globalOptions.requestUrlFilter(request.url)); | ||
if (options.convert) { | ||
let keyConversionFun = options.underscoreNumbers ? snakeCaseUnderscoreNumbers : snakeCase; | ||
request.params = convertObjectKeys(request.params, keyConversionFun); | ||
request.body = convertObjectKeys(request.body, keyConversionFun); | ||
} | ||
next((response) => { | ||
if (!responseUrlFilter(response.url)) { | ||
return response; | ||
let options = mergeOptions(globalOptions, globalOptions.responseUrlFilter(response.url)); | ||
if (options.convert) { | ||
response.body = convertObjectKeys(response.body, camelCase); | ||
} | ||
const convertedBody = convertObjectKeys(response.body, camelCase); | ||
response.body = convertedBody; | ||
return response; | ||
@@ -89,0 +105,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14249
242
74
6