Bugzilla – Attachment 162110 Details for
Bug 36084
Pass CSRF token to SVC scripts (1/2)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 36084: Bring fetch for everywhere
Bug-36084-Bring-fetch-for-everywhere.patch (text/plain), 6.18 KB, created by
Jonathan Druart
on 2024-02-13 14:25:10 UTC
(
hide
)
Description:
Bug 36084: Bring fetch for everywhere
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2024-02-13 14:25:10 UTC
Size:
6.18 KB
patch
obsolete
>From 89da9d7ef5eac3eb427b9d70d0bf1fd2651148f6 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Tue, 13 Feb 2024 15:08:25 +0100 >Subject: [PATCH] Bug 36084: Bring fetch for everywhere > >We are retrieving the awesome fetch modules from Vue, so that it can be >used in other areas. Here we will use it to inject the CSRF token to the >header of every POST request. >--- > .../intranet-tmpl/prog/js/fetch/api-client.js | 5 ++ > .../js/fetch/article-request-api-client.js | 62 ++++++++++++++ > .../prog/js/fetch/http-client.js | 83 +++++++++++++++++++ > 3 files changed, 150 insertions(+) > create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js > create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/article-request-api-client.js > create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js > >diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js >new file mode 100644 >index 00000000000..ed27a7c36c2 >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js >@@ -0,0 +1,5 @@ >+import ArticleRequestAPIClient from "./article-request-api-client.js"; >+ >+export const APIClient = { >+ article_request: new ArticleRequestAPIClient(), >+}; >diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/article-request-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/article-request-api-client.js >new file mode 100644 >index 00000000000..466b39721a0 >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/article-request-api-client.js >@@ -0,0 +1,62 @@ >+import HttpClient from "./http-client.js"; >+ >+export class ArticleRequestAPIClient extends HttpClient { >+ constructor() { >+ super({ >+ baseURL: "/cgi-bin/koha/svc/article_request", >+ headers: { >+ "Content-Type": >+ "application/x-www-form-urlencoded;charset=utf-8", >+ }, >+ }); >+ } >+ >+ get articleRequests() { >+ return { >+ process: id => >+ this.post({ >+ endpoint: "", >+ body: "id=%s&op=%s".format( >+ id, >+ 'cud-process', >+ ), >+ }), >+ complete: id => >+ this.post({ >+ endpoint: "", >+ body: "id=%s&op=%s".format( >+ id, >+ 'cud-complete', >+ ), >+ }), >+ pending: id => >+ this.post({ >+ endpoint: "", >+ body: "id=%s&op=%s".format( >+ id, >+ 'cud-pending', >+ ), >+ }), >+ update_urls: (id, urls) => >+ this.post({ >+ endpoint: "", >+ body: "id=%s&urls=%s&op=%s".format( >+ id, >+ urls, >+ 'cud-update_urls' >+ ), >+ }), >+ update_library_id: (id, library_id) => >+ this.post({ >+ endpoint: "", >+ body: "id=%s&library_id=%s&op=%s".format( >+ id, >+ library_id, >+ 'cud-update_library_id' >+ ), >+ }), >+ }; >+ } >+} >+ >+export default ArticleRequestAPIClient; >diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js >new file mode 100644 >index 00000000000..225a5e6c521 >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js >@@ -0,0 +1,83 @@ >+//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", >+ }; >+ this.csrf_token = $('meta[name="csrf-token"]').attr('content'); >+ } >+ >+ 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 => { >+ if (!response.ok) { >+ return response.text().then(text => { >+ let message; >+ if (text) { >+ let json = JSON.parse(text); >+ message = >+ json.error || >+ json.errors.map(e => e.message).join("\n") || >+ json; >+ } else { >+ message = response.statusText; >+ } >+ throw new Error(message); >+ }); >+ } >+ return return_response ? response : response.json(); >+ }) >+ .then(result => { >+ res = result; >+ }) >+ .catch(err => { >+ error = err; >+ //setError(err); >+ console.error(err); >+ }) >+ .then(() => { >+ //if (mark_submitting) submitted(); >+ }); >+ >+ if (error) throw Error(error); >+ >+ return res; >+ } >+ >+ post(params = {}) { >+ const body = params.body >+ ? typeof params.body === "string" >+ ? params.body >+ : JSON.stringify(params.body) >+ : params.data || undefined; >+ let csrf_token = { csrf_token: this.csrf_token }; >+ let headers = { ...csrf_token, ...params.headers }; >+ return this._fetchJSON( >+ params.endpoint, >+ headers, >+ { >+ ...params.options, >+ body, >+ method: "POST", >+ }, >+ false, >+ true >+ ); >+ } >+ >+} >+ >+export default HttpClient; >-- >2.34.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 36084
:
162108
|
162109
|
162110
|
162111
|
162112
|
162133
|
162147
|
162150
|
162712