2019-11-08 13:28:11 -05:00
|
|
|
import { Promise } from "rsvp";
|
2016-09-22 14:39:36 -04:00
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
2019-10-23 13:25:25 -04:00
|
|
|
import jQuery from "jquery";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { run } from "@ember/runloop";
|
2016-08-25 13:14:56 -04:00
|
|
|
|
|
|
|
let token;
|
|
|
|
|
2016-09-08 16:58:07 -04:00
|
|
|
export function getToken() {
|
2016-08-25 13:14:56 -04:00
|
|
|
if (!token) {
|
2021-10-25 04:24:53 -04:00
|
|
|
token = document.querySelector('meta[name="csrf-token"]')?.content;
|
2016-08-25 13:14:56 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 16:58:07 -04:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ajax(args) {
|
2021-07-21 07:49:21 -04:00
|
|
|
let url;
|
|
|
|
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
url = arguments[0];
|
|
|
|
args = arguments[1];
|
|
|
|
} else {
|
|
|
|
url = args.url;
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:28:11 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-09-08 16:58:07 -04:00
|
|
|
args.headers = { "X-CSRF-Token": getToken() };
|
2019-10-30 09:48:24 -04:00
|
|
|
args.success = (data) => run(null, resolve, data);
|
|
|
|
args.error = (xhr) => run(null, reject, xhr);
|
2021-07-21 07:49:21 -04:00
|
|
|
args.url = getUrl(url);
|
2019-10-23 13:25:25 -04:00
|
|
|
jQuery.ajax(args);
|
2016-08-25 13:14:56 -04:00
|
|
|
});
|
|
|
|
}
|