Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

ts: Convert csrf module to TypeScript.

Priyank Patela8cdc968cccc
diff --git a/static/js/csrf.js b/static/js/csrf.ts
similarity index 66%
rename from static/js/csrf.js
rename to static/js/csrf.ts
index 7287d78e5b3..44e91686a44 100644
--- a/static/js/csrf.js
+++ b/static/js/csrf.ts
@@ -1,6 +1,6 @@
import $ from "jquery";
-export let csrf_token;
+export let csrf_token: string | undefined;
$(() => {
// This requires that we used Jinja2's {% csrf_input %} somewhere on the page.
@@ -11,7 +11,11 @@ $(() => {
}
$.ajaxSetup({
- beforeSend(xhr, settings) {
+ beforeSend(xhr: JQuery.jqXHR, settings: JQuery.AjaxSettings) {
+ if (settings.url === undefined || csrf_token === undefined) {
+ throw new Error("settings.url and/or csrf_token are missing.");
+ }
+
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", csrf_token);
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index 747adcd74a6..c054853fb7c 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -53,7 +53,7 @@ EXEMPT_FILES = {
"static/js/condense.js",
"static/js/confirm_dialog.js",
"static/js/copy_and_paste.js",
- "static/js/csrf.js",
+ "static/js/csrf.ts",
"static/js/css_variables.js",
"static/js/debug.js",
"static/js/deprecated_feature_notice.js",