angular-webpack-plugin
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -59,2 +59,4 @@ // © Copyright 2014 Paul Thomas <paul@stackfull.com>. | ||
this, this.addDependencyFactories); | ||
bindCallbackMethod(compiler.parser, "expression window.angular", | ||
this, this.addAngularVariable); | ||
bindCallbackMethod(compiler.parser, "expression angular", | ||
@@ -201,2 +203,6 @@ this, this.addAngularVariable); | ||
this._addModuleDefinition(parser, expr, mod); | ||
// Some libraries, such as angular-classy, will break compilation without this check | ||
if( !deps.items ){ | ||
return true; | ||
} | ||
return deps.items.every( | ||
@@ -203,0 +209,0 @@ this._addDependency.bind(this, parser, expr)); |
{ | ||
"name": "angular-webpack-plugin", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "lib/index.js", | ||
@@ -23,3 +23,3 @@ "description": "Makes webpack aware of AngularJS modules.", | ||
"peerDependencies": { | ||
"webpack": "^1.1.0", | ||
"webpack": "^1.4.0", | ||
"exports-loader": "^0.6.2" | ||
@@ -30,14 +30,14 @@ }, | ||
"grunt-contrib-watch": "~0.6.0", | ||
"load-grunt-tasks": "~0.4.0", | ||
"time-grunt": "0.3.1", | ||
"jshint-stylish": "~0.1.3", | ||
"webpack": "1.1.8", | ||
"async": "0.7.0", | ||
"load-grunt-tasks": "~1.0.0", | ||
"time-grunt": "1.0.0", | ||
"jshint-stylish": "~1.0.0", | ||
"webpack": "^1.4.0", | ||
"async": "0.9.0", | ||
"lodash": "~2.4.1", | ||
"underscore.string": "~2.3.3", | ||
"underscore.string": "~2.4.0", | ||
"karma": "~0.12.0", | ||
"webpack-dev-server": "~1.2.4", | ||
"karma-webpack": "1.1.0", | ||
"grunt-karma": "~0.8.2", | ||
"karma-jasmine": "0.1.5", | ||
"webpack-dev-server": "~1.6.6", | ||
"karma-webpack": "1.3.1", | ||
"grunt-karma": "~0.9.0", | ||
"karma-jasmine": "0.3.2", | ||
"karma-phantomjs-launcher": "~0.1.2", | ||
@@ -44,0 +44,0 @@ "exports-loader": "0.6.2", |
@@ -8,11 +8,36 @@ # angular-webpack-plugin [](https://travis-ci.org/stackfull/angular-webpack-plugin) | ||
It is at the stage now where you can use it to get angular apps webpacked without needing `require()` in your code, but it does it by trying to map between angular module names and file names. The conventions for doing this are various, so it will not fit all cases. Please submit an issue on github if it isn't working for your modules and I'll try to iron out the wrinkles over time. | ||
It is at the stage now where you can use it to get angular apps webpacked | ||
without needing `require()` in your code, but it does it by trying to map | ||
between angular module names and file names. The conventions for doing this are | ||
various, so it will not fit all cases. Please submit an issue on github if it | ||
isn't working for your modules and I'll try to iron out the wrinkles over time. | ||
To see it in action, I've [forked the angular-seed project](https://github.com/stackfull/angular-seed) | ||
## Development | ||
Given how complex the problem is, regular test frameworks can't help much. I've | ||
developed a small grunt task to run compile scenarios and check the output. It's | ||
less than ideal and a little fragile. To add new scenarios, create a directory | ||
in test/scenarios containing a webpack.conf.js config file, an `in` and an `out` | ||
directory. The `in` directory will be used as the source and the output will | ||
be compared against `out/bundle.js` (ignoring comments etc.). | ||
To run the scenarios:: | ||
grunt webpackScenario | ||
by default, it won't show what the output was when it doesn't match, so use | ||
grunt --debug webpackScenario | ||
In addition, karma tests in the `verify` directory check that the output makes | ||
a viable executable using the `karma.conf.js` file. | ||
The default grunt task checks everything. | ||
## Release History | ||
#### 0.0.1 - 22 Apr 2014 | ||
First release | ||
#### 0.0.3 - 7 Dec 2014 | ||
Webpack 1.4 and fix for modules using window.angular | ||
@@ -22,4 +47,7 @@ #### 0.0.2 - 28 July 2014 | ||
#### 0.0.1 - 22 Apr 2014 | ||
First release | ||
## License | ||
Copyright (c) 2014 Paul Thomas. Licensed under the MIT license. |
@@ -57,3 +57,4 @@ | ||
alias: { | ||
angular: path.resolve(__dirname, 'stubng.js') | ||
angular: path.resolve(__dirname, 'ng.stubjs'), | ||
ngRoute: path.resolve(__dirname, 'ngroute.stubjs') | ||
} | ||
@@ -60,0 +61,0 @@ }, |
@@ -20,5 +20,7 @@ ([ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(3))) | ||
} | ||
]) | ||
@@ -18,5 +18,7 @@ ([ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
// stub | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(2))) | ||
} | ||
]) |
@@ -29,12 +29,5 @@ // Karma configuration | ||
alias: { | ||
angular$: '../../../vendor/angular/angular.js' | ||
angular$: path.resolve(__dirname, '../../vendor/angular/angular.js') | ||
} | ||
}, | ||
module: { | ||
noParse: /angular\.js$/, | ||
loaders: [{ | ||
test: /angular\.js$/, | ||
loader: 'exports?window.angular' | ||
}] | ||
}, | ||
plugins: [new AngularPlugin()] | ||
@@ -41,0 +34,0 @@ } |
@@ -8,6 +8,8 @@ ([ | ||
}, | ||
function(module, exports, require) { | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
// angular.js stub | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(1))) | ||
} | ||
]) |
@@ -16,6 +16,8 @@ ([ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
// stub | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(2))) | ||
} | ||
]) | ||
@@ -9,4 +9,6 @@ ([ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(1))) | ||
} | ||
]) |
@@ -20,4 +20,6 @@ ([ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(3))) | ||
} | ||
]) |
@@ -18,4 +18,6 @@ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(2))) | ||
} | ||
]) |
@@ -20,4 +20,6 @@ ([ | ||
function(module, exports, __webpack_require__) { | ||
(function(angular) { | ||
module.exports = window.angular | ||
}.call(exports, __webpack_require__(1))) | ||
} | ||
]) |
/** | ||
* @license AngularJS v1.2.14 | ||
* @license AngularJS v1.3.5 | ||
* (c) 2010-2014 Google, Inc. http://angularjs.org | ||
@@ -38,3 +38,3 @@ * License: MIT | ||
angular.forEach(dst, function(value, key){ | ||
angular.forEach(dst, function(value, key) { | ||
delete dst[key]; | ||
@@ -82,2 +82,14 @@ }); | ||
* | ||
* By default, trailing slashes will be stripped from the calculated URLs, | ||
* which can pose problems with server backends that do not expect that | ||
* behavior. This can be disabled by configuring the `$resourceProvider` like | ||
* this: | ||
* | ||
* ```js | ||
app.config(['$resourceProvider', function($resourceProvider) { | ||
// Don't strip trailing slashes from calculated URLs | ||
$resourceProvider.defaults.stripTrailingSlashes = false; | ||
}]); | ||
* ``` | ||
* | ||
* @param {string} url A parametrized URL template with parameters prefixed by `:` as in | ||
@@ -104,8 +116,10 @@ * `/user/:username`. If you are using a URL with a port number (e.g. | ||
* | ||
* If the parameter value is prefixed with `@` then the value of that parameter is extracted from | ||
* the data object (useful for non-GET operations). | ||
* If the parameter value is prefixed with `@` then the value for that parameter will be extracted | ||
* from the corresponding property on the `data` object (provided when calling an action method). For | ||
* example, if the `defaultParam` object is `{someParam: '@someProp'}` then the value of `someParam` | ||
* will be `data.someProp`. | ||
* | ||
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend | ||
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend | ||
* the default set of resource actions. The declaration should be created in the format of {@link | ||
* ng.$http#usage_parameters $http.config}: | ||
* ng.$http#usage $http.config}: | ||
* | ||
@@ -120,4 +134,4 @@ * {action1: {method:?, params:?, isArray:?, headers:?, ...}, | ||
* your resource object. | ||
* - **`method`** – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, | ||
* `DELETE`, and `JSONP`. | ||
* - **`method`** – {string} – Case insensitive HTTP method (e.g. `GET`, `POST`, `PUT`, | ||
* `DELETE`, `JSONP`, etc). | ||
* - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of | ||
@@ -134,2 +148,5 @@ * the parameter value is a function, it will be executed every time when a param value needs to | ||
* request body and headers and returns its transformed (typically serialized) version. | ||
* By default, transformRequest will contain one function that checks if the request data is | ||
* an object and serializes to using `angular.toJson`. To prevent this behavior, set | ||
* `transformRequest` to an empty array: `transformRequest: []` | ||
* - **`transformResponse`** – | ||
@@ -139,2 +156,5 @@ * `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` – | ||
* response body and headers and returns its transformed (typically deserialized) version. | ||
* By default, transformResponse will contain one function that checks if the response looks like | ||
* a JSON string and deserializes it using `angular.fromJson`. To prevent this behavior, set | ||
* `transformResponse` to an empty array: `transformResponse: []` | ||
* - **`cache`** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the | ||
@@ -156,2 +176,10 @@ * GET request, otherwise if a cache instance built with | ||
* | ||
* @param {Object} options Hash with custom settings that should extend the | ||
* default `$resourceProvider` behavior. The only supported option is | ||
* | ||
* Where: | ||
* | ||
* - **`stripTrailingSlashes`** – {boolean} – If true then the trailing | ||
* slashes from any calculated URL will be stripped. (Defaults to true.) | ||
* | ||
* @returns {Object} A resource "class" object with methods for the default set of resource actions | ||
@@ -214,2 +242,5 @@ * optionally extended with custom `actions`. The default set contains these actions: | ||
* | ||
* If an interceptor object was provided, the promise will instead be resolved with the value | ||
* returned by the interceptor. | ||
* | ||
* - `$resolved`: `true` after first server interaction is completed (either with success or | ||
@@ -269,3 +300,3 @@ * rejection), `false` before that. Knowing if the Resource has been resolved is useful in | ||
var User = $resource('/user/:userId', {userId:'@id'}); | ||
var user = User.get({userId:123}, function() { | ||
User.get({userId:123}, function(user) { | ||
user.abc = true; | ||
@@ -290,2 +321,12 @@ user.$save(); | ||
``` | ||
* | ||
* You can also access the raw `$http` promise via the `$promise` property on the object returned | ||
* | ||
``` | ||
var User = $resource('/user/:userId', {userId:'@id'}); | ||
User.get({userId:123}) | ||
.$promise.then(function(user) { | ||
$scope.user = user; | ||
}); | ||
``` | ||
@@ -295,7 +336,7 @@ * # Creating a custom 'PUT' request | ||
* ```js | ||
* var app = angular.module('app', ['ngResource', 'ngRoute']); | ||
* var app = angular.module('app', ['ngResource', 'ngRoute']); | ||
* | ||
* // Some APIs expect a PUT request in the format URL/object/ID | ||
* // Here we are creating an 'update' method | ||
* app.factory('Notes', ['$resource', function($resource) { | ||
* // Some APIs expect a PUT request in the format URL/object/ID | ||
* // Here we are creating an 'update' method | ||
* app.factory('Notes', ['$resource', function($resource) { | ||
* return $resource('/notes/:id', null, | ||
@@ -305,7 +346,7 @@ * { | ||
* }); | ||
* }]); | ||
* }]); | ||
* | ||
* // In our controller we get the ID from the URL using ngRoute and $routeParams | ||
* // We pass in $routeParams and our Notes factory along with $scope | ||
* app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes', | ||
* // In our controller we get the ID from the URL using ngRoute and $routeParams | ||
* // We pass in $routeParams and our Notes factory along with $scope | ||
* app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes', | ||
function($scope, $routeParams, Notes) { | ||
@@ -320,16 +361,26 @@ * // First get a note object from the factory | ||
* // This will PUT /notes/ID with the note object in the request payload | ||
* }]); | ||
* }]); | ||
* ``` | ||
*/ | ||
angular.module('ngResource', ['ng']). | ||
factory('$resource', ['$http', '$q', function($http, $q) { | ||
provider('$resource', function() { | ||
var provider = this; | ||
var DEFAULT_ACTIONS = { | ||
'get': {method:'GET'}, | ||
'save': {method:'POST'}, | ||
'query': {method:'GET', isArray:true}, | ||
'remove': {method:'DELETE'}, | ||
'delete': {method:'DELETE'} | ||
this.defaults = { | ||
// Strip slashes by default | ||
stripTrailingSlashes: true, | ||
// Default actions configuration | ||
actions: { | ||
'get': {method: 'GET'}, | ||
'save': {method: 'POST'}, | ||
'query': {method: 'GET', isArray: true}, | ||
'remove': {method: 'DELETE'}, | ||
'delete': {method: 'DELETE'} | ||
} | ||
}; | ||
var noop = angular.noop, | ||
this.$get = ['$http', '$q', function($http, $q) { | ||
var noop = angular.noop, | ||
forEach = angular.forEach, | ||
@@ -340,50 +391,50 @@ extend = angular.extend, | ||
/** | ||
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow | ||
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set (pchar) allowed in path | ||
* segments: | ||
* segment = *pchar | ||
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
* pct-encoded = "%" HEXDIG HEXDIG | ||
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | ||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | ||
* / "*" / "+" / "," / ";" / "=" | ||
*/ | ||
function encodeUriSegment(val) { | ||
return encodeUriQuery(val, true). | ||
replace(/%26/gi, '&'). | ||
replace(/%3D/gi, '='). | ||
replace(/%2B/gi, '+'); | ||
} | ||
/** | ||
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow | ||
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set | ||
* (pchar) allowed in path segments: | ||
* segment = *pchar | ||
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
* pct-encoded = "%" HEXDIG HEXDIG | ||
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | ||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | ||
* / "*" / "+" / "," / ";" / "=" | ||
*/ | ||
function encodeUriSegment(val) { | ||
return encodeUriQuery(val, true). | ||
replace(/%26/gi, '&'). | ||
replace(/%3D/gi, '='). | ||
replace(/%2B/gi, '+'); | ||
} | ||
/** | ||
* This method is intended for encoding *key* or *value* parts of query component. We need a | ||
* custom method because encodeURIComponent is too aggressive and encodes stuff that doesn't | ||
* have to be encoded per http://tools.ietf.org/html/rfc3986: | ||
* query = *( pchar / "/" / "?" ) | ||
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | ||
* pct-encoded = "%" HEXDIG HEXDIG | ||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | ||
* / "*" / "+" / "," / ";" / "=" | ||
*/ | ||
function encodeUriQuery(val, pctEncodeSpaces) { | ||
return encodeURIComponent(val). | ||
replace(/%40/gi, '@'). | ||
replace(/%3A/gi, ':'). | ||
replace(/%24/g, '$'). | ||
replace(/%2C/gi, ','). | ||
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+')); | ||
} | ||
/** | ||
* This method is intended for encoding *key* or *value* parts of query component. We need a | ||
* custom method because encodeURIComponent is too aggressive and encodes stuff that doesn't | ||
* have to be encoded per http://tools.ietf.org/html/rfc3986: | ||
* query = *( pchar / "/" / "?" ) | ||
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | ||
* pct-encoded = "%" HEXDIG HEXDIG | ||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | ||
* / "*" / "+" / "," / ";" / "=" | ||
*/ | ||
function encodeUriQuery(val, pctEncodeSpaces) { | ||
return encodeURIComponent(val). | ||
replace(/%40/gi, '@'). | ||
replace(/%3A/gi, ':'). | ||
replace(/%24/g, '$'). | ||
replace(/%2C/gi, ','). | ||
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+')); | ||
} | ||
function Route(template, defaults) { | ||
this.template = template; | ||
this.defaults = defaults || {}; | ||
this.urlParams = {}; | ||
} | ||
function Route(template, defaults) { | ||
this.template = template; | ||
this.defaults = extend({}, provider.defaults, defaults); | ||
this.urlParams = {}; | ||
} | ||
Route.prototype = { | ||
setUrlParams: function(config, params, actionUrl) { | ||
var self = this, | ||
Route.prototype = { | ||
setUrlParams: function(config, params, actionUrl) { | ||
var self = this, | ||
url = actionUrl || self.template, | ||
@@ -393,181 +444,199 @@ val, | ||
var urlParams = self.urlParams = {}; | ||
forEach(url.split(/\W/), function(param){ | ||
if (param === 'hasOwnProperty') { | ||
throw $resourceMinErr('badname', "hasOwnProperty is not a valid parameter name."); | ||
} | ||
if (!(new RegExp("^\\d+$").test(param)) && param && | ||
(new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) { | ||
urlParams[param] = true; | ||
} | ||
}); | ||
url = url.replace(/\\:/g, ':'); | ||
var urlParams = self.urlParams = {}; | ||
forEach(url.split(/\W/), function(param) { | ||
if (param === 'hasOwnProperty') { | ||
throw $resourceMinErr('badname', "hasOwnProperty is not a valid parameter name."); | ||
} | ||
if (!(new RegExp("^\\d+$").test(param)) && param && | ||
(new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) { | ||
urlParams[param] = true; | ||
} | ||
}); | ||
url = url.replace(/\\:/g, ':'); | ||
params = params || {}; | ||
forEach(self.urlParams, function(_, urlParam){ | ||
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam]; | ||
if (angular.isDefined(val) && val !== null) { | ||
encodedVal = encodeUriSegment(val); | ||
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) { | ||
return encodedVal + p1; | ||
}); | ||
} else { | ||
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W|$)", "g"), function(match, | ||
leadingSlashes, tail) { | ||
if (tail.charAt(0) == '/') { | ||
return tail; | ||
} else { | ||
return leadingSlashes + tail; | ||
} | ||
}); | ||
params = params || {}; | ||
forEach(self.urlParams, function(_, urlParam) { | ||
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam]; | ||
if (angular.isDefined(val) && val !== null) { | ||
encodedVal = encodeUriSegment(val); | ||
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) { | ||
return encodedVal + p1; | ||
}); | ||
} else { | ||
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W|$)", "g"), function(match, | ||
leadingSlashes, tail) { | ||
if (tail.charAt(0) == '/') { | ||
return tail; | ||
} else { | ||
return leadingSlashes + tail; | ||
} | ||
}); | ||
} | ||
}); | ||
// strip trailing slashes and set the url (unless this behavior is specifically disabled) | ||
if (self.defaults.stripTrailingSlashes) { | ||
url = url.replace(/\/+$/, '') || '/'; | ||
} | ||
}); | ||
// strip trailing slashes and set the url | ||
url = url.replace(/\/+$/, '') || '/'; | ||
// then replace collapse `/.` if found in the last URL path segment before the query | ||
// E.g. `http://url.com/id./format?q=x` becomes `http://url.com/id.format?q=x` | ||
url = url.replace(/\/\.(?=\w+($|\?))/, '.'); | ||
// replace escaped `/\.` with `/.` | ||
config.url = url.replace(/\/\\\./, '/.'); | ||
// then replace collapse `/.` if found in the last URL path segment before the query | ||
// E.g. `http://url.com/id./format?q=x` becomes `http://url.com/id.format?q=x` | ||
url = url.replace(/\/\.(?=\w+($|\?))/, '.'); | ||
// replace escaped `/\.` with `/.` | ||
config.url = url.replace(/\/\\\./, '/.'); | ||
// set params - delegate param encoding to $http | ||
forEach(params, function(value, key){ | ||
if (!self.urlParams[key]) { | ||
config.params = config.params || {}; | ||
config.params[key] = value; | ||
} | ||
}); | ||
} | ||
}; | ||
// set params - delegate param encoding to $http | ||
forEach(params, function(value, key) { | ||
if (!self.urlParams[key]) { | ||
config.params = config.params || {}; | ||
config.params[key] = value; | ||
} | ||
}); | ||
} | ||
}; | ||
function resourceFactory(url, paramDefaults, actions) { | ||
var route = new Route(url); | ||
function resourceFactory(url, paramDefaults, actions, options) { | ||
var route = new Route(url, options); | ||
actions = extend({}, DEFAULT_ACTIONS, actions); | ||
actions = extend({}, provider.defaults.actions, actions); | ||
function extractParams(data, actionParams){ | ||
var ids = {}; | ||
actionParams = extend({}, paramDefaults, actionParams); | ||
forEach(actionParams, function(value, key){ | ||
if (isFunction(value)) { value = value(); } | ||
ids[key] = value && value.charAt && value.charAt(0) == '@' ? | ||
lookupDottedPath(data, value.substr(1)) : value; | ||
}); | ||
return ids; | ||
} | ||
function extractParams(data, actionParams) { | ||
var ids = {}; | ||
actionParams = extend({}, paramDefaults, actionParams); | ||
forEach(actionParams, function(value, key) { | ||
if (isFunction(value)) { value = value(); } | ||
ids[key] = value && value.charAt && value.charAt(0) == '@' ? | ||
lookupDottedPath(data, value.substr(1)) : value; | ||
}); | ||
return ids; | ||
} | ||
function defaultResponseInterceptor(response) { | ||
return response.resource; | ||
} | ||
function defaultResponseInterceptor(response) { | ||
return response.resource; | ||
} | ||
function Resource(value){ | ||
shallowClearAndCopy(value || {}, this); | ||
} | ||
function Resource(value) { | ||
shallowClearAndCopy(value || {}, this); | ||
} | ||
forEach(actions, function(action, name) { | ||
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method); | ||
Resource.prototype.toJSON = function() { | ||
var data = extend({}, this); | ||
delete data.$promise; | ||
delete data.$resolved; | ||
return data; | ||
}; | ||
Resource[name] = function(a1, a2, a3, a4) { | ||
var params = {}, data, success, error; | ||
forEach(actions, function(action, name) { | ||
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method); | ||
/* jshint -W086 */ /* (purposefully fall through case statements) */ | ||
switch(arguments.length) { | ||
case 4: | ||
error = a4; | ||
success = a3; | ||
//fallthrough | ||
case 3: | ||
case 2: | ||
if (isFunction(a2)) { | ||
if (isFunction(a1)) { | ||
success = a1; | ||
error = a2; | ||
break; | ||
} | ||
Resource[name] = function(a1, a2, a3, a4) { | ||
var params = {}, data, success, error; | ||
success = a2; | ||
error = a3; | ||
/* jshint -W086 */ /* (purposefully fall through case statements) */ | ||
switch (arguments.length) { | ||
case 4: | ||
error = a4; | ||
success = a3; | ||
//fallthrough | ||
} else { | ||
params = a1; | ||
data = a2; | ||
success = a3; | ||
break; | ||
case 3: | ||
case 2: | ||
if (isFunction(a2)) { | ||
if (isFunction(a1)) { | ||
success = a1; | ||
error = a2; | ||
break; | ||
} | ||
success = a2; | ||
error = a3; | ||
//fallthrough | ||
} else { | ||
params = a1; | ||
data = a2; | ||
success = a3; | ||
break; | ||
} | ||
case 1: | ||
if (isFunction(a1)) success = a1; | ||
else if (hasBody) data = a1; | ||
else params = a1; | ||
break; | ||
case 0: break; | ||
default: | ||
throw $resourceMinErr('badargs', | ||
"Expected up to 4 arguments [params, data, success, error], got {0} arguments", | ||
arguments.length); | ||
} | ||
case 1: | ||
if (isFunction(a1)) success = a1; | ||
else if (hasBody) data = a1; | ||
else params = a1; | ||
break; | ||
case 0: break; | ||
default: | ||
throw $resourceMinErr('badargs', | ||
"Expected up to 4 arguments [params, data, success, error], got {0} arguments", | ||
arguments.length); | ||
} | ||
/* jshint +W086 */ /* (purposefully fall through case statements) */ | ||
/* jshint +W086 */ /* (purposefully fall through case statements) */ | ||
var isInstanceCall = this instanceof Resource; | ||
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data)); | ||
var httpConfig = {}; | ||
var responseInterceptor = action.interceptor && action.interceptor.response || | ||
defaultResponseInterceptor; | ||
var responseErrorInterceptor = action.interceptor && action.interceptor.responseError || | ||
undefined; | ||
var isInstanceCall = this instanceof Resource; | ||
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data)); | ||
var httpConfig = {}; | ||
var responseInterceptor = action.interceptor && action.interceptor.response || | ||
defaultResponseInterceptor; | ||
var responseErrorInterceptor = action.interceptor && action.interceptor.responseError || | ||
undefined; | ||
forEach(action, function(value, key) { | ||
if (key != 'params' && key != 'isArray' && key != 'interceptor') { | ||
httpConfig[key] = copy(value); | ||
} | ||
}); | ||
forEach(action, function(value, key) { | ||
if (key != 'params' && key != 'isArray' && key != 'interceptor') { | ||
httpConfig[key] = copy(value); | ||
} | ||
}); | ||
if (hasBody) httpConfig.data = data; | ||
route.setUrlParams(httpConfig, | ||
extend({}, extractParams(data, action.params || {}), params), | ||
action.url); | ||
if (hasBody) httpConfig.data = data; | ||
route.setUrlParams(httpConfig, | ||
extend({}, extractParams(data, action.params || {}), params), | ||
action.url); | ||
var promise = $http(httpConfig).then(function(response) { | ||
var data = response.data, | ||
var promise = $http(httpConfig).then(function(response) { | ||
var data = response.data, | ||
promise = value.$promise; | ||
if (data) { | ||
// Need to convert action.isArray to boolean in case it is undefined | ||
// jshint -W018 | ||
if (angular.isArray(data) !== (!!action.isArray)) { | ||
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' + | ||
'response to contain an {0} but got an {1}', | ||
action.isArray?'array':'object', angular.isArray(data)?'array':'object'); | ||
if (data) { | ||
// Need to convert action.isArray to boolean in case it is undefined | ||
// jshint -W018 | ||
if (angular.isArray(data) !== (!!action.isArray)) { | ||
throw $resourceMinErr('badcfg', | ||
'Error in resource configuration for action `{0}`. Expected response to ' + | ||
'contain an {1} but got an {2}', name, action.isArray ? 'array' : 'object', | ||
angular.isArray(data) ? 'array' : 'object'); | ||
} | ||
// jshint +W018 | ||
if (action.isArray) { | ||
value.length = 0; | ||
forEach(data, function(item) { | ||
if (typeof item === "object") { | ||
value.push(new Resource(item)); | ||
} else { | ||
// Valid JSON values may be string literals, and these should not be converted | ||
// into objects. These items will not have access to the Resource prototype | ||
// methods, but unfortunately there | ||
value.push(item); | ||
} | ||
}); | ||
} else { | ||
shallowClearAndCopy(data, value); | ||
value.$promise = promise; | ||
} | ||
} | ||
// jshint +W018 | ||
if (action.isArray) { | ||
value.length = 0; | ||
forEach(data, function(item) { | ||
value.push(new Resource(item)); | ||
}); | ||
} else { | ||
shallowClearAndCopy(data, value); | ||
value.$promise = promise; | ||
} | ||
} | ||
value.$resolved = true; | ||
value.$resolved = true; | ||
response.resource = value; | ||
response.resource = value; | ||
return response; | ||
}, function(response) { | ||
value.$resolved = true; | ||
return response; | ||
}, function(response) { | ||
value.$resolved = true; | ||
(error||noop)(response); | ||
(error || noop)(response); | ||
return $q.reject(response); | ||
}); | ||
return $q.reject(response); | ||
}); | ||
promise = promise.then( | ||
promise = promise.then( | ||
function(response) { | ||
var value = responseInterceptor(response); | ||
(success||noop)(value, response.headers); | ||
(success || noop)(value, response.headers); | ||
return value; | ||
@@ -577,37 +646,38 @@ }, | ||
if (!isInstanceCall) { | ||
// we are creating instance / collection | ||
// - set the initial promise | ||
// - return the instance / collection | ||
value.$promise = promise; | ||
value.$resolved = false; | ||
if (!isInstanceCall) { | ||
// we are creating instance / collection | ||
// - set the initial promise | ||
// - return the instance / collection | ||
value.$promise = promise; | ||
value.$resolved = false; | ||
return value; | ||
} | ||
return value; | ||
} | ||
// instance call | ||
return promise; | ||
}; | ||
// instance call | ||
return promise; | ||
}; | ||
Resource.prototype['$' + name] = function(params, success, error) { | ||
if (isFunction(params)) { | ||
error = success; success = params; params = {}; | ||
} | ||
var result = Resource[name].call(this, params, this, success, error); | ||
return result.$promise || result; | ||
Resource.prototype['$' + name] = function(params, success, error) { | ||
if (isFunction(params)) { | ||
error = success; success = params; params = {}; | ||
} | ||
var result = Resource[name].call(this, params, this, success, error); | ||
return result.$promise || result; | ||
}; | ||
}); | ||
Resource.bind = function(additionalParamDefaults) { | ||
return resourceFactory(url, extend({}, paramDefaults, additionalParamDefaults), actions); | ||
}; | ||
}); | ||
Resource.bind = function(additionalParamDefaults){ | ||
return resourceFactory(url, extend({}, paramDefaults, additionalParamDefaults), actions); | ||
}; | ||
return Resource; | ||
} | ||
return Resource; | ||
} | ||
return resourceFactory; | ||
}]; | ||
}); | ||
return resourceFactory; | ||
}]); | ||
})(window, window.angular); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
1121043
62
28072
52
1
4
2