DEV: Use type instead of method in ajax calls (#8974)

Even though `type` is an alias for `method`, we have custom logic in `/discourse/lib/ajax` that checks only `type`, and ~200 other ajax calls in the codebase already use `type` param.
This commit is contained in:
Jarek Radosz
2020-03-26 21:00:10 +01:00
committed by GitHub
parent 17211b940f
commit 67b34600d5
27 changed files with 42 additions and 42 deletions

View File

@ -18,7 +18,7 @@ export default Controller.extend(bufferedProperty("model"), {
}); });
ajax("robots.json", { ajax("robots.json", {
method: "PUT", type: "PUT",
data: { robots_txt: this.buffered.get("robots_txt") } data: { robots_txt: this.buffered.get("robots_txt") }
}) })
.then(data => { .then(data => {
@ -34,7 +34,7 @@ export default Controller.extend(bufferedProperty("model"), {
isSaving: true, isSaving: true,
saved: false saved: false
}); });
ajax("robots.json", { method: "DELETE" }) ajax("robots.json", { type: "DELETE" })
.then(data => { .then(data => {
this.buffered.set("robots_txt", data.robots_txt); this.buffered.set("robots_txt", data.robots_txt);
this.commitBuffer(); this.commitBuffer();

View File

@ -92,7 +92,7 @@ export default Controller.extend({
result => { result => {
if (result) { if (result) {
ajax(`/admin/logs/watched_words/action/${actionKey}.json`, { ajax(`/admin/logs/watched_words/action/${actionKey}.json`, {
method: "DELETE" type: "DELETE"
}).then(() => { }).then(() => {
const action = this.findAction(actionKey); const action = this.findAction(actionKey);
if (action) { if (action) {

View File

@ -65,7 +65,7 @@ export default Controller.extend(ModalFunctionality, {
ajax("/admin/badges/badge_groupings", { ajax("/admin/badges/badge_groupings", {
data: { ids: groupIds, names }, data: { ids: groupIds, names },
method: "POST" type: "POST"
}).then( }).then(
data => { data => {
items = this.model; items = this.model;

View File

@ -84,7 +84,7 @@ export default Controller.extend(ModalFunctionality, {
const checked = this.privateChecked; const checked = this.privateChecked;
if (checked && !this._keyLoading) { if (checked && !this._keyLoading) {
this._keyLoading = true; this._keyLoading = true;
ajax(this.keyGenUrl, { method: "POST" }) ajax(this.keyGenUrl, { type: "POST" })
.then(pair => { .then(pair => {
this.setProperties({ this.setProperties({
privateKey: pair.private_key, privateKey: pair.private_key,

View File

@ -31,7 +31,7 @@ export default Controller.extend(ModalFunctionality, {
category_ids: this._extractSelectedIds(this.categories), category_ids: this._extractSelectedIds(this.categories),
topic_ids: this._extractSelectedIds(this.topics) topic_ids: this._extractSelectedIds(this.topics)
}, },
method: "POST" type: "POST"
}) })
.then( .then(
() => this.send("closeModal"), () => this.send("closeModal"),

View File

@ -5,7 +5,7 @@ const { getProperties } = Ember;
export default RestModel.extend({ export default RestModel.extend({
revert() { revert() {
return ajax(`/admin/customize/email_templates/${this.id}`, { return ajax(`/admin/customize/email_templates/${this.id}`, {
method: "DELETE" type: "DELETE"
}).then(result => }).then(result =>
getProperties(result.email_template, "subject", "body", "can_revert") getProperties(result.email_template, "subject", "body", "can_revert")
); );

View File

@ -10,7 +10,7 @@ const ScreenedEmail = EmberObject.extend({
clearBlock: function() { clearBlock: function() {
return ajax("/admin/logs/screened_emails/" + this.id, { return ajax("/admin/logs/screened_emails/" + this.id, {
method: "DELETE" type: "DELETE"
}); });
} }
}); });

View File

@ -5,7 +5,7 @@ const { getProperties } = Ember;
export default RestModel.extend({ export default RestModel.extend({
revert() { revert() {
return ajax(`/admin/customize/site_texts/${this.id}`, { return ajax(`/admin/customize/site_texts/${this.id}`, {
method: "DELETE" type: "DELETE"
}).then(result => getProperties(result.site_text, "value", "can_revert")); }).then(result => getProperties(result.site_text, "value", "can_revert"));
} }
}); });

View File

@ -40,7 +40,7 @@ export default Route.extend({
preview(badge, explain) { preview(badge, explain) {
badge.set("preview_loading", true); badge.set("preview_loading", true);
ajax("/admin/badges/preview.json", { ajax("/admin/badges/preview.json", {
method: "post", type: "POST",
data: { data: {
sql: badge.get("query"), sql: badge.get("query"),
target_posts: !!badge.get("target_posts"), target_posts: !!badge.get("target_posts"),

View File

@ -13,7 +13,7 @@ export default RestAdapter.extend({
createRecord(store, type, args) { createRecord(store, type, args) {
const typeField = underscore(type); const typeField = underscore(type);
args.nested_post = true; args.nested_post = true;
return ajax(this.pathFor(store, type), { method: "POST", data: args }).then( return ajax(this.pathFor(store, type), { type: "POST", data: args }).then(
function(json) { function(json) {
return new Result(json[typeField], json); return new Result(json[typeField], json);
} }

View File

@ -133,7 +133,7 @@ export default EmberObject.extend({
destroyRecord(store, type, record) { destroyRecord(store, type, record) {
return ajax(this.pathFor(store, type, record.get("id")), { return ajax(this.pathFor(store, type, record.get("id")), {
method: "DELETE" type: "DELETE"
}); });
} }
}); });

View File

@ -14,7 +14,7 @@ export default Component.extend({
actions: { actions: {
unclaim() { unclaim() {
ajax(`/reviewable_claimed_topics/${this.topicId}`, { ajax(`/reviewable_claimed_topics/${this.topicId}`, {
method: "DELETE" type: "DELETE"
}).then(() => { }).then(() => {
this.set("claimedBy", null); this.set("claimedBy", null);
}); });

View File

@ -97,7 +97,7 @@ export default Component.extend({
return ajax( return ajax(
`/review/${reviewable.id}/perform/${action.id}?version=${version}`, `/review/${reviewable.id}/perform/${action.id}?version=${version}`,
{ {
method: "PUT" type: "PUT"
} }
) )
.then(result => { .then(result => {

View File

@ -63,7 +63,7 @@ export default Controller.extend(ModalFunctionality, {
return ajax( return ajax(
`/user_avatar/${this.get("user.username")}/refresh_gravatar.json`, `/user_avatar/${this.get("user.username")}/refresh_gravatar.json`,
{ method: "POST" } { type: "POST" }
) )
.then(result => { .then(result => {
if (!result.gravatar_upload_id) { if (!result.gravatar_upload_id) {

View File

@ -31,7 +31,7 @@ export default Controller.extend(ModalFunctionality, {
ajax("/admin/groups/bulk", { ajax("/admin/groups/bulk", {
data: { users, group_id: this.get("model.id") }, data: { users, group_id: this.get("model.id") },
method: "PUT" type: "PUT"
}) })
.then(result => { .then(result => {
this.set("result", result); this.set("result", result);

View File

@ -15,7 +15,7 @@ export default Controller.extend({
this.set("saving", true); this.set("saving", true);
ajax("/review/settings", { ajax("/review/settings", {
method: "PUT", type: "PUT",
data: { reviewable_priorities: priorities } data: { reviewable_priorities: priorities }
}) })
.then(() => { .then(() => {

View File

@ -23,7 +23,7 @@ export default Controller.extend({
markFaqRead() { markFaqRead() {
const currentUser = this.currentUser; const currentUser = this.currentUser;
if (currentUser) { if (currentUser) {
ajax(userPath("read-faq"), { method: "POST" }).then(() => { ajax(userPath("read-faq"), { type: "POST" }).then(() => {
currentUser.set("read_faq", true); currentUser.set("read_faq", true);
}); });
} }

View File

@ -28,7 +28,7 @@ export default Controller.extend({
actions: { actions: {
resetNew() { resetNew() {
ajax("/notifications/mark-read", { method: "PUT" }).then(() => { ajax("/notifications/mark-read", { type: "PUT" }).then(() => {
this.model.forEach(n => n.set("read", true)); this.model.forEach(n => n.set("read", true));
}); });
}, },

View File

@ -139,6 +139,11 @@ export function ajax() {
}); });
}; };
if (args.method) {
args.type = args.method;
delete args.method;
}
// We default to JSON on GET. If we don't, sometimes if the server doesn't return the proper header // We default to JSON on GET. If we don't, sometimes if the server doesn't return the proper header
// it will not be parsed as an object. // it will not be parsed as an object.
if (!args.type) args.type = "GET"; if (!args.type) args.type = "GET";

View File

@ -3,7 +3,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
function exportEntityByType(type, entity, args) { function exportEntityByType(type, entity, args) {
return ajax("/export_csv/export_entity.json", { return ajax("/export_csv/export_entity.json", {
method: "POST", type: "POST",
data: { entity, args } data: { entity, args }
}); });
} }

View File

@ -634,7 +634,7 @@ const Topic = RestModel.extend({
updateDestinationCategory(categoryId) { updateDestinationCategory(categoryId) {
this.set("destination_category_id", categoryId); this.set("destination_category_id", categoryId);
return ajax(`/t/${this.id}/shared-draft`, { return ajax(`/t/${this.id}/shared-draft`, {
method: "PUT", type: "PUT",
data: { category_id: categoryId } data: { category_id: categoryId }
}); });
}, },

View File

@ -29,7 +29,7 @@ const ApplicationRoute = DiscourseRoute.extend(OpenComposer, {
actions: { actions: {
toggleAnonymous() { toggleAnonymous() {
ajax(userPath("toggle-anon"), { method: "POST" }).then(() => { ajax(userPath("toggle-anon"), { type: "POST" }).then(() => {
window.location.reload(); window.location.reload();
}); });
}, },

View File

@ -26,7 +26,7 @@ createWidget("priority-faq-link", {
click(e) { click(e) {
e.preventDefault(); e.preventDefault();
if (this.siteSettings.faq_url === this.attrs.href) { if (this.siteSettings.faq_url === this.attrs.href) {
ajax(userPath("read-faq"), { method: "POST" }).then(() => { ajax(userPath("read-faq"), { type: "POST" }).then(() => {
this.currentUser.set("read_faq", true); this.currentUser.set("read_faq", true);
DiscourseURL.routeToTag($(e.target).closest("a")[0]); DiscourseURL.routeToTag($(e.target).closest("a")[0]);
}); });

View File

@ -7,7 +7,7 @@ createWidgetFrom(QuickAccessPanel, "quick-access-notifications", {
emptyStatePlaceholderItemKey: "notifications.empty", emptyStatePlaceholderItemKey: "notifications.empty",
markReadRequest() { markReadRequest() {
return ajax("/notifications/mark-read", { method: "PUT" }); return ajax("/notifications/mark-read", { type: "PUT" });
}, },
newItemsLoaded() { newItemsLoaded() {

View File

@ -15,7 +15,7 @@ export function lookupUncachedUploadUrls(urls, ajax) {
} }
return ajax("/uploads/lookup-urls", { return ajax("/uploads/lookup-urls", {
method: "POST", type: "POST",
data: { short_urls: urls } data: { short_urls: urls }
}).then(uploads => { }).then(uploads => {
uploads.forEach(upload => { uploads.forEach(upload => {

View File

@ -100,13 +100,11 @@ QUnit.test("creating simultaneously", assert => {
}); });
}); });
QUnit.test("destroyRecord", assert => { QUnit.test("destroyRecord", async assert => {
const store = createStore(); const store = createStore();
return store.find("widget", 123).then(function(widget) { const widget = await store.find("widget", 123);
widget.destroyRecord().then(function(result) {
assert.ok(result); assert.ok(await widget.destroyRecord());
});
});
}); });
QUnit.test("custom api name", async assert => { QUnit.test("custom api name", async assert => {

View File

@ -114,21 +114,18 @@ QUnit.test("findAll", async assert => {
assert.equal(widget.get("name"), "Evil Repellant"); assert.equal(widget.get("name"), "Evil Repellant");
}); });
QUnit.test("destroyRecord", function(assert) { QUnit.test("destroyRecord", async assert => {
const store = createStore(); const store = createStore();
return store.find("widget", 123).then(function(w) { const widget = await store.find("widget", 123);
store.destroyRecord("widget", w).then(function(result) {
assert.ok(result); assert.ok(await store.destroyRecord("widget", widget));
});
});
}); });
QUnit.test("destroyRecord when new", function(assert) { QUnit.test("destroyRecord when new", async assert => {
const store = createStore(); const store = createStore();
const w = store.createRecord("widget", { name: "hello" }); const widget = store.createRecord("widget", { name: "hello" });
store.destroyRecord("widget", w).then(function(result) {
assert.ok(result); assert.ok(await store.destroyRecord("widget", widget));
});
}); });
QUnit.test("find embedded", async assert => { QUnit.test("find embedded", async assert => {