Jonathan Druart
1139972748
Test plan: = Koha = Apply the first patch, prove xt/vue_tidy.t and notice the failure Apply this patch, now it passes = QA test = Apply the change to the merge request linked with this qa-test-tools' issue: https://gitlab.com/koha-community/qa-test-tools/-/issues/62 inside your ktd container (at /kohadevbox/qa-test-tools/) Edit a .js within koha-tmpl/intranet-tmpl/prog/js/vue and a .ts file (cypress test) with something not pretty Commit and run the QA script => It's failing! Pretty the change, commit again, run the QA script => It's happy! = KTD - git hook = Go to the merge request linked with this ktd's issue: https://gitlab.com/koha-community/koha-testing-docker/-/issues/374 Copy the modified git hook to .git/hooks/ktd/pre-commit Edit a .js within koha-tmpl/intranet-tmpl/prog/js/vue and a .ts file (cypress test) with something not pretty Commit => Notice that the commit content is pretty! Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
158 lines
4.1 KiB
JavaScript
158 lines
4.1 KiB
JavaScript
import { setError, submitting, submitted } from "../messages";
|
|
|
|
class HttpClient {
|
|
constructor(options = {}) {
|
|
this._baseURL = options.baseURL || "";
|
|
this._headers = options.headers || {
|
|
"Content-Type": "application/json;charset=utf-8",
|
|
};
|
|
}
|
|
|
|
async _fetchJSON(
|
|
endpoint,
|
|
headers = {},
|
|
options = {},
|
|
return_response = false,
|
|
mark_submitting = false
|
|
) {
|
|
let res, error;
|
|
if (mark_submitting) submitting();
|
|
await fetch(this._baseURL + endpoint, {
|
|
...options,
|
|
headers: { ...this._headers, ...headers },
|
|
})
|
|
.then(response => this.checkError(response, return_response))
|
|
.then(
|
|
result => {
|
|
res = result;
|
|
},
|
|
err => {
|
|
error = err;
|
|
setError(err.toString());
|
|
}
|
|
)
|
|
.catch(err => {
|
|
error = err;
|
|
setError(err);
|
|
})
|
|
.then(() => {
|
|
if (mark_submitting) submitted();
|
|
});
|
|
|
|
if (error) throw Error(error);
|
|
|
|
return res;
|
|
}
|
|
|
|
get(params = {}) {
|
|
return this._fetchJSON(params.endpoint, params.headers, {
|
|
...params.options,
|
|
method: "GET",
|
|
});
|
|
}
|
|
|
|
getAll(params = {}) {
|
|
let url =
|
|
params.endpoint +
|
|
"?" +
|
|
new URLSearchParams({
|
|
_per_page: -1,
|
|
...(params.query && { q: JSON.stringify(params.query) }),
|
|
});
|
|
return this._fetchJSON(url, params.headers, {
|
|
...params.options,
|
|
method: "GET",
|
|
});
|
|
}
|
|
|
|
post(params = {}) {
|
|
const body = params.body
|
|
? typeof params.body === "string"
|
|
? params.body
|
|
: JSON.stringify(params.body)
|
|
: undefined;
|
|
return this._fetchJSON(
|
|
params.endpoint,
|
|
params.headers,
|
|
{
|
|
...params.options,
|
|
body,
|
|
method: "POST",
|
|
},
|
|
false,
|
|
true
|
|
);
|
|
}
|
|
|
|
put(params = {}) {
|
|
const body = params.body
|
|
? typeof params.body === "string"
|
|
? params.body
|
|
: JSON.stringify(params.body)
|
|
: undefined;
|
|
return this._fetchJSON(
|
|
params.endpoint,
|
|
params.headers,
|
|
{
|
|
...params.options,
|
|
body,
|
|
method: "PUT",
|
|
},
|
|
false,
|
|
true
|
|
);
|
|
}
|
|
|
|
delete(params = {}) {
|
|
return this._fetchJSON(
|
|
params.endpoint,
|
|
params.headers,
|
|
{
|
|
parseResponse: false,
|
|
...params.options,
|
|
method: "DELETE",
|
|
},
|
|
true,
|
|
true
|
|
);
|
|
}
|
|
|
|
count(params = {}) {
|
|
let res;
|
|
return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
|
|
response => {
|
|
if (response) {
|
|
return response.headers.get("X-Total-Count");
|
|
}
|
|
},
|
|
error => {
|
|
setError(error.toString());
|
|
}
|
|
);
|
|
}
|
|
|
|
patch(params = {}) {
|
|
const body = params.body
|
|
? typeof params.body === "string"
|
|
? params.body
|
|
: JSON.stringify(params.body)
|
|
: undefined;
|
|
return this._fetchJSON(params.endpoint, params.headers, {
|
|
...params.options,
|
|
body,
|
|
method: "PATCH",
|
|
});
|
|
}
|
|
|
|
checkError(response, return_response = 0) {
|
|
if (response.status >= 200 && response.status <= 299) {
|
|
return return_response ? response : response.json();
|
|
} else {
|
|
console.log("Server returned an error:");
|
|
console.log(response);
|
|
throw Error("%s (%s)".format(response.statusText, response.status));
|
|
}
|
|
}
|
|
}
|
|
|
|
export default HttpClient;
|