@angular-wave/angular.ts 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/.eslintrc.cjs +29 -0
- package/.github/workflows/playwright.yml +27 -0
- package/CHANGELOG.md +17974 -0
- package/CODE_OF_CONDUCT.md +3 -0
- package/CONTRIBUTING.md +246 -0
- package/DEVELOPERS.md +488 -0
- package/LICENSE +22 -0
- package/Makefile +31 -0
- package/README.md +115 -0
- package/RELEASE.md +98 -0
- package/SECURITY.md +16 -0
- package/TRIAGING.md +135 -0
- package/css/angular.css +22 -0
- package/dist/angular-ts.cjs.js +36843 -0
- package/dist/angular-ts.esm.js +36841 -0
- package/dist/angular-ts.umd.js +36848 -0
- package/dist/build/angular-animate.js +4272 -0
- package/dist/build/angular-aria.js +426 -0
- package/dist/build/angular-message-format.js +1072 -0
- package/dist/build/angular-messages.js +829 -0
- package/dist/build/angular-mocks.js +3757 -0
- package/dist/build/angular-parse-ext.js +1275 -0
- package/dist/build/angular-resource.js +911 -0
- package/dist/build/angular-route.js +1266 -0
- package/dist/build/angular-sanitize.js +891 -0
- package/dist/build/angular-touch.js +368 -0
- package/dist/build/angular.js +36600 -0
- package/e2e/unit.spec.ts +15 -0
- package/images/android-chrome-192x192.png +0 -0
- package/images/android-chrome-512x512.png +0 -0
- package/images/apple-touch-icon.png +0 -0
- package/images/favicon-16x16.png +0 -0
- package/images/favicon-32x32.png +0 -0
- package/images/favicon.ico +0 -0
- package/images/site.webmanifest +1 -0
- package/index.html +104 -0
- package/package.json +47 -0
- package/playwright.config.ts +78 -0
- package/public/circle.html +1 -0
- package/public/my_child_directive.html +1 -0
- package/public/my_directive.html +1 -0
- package/public/my_other_directive.html +1 -0
- package/public/test.html +1 -0
- package/rollup.config.js +31 -0
- package/src/animations/animateCache.js +55 -0
- package/src/animations/animateChildrenDirective.js +105 -0
- package/src/animations/animateCss.js +1139 -0
- package/src/animations/animateCssDriver.js +291 -0
- package/src/animations/animateJs.js +367 -0
- package/src/animations/animateJsDriver.js +67 -0
- package/src/animations/animateQueue.js +851 -0
- package/src/animations/animation.js +506 -0
- package/src/animations/module.js +779 -0
- package/src/animations/ngAnimateSwap.js +119 -0
- package/src/animations/rafScheduler.js +50 -0
- package/src/animations/shared.js +378 -0
- package/src/constants.js +20 -0
- package/src/core/animate.js +845 -0
- package/src/core/animateCss.js +73 -0
- package/src/core/animateRunner.js +195 -0
- package/src/core/attributes.js +199 -0
- package/src/core/cache.js +45 -0
- package/src/core/compile.js +4727 -0
- package/src/core/controller.js +225 -0
- package/src/core/exceptionHandler.js +63 -0
- package/src/core/filter.js +146 -0
- package/src/core/interpolate.js +442 -0
- package/src/core/interval.js +188 -0
- package/src/core/intervalFactory.js +57 -0
- package/src/core/location.js +1086 -0
- package/src/core/parser/parse.js +2562 -0
- package/src/core/parser/parse.md +13 -0
- package/src/core/q.js +746 -0
- package/src/core/rootScope.js +1596 -0
- package/src/core/sanitizeUri.js +85 -0
- package/src/core/sce.js +1161 -0
- package/src/core/taskTrackerFactory.js +125 -0
- package/src/core/timeout.js +121 -0
- package/src/core/urlUtils.js +187 -0
- package/src/core/utils.js +1349 -0
- package/src/directive/a.js +37 -0
- package/src/directive/attrs.js +283 -0
- package/src/directive/bind.js +51 -0
- package/src/directive/bind.md +142 -0
- package/src/directive/change.js +12 -0
- package/src/directive/change.md +25 -0
- package/src/directive/cloak.js +12 -0
- package/src/directive/cloak.md +24 -0
- package/src/directive/events.js +75 -0
- package/src/directive/events.md +166 -0
- package/src/directive/form.js +725 -0
- package/src/directive/init.js +15 -0
- package/src/directive/init.md +41 -0
- package/src/directive/input.js +1783 -0
- package/src/directive/list.js +46 -0
- package/src/directive/list.md +22 -0
- package/src/directive/ngClass.js +249 -0
- package/src/directive/ngController.js +64 -0
- package/src/directive/ngCsp.js +82 -0
- package/src/directive/ngIf.js +134 -0
- package/src/directive/ngInclude.js +217 -0
- package/src/directive/ngModel.js +1356 -0
- package/src/directive/ngModelOptions.js +509 -0
- package/src/directive/ngOptions.js +670 -0
- package/src/directive/ngRef.js +90 -0
- package/src/directive/ngRepeat.js +650 -0
- package/src/directive/ngShowHide.js +255 -0
- package/src/directive/ngSwitch.js +178 -0
- package/src/directive/ngTransclude.js +98 -0
- package/src/directive/non-bindable.js +11 -0
- package/src/directive/non-bindable.md +17 -0
- package/src/directive/script.js +30 -0
- package/src/directive/select.js +624 -0
- package/src/directive/style.js +25 -0
- package/src/directive/style.md +23 -0
- package/src/directive/validators.js +329 -0
- package/src/exts/aria.js +544 -0
- package/src/exts/messages.js +852 -0
- package/src/filters/filter.js +207 -0
- package/src/filters/filter.md +69 -0
- package/src/filters/filters.js +239 -0
- package/src/filters/json.md +16 -0
- package/src/filters/limit-to.js +43 -0
- package/src/filters/limit-to.md +19 -0
- package/src/filters/order-by.js +183 -0
- package/src/filters/order-by.md +83 -0
- package/src/index.js +13 -0
- package/src/injector.js +1034 -0
- package/src/jqLite.js +1117 -0
- package/src/loader.js +1320 -0
- package/src/public.js +215 -0
- package/src/routeToRegExp.js +41 -0
- package/src/services/anchorScroll.js +135 -0
- package/src/services/browser.js +321 -0
- package/src/services/cacheFactory.js +398 -0
- package/src/services/cookieReader.js +72 -0
- package/src/services/document.js +64 -0
- package/src/services/http.js +1537 -0
- package/src/services/httpBackend.js +206 -0
- package/src/services/log.js +160 -0
- package/src/services/templateRequest.js +139 -0
- package/test/angular.spec.js +2153 -0
- package/test/aria/aria.spec.js +1245 -0
- package/test/binding.spec.js +504 -0
- package/test/build-test.html +14 -0
- package/test/injector.spec.js +2327 -0
- package/test/jasmine/jasmine-5.1.2/boot0.js +65 -0
- package/test/jasmine/jasmine-5.1.2/boot1.js +133 -0
- package/test/jasmine/jasmine-5.1.2/jasmine-html.js +963 -0
- package/test/jasmine/jasmine-5.1.2/jasmine.css +320 -0
- package/test/jasmine/jasmine-5.1.2/jasmine.js +10824 -0
- package/test/jasmine/jasmine-5.1.2/jasmine_favicon.png +0 -0
- package/test/jasmine/jasmine-browser.json +17 -0
- package/test/jasmine/jasmine.json +9 -0
- package/test/jqlite.spec.js +2133 -0
- package/test/loader.spec.js +219 -0
- package/test/messages/messages.spec.js +1146 -0
- package/test/min-err.spec.js +174 -0
- package/test/mock-test.html +13 -0
- package/test/module-test.html +15 -0
- package/test/ng/anomate.spec.js +606 -0
- package/test/ng/cache-factor.spec.js +334 -0
- package/test/ng/compile.spec.js +17956 -0
- package/test/ng/controller-provider.spec.js +227 -0
- package/test/ng/cookie-reader.spec.js +98 -0
- package/test/ng/directive/a.spec.js +192 -0
- package/test/ng/directive/bind.spec.js +334 -0
- package/test/ng/directive/boolean.spec.js +136 -0
- package/test/ng/directive/change.spec.js +71 -0
- package/test/ng/directive/class.spec.js +858 -0
- package/test/ng/directive/click.spec.js +38 -0
- package/test/ng/directive/cloak.spec.js +44 -0
- package/test/ng/directive/constoller.spec.js +194 -0
- package/test/ng/directive/element-style.spec.js +92 -0
- package/test/ng/directive/event.spec.js +282 -0
- package/test/ng/directive/form.spec.js +1518 -0
- package/test/ng/directive/href.spec.js +143 -0
- package/test/ng/directive/if.spec.js +402 -0
- package/test/ng/directive/include.spec.js +828 -0
- package/test/ng/directive/init.spec.js +68 -0
- package/test/ng/directive/input.spec.js +3810 -0
- package/test/ng/directive/list.spec.js +170 -0
- package/test/ng/directive/model-options.spec.js +1008 -0
- package/test/ng/directive/model.spec.js +1905 -0
- package/test/ng/directive/non-bindable.spec.js +55 -0
- package/test/ng/directive/options.spec.js +3583 -0
- package/test/ng/directive/ref.spec.js +575 -0
- package/test/ng/directive/repeat.spec.js +1675 -0
- package/test/ng/directive/script.spec.js +52 -0
- package/test/ng/directive/scrset.spec.js +67 -0
- package/test/ng/directive/select.spec.js +2541 -0
- package/test/ng/directive/show-hide.spec.js +253 -0
- package/test/ng/directive/src.spec.js +157 -0
- package/test/ng/directive/style.spec.js +178 -0
- package/test/ng/directive/switch.spec.js +647 -0
- package/test/ng/directive/validators.spec.js +717 -0
- package/test/ng/document.spec.js +52 -0
- package/test/ng/filter/filter.spec.js +714 -0
- package/test/ng/filter/filters.spec.js +35 -0
- package/test/ng/filter/limit-to.spec.js +251 -0
- package/test/ng/filter/order-by.spec.js +891 -0
- package/test/ng/filter.spec.js +149 -0
- package/test/ng/http-backend.spec.js +398 -0
- package/test/ng/http.spec.js +4071 -0
- package/test/ng/interpolate.spec.js +642 -0
- package/test/ng/interval.spec.js +343 -0
- package/test/ng/location.spec.js +3488 -0
- package/test/ng/on.spec.js +229 -0
- package/test/ng/parse.spec.js +4655 -0
- package/test/ng/prop.spec.js +805 -0
- package/test/ng/q.spec.js +2904 -0
- package/test/ng/root-element.spec.js +16 -0
- package/test/ng/sanitize-uri.spec.js +249 -0
- package/test/ng/sce.spec.js +660 -0
- package/test/ng/scope.spec.js +3442 -0
- package/test/ng/template-request.spec.js +236 -0
- package/test/ng/timeout.spec.js +351 -0
- package/test/ng/url-utils.spec.js +156 -0
- package/test/ng/utils.spec.js +144 -0
- package/test/original-test.html +21 -0
- package/test/public.spec.js +34 -0
- package/test/sanitize/bing-html.spec.js +36 -0
- package/test/server/express.js +158 -0
- package/test/test-utils.js +11 -0
- package/tsconfig.json +17 -0
- package/types/angular.d.ts +138 -0
- package/types/global.d.ts +9 -0
- package/types/index.d.ts +2357 -0
- package/types/jqlite.d.ts +558 -0
- package/vite.config.js +14 -0
|
@@ -0,0 +1,1537 @@
|
|
|
1
|
+
import { urlIsAllowedOriginFactory } from "../core/urlUtils";
|
|
2
|
+
import {
|
|
3
|
+
minErr,
|
|
4
|
+
isObject,
|
|
5
|
+
isDate,
|
|
6
|
+
toJson,
|
|
7
|
+
forEachSorted,
|
|
8
|
+
isUndefined,
|
|
9
|
+
isFunction,
|
|
10
|
+
isArray,
|
|
11
|
+
forEach,
|
|
12
|
+
encodeUriQuery,
|
|
13
|
+
isString,
|
|
14
|
+
fromJson,
|
|
15
|
+
createMap,
|
|
16
|
+
lowercase,
|
|
17
|
+
trim,
|
|
18
|
+
isFile,
|
|
19
|
+
isBlob,
|
|
20
|
+
isFormData,
|
|
21
|
+
shallowCopy,
|
|
22
|
+
isDefined,
|
|
23
|
+
extend,
|
|
24
|
+
uppercase,
|
|
25
|
+
isPromiseLike,
|
|
26
|
+
} from "../core/utils";
|
|
27
|
+
|
|
28
|
+
const APPLICATION_JSON = "application/json";
|
|
29
|
+
const CONTENT_TYPE_APPLICATION_JSON = {
|
|
30
|
+
"Content-Type": `${APPLICATION_JSON};charset=utf-8`,
|
|
31
|
+
};
|
|
32
|
+
const JSON_START = /^\[|^\{(?!\{)/;
|
|
33
|
+
const JSON_ENDS = {
|
|
34
|
+
"[": /]$/,
|
|
35
|
+
"{": /}$/,
|
|
36
|
+
};
|
|
37
|
+
const JSON_PROTECTION_PREFIX = /^\)]\}',?\n/;
|
|
38
|
+
const $httpMinErr = minErr("$http");
|
|
39
|
+
|
|
40
|
+
function serializeValue(v) {
|
|
41
|
+
if (isObject(v)) {
|
|
42
|
+
return isDate(v) ? v.toISOString() : toJson(v);
|
|
43
|
+
}
|
|
44
|
+
return v;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function $HttpParamSerializerProvider() {
|
|
48
|
+
/**
|
|
49
|
+
* @ngdoc service
|
|
50
|
+
* @name $httpParamSerializer
|
|
51
|
+
* @description
|
|
52
|
+
*
|
|
53
|
+
* Default {@link $http `$http`} params serializer that converts objects to strings
|
|
54
|
+
* according to the following rules:
|
|
55
|
+
*
|
|
56
|
+
* * `{'foo': 'bar'}` results in `foo=bar`
|
|
57
|
+
* * `{'foo': Date.now()}` results in `foo=2015-04-01T09%3A50%3A49.262Z` (`toISOString()` and encoded representation of a Date object)
|
|
58
|
+
* * `{'foo': ['bar', 'baz']}` results in `foo=bar&foo=baz` (repeated key for each array element)
|
|
59
|
+
* * `{'foo': {'bar':'baz'}}` results in `foo=%7B%22bar%22%3A%22baz%22%7D` (stringified and encoded representation of an object)
|
|
60
|
+
*
|
|
61
|
+
* Note that serializer will sort the request parameters alphabetically.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
this.$get = function () {
|
|
65
|
+
return function ngParamSerializer(params) {
|
|
66
|
+
if (!params) return "";
|
|
67
|
+
const parts = [];
|
|
68
|
+
forEachSorted(params, (value, key) => {
|
|
69
|
+
if (value === null || isUndefined(value) || isFunction(value)) return;
|
|
70
|
+
if (isArray(value)) {
|
|
71
|
+
forEach(value, (v) => {
|
|
72
|
+
parts.push(
|
|
73
|
+
`${encodeUriQuery(key)}=${encodeUriQuery(serializeValue(v))}`,
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
parts.push(
|
|
78
|
+
`${encodeUriQuery(key)}=${encodeUriQuery(serializeValue(value))}`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return parts.join("&");
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function $HttpParamSerializerJQLikeProvider() {
|
|
89
|
+
/**
|
|
90
|
+
* @ngdoc service
|
|
91
|
+
* @name $httpParamSerializerJQLike
|
|
92
|
+
*
|
|
93
|
+
* @description
|
|
94
|
+
*
|
|
95
|
+
* Alternative {@link $http `$http`} params serializer that follows
|
|
96
|
+
* jQuery's [`param()`](http://api.jquery.com/jquery.param/) method logic.
|
|
97
|
+
* The serializer will also sort the params alphabetically.
|
|
98
|
+
*
|
|
99
|
+
* To use it for serializing `$http` request parameters, set it as the `paramSerializer` property:
|
|
100
|
+
*
|
|
101
|
+
* ```js
|
|
102
|
+
* $http({
|
|
103
|
+
* url: myUrl,
|
|
104
|
+
* method: 'GET',
|
|
105
|
+
* params: myParams,
|
|
106
|
+
* paramSerializer: '$httpParamSerializerJQLike'
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* It is also possible to set it as the default `paramSerializer` in the
|
|
111
|
+
* {@link $httpProvider#defaults `$httpProvider`}.
|
|
112
|
+
*
|
|
113
|
+
* Additionally, you can inject the serializer and use it explicitly, for example to serialize
|
|
114
|
+
* form data for submission:
|
|
115
|
+
*
|
|
116
|
+
* ```js
|
|
117
|
+
* .controller(function($http, $httpParamSerializerJQLike) {
|
|
118
|
+
* //...
|
|
119
|
+
*
|
|
120
|
+
* $http({
|
|
121
|
+
* url: myUrl,
|
|
122
|
+
* method: 'POST',
|
|
123
|
+
* data: $httpParamSerializerJQLike(myData),
|
|
124
|
+
* headers: {
|
|
125
|
+
* 'Content-Type': 'application/x-www-form-urlencoded'
|
|
126
|
+
* }
|
|
127
|
+
* });
|
|
128
|
+
*
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
this.$get = function () {
|
|
134
|
+
return function jQueryLikeParamSerializer(params) {
|
|
135
|
+
if (!params) return "";
|
|
136
|
+
const parts = [];
|
|
137
|
+
serialize(params, "", true);
|
|
138
|
+
return parts.join("&");
|
|
139
|
+
|
|
140
|
+
function serialize(toSerialize, prefix, topLevel) {
|
|
141
|
+
if (isArray(toSerialize)) {
|
|
142
|
+
forEach(toSerialize, (value, index) => {
|
|
143
|
+
serialize(value, `${prefix}[${isObject(value) ? index : ""}]`);
|
|
144
|
+
});
|
|
145
|
+
} else if (isObject(toSerialize) && !isDate(toSerialize)) {
|
|
146
|
+
forEachSorted(toSerialize, (value, key) => {
|
|
147
|
+
serialize(
|
|
148
|
+
value,
|
|
149
|
+
prefix + (topLevel ? "" : "[") + key + (topLevel ? "" : "]"),
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
} else {
|
|
153
|
+
if (isFunction(toSerialize)) {
|
|
154
|
+
toSerialize = toSerialize();
|
|
155
|
+
}
|
|
156
|
+
parts.push(
|
|
157
|
+
`${encodeUriQuery(prefix)}=${
|
|
158
|
+
toSerialize == null
|
|
159
|
+
? ""
|
|
160
|
+
: encodeUriQuery(serializeValue(toSerialize))
|
|
161
|
+
}`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function defaultHttpResponseTransform(data, headers) {
|
|
170
|
+
if (isString(data)) {
|
|
171
|
+
// Strip json vulnerability protection prefix and trim whitespace
|
|
172
|
+
const tempData = data.replace(JSON_PROTECTION_PREFIX, "").trim();
|
|
173
|
+
|
|
174
|
+
if (tempData) {
|
|
175
|
+
const contentType = headers("Content-Type");
|
|
176
|
+
const hasJsonContentType =
|
|
177
|
+
contentType && contentType.indexOf(APPLICATION_JSON) === 0;
|
|
178
|
+
|
|
179
|
+
if (hasJsonContentType || isJsonLike(tempData)) {
|
|
180
|
+
try {
|
|
181
|
+
data = fromJson(tempData);
|
|
182
|
+
} catch (e) {
|
|
183
|
+
if (!hasJsonContentType) {
|
|
184
|
+
return data;
|
|
185
|
+
}
|
|
186
|
+
throw $httpMinErr(
|
|
187
|
+
"baddata",
|
|
188
|
+
'Data must be a valid JSON object. Received: "{0}". ' +
|
|
189
|
+
'Parse error: "{1}"',
|
|
190
|
+
data,
|
|
191
|
+
e,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return data;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function isJsonLike(str) {
|
|
202
|
+
const jsonStart = str.match(JSON_START);
|
|
203
|
+
return jsonStart && JSON_ENDS[jsonStart[0]].test(str);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Parse headers into key value object
|
|
208
|
+
*
|
|
209
|
+
* @param {string} headers Raw headers as a string
|
|
210
|
+
* @returns {Object} Parsed headers as key value object
|
|
211
|
+
*/
|
|
212
|
+
function parseHeaders(headers) {
|
|
213
|
+
const parsed = createMap();
|
|
214
|
+
let i;
|
|
215
|
+
|
|
216
|
+
function fillInParsed(key, val) {
|
|
217
|
+
if (key) {
|
|
218
|
+
parsed[key] = parsed[key] ? `${parsed[key]}, ${val}` : val;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (isString(headers)) {
|
|
223
|
+
forEach(headers.split("\n"), (line) => {
|
|
224
|
+
i = line.indexOf(":");
|
|
225
|
+
fillInParsed(
|
|
226
|
+
lowercase(trim(line.substr(0, i))),
|
|
227
|
+
trim(line.substr(i + 1)),
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
} else if (isObject(headers)) {
|
|
231
|
+
forEach(headers, (headerVal, headerKey) => {
|
|
232
|
+
fillInParsed(lowercase(headerKey), trim(headerVal));
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return parsed;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Returns a function that provides access to parsed headers.
|
|
241
|
+
*
|
|
242
|
+
* Headers are lazy parsed when first requested.
|
|
243
|
+
* @see parseHeaders
|
|
244
|
+
*
|
|
245
|
+
* @param {(string|Object)} headers Headers to provide access to.
|
|
246
|
+
* @returns {function(string=)} Returns a getter function which if called with:
|
|
247
|
+
*
|
|
248
|
+
* - if called with an argument returns a single header value or null
|
|
249
|
+
* - if called with no arguments returns an object containing all headers.
|
|
250
|
+
*/
|
|
251
|
+
function headersGetter(headers) {
|
|
252
|
+
let headersObj;
|
|
253
|
+
|
|
254
|
+
return function (name) {
|
|
255
|
+
if (!headersObj) headersObj = parseHeaders(headers);
|
|
256
|
+
|
|
257
|
+
if (name) {
|
|
258
|
+
let value = headersObj[lowercase(name)];
|
|
259
|
+
if (value === undefined) {
|
|
260
|
+
value = null;
|
|
261
|
+
}
|
|
262
|
+
return value;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return headersObj;
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Chain all given functions
|
|
271
|
+
*
|
|
272
|
+
* This function is used for both request and response transforming
|
|
273
|
+
*
|
|
274
|
+
* @param {*} data Data to transform.
|
|
275
|
+
* @param {function(string=)} headers HTTP headers getter fn.
|
|
276
|
+
* @param {number} status HTTP status code of the response.
|
|
277
|
+
* @param {(Function|Array.<Function>)} fns Function or an array of functions.
|
|
278
|
+
* @returns {*} Transformed data.
|
|
279
|
+
*/
|
|
280
|
+
function transformData(data, headers, status, fns) {
|
|
281
|
+
if (isFunction(fns)) {
|
|
282
|
+
return fns(data, headers, status);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
forEach(fns, (fn) => {
|
|
286
|
+
data = fn(data, headers, status);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
return data;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function isSuccess(status) {
|
|
293
|
+
return status >= 200 && status < 300;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* @ngdoc provider
|
|
298
|
+
* @name $httpProvider
|
|
299
|
+
*
|
|
300
|
+
*
|
|
301
|
+
* @description
|
|
302
|
+
* Use `$httpProvider` to change the default behavior of the {@link ng.$http $http} service.
|
|
303
|
+
*/
|
|
304
|
+
export function $HttpProvider() {
|
|
305
|
+
/**
|
|
306
|
+
* @ngdoc property
|
|
307
|
+
* @name $httpProvider#defaults
|
|
308
|
+
* @description
|
|
309
|
+
*
|
|
310
|
+
* Object containing default values for all {@link ng.$http $http} requests.
|
|
311
|
+
*
|
|
312
|
+
* - **`defaults.cache`** - {boolean|Object} - A boolean value or object created with
|
|
313
|
+
* {@link ng.$cacheFactory `$cacheFactory`} to enable or disable caching of HTTP responses
|
|
314
|
+
* by default. See {@link $http#caching $http Caching} for more information.
|
|
315
|
+
*
|
|
316
|
+
* - **`defaults.headers`** - {Object} - Default headers for all $http requests.
|
|
317
|
+
* Refer to {@link ng.$http#setting-http-headers $http} for documentation on
|
|
318
|
+
* setting default headers.
|
|
319
|
+
* - **`defaults.headers.common`**
|
|
320
|
+
* - **`defaults.headers.post`**
|
|
321
|
+
* - **`defaults.headers.put`**
|
|
322
|
+
* - **`defaults.headers.patch`**
|
|
323
|
+
* *
|
|
324
|
+
* - **`defaults.paramSerializer`** - `{string|function(Object<string,string>):string}` - A function
|
|
325
|
+
* used to the prepare string representation of request parameters (specified as an object).
|
|
326
|
+
* If specified as string, it is interpreted as a function registered with the {@link auto.$injector $injector}.
|
|
327
|
+
* Defaults to {@link ng.$httpParamSerializer $httpParamSerializer}.
|
|
328
|
+
*
|
|
329
|
+
* - **`defaults.transformRequest`** -
|
|
330
|
+
* `{Array<function(data, headersGetter)>|function(data, headersGetter)}` -
|
|
331
|
+
* An array of functions (or a single function) which are applied to the request data.
|
|
332
|
+
* By default, this is an array with one request transformation function:
|
|
333
|
+
*
|
|
334
|
+
* - If the `data` property of the request configuration object contains an object, serialize it
|
|
335
|
+
* into JSON format.
|
|
336
|
+
*
|
|
337
|
+
* - **`defaults.transformResponse`** -
|
|
338
|
+
* `{Array<function(data, headersGetter, status)>|function(data, headersGetter, status)}` -
|
|
339
|
+
* An array of functions (or a single function) which are applied to the response data. By default,
|
|
340
|
+
* this is an array which applies one response transformation function that does two things:
|
|
341
|
+
*
|
|
342
|
+
* - If XSRF prefix is detected, strip it
|
|
343
|
+
* (see {@link ng.$http#security-considerations Security Considerations in the $http docs}).
|
|
344
|
+
* - If the `Content-Type` is `application/json` or the response looks like JSON,
|
|
345
|
+
* deserialize it using a JSON parser.
|
|
346
|
+
*
|
|
347
|
+
* - **`defaults.xsrfCookieName`** - {string} - Name of cookie containing the XSRF token.
|
|
348
|
+
* Defaults value is `'XSRF-TOKEN'`.
|
|
349
|
+
*
|
|
350
|
+
* - **`defaults.xsrfHeaderName`** - {string} - Name of HTTP header to populate with the
|
|
351
|
+
* XSRF token. Defaults value is `'X-XSRF-TOKEN'`.
|
|
352
|
+
*
|
|
353
|
+
*/
|
|
354
|
+
const defaults = (this.defaults = {
|
|
355
|
+
// transform incoming response data
|
|
356
|
+
transformResponse: [defaultHttpResponseTransform],
|
|
357
|
+
|
|
358
|
+
// transform outgoing request data
|
|
359
|
+
transformRequest: [
|
|
360
|
+
function (d) {
|
|
361
|
+
return isObject(d) && !isFile(d) && !isBlob(d) && !isFormData(d)
|
|
362
|
+
? toJson(d)
|
|
363
|
+
: d;
|
|
364
|
+
},
|
|
365
|
+
],
|
|
366
|
+
|
|
367
|
+
// default headers
|
|
368
|
+
headers: {
|
|
369
|
+
common: {
|
|
370
|
+
Accept: "application/json, text/plain, */*",
|
|
371
|
+
},
|
|
372
|
+
post: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
|
|
373
|
+
put: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
|
|
374
|
+
patch: shallowCopy(CONTENT_TYPE_APPLICATION_JSON),
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
xsrfCookieName: "XSRF-TOKEN",
|
|
378
|
+
xsrfHeaderName: "X-XSRF-TOKEN",
|
|
379
|
+
|
|
380
|
+
paramSerializer: "$httpParamSerializer",
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
let useApplyAsync = false;
|
|
384
|
+
/**
|
|
385
|
+
* @ngdoc method
|
|
386
|
+
* @name $httpProvider#useApplyAsync
|
|
387
|
+
* @description
|
|
388
|
+
*
|
|
389
|
+
* Configure $http service to combine processing of multiple http responses received at around
|
|
390
|
+
* the same time via {@link ng.$rootScope.Scope#$applyAsync $rootScope.$applyAsync}. This can result in
|
|
391
|
+
* significant performance improvement for bigger applications that make many HTTP requests
|
|
392
|
+
* concurrently (common during application bootstrap).
|
|
393
|
+
*
|
|
394
|
+
* Defaults to false. If no value is specified, returns the current configured value.
|
|
395
|
+
*
|
|
396
|
+
* @param {boolean=} value If true, when requests are loaded, they will schedule a deferred
|
|
397
|
+
* "apply" on the next tick, giving time for subsequent requests in a roughly ~10ms window
|
|
398
|
+
* to load and share the same digest cycle.
|
|
399
|
+
*
|
|
400
|
+
* @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining.
|
|
401
|
+
* otherwise, returns the current configured value.
|
|
402
|
+
*/
|
|
403
|
+
this.useApplyAsync = function (value) {
|
|
404
|
+
if (isDefined(value)) {
|
|
405
|
+
useApplyAsync = !!value;
|
|
406
|
+
return this;
|
|
407
|
+
}
|
|
408
|
+
return useApplyAsync;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* @ngdoc property
|
|
413
|
+
* @name $httpProvider#interceptors
|
|
414
|
+
* @description
|
|
415
|
+
*
|
|
416
|
+
* Array containing service factories for all synchronous or asynchronous {@link ng.$http $http}
|
|
417
|
+
* pre-processing of request or postprocessing of responses.
|
|
418
|
+
*
|
|
419
|
+
* These service factories are ordered by request, i.e. they are applied in the same order as the
|
|
420
|
+
* array, on request, but reverse order, on response.
|
|
421
|
+
*
|
|
422
|
+
* {@link ng.$http#interceptors Interceptors detailed info}
|
|
423
|
+
*/
|
|
424
|
+
const interceptorFactories = (this.interceptors = []);
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* @ngdoc property
|
|
428
|
+
* @name $httpProvider#xsrfTrustedOrigins
|
|
429
|
+
* @description
|
|
430
|
+
*
|
|
431
|
+
* Array containing URLs whose origins are trusted to receive the XSRF token. See the
|
|
432
|
+
* {@link ng.$http#security-considerations Security Considerations} sections for more details on
|
|
433
|
+
* XSRF.
|
|
434
|
+
*
|
|
435
|
+
* **Note:** An "origin" consists of the [URI scheme](https://en.wikipedia.org/wiki/URI_scheme),
|
|
436
|
+
* the [hostname](https://en.wikipedia.org/wiki/Hostname) and the
|
|
437
|
+
* [port number](https://en.wikipedia.org/wiki/Port_(computer_networking). For `http:` and
|
|
438
|
+
* `https:`, the port number can be omitted if using th default ports (80 and 443 respectively).
|
|
439
|
+
* Examples: `http://example.com`, `https://api.example.com:9876`
|
|
440
|
+
*
|
|
441
|
+
* <div class="alert alert-warning">
|
|
442
|
+
* It is not possible to trust specific URLs/paths. The `path`, `query` and `fragment` parts
|
|
443
|
+
* of a URL will be ignored. For example, `https://foo.com/path/bar?query=baz#fragment` will be
|
|
444
|
+
* treated as `https://foo.com`, meaning that **all** requests to URLs starting with
|
|
445
|
+
* `https://foo.com/` will include the XSRF token.
|
|
446
|
+
* </div>
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
*
|
|
450
|
+
* ```js
|
|
451
|
+
* // App served from `https://example.com/`.
|
|
452
|
+
* angular.
|
|
453
|
+
* module('xsrfTrustedOriginsExample', []).
|
|
454
|
+
* config(['$httpProvider', function($httpProvider) {
|
|
455
|
+
* $httpProvider.xsrfTrustedOrigins.push('https://api.example.com');
|
|
456
|
+
* }]).
|
|
457
|
+
* run(['$http', function($http) {
|
|
458
|
+
* // The XSRF token will be sent.
|
|
459
|
+
* $http.get('https://api.example.com/preferences').then(...);
|
|
460
|
+
*
|
|
461
|
+
* // The XSRF token will NOT be sent.
|
|
462
|
+
* $http.get('https://stats.example.com/activity').then(...);
|
|
463
|
+
* }]);
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
const xsrfTrustedOrigins = (this.xsrfTrustedOrigins = []);
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* @ngdoc property
|
|
470
|
+
* @name $httpProvider#xsrfWhitelistedOrigins
|
|
471
|
+
* @description
|
|
472
|
+
*
|
|
473
|
+
* @deprecated
|
|
474
|
+
* sinceVersion="1.8.1"
|
|
475
|
+
*
|
|
476
|
+
* This property is deprecated. Use {@link $httpProvider#xsrfTrustedOrigins xsrfTrustedOrigins}
|
|
477
|
+
* instead.
|
|
478
|
+
*/
|
|
479
|
+
Object.defineProperty(this, "xsrfWhitelistedOrigins", {
|
|
480
|
+
get() {
|
|
481
|
+
return this.xsrfTrustedOrigins;
|
|
482
|
+
},
|
|
483
|
+
set(origins) {
|
|
484
|
+
this.xsrfTrustedOrigins = origins;
|
|
485
|
+
},
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
this.$get = [
|
|
489
|
+
"$browser",
|
|
490
|
+
"$httpBackend",
|
|
491
|
+
"$$cookieReader",
|
|
492
|
+
"$cacheFactory",
|
|
493
|
+
"$rootScope",
|
|
494
|
+
"$q",
|
|
495
|
+
"$injector",
|
|
496
|
+
"$sce",
|
|
497
|
+
function (
|
|
498
|
+
$browser,
|
|
499
|
+
$httpBackend,
|
|
500
|
+
$$cookieReader,
|
|
501
|
+
$cacheFactory,
|
|
502
|
+
$rootScope,
|
|
503
|
+
$q,
|
|
504
|
+
$injector,
|
|
505
|
+
$sce,
|
|
506
|
+
) {
|
|
507
|
+
const defaultCache = $cacheFactory("$http");
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Make sure that default param serializer is exposed as a function
|
|
511
|
+
*/
|
|
512
|
+
defaults.paramSerializer = isString(defaults.paramSerializer)
|
|
513
|
+
? $injector.get(defaults.paramSerializer)
|
|
514
|
+
: defaults.paramSerializer;
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Interceptors stored in reverse order. Inner interceptors before outer interceptors.
|
|
518
|
+
* The reversal is needed so that we can build up the interception chain around the
|
|
519
|
+
* server request.
|
|
520
|
+
*/
|
|
521
|
+
const reversedInterceptors = [];
|
|
522
|
+
|
|
523
|
+
forEach(interceptorFactories, (interceptorFactory) => {
|
|
524
|
+
reversedInterceptors.unshift(
|
|
525
|
+
isString(interceptorFactory)
|
|
526
|
+
? $injector.get(interceptorFactory)
|
|
527
|
+
: $injector.invoke(interceptorFactory),
|
|
528
|
+
);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* A function to check request URLs against a list of allowed origins.
|
|
533
|
+
*/
|
|
534
|
+
const urlIsAllowedOrigin = urlIsAllowedOriginFactory(xsrfTrustedOrigins);
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* @ngdoc service
|
|
538
|
+
* @kind function
|
|
539
|
+
* @name $http
|
|
540
|
+
* @requires ng.$httpBackend
|
|
541
|
+
* @requires $cacheFactory
|
|
542
|
+
* @requires $rootScope
|
|
543
|
+
* @requires $q
|
|
544
|
+
* @requires $injector
|
|
545
|
+
*
|
|
546
|
+
* @description
|
|
547
|
+
* The `$http` service is a core AngularJS service that facilitates communication with the remote
|
|
548
|
+
* HTTP servers via the browser's [XMLHttpRequest](https://developer.mozilla.org/en/xmlhttprequest)
|
|
549
|
+
* object or via [JSONP](http://en.wikipedia.org/wiki/JSONP).
|
|
550
|
+
*
|
|
551
|
+
* For unit testing applications that use `$http` service, see
|
|
552
|
+
* {@link ngMock.$httpBackend $httpBackend mock}.
|
|
553
|
+
*
|
|
554
|
+
* For a higher level of abstraction, please check out the {@link ngResource.$resource
|
|
555
|
+
* $resource} service.
|
|
556
|
+
*
|
|
557
|
+
* The $http API is based on the {@link ng.$q deferred/promise APIs} exposed by
|
|
558
|
+
* the $q service. While for simple usage patterns this doesn't matter much, for advanced usage
|
|
559
|
+
* it is important to familiarize yourself with these APIs and the guarantees they provide.
|
|
560
|
+
*
|
|
561
|
+
*
|
|
562
|
+
* ## General usage
|
|
563
|
+
* The `$http` service is a function which takes a single argument — a {@link $http#usage configuration object} —
|
|
564
|
+
* that is used to generate an HTTP request and returns a {@link ng.$q promise} that is
|
|
565
|
+
* resolved (request success) or rejected (request failure) with a
|
|
566
|
+
* {@link ng.$http#$http-returns response} object.
|
|
567
|
+
*
|
|
568
|
+
* ```js
|
|
569
|
+
* // Simple GET request example:
|
|
570
|
+
* $http({
|
|
571
|
+
* method: 'GET',
|
|
572
|
+
* url: '/someUrl'
|
|
573
|
+
* }).then(function successCallback(response) {
|
|
574
|
+
* // this callback will be called asynchronously
|
|
575
|
+
* // when the response is available
|
|
576
|
+
* }, function errorCallback(response) {
|
|
577
|
+
* // called asynchronously if an error occurs
|
|
578
|
+
* // or server returns response with an error status.
|
|
579
|
+
* });
|
|
580
|
+
* ```
|
|
581
|
+
*
|
|
582
|
+
*
|
|
583
|
+
* ## Shortcut methods
|
|
584
|
+
*
|
|
585
|
+
* Shortcut methods are also available. All shortcut methods require passing in the URL, and
|
|
586
|
+
* request data must be passed in for POST/PUT requests. An optional config can be passed as the
|
|
587
|
+
* last argument.
|
|
588
|
+
*
|
|
589
|
+
* ```js
|
|
590
|
+
* $http.get('/someUrl', config).then(successCallback, errorCallback);
|
|
591
|
+
* $http.post('/someUrl', data, config).then(successCallback, errorCallback);
|
|
592
|
+
* ```
|
|
593
|
+
*
|
|
594
|
+
* Complete list of shortcut methods:
|
|
595
|
+
*
|
|
596
|
+
* - {@link ng.$http#get $http.get}
|
|
597
|
+
* - {@link ng.$http#head $http.head}
|
|
598
|
+
* - {@link ng.$http#post $http.post}
|
|
599
|
+
* - {@link ng.$http#put $http.put}
|
|
600
|
+
* - {@link ng.$http#delete $http.delete}
|
|
601
|
+
* - {@link ng.$http#patch $http.patch}
|
|
602
|
+
*
|
|
603
|
+
*
|
|
604
|
+
* ```
|
|
605
|
+
* $http.get(...);
|
|
606
|
+
* $httpBackend.flush();
|
|
607
|
+
* ```
|
|
608
|
+
*
|
|
609
|
+
* ## Setting HTTP Headers
|
|
610
|
+
*
|
|
611
|
+
* The $http service will automatically add certain HTTP headers to all requests. These defaults
|
|
612
|
+
* can be fully configured by accessing the `$httpProvider.defaults.headers` configuration
|
|
613
|
+
* object, which currently contains this default configuration:
|
|
614
|
+
*
|
|
615
|
+
* - `$httpProvider.defaults.headers.common` (headers that are common for all requests):
|
|
616
|
+
* - <code>Accept: application/json, text/plain, \*/\*</code>
|
|
617
|
+
* - `$httpProvider.defaults.headers.post`: (header defaults for POST requests)
|
|
618
|
+
* - `Content-Type: application/json`
|
|
619
|
+
* - `$httpProvider.defaults.headers.put` (header defaults for PUT requests)
|
|
620
|
+
* - `Content-Type: application/json`
|
|
621
|
+
*
|
|
622
|
+
* To add or overwrite these defaults, simply add or remove a property from these configuration
|
|
623
|
+
* objects. To add headers for an HTTP method other than POST or PUT, simply add a new object
|
|
624
|
+
* with the lowercased HTTP method name as the key, e.g.
|
|
625
|
+
* `$httpProvider.defaults.headers.get = { 'My-Header' : 'value' }`.
|
|
626
|
+
*
|
|
627
|
+
* The defaults can also be set at runtime via the `$http.defaults` object in the same
|
|
628
|
+
* fashion. For example:
|
|
629
|
+
*
|
|
630
|
+
* ```
|
|
631
|
+
* module.run(function($http) {
|
|
632
|
+
* $http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w';
|
|
633
|
+
* });
|
|
634
|
+
* ```
|
|
635
|
+
*
|
|
636
|
+
* In addition, you can supply a `headers` property in the config object passed when
|
|
637
|
+
* calling `$http(config)`, which overrides the defaults without changing them globally.
|
|
638
|
+
*
|
|
639
|
+
* To explicitly remove a header automatically added via $httpProvider.defaults.headers on a per request basis,
|
|
640
|
+
* Use the `headers` property, setting the desired header to `undefined`. For example:
|
|
641
|
+
*
|
|
642
|
+
* ```js
|
|
643
|
+
* let req = {
|
|
644
|
+
* method: 'POST',
|
|
645
|
+
* url: 'http://example.com',
|
|
646
|
+
* headers: {
|
|
647
|
+
* 'Content-Type': undefined
|
|
648
|
+
* },
|
|
649
|
+
* data: { test: 'test' }
|
|
650
|
+
* }
|
|
651
|
+
*
|
|
652
|
+
* $http(req).then(function(){...}, function(){...});
|
|
653
|
+
* ```
|
|
654
|
+
*
|
|
655
|
+
* ## Transforming Requests and Responses
|
|
656
|
+
*
|
|
657
|
+
* Both requests and responses can be transformed using transformation functions: `transformRequest`
|
|
658
|
+
* and `transformResponse`. These properties can be a single function that returns
|
|
659
|
+
* the transformed value (`function(data, headersGetter, status)`) or an array of such transformation functions,
|
|
660
|
+
* which allows you to `push` or `unshift` a new transformation function into the transformation chain.
|
|
661
|
+
*
|
|
662
|
+
* <div class="alert alert-warning">
|
|
663
|
+
* **Note:** AngularJS does not make a copy of the `data` parameter before it is passed into the `transformRequest` pipeline.
|
|
664
|
+
* That means changes to the properties of `data` are not local to the transform function (since Javascript passes objects by reference).
|
|
665
|
+
* For example, when calling `$http.get(url, $scope.myObject)`, modifications to the object's properties in a transformRequest
|
|
666
|
+
* function will be reflected on the scope and in any templates where the object is data-bound.
|
|
667
|
+
* To prevent this, transform functions should have no side-effects.
|
|
668
|
+
* If you need to modify properties, it is recommended to make a copy of the data, or create new object to return.
|
|
669
|
+
* </div>
|
|
670
|
+
*
|
|
671
|
+
* ### Default Transformations
|
|
672
|
+
*
|
|
673
|
+
* The `$httpProvider` provider and `$http` service expose `defaults.transformRequest` and
|
|
674
|
+
* `defaults.transformResponse` properties. If a request does not provide its own transformations
|
|
675
|
+
* then these will be applied.
|
|
676
|
+
*
|
|
677
|
+
* You can augment or replace the default transformations by modifying these properties by adding to or
|
|
678
|
+
* replacing the array.
|
|
679
|
+
*
|
|
680
|
+
* AngularJS provides the following default transformations:
|
|
681
|
+
*
|
|
682
|
+
* Request transformations (`$httpProvider.defaults.transformRequest` and `$http.defaults.transformRequest`) is
|
|
683
|
+
* an array with one function that does the following:
|
|
684
|
+
*
|
|
685
|
+
* - If the `data` property of the request configuration object contains an object, serialize it
|
|
686
|
+
* into JSON format.
|
|
687
|
+
*
|
|
688
|
+
* Response transformations (`$httpProvider.defaults.transformResponse` and `$http.defaults.transformResponse`) is
|
|
689
|
+
* an array with one function that does the following:
|
|
690
|
+
*
|
|
691
|
+
* - If XSRF prefix is detected, strip it (see Security Considerations section below).
|
|
692
|
+
* - If the `Content-Type` is `application/json` or the response looks like JSON,
|
|
693
|
+
* deserialize it using a JSON parser.
|
|
694
|
+
*
|
|
695
|
+
*
|
|
696
|
+
* ### Overriding the Default Transformations Per Request
|
|
697
|
+
*
|
|
698
|
+
* If you wish to override the request/response transformations only for a single request then provide
|
|
699
|
+
* `transformRequest` and/or `transformResponse` properties on the configuration object passed
|
|
700
|
+
* into `$http`.
|
|
701
|
+
*
|
|
702
|
+
* Note that if you provide these properties on the config object the default transformations will be
|
|
703
|
+
* overwritten. If you wish to augment the default transformations then you must include them in your
|
|
704
|
+
* local transformation array.
|
|
705
|
+
*
|
|
706
|
+
* The following code demonstrates adding a new response transformation to be run after the default response
|
|
707
|
+
* transformations have been run.
|
|
708
|
+
*
|
|
709
|
+
* ```js
|
|
710
|
+
* function appendTransform(defaults, transform) {
|
|
711
|
+
*
|
|
712
|
+
* // We can't guarantee that the default transformation is an array
|
|
713
|
+
* defaults = angular.isArray(defaults) ? defaults : [defaults];
|
|
714
|
+
*
|
|
715
|
+
* // Append the new transformation to the defaults
|
|
716
|
+
* return defaults.concat(transform);
|
|
717
|
+
* }
|
|
718
|
+
*
|
|
719
|
+
* $http({
|
|
720
|
+
* url: '...',
|
|
721
|
+
* method: 'GET',
|
|
722
|
+
* transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
|
|
723
|
+
* return doTransform(value);
|
|
724
|
+
* })
|
|
725
|
+
* });
|
|
726
|
+
* ```
|
|
727
|
+
*
|
|
728
|
+
*
|
|
729
|
+
* ## Caching
|
|
730
|
+
*
|
|
731
|
+
* {@link ng.$http `$http`} responses are not cached by default. To enable caching, you must
|
|
732
|
+
* set the config.cache value or the default cache value to TRUE or to a cache object (created
|
|
733
|
+
* with {@link ng.$cacheFactory `$cacheFactory`}). If defined, the value of config.cache takes
|
|
734
|
+
* precedence over the default cache value.
|
|
735
|
+
*
|
|
736
|
+
* In order to:
|
|
737
|
+
* * cache all responses - set the default cache value to TRUE or to a cache object
|
|
738
|
+
* * cache a specific response - set config.cache value to TRUE or to a cache object
|
|
739
|
+
*
|
|
740
|
+
* If caching is enabled, but neither the default cache nor config.cache are set to a cache object,
|
|
741
|
+
* then the default `$cacheFactory("$http")` object is used.
|
|
742
|
+
*
|
|
743
|
+
* The default cache value can be set by updating the
|
|
744
|
+
* {@link ng.$http#defaults `$http.defaults.cache`} property or the
|
|
745
|
+
* {@link $httpProvider#defaults `$httpProvider.defaults.cache`} property.
|
|
746
|
+
*
|
|
747
|
+
* When caching is enabled, {@link ng.$http `$http`} stores the response from the server using
|
|
748
|
+
* the relevant cache object. The next time the same request is made, the response is returned
|
|
749
|
+
* from the cache without sending a request to the server.
|
|
750
|
+
*
|
|
751
|
+
* Take note that:
|
|
752
|
+
*
|
|
753
|
+
* * Only GET and JSONP requests are cached.
|
|
754
|
+
* * The cache key is the request URL including search parameters; headers are not considered.
|
|
755
|
+
* * Cached responses are returned asynchronously, in the same way as responses from the server.
|
|
756
|
+
* * If multiple identical requests are made using the same cache, which is not yet populated,
|
|
757
|
+
* one request will be made to the server and remaining requests will return the same response.
|
|
758
|
+
* * A cache-control header on the response does not affect if or how responses are cached.
|
|
759
|
+
*
|
|
760
|
+
*
|
|
761
|
+
* ## Interceptors
|
|
762
|
+
*
|
|
763
|
+
* Before you start creating interceptors, be sure to understand the
|
|
764
|
+
* {@link ng.$q $q and deferred/promise APIs}.
|
|
765
|
+
*
|
|
766
|
+
* For purposes of global error handling, authentication, or any kind of synchronous or
|
|
767
|
+
* asynchronous pre-processing of request or postprocessing of responses, it is desirable to be
|
|
768
|
+
* able to intercept requests before they are handed to the server and
|
|
769
|
+
* responses before they are handed over to the application code that
|
|
770
|
+
* initiated these requests. The interceptors leverage the {@link ng.$q
|
|
771
|
+
* promise APIs} to fulfill this need for both synchronous and asynchronous pre-processing.
|
|
772
|
+
*
|
|
773
|
+
* The interceptors are service factories that are registered with the `$httpProvider` by
|
|
774
|
+
* adding them to the `$httpProvider.interceptors` array. The factory is called and
|
|
775
|
+
* injected with dependencies (if specified) and returns the interceptor.
|
|
776
|
+
*
|
|
777
|
+
* There are two kinds of interceptors (and two kinds of rejection interceptors):
|
|
778
|
+
*
|
|
779
|
+
* * `request`: interceptors get called with a http {@link $http#usage config} object. The function is free to
|
|
780
|
+
* modify the `config` object or create a new one. The function needs to return the `config`
|
|
781
|
+
* object directly, or a promise containing the `config` or a new `config` object.
|
|
782
|
+
* * `requestError`: interceptor gets called when a previous interceptor threw an error or
|
|
783
|
+
* resolved with a rejection.
|
|
784
|
+
* * `response`: interceptors get called with http `response` object. The function is free to
|
|
785
|
+
* modify the `response` object or create a new one. The function needs to return the `response`
|
|
786
|
+
* object directly, or as a promise containing the `response` or a new `response` object.
|
|
787
|
+
* * `responseError`: interceptor gets called when a previous interceptor threw an error or
|
|
788
|
+
* resolved with a rejection.
|
|
789
|
+
*
|
|
790
|
+
*
|
|
791
|
+
* ```js
|
|
792
|
+
* // register the interceptor as a service
|
|
793
|
+
* $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
|
|
794
|
+
* return {
|
|
795
|
+
* // optional method
|
|
796
|
+
* 'request': function(config) {
|
|
797
|
+
* // do something on success
|
|
798
|
+
* return config;
|
|
799
|
+
* },
|
|
800
|
+
*
|
|
801
|
+
* // optional method
|
|
802
|
+
* 'requestError': function(rejection) {
|
|
803
|
+
* // do something on error
|
|
804
|
+
* if (canRecover(rejection)) {
|
|
805
|
+
* return responseOrNewPromise
|
|
806
|
+
* }
|
|
807
|
+
* return $q.reject(rejection);
|
|
808
|
+
* },
|
|
809
|
+
*
|
|
810
|
+
*
|
|
811
|
+
*
|
|
812
|
+
* // optional method
|
|
813
|
+
* 'response': function(response) {
|
|
814
|
+
* // do something on success
|
|
815
|
+
* return response;
|
|
816
|
+
* },
|
|
817
|
+
*
|
|
818
|
+
* // optional method
|
|
819
|
+
* 'responseError': function(rejection) {
|
|
820
|
+
* // do something on error
|
|
821
|
+
* if (canRecover(rejection)) {
|
|
822
|
+
* return responseOrNewPromise
|
|
823
|
+
* }
|
|
824
|
+
* return $q.reject(rejection);
|
|
825
|
+
* }
|
|
826
|
+
* };
|
|
827
|
+
* });
|
|
828
|
+
*
|
|
829
|
+
* $httpProvider.interceptors.push('myHttpInterceptor');
|
|
830
|
+
*
|
|
831
|
+
*
|
|
832
|
+
* // alternatively, register the interceptor via an anonymous factory
|
|
833
|
+
* $httpProvider.interceptors.push(function($q, dependency1, dependency2) {
|
|
834
|
+
* return {
|
|
835
|
+
* 'request': function(config) {
|
|
836
|
+
* // same as above
|
|
837
|
+
* },
|
|
838
|
+
*
|
|
839
|
+
* 'response': function(response) {
|
|
840
|
+
* // same as above
|
|
841
|
+
* }
|
|
842
|
+
* };
|
|
843
|
+
* });
|
|
844
|
+
* ```
|
|
845
|
+
*
|
|
846
|
+
* ## Security Considerations
|
|
847
|
+
*
|
|
848
|
+
* When designing web applications, consider security threats from:
|
|
849
|
+
*
|
|
850
|
+
* - [JSON vulnerability](http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx)
|
|
851
|
+
* - [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery)
|
|
852
|
+
*
|
|
853
|
+
* Both server and the client must cooperate in order to eliminate these threats. AngularJS comes
|
|
854
|
+
* pre-configured with strategies that address these issues, but for this to work backend server
|
|
855
|
+
* cooperation is required.
|
|
856
|
+
*
|
|
857
|
+
* ### JSON Vulnerability Protection
|
|
858
|
+
*
|
|
859
|
+
* A [JSON vulnerability](http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx)
|
|
860
|
+
* allows third party website to turn your JSON resource URL into
|
|
861
|
+
* [JSONP](http://en.wikipedia.org/wiki/JSONP) request under some conditions. To
|
|
862
|
+
* counter this your server can prefix all JSON requests with following string `")]}',\n"`.
|
|
863
|
+
* AngularJS will automatically strip the prefix before processing it as JSON.
|
|
864
|
+
*
|
|
865
|
+
* For example if your server needs to return:
|
|
866
|
+
* ```js
|
|
867
|
+
* ['one','two']
|
|
868
|
+
* ```
|
|
869
|
+
*
|
|
870
|
+
* which is vulnerable to attack, your server can return:
|
|
871
|
+
* ```js
|
|
872
|
+
* )]}',
|
|
873
|
+
* ['one','two']
|
|
874
|
+
* ```
|
|
875
|
+
*
|
|
876
|
+
* AngularJS will strip the prefix, before processing the JSON.
|
|
877
|
+
*
|
|
878
|
+
*
|
|
879
|
+
* ### Cross Site Request Forgery (XSRF) Protection
|
|
880
|
+
*
|
|
881
|
+
* [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) is an attack technique by
|
|
882
|
+
* which the attacker can trick an authenticated user into unknowingly executing actions on your
|
|
883
|
+
* website. AngularJS provides a mechanism to counter XSRF. When performing XHR requests, the
|
|
884
|
+
* $http service reads a token from a cookie (by default, `XSRF-TOKEN`) and sets it as an HTTP
|
|
885
|
+
* header (by default `X-XSRF-TOKEN`). Since only JavaScript that runs on your domain could read
|
|
886
|
+
* the cookie, your server can be assured that the XHR came from JavaScript running on your
|
|
887
|
+
* domain.
|
|
888
|
+
*
|
|
889
|
+
* To take advantage of this, your server needs to set a token in a JavaScript readable session
|
|
890
|
+
* cookie called `XSRF-TOKEN` on the first HTTP GET request. On subsequent XHR requests the
|
|
891
|
+
* server can verify that the cookie matches the `X-XSRF-TOKEN` HTTP header, and therefore be
|
|
892
|
+
* sure that only JavaScript running on your domain could have sent the request. The token must
|
|
893
|
+
* be unique for each user and must be verifiable by the server (to prevent the JavaScript from
|
|
894
|
+
* making up its own tokens). We recommend that the token is a digest of your site's
|
|
895
|
+
* authentication cookie with a [salt](https://en.wikipedia.org/wiki/Salt_(cryptography))
|
|
896
|
+
* for added security.
|
|
897
|
+
*
|
|
898
|
+
* The header will — by default — **not** be set for cross-domain requests. This
|
|
899
|
+
* prevents unauthorized servers (e.g. malicious or compromised 3rd-party APIs) from gaining
|
|
900
|
+
* access to your users' XSRF tokens and exposing them to Cross Site Request Forgery. If you
|
|
901
|
+
* want to, you can trust additional origins to also receive the XSRF token, by adding them
|
|
902
|
+
* to {@link ng.$httpProvider#xsrfTrustedOrigins xsrfTrustedOrigins}. This might be
|
|
903
|
+
* useful, for example, if your application, served from `example.com`, needs to access your API
|
|
904
|
+
* at `api.example.com`.
|
|
905
|
+
* See {@link ng.$httpProvider#xsrfTrustedOrigins $httpProvider.xsrfTrustedOrigins} for
|
|
906
|
+
* more details.
|
|
907
|
+
*
|
|
908
|
+
* <div class="alert alert-danger">
|
|
909
|
+
* **Warning**<br />
|
|
910
|
+
* Only trusted origins that you have control over and make sure you understand the
|
|
911
|
+
* implications of doing so.
|
|
912
|
+
* </div>
|
|
913
|
+
*
|
|
914
|
+
* The name of the cookie and the header can be specified using the `xsrfCookieName` and
|
|
915
|
+
* `xsrfHeaderName` properties of either `$httpProvider.defaults` at config-time,
|
|
916
|
+
* `$http.defaults` at run-time, or the per-request config object.
|
|
917
|
+
*
|
|
918
|
+
* In order to prevent collisions in environments where multiple AngularJS apps share the
|
|
919
|
+
* same domain or subdomain, we recommend that each application uses a unique cookie name.
|
|
920
|
+
*
|
|
921
|
+
*
|
|
922
|
+
* @param {object} config Object describing the request to be made and how it should be
|
|
923
|
+
* processed. The object has following properties:
|
|
924
|
+
*
|
|
925
|
+
* - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc)
|
|
926
|
+
* - **url** – `{string|TrustedObject}` – Absolute or relative URL of the resource that is being requested;
|
|
927
|
+
* or an object created by a call to `$sce.trustAsResourceUrl(url)`.
|
|
928
|
+
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be serialized
|
|
929
|
+
* with the `paramSerializer` and appended as GET parameters.
|
|
930
|
+
* - **data** – `{string|Object}` – Data to be sent as the request message data.
|
|
931
|
+
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
|
|
932
|
+
* HTTP headers to send to the server. If the return value of a function is null, the
|
|
933
|
+
* header will not be sent. Functions accept a config object as an argument.
|
|
934
|
+
* - **eventHandlers** - `{Object}` - Event listeners to be bound to the XMLHttpRequest object.
|
|
935
|
+
* To bind events to the XMLHttpRequest upload object, use `uploadEventHandlers`.
|
|
936
|
+
* The handler will be called in the context of a `$apply` block.
|
|
937
|
+
* - **uploadEventHandlers** - `{Object}` - Event listeners to be bound to the XMLHttpRequest upload
|
|
938
|
+
* object. To bind events to the XMLHttpRequest object, use `eventHandlers`.
|
|
939
|
+
* The handler will be called in the context of a `$apply` block.
|
|
940
|
+
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
|
|
941
|
+
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
|
|
942
|
+
* - **transformRequest** –
|
|
943
|
+
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
|
944
|
+
* transform function or an array of such functions. The transform function takes the http
|
|
945
|
+
* request body and headers and returns its transformed (typically serialized) version.
|
|
946
|
+
* See {@link ng.$http#overriding-the-default-transformations-per-request
|
|
947
|
+
* Overriding the Default Transformations}
|
|
948
|
+
* - **transformResponse** –
|
|
949
|
+
* `{function(data, headersGetter, status)|Array.<function(data, headersGetter, status)>}` –
|
|
950
|
+
* transform function or an array of such functions. The transform function takes the http
|
|
951
|
+
* response body, headers and status and returns its transformed (typically deserialized) version.
|
|
952
|
+
* See {@link ng.$http#overriding-the-default-transformations-per-request
|
|
953
|
+
* Overriding the Default Transformations}
|
|
954
|
+
* - **paramSerializer** - `{string|function(Object<string,string>):string}` - A function used to
|
|
955
|
+
* prepare the string representation of request parameters (specified as an object).
|
|
956
|
+
* If specified as string, it is interpreted as function registered with the
|
|
957
|
+
* {@link $injector $injector}, which means you can create your own serializer
|
|
958
|
+
* by registering it as a {@link auto.$provide#service service}.
|
|
959
|
+
* The default serializer is the {@link $httpParamSerializer $httpParamSerializer};
|
|
960
|
+
* alternatively, you can use the {@link $httpParamSerializerJQLike $httpParamSerializerJQLike}
|
|
961
|
+
* - **cache** – `{boolean|Object}` – A boolean value or object created with
|
|
962
|
+
* {@link ng.$cacheFactory `$cacheFactory`} to enable or disable caching of the HTTP response.
|
|
963
|
+
* See {@link $http#caching $http Caching} for more information.
|
|
964
|
+
* - **timeout** – `{number|Promise}` – timeout in milliseconds, or {@link ng.$q promise}
|
|
965
|
+
* that should abort the request when resolved.
|
|
966
|
+
*
|
|
967
|
+
* A numerical timeout or a promise returned from {@link ng.$timeout $timeout}, will set
|
|
968
|
+
* the `xhrStatus` in the {@link $http#$http-returns response} to "timeout", and any other
|
|
969
|
+
* resolved promise will set it to "abort", following standard XMLHttpRequest behavior.
|
|
970
|
+
*
|
|
971
|
+
* - **withCredentials** - `{boolean}` - whether to set the `withCredentials` flag on the
|
|
972
|
+
* XHR object. See [requests with credentials](https://developer.mozilla.org/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials)
|
|
973
|
+
* for more information.
|
|
974
|
+
* - **responseType** - `{string}` - see
|
|
975
|
+
* [XMLHttpRequest.responseType](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-responsetype).
|
|
976
|
+
*
|
|
977
|
+
* @returns {HttpPromise} A {@link ng.$q `Promise}` that will be resolved (request success)
|
|
978
|
+
* or rejected (request failure) with a response object.
|
|
979
|
+
*
|
|
980
|
+
* The response object has these properties:
|
|
981
|
+
*
|
|
982
|
+
* - **data** – `{string|Object}` – The response body transformed with
|
|
983
|
+
* the transform functions.
|
|
984
|
+
* - **status** – `{number}` – HTTP status code of the response.
|
|
985
|
+
* - **headers** – `{function([headerName])}` – Header getter function.
|
|
986
|
+
* - **config** – `{Object}` – The configuration object that was used
|
|
987
|
+
* to generate the request.
|
|
988
|
+
* - **statusText** – `{string}` – HTTP status text of the response.
|
|
989
|
+
* - **xhrStatus** – `{string}` – Status of the XMLHttpRequest
|
|
990
|
+
* (`complete`, `error`, `timeout` or `abort`).
|
|
991
|
+
*
|
|
992
|
+
*
|
|
993
|
+
* A response status code between 200 and 299 is considered a success status
|
|
994
|
+
* and will result in the success callback being called. Any response status
|
|
995
|
+
* code outside of that range is considered an error status and will result
|
|
996
|
+
* in the error callback being called.
|
|
997
|
+
* Also, status codes less than -1 are normalized to zero. -1 usually means
|
|
998
|
+
* the request was aborted, e.g. using a `config.timeout`. More information
|
|
999
|
+
* about the status might be available in the `xhrStatus` property.
|
|
1000
|
+
*
|
|
1001
|
+
* Note that if the response is a redirect, XMLHttpRequest will transparently
|
|
1002
|
+
* follow it, meaning that the outcome (success or error) will be determined
|
|
1003
|
+
* by the final response status code.
|
|
1004
|
+
*
|
|
1005
|
+
*
|
|
1006
|
+
* @property {Array.<Object>} pendingRequests Array of config objects for currently pending
|
|
1007
|
+
* requests. This is primarily meant to be used for debugging purposes.
|
|
1008
|
+
*
|
|
1009
|
+
*/
|
|
1010
|
+
function $http(requestConfig) {
|
|
1011
|
+
if (!isObject(requestConfig)) {
|
|
1012
|
+
throw minErr("$http")(
|
|
1013
|
+
"badreq",
|
|
1014
|
+
"Http request configuration must be an object. Received: {0}",
|
|
1015
|
+
requestConfig,
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
if (!isString($sce.valueOf(requestConfig.url))) {
|
|
1020
|
+
throw minErr("$http")(
|
|
1021
|
+
"badreq",
|
|
1022
|
+
"Http request configuration url must be a string or a $sce trusted object. Received: {0}",
|
|
1023
|
+
requestConfig.url,
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
const config = extend(
|
|
1028
|
+
{
|
|
1029
|
+
method: "get",
|
|
1030
|
+
transformRequest: defaults.transformRequest,
|
|
1031
|
+
transformResponse: defaults.transformResponse,
|
|
1032
|
+
paramSerializer: defaults.paramSerializer,
|
|
1033
|
+
},
|
|
1034
|
+
requestConfig,
|
|
1035
|
+
);
|
|
1036
|
+
|
|
1037
|
+
config.headers = mergeHeaders(requestConfig);
|
|
1038
|
+
config.method = uppercase(config.method);
|
|
1039
|
+
config.paramSerializer = isString(config.paramSerializer)
|
|
1040
|
+
? $injector.get(config.paramSerializer)
|
|
1041
|
+
: config.paramSerializer;
|
|
1042
|
+
|
|
1043
|
+
$browser.$$incOutstandingRequestCount("$http");
|
|
1044
|
+
|
|
1045
|
+
const requestInterceptors = [];
|
|
1046
|
+
const responseInterceptors = [];
|
|
1047
|
+
let promise = $q.resolve(config);
|
|
1048
|
+
|
|
1049
|
+
// apply interceptors
|
|
1050
|
+
forEach(reversedInterceptors, (interceptor) => {
|
|
1051
|
+
if (interceptor.request || interceptor.requestError) {
|
|
1052
|
+
requestInterceptors.unshift(
|
|
1053
|
+
interceptor.request,
|
|
1054
|
+
interceptor.requestError,
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
if (interceptor.response || interceptor.responseError) {
|
|
1058
|
+
responseInterceptors.push(
|
|
1059
|
+
interceptor.response,
|
|
1060
|
+
interceptor.responseError,
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
promise = chainInterceptors(promise, requestInterceptors);
|
|
1066
|
+
promise = promise.then(serverRequest);
|
|
1067
|
+
promise = chainInterceptors(promise, responseInterceptors);
|
|
1068
|
+
promise = promise.finally(completeOutstandingRequest);
|
|
1069
|
+
|
|
1070
|
+
return promise;
|
|
1071
|
+
|
|
1072
|
+
function chainInterceptors(promise, interceptors) {
|
|
1073
|
+
for (let i = 0, ii = interceptors.length; i < ii; ) {
|
|
1074
|
+
const thenFn = interceptors[i++];
|
|
1075
|
+
const rejectFn = interceptors[i++];
|
|
1076
|
+
|
|
1077
|
+
promise = promise.then(thenFn, rejectFn);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
interceptors.length = 0;
|
|
1081
|
+
|
|
1082
|
+
return promise;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
function completeOutstandingRequest() {
|
|
1086
|
+
$browser.$$completeOutstandingRequest(() => {}, "$http");
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
function executeHeaderFns(headers, config) {
|
|
1090
|
+
let headerContent;
|
|
1091
|
+
const processedHeaders = {};
|
|
1092
|
+
|
|
1093
|
+
forEach(headers, (headerFn, header) => {
|
|
1094
|
+
if (isFunction(headerFn)) {
|
|
1095
|
+
headerContent = headerFn(config);
|
|
1096
|
+
if (headerContent != null) {
|
|
1097
|
+
processedHeaders[header] = headerContent;
|
|
1098
|
+
}
|
|
1099
|
+
} else {
|
|
1100
|
+
processedHeaders[header] = headerFn;
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
return processedHeaders;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function mergeHeaders(config) {
|
|
1108
|
+
let defHeaders = defaults.headers,
|
|
1109
|
+
reqHeaders = extend({}, config.headers);
|
|
1110
|
+
|
|
1111
|
+
defHeaders = extend(
|
|
1112
|
+
{},
|
|
1113
|
+
defHeaders.common,
|
|
1114
|
+
defHeaders[lowercase(config.method)],
|
|
1115
|
+
);
|
|
1116
|
+
|
|
1117
|
+
Object.keys(defHeaders).forEach((defHeaderName) => {
|
|
1118
|
+
if (!reqHeaders[lowercase(defHeaderName)]) {
|
|
1119
|
+
reqHeaders[defHeaderName] = defHeaders[defHeaderName];
|
|
1120
|
+
}
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
// execute if header value is a function for merged headers
|
|
1124
|
+
return executeHeaderFns(reqHeaders, shallowCopy(config));
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
function serverRequest(config) {
|
|
1128
|
+
const { headers } = config;
|
|
1129
|
+
const reqData = transformData(
|
|
1130
|
+
config.data,
|
|
1131
|
+
headersGetter(headers),
|
|
1132
|
+
undefined,
|
|
1133
|
+
config.transformRequest,
|
|
1134
|
+
);
|
|
1135
|
+
|
|
1136
|
+
// strip content-type if data is undefined
|
|
1137
|
+
if (isUndefined(reqData)) {
|
|
1138
|
+
forEach(headers, (value, header) => {
|
|
1139
|
+
if (lowercase(header) === "content-type") {
|
|
1140
|
+
delete headers[header];
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
if (
|
|
1146
|
+
isUndefined(config.withCredentials) &&
|
|
1147
|
+
!isUndefined(defaults.withCredentials)
|
|
1148
|
+
) {
|
|
1149
|
+
config.withCredentials = defaults.withCredentials;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
// send request
|
|
1153
|
+
return sendReq(config, reqData).then(
|
|
1154
|
+
transformResponse,
|
|
1155
|
+
transformResponse,
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
function transformResponse(response) {
|
|
1160
|
+
// make a copy since the response must be cacheable
|
|
1161
|
+
const resp = extend({}, response);
|
|
1162
|
+
resp.data = transformData(
|
|
1163
|
+
response.data,
|
|
1164
|
+
response.headers,
|
|
1165
|
+
response.status,
|
|
1166
|
+
config.transformResponse,
|
|
1167
|
+
);
|
|
1168
|
+
return isSuccess(response.status) ? resp : $q.reject(resp);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
$http.pendingRequests = [];
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* @ngdoc method
|
|
1176
|
+
* @name $http#get
|
|
1177
|
+
*
|
|
1178
|
+
* @description
|
|
1179
|
+
* Shortcut method to perform `GET` request.
|
|
1180
|
+
*
|
|
1181
|
+
* @param {string|TrustedObject} url Absolute or relative URL of the resource that is being requested;
|
|
1182
|
+
* or an object created by a call to `$sce.trustAsResourceUrl(url)`.
|
|
1183
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1184
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1185
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1186
|
+
*/
|
|
1187
|
+
|
|
1188
|
+
/**
|
|
1189
|
+
* @ngdoc method
|
|
1190
|
+
* @name $http#delete
|
|
1191
|
+
*
|
|
1192
|
+
* @description
|
|
1193
|
+
* Shortcut method to perform `DELETE` request.
|
|
1194
|
+
*
|
|
1195
|
+
* @param {string|TrustedObject} url Absolute or relative URL of the resource that is being requested;
|
|
1196
|
+
* or an object created by a call to `$sce.trustAsResourceUrl(url)`.
|
|
1197
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1198
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1199
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1200
|
+
*/
|
|
1201
|
+
|
|
1202
|
+
/**
|
|
1203
|
+
* @ngdoc method
|
|
1204
|
+
* @name $http#head
|
|
1205
|
+
*
|
|
1206
|
+
* @description
|
|
1207
|
+
* Shortcut method to perform `HEAD` request.
|
|
1208
|
+
*
|
|
1209
|
+
* @param {string|TrustedObject} url Absolute or relative URL of the resource that is being requested;
|
|
1210
|
+
* or an object created by a call to `$sce.trustAsResourceUrl(url)`.
|
|
1211
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1212
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1213
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1214
|
+
*/
|
|
1215
|
+
|
|
1216
|
+
/**
|
|
1217
|
+
* @ngdoc method
|
|
1218
|
+
* @name $http#jsonp
|
|
1219
|
+
*
|
|
1220
|
+
* @description
|
|
1221
|
+
* Shortcut method to perform `JSONP` request.
|
|
1222
|
+
*
|
|
1223
|
+
* Note that, since JSONP requests are sensitive because the response is given full access to the browser,
|
|
1224
|
+
* the url must be declared, via {@link $sce} as a trusted resource URL.
|
|
1225
|
+
* You can trust a URL by adding it to the trusted resource URL list via
|
|
1226
|
+
* {@link $sceDelegateProvider#trustedResourceUrlList `$sceDelegateProvider.trustedResourceUrlList`} or
|
|
1227
|
+
* by explicitly trusting the URL via {@link $sce#trustAsResourceUrl `$sce.trustAsResourceUrl(url)`}.
|
|
1228
|
+
*
|
|
1229
|
+
* You should avoid generating the URL for the JSONP request from user provided data.
|
|
1230
|
+
* Provide additional query parameters via `params` property of the `config` parameter, rather than
|
|
1231
|
+
* modifying the URL itself.
|
|
1232
|
+
*
|
|
1233
|
+
* You can also specify a default callback parameter name in `$http.defaults.jsonpCallbackParam`.
|
|
1234
|
+
* Initially this is set to `'callback'`.
|
|
1235
|
+
*
|
|
1236
|
+
* <div class="alert alert-danger">
|
|
1237
|
+
* You can no longer use the `JSON_CALLBACK` string as a placeholder for specifying where the callback
|
|
1238
|
+
* parameter value should go.
|
|
1239
|
+
* </div>
|
|
1240
|
+
*
|
|
1241
|
+
* If you would like to customise where and how the callbacks are stored then try overriding
|
|
1242
|
+
* or decorating the {@link $jsonpCallbacks} service.
|
|
1243
|
+
*
|
|
1244
|
+
* @param {string|TrustedObject} url Absolute or relative URL of the resource that is being requested;
|
|
1245
|
+
* or an object created by a call to `$sce.trustAsResourceUrl(url)`.
|
|
1246
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1247
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1248
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1249
|
+
*/
|
|
1250
|
+
createShortMethods("get", "delete", "head");
|
|
1251
|
+
|
|
1252
|
+
/**
|
|
1253
|
+
* @ngdoc method
|
|
1254
|
+
* @name $http#post
|
|
1255
|
+
*
|
|
1256
|
+
* @description
|
|
1257
|
+
* Shortcut method to perform `POST` request.
|
|
1258
|
+
*
|
|
1259
|
+
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
1260
|
+
* @param {*} data Request content
|
|
1261
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1262
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1263
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1264
|
+
*/
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* @ngdoc method
|
|
1268
|
+
* @name $http#put
|
|
1269
|
+
*
|
|
1270
|
+
* @description
|
|
1271
|
+
* Shortcut method to perform `PUT` request.
|
|
1272
|
+
*
|
|
1273
|
+
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
1274
|
+
* @param {*} data Request content
|
|
1275
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1276
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1277
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1278
|
+
*/
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
* @ngdoc method
|
|
1282
|
+
* @name $http#patch
|
|
1283
|
+
*
|
|
1284
|
+
* @description
|
|
1285
|
+
* Shortcut method to perform `PATCH` request.
|
|
1286
|
+
*
|
|
1287
|
+
* @param {string} url Relative or absolute URL specifying the destination of the request
|
|
1288
|
+
* @param {*} data Request content
|
|
1289
|
+
* @param {Object=} config Optional configuration object. See {@link ng.$http#$http-arguments `$http()` arguments}.
|
|
1290
|
+
* @returns {HttpPromise} A Promise that will be resolved or rejected with a response object.
|
|
1291
|
+
* See {@link ng.$http#$http-returns `$http()` return value}.
|
|
1292
|
+
*/
|
|
1293
|
+
createShortMethodsWithData("post", "put", "patch");
|
|
1294
|
+
|
|
1295
|
+
/**
|
|
1296
|
+
* @ngdoc property
|
|
1297
|
+
* @name $http#defaults
|
|
1298
|
+
*
|
|
1299
|
+
* @description
|
|
1300
|
+
* Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of
|
|
1301
|
+
* default headers, withCredentials as well as request and response transformations.
|
|
1302
|
+
*
|
|
1303
|
+
* See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above.
|
|
1304
|
+
*/
|
|
1305
|
+
$http.defaults = defaults;
|
|
1306
|
+
|
|
1307
|
+
return $http;
|
|
1308
|
+
|
|
1309
|
+
function createShortMethods(...names) {
|
|
1310
|
+
names.forEach((name) => {
|
|
1311
|
+
$http[name] = function (url, config) {
|
|
1312
|
+
return $http(
|
|
1313
|
+
extend({}, config || {}, {
|
|
1314
|
+
method: name,
|
|
1315
|
+
url,
|
|
1316
|
+
}),
|
|
1317
|
+
);
|
|
1318
|
+
};
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
function createShortMethodsWithData(...names) {
|
|
1323
|
+
names.forEach((name) => {
|
|
1324
|
+
$http[name] = function (url, data, config) {
|
|
1325
|
+
return $http(
|
|
1326
|
+
extend({}, config || {}, {
|
|
1327
|
+
method: name,
|
|
1328
|
+
url,
|
|
1329
|
+
data,
|
|
1330
|
+
}),
|
|
1331
|
+
);
|
|
1332
|
+
};
|
|
1333
|
+
});
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/**
|
|
1337
|
+
* Makes the request.
|
|
1338
|
+
*
|
|
1339
|
+
* !!! ACCESSES CLOSURE VARS:
|
|
1340
|
+
* $httpBackend, defaults, $log, $rootScope, defaultCache, $http.pendingRequests
|
|
1341
|
+
*/
|
|
1342
|
+
function sendReq(config, reqData) {
|
|
1343
|
+
const deferred = $q.defer();
|
|
1344
|
+
const { promise } = deferred;
|
|
1345
|
+
let cache;
|
|
1346
|
+
let cachedResp;
|
|
1347
|
+
const reqHeaders = config.headers;
|
|
1348
|
+
let { url } = config;
|
|
1349
|
+
|
|
1350
|
+
if (!isString(url)) {
|
|
1351
|
+
// If it is not a string then the URL must be a $sce trusted object
|
|
1352
|
+
url = $sce.valueOf(url);
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
url = buildUrl(url, config.paramSerializer(config.params));
|
|
1356
|
+
|
|
1357
|
+
$http.pendingRequests.push(config);
|
|
1358
|
+
promise.then(removePendingReq, removePendingReq);
|
|
1359
|
+
|
|
1360
|
+
if (
|
|
1361
|
+
(config.cache || defaults.cache) &&
|
|
1362
|
+
config.cache !== false &&
|
|
1363
|
+
config.method === "GET"
|
|
1364
|
+
) {
|
|
1365
|
+
cache = isObject(config.cache)
|
|
1366
|
+
? config.cache
|
|
1367
|
+
: isObject(/** @type {?} */ (defaults).cache)
|
|
1368
|
+
? /** @type {?} */ (defaults).cache
|
|
1369
|
+
: defaultCache;
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
if (cache) {
|
|
1373
|
+
cachedResp = cache.get(url);
|
|
1374
|
+
if (isDefined(cachedResp)) {
|
|
1375
|
+
if (isPromiseLike(cachedResp)) {
|
|
1376
|
+
// cached request has already been sent, but there is no response yet
|
|
1377
|
+
cachedResp.then(
|
|
1378
|
+
resolvePromiseWithResult,
|
|
1379
|
+
resolvePromiseWithResult,
|
|
1380
|
+
);
|
|
1381
|
+
} else {
|
|
1382
|
+
// serving from cache
|
|
1383
|
+
if (isArray(cachedResp)) {
|
|
1384
|
+
resolvePromise(
|
|
1385
|
+
cachedResp[1],
|
|
1386
|
+
cachedResp[0],
|
|
1387
|
+
shallowCopy(cachedResp[2]),
|
|
1388
|
+
cachedResp[3],
|
|
1389
|
+
cachedResp[4],
|
|
1390
|
+
);
|
|
1391
|
+
} else {
|
|
1392
|
+
resolvePromise(cachedResp, 200, {}, "OK", "complete");
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
} else {
|
|
1396
|
+
// put the promise for the non-transformed response into cache as a placeholder
|
|
1397
|
+
cache.put(url, promise);
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
// if we won't have the response in cache, set the xsrf headers and
|
|
1402
|
+
// send the request to the backend
|
|
1403
|
+
if (isUndefined(cachedResp)) {
|
|
1404
|
+
const xsrfValue = urlIsAllowedOrigin(config.url)
|
|
1405
|
+
? $$cookieReader()[config.xsrfCookieName || defaults.xsrfCookieName]
|
|
1406
|
+
: undefined;
|
|
1407
|
+
if (xsrfValue) {
|
|
1408
|
+
reqHeaders[config.xsrfHeaderName || defaults.xsrfHeaderName] =
|
|
1409
|
+
xsrfValue;
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
$httpBackend(
|
|
1413
|
+
config.method,
|
|
1414
|
+
url,
|
|
1415
|
+
reqData,
|
|
1416
|
+
done,
|
|
1417
|
+
reqHeaders,
|
|
1418
|
+
config.timeout,
|
|
1419
|
+
config.withCredentials,
|
|
1420
|
+
config.responseType,
|
|
1421
|
+
createApplyHandlers(config.eventHandlers),
|
|
1422
|
+
createApplyHandlers(config.uploadEventHandlers),
|
|
1423
|
+
);
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
return promise;
|
|
1427
|
+
|
|
1428
|
+
function createApplyHandlers(eventHandlers) {
|
|
1429
|
+
if (eventHandlers) {
|
|
1430
|
+
const applyHandlers = {};
|
|
1431
|
+
forEach(eventHandlers, (eventHandler, key) => {
|
|
1432
|
+
applyHandlers[key] = function (event) {
|
|
1433
|
+
if (useApplyAsync) {
|
|
1434
|
+
$rootScope.$applyAsync(callEventHandler);
|
|
1435
|
+
} else if ($rootScope.$$phase) {
|
|
1436
|
+
callEventHandler();
|
|
1437
|
+
} else {
|
|
1438
|
+
$rootScope.$apply(callEventHandler);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
function callEventHandler() {
|
|
1442
|
+
eventHandler(event);
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
});
|
|
1446
|
+
return applyHandlers;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
/**
|
|
1451
|
+
* Callback registered to $httpBackend():
|
|
1452
|
+
* - caches the response if desired
|
|
1453
|
+
* - resolves the raw $http promise
|
|
1454
|
+
* - calls $apply
|
|
1455
|
+
*/
|
|
1456
|
+
function done(status, response, headersString, statusText, xhrStatus) {
|
|
1457
|
+
if (cache) {
|
|
1458
|
+
if (isSuccess(status)) {
|
|
1459
|
+
cache.put(url, [
|
|
1460
|
+
status,
|
|
1461
|
+
response,
|
|
1462
|
+
parseHeaders(headersString),
|
|
1463
|
+
statusText,
|
|
1464
|
+
xhrStatus,
|
|
1465
|
+
]);
|
|
1466
|
+
} else {
|
|
1467
|
+
// remove promise from the cache
|
|
1468
|
+
cache.remove(url);
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
function resolveHttpPromise() {
|
|
1473
|
+
resolvePromise(
|
|
1474
|
+
response,
|
|
1475
|
+
status,
|
|
1476
|
+
headersString,
|
|
1477
|
+
statusText,
|
|
1478
|
+
xhrStatus,
|
|
1479
|
+
);
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
if (useApplyAsync) {
|
|
1483
|
+
$rootScope.$applyAsync(resolveHttpPromise);
|
|
1484
|
+
} else {
|
|
1485
|
+
resolveHttpPromise();
|
|
1486
|
+
if (!$rootScope.$$phase) $rootScope.$apply();
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
/**
|
|
1491
|
+
* Resolves the raw $http promise.
|
|
1492
|
+
*/
|
|
1493
|
+
function resolvePromise(
|
|
1494
|
+
response,
|
|
1495
|
+
status,
|
|
1496
|
+
headers,
|
|
1497
|
+
statusText,
|
|
1498
|
+
xhrStatus,
|
|
1499
|
+
) {
|
|
1500
|
+
// status: HTTP response status code, 0, -1 (aborted by timeout / promise)
|
|
1501
|
+
status = status >= -1 ? status : 0;
|
|
1502
|
+
|
|
1503
|
+
(isSuccess(status) ? deferred.resolve : deferred.reject)({
|
|
1504
|
+
data: response,
|
|
1505
|
+
status,
|
|
1506
|
+
headers: headersGetter(headers),
|
|
1507
|
+
config,
|
|
1508
|
+
statusText,
|
|
1509
|
+
xhrStatus,
|
|
1510
|
+
});
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
function resolvePromiseWithResult(result) {
|
|
1514
|
+
resolvePromise(
|
|
1515
|
+
result.data,
|
|
1516
|
+
result.status,
|
|
1517
|
+
shallowCopy(result.headers()),
|
|
1518
|
+
result.statusText,
|
|
1519
|
+
result.xhrStatus,
|
|
1520
|
+
);
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
function removePendingReq() {
|
|
1524
|
+
const idx = $http.pendingRequests.indexOf(config);
|
|
1525
|
+
if (idx !== -1) $http.pendingRequests.splice(idx, 1);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
function buildUrl(url, serializedParams) {
|
|
1530
|
+
if (serializedParams.length > 0) {
|
|
1531
|
+
url += (url.indexOf("?") === -1 ? "?" : "&") + serializedParams;
|
|
1532
|
+
}
|
|
1533
|
+
return url;
|
|
1534
|
+
}
|
|
1535
|
+
},
|
|
1536
|
+
];
|
|
1537
|
+
}
|