Comparing version 1.0.2 to 1.1.0
@@ -247,2 +247,83 @@ /** | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
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; | ||
}; | ||
var objectWithoutProperties = function (obj, keys) { | ||
var target = {}; | ||
for (var i in obj) { | ||
if (keys.indexOf(i) >= 0) continue; | ||
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; | ||
target[i] = obj[i]; | ||
} | ||
return target; | ||
}; | ||
var BODY_KEY_CASE_OPTIONS = { | ||
SNAKE_CASE: changeCaseObject_1.snakeCase, | ||
CAMEL_CASE: changeCaseObject_1.camelCase, | ||
PARAM_CASE: changeCaseObject_1.paramCase | ||
}; | ||
var MetaOptions = function () { | ||
function MetaOptions(options) { | ||
classCallCheck(this, MetaOptions); | ||
if (!options.bodyKeyCase || !this.isValidBodyKeyCase(options.bodyKeyCase)) { | ||
throw new Error('This body key formatting option is not allowed.'); | ||
} | ||
this.bodyKeyCase = options.bodyKeyCase; | ||
} | ||
createClass(MetaOptions, [{ | ||
key: 'isValidBodyKeyCase', | ||
value: function isValidBodyKeyCase(bodyKeyCase) { | ||
return Object.keys(BODY_KEY_CASE_OPTIONS).includes(bodyKeyCase); | ||
} | ||
}, { | ||
key: 'getBodyKeyConverter', | ||
value: function getBodyKeyConverter() { | ||
return BODY_KEY_CASE_OPTIONS[this.bodyKeyCase]; | ||
} | ||
}]); | ||
return MetaOptions; | ||
}(); | ||
var strictUriEncode = function (str) { | ||
@@ -436,57 +517,10 @@ return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var ALLOWED_REQUEST_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
var defaultMetaOptions = { bodyKeyCase: 'SNAKE_CASE' }; | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
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; | ||
}; | ||
var objectWithoutProperties = function (obj, keys) { | ||
var target = {}; | ||
for (var i in obj) { | ||
if (keys.indexOf(i) >= 0) continue; | ||
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; | ||
target[i] = obj[i]; | ||
} | ||
return target; | ||
}; | ||
var ALLOWED_REQUEST_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']; | ||
var Snuffles = function () { | ||
function Snuffles(baseUrl) { | ||
var defaultOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var defaultRequestOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var metaOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultMetaOptions; | ||
classCallCheck(this, Snuffles); | ||
@@ -499,3 +533,4 @@ | ||
this.baseUrl = baseUrl; | ||
this.defaultOptions = defaultOptions; | ||
this.defaultRequestOptions = defaultRequestOptions; | ||
this.metaOptions = new MetaOptions(metaOptions); | ||
} | ||
@@ -561,3 +596,3 @@ | ||
var url = this.fullUrl(path); | ||
var fullOptions = deepmerge_1(this.defaultOptions, options); | ||
var fullOptions = deepmerge_1(this.defaultRequestOptions, options); | ||
@@ -575,4 +610,4 @@ if (!fullOptions.method || !this.validMethod(fullOptions.method)) { | ||
if (requestOptions.body) { | ||
var snakeCasedBody = changeCaseObject_1.snakeCase(requestOptions.body); | ||
requestOptions.body = JSON.stringify(snakeCasedBody); | ||
var casedBody = this.formatBody(requestOptions.body); | ||
requestOptions.body = JSON.stringify(casedBody); | ||
} | ||
@@ -594,2 +629,7 @@ | ||
} | ||
}, { | ||
key: 'formatBody', | ||
value: function formatBody(body) { | ||
return this.metaOptions.getBodyKeyConverter()(body); | ||
} | ||
}]); | ||
@@ -596,0 +636,0 @@ return Snuffles; |
@@ -249,2 +249,83 @@ 'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
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; | ||
}; | ||
var objectWithoutProperties = function (obj, keys) { | ||
var target = {}; | ||
for (var i in obj) { | ||
if (keys.indexOf(i) >= 0) continue; | ||
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; | ||
target[i] = obj[i]; | ||
} | ||
return target; | ||
}; | ||
var BODY_KEY_CASE_OPTIONS = { | ||
SNAKE_CASE: changeCaseObject_1.snakeCase, | ||
CAMEL_CASE: changeCaseObject_1.camelCase, | ||
PARAM_CASE: changeCaseObject_1.paramCase | ||
}; | ||
var MetaOptions = function () { | ||
function MetaOptions(options) { | ||
classCallCheck(this, MetaOptions); | ||
if (!options.bodyKeyCase || !this.isValidBodyKeyCase(options.bodyKeyCase)) { | ||
throw new Error('This body key formatting option is not allowed.'); | ||
} | ||
this.bodyKeyCase = options.bodyKeyCase; | ||
} | ||
createClass(MetaOptions, [{ | ||
key: 'isValidBodyKeyCase', | ||
value: function isValidBodyKeyCase(bodyKeyCase) { | ||
return Object.keys(BODY_KEY_CASE_OPTIONS).includes(bodyKeyCase); | ||
} | ||
}, { | ||
key: 'getBodyKeyConverter', | ||
value: function getBodyKeyConverter() { | ||
return BODY_KEY_CASE_OPTIONS[this.bodyKeyCase]; | ||
} | ||
}]); | ||
return MetaOptions; | ||
}(); | ||
var strictUriEncode = function (str) { | ||
@@ -438,57 +519,10 @@ return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var ALLOWED_REQUEST_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
var defaultMetaOptions = { bodyKeyCase: 'SNAKE_CASE' }; | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
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; | ||
}; | ||
var objectWithoutProperties = function (obj, keys) { | ||
var target = {}; | ||
for (var i in obj) { | ||
if (keys.indexOf(i) >= 0) continue; | ||
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; | ||
target[i] = obj[i]; | ||
} | ||
return target; | ||
}; | ||
var ALLOWED_REQUEST_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']; | ||
var Snuffles = function () { | ||
function Snuffles(baseUrl) { | ||
var defaultOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var defaultRequestOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var metaOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultMetaOptions; | ||
classCallCheck(this, Snuffles); | ||
@@ -501,3 +535,4 @@ | ||
this.baseUrl = baseUrl; | ||
this.defaultOptions = defaultOptions; | ||
this.defaultRequestOptions = defaultRequestOptions; | ||
this.metaOptions = new MetaOptions(metaOptions); | ||
} | ||
@@ -563,3 +598,3 @@ | ||
var url = this.fullUrl(path); | ||
var fullOptions = deepmerge_1(this.defaultOptions, options); | ||
var fullOptions = deepmerge_1(this.defaultRequestOptions, options); | ||
@@ -577,4 +612,4 @@ if (!fullOptions.method || !this.validMethod(fullOptions.method)) { | ||
if (requestOptions.body) { | ||
var snakeCasedBody = changeCaseObject_1.snakeCase(requestOptions.body); | ||
requestOptions.body = JSON.stringify(snakeCasedBody); | ||
var casedBody = this.formatBody(requestOptions.body); | ||
requestOptions.body = JSON.stringify(casedBody); | ||
} | ||
@@ -596,2 +631,7 @@ | ||
} | ||
}, { | ||
key: 'formatBody', | ||
value: function formatBody(body) { | ||
return this.metaOptions.getBodyKeyConverter()(body); | ||
} | ||
}]); | ||
@@ -598,0 +638,0 @@ return Snuffles; |
{ | ||
"name": "snuffles", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A wrapper around the native fetch function, returning the response body as a camelCased object", | ||
@@ -22,7 +22,2 @@ "author": "railslove", | ||
}, | ||
"peerDependencies": { | ||
"prop-types": "15.5.4", | ||
"react": "^5.0.0 || 16.0.0", | ||
"react-dom": "15.0.0 || 16.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -29,0 +24,0 @@ "babel-core": "6.26.3", |
@@ -14,3 +14,2 @@ <p align="center"> | ||
## Installation | ||
@@ -28,3 +27,3 @@ | ||
export default function myApiWrapper() { | ||
const defaultOptions = { | ||
const defaultRequestOptions = { | ||
headers: { | ||
@@ -35,4 +34,12 @@ 'X-AUTH-TOKEN': 'my-secret-token' | ||
const api = new Snuffles('http://base-url.tld', defaultOptions) | ||
const metaOptions = { | ||
bodyKeyCase: 'CAMEL_CASE' | ||
} | ||
const api = new Snuffles( | ||
'http://base-url.tld', | ||
defaultRequestOptions, | ||
metaOptions | ||
) | ||
const user = api.get('/user') | ||
@@ -43,24 +50,47 @@ } | ||
To create a new instance of Snuffles: | ||
```js | ||
const api = new Snuffles(baseUrl[, defaultOptions]) | ||
const api = new Snuffles(baseUrl[, defaultRequestOptions, metaOptions]) | ||
``` | ||
* __`baseUrl`__: The base url of the API you want to make requests agains | ||
* __`defaultOptions`__ (_optional_): An Object, containing a set of default options you want to sent in every request, e.g. headers for authentication | ||
- **`baseUrl`**: The base url of the API you want to make requests agains | ||
- **`defaultRequestOptions`** (_optional_): An Object, containing a set of default options you want to sent in every request, e.g. headers for authentication | ||
- **`metaOptions`** (_optional_): An object containing meta configuration for Snuffles. For possible options, please refer to the list below | ||
### Default Request Options | ||
Snuffles accepts all options that fetch accepts as its `init` parameter ([docs](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch)). In fact, snuffles does not validate the options that are passed at all. | ||
### Meta Options | ||
The `metaOptions` object accepts the following configureations: | ||
- **`bodyKeyCase`**: A string defining which casing the keys of a request body for **outgoing requests** should have. Can be either of `SNAKE_CASE`, `CAMEL_CASE` or `PARAM_CASE`. | ||
If no object is passed for `metaOptions`, the following defaul configuration will be used: | ||
```javascript | ||
{ | ||
bodyKeyCase: 'SNAKE_CASE' | ||
} | ||
``` | ||
### Supported HTTP Methods | ||
As of now, Snuffles has wrappers for 5 request methods: | ||
* `get(path[, options])` | ||
* `post(path[, options])` | ||
* `put(path[, options])` | ||
* `patch(path[, options])` | ||
* `delete(path[, options])` | ||
- `get(path[, options])` | ||
- `post(path[, options])` | ||
- `put(path[, options])` | ||
- `patch(path[, options])` | ||
- `delete(path[, options])` | ||
Where | ||
* __`path`__: the path you want that specific request to go to | ||
* __`options`__ (_optional_): An Object containing a set of options you want to merge with the base options on this specific request. Options passed to the wrapper functions are deep-merged, but will override identical keys. | ||
### Options | ||
Snuffles accepts all options that fetch accepts as its `init` parameter ([docs](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch)). In fact, snuffles does not validate the options that are passed at all. | ||
- **`path`**: the path you want that specific request to go to | ||
- **`options`** (_optional_): An Object containing a set of options you want to merge with the base options on this specific request. Options passed to the wrapper functions are deep-merged, but will override identical keys. | ||
### Using querystrings | ||
Snuffles does support the setting of querystrings via its options parameter. You can pass in a `query` object with the desired key-value-pairs. | ||
Snuffles does support the setting of querystrings via its options parameter. You can pass in a `query` object with the desired key-value-pairs. | ||
For example: | ||
@@ -73,4 +103,4 @@ | ||
query: { | ||
'name': 'sirius', | ||
'animal': 'dog' | ||
name: 'sirius', | ||
animal: 'dog' | ||
} | ||
@@ -85,2 +115,3 @@ } | ||
### Casing | ||
Snuffles will take care of transforming the casing of response and request | ||
@@ -92,2 +123,3 @@ bodies, so that you can pass in a camelCased object as a request body (passed | ||
#### Response bodies | ||
Assuming `GET https://your-api/users/1` would return a response with a body of | ||
@@ -101,2 +133,3 @@ | ||
``` | ||
If you make this request with snuffles, it would look like | ||
@@ -124,4 +157,4 @@ | ||
body: { | ||
'userName': 'sirius', | ||
'paidUser': true | ||
userName: 'sirius', | ||
paidUser: true | ||
} | ||
@@ -128,0 +161,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
160437
3
1165
175