Skip to content

Commit d2e32f0

Browse files
committed
Allow proxy config for message
1 parent c230392 commit d2e32f0

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

src/components/Message.vue

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
</template>
1616

1717
<script>
18+
import fetchOptions from "@/mixins/fetchOptions.js";
19+
1820
export default {
1921
name: "Message",
22+
mixins: [fetchOptions],
2023
props: {
2124
item: Object,
25+
proxy: Object,
2226
},
2327
data: function () {
2428
return {
@@ -67,14 +71,15 @@ export default {
6771
},
6872
6973
downloadMessage: function (url) {
70-
return fetch(url, { headers: { Accept: "application/json" } }).then(
71-
function (response) {
72-
if (response.status != 200) {
73-
return;
74-
}
75-
return response.json();
76-
},
77-
);
74+
const defaultHeaders = this.item?.headers || { Accept: "application/json" };
75+
const options = this.buildFetchOptions(defaultHeaders);
76+
77+
return fetch(url, options).then((response) => {
78+
if (response.status !== 200) {
79+
return;
80+
}
81+
return response.json();
82+
});
7883
},
7984
8085
mapRemoteMessage: function (message) {

src/mixins/fetchOptions.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// mixins/proxy.js
2+
export default {
3+
props: {
4+
proxy: Object,
5+
},
6+
methods: {
7+
buildFetchOptions(init = {}, options = {}) {
8+
if (this.proxy?.useCredentials) {
9+
options.credentials = "include";
10+
}
11+
12+
if (this.proxy?.headers && !!this.proxy.headers) {
13+
options.headers = this.proxy.headers;
14+
}
15+
16+
// Each item can override the credential settings
17+
if (this.item.useCredentials !== undefined) {
18+
options.credentials =
19+
this.item.useCredentials === true ? "include" : "omit";
20+
}
21+
22+
// Each item can have their own headers
23+
if (this.item?.headers && !!this.item.headers) {
24+
options.headers = this.item.headers;
25+
}
26+
27+
return Object.assign(options, init);
28+
},
29+
},
30+
};

src/mixins/service.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import fetchOptions from "@/mixins/fetchOptions.js";
2+
13
export default {
4+
mixins: [fetchOptions],
25
props: {
36
proxy: Object,
47
},
@@ -13,29 +16,6 @@ export default {
1316
},
1417
methods: {
1518
fetch: function (path, init, json = true) {
16-
let options = {};
17-
18-
if (this.proxy?.useCredentials) {
19-
options.credentials = "include";
20-
}
21-
22-
if (this.proxy?.headers && !!this.proxy.headers) {
23-
options.headers = this.proxy.headers;
24-
}
25-
26-
// Each item can override the credential settings
27-
if (this.item.useCredentials !== undefined) {
28-
options.credentials =
29-
this.item.useCredentials === true ? "include" : "omit";
30-
}
31-
32-
// Each item can have their own headers
33-
if (this.item.headers !== undefined && !!this.item.headers) {
34-
options.headers = this.item.headers;
35-
}
36-
37-
options = Object.assign(options, init);
38-
3919
if (path.startsWith("/")) {
4020
path = path.slice(1);
4121
}
@@ -46,6 +26,8 @@ export default {
4626
url = `${this.endpoint}/${path}`;
4727
}
4828

29+
const options = this.buildFetchOptions(init);
30+
4931
return fetch(url, options).then((response) => {
5032
let success = response.ok;
5133
if (Array.isArray(this.item.successCodes)) {

0 commit comments

Comments
 (0)