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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 42 additions and 42 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,7 +10,7 @@ const ScreenedEmail = EmberObject.extend({
clearBlock: function() {
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({
revert() {
return ajax(`/admin/customize/site_texts/${this.id}`, {
method: "DELETE"
type: "DELETE"
}).then(result => getProperties(result.site_text, "value", "can_revert"));
}
});

View File

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

View File

@ -13,7 +13,7 @@ export default RestAdapter.extend({
createRecord(store, type, args) {
const typeField = underscore(type);
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) {
return new Result(json[typeField], json);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -28,7 +28,7 @@ export default Controller.extend({
actions: {
resetNew() {
ajax("/notifications/mark-read", { method: "PUT" }).then(() => {
ajax("/notifications/mark-read", { type: "PUT" }).then(() => {
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
// it will not be parsed as an object.
if (!args.type) args.type = "GET";

View File

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

View File

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

View File

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

View File

@ -26,7 +26,7 @@ createWidget("priority-faq-link", {
click(e) {
e.preventDefault();
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);
DiscourseURL.routeToTag($(e.target).closest("a")[0]);
});

View File

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

View File

@ -15,7 +15,7 @@ export function lookupUncachedUploadUrls(urls, ajax) {
}
return ajax("/uploads/lookup-urls", {
method: "POST",
type: "POST",
data: { short_urls: urls }
}).then(uploads => {
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();
return store.find("widget", 123).then(function(widget) {
widget.destroyRecord().then(function(result) {
assert.ok(result);
});
});
const widget = await store.find("widget", 123);
assert.ok(await widget.destroyRecord());
});
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");
});
QUnit.test("destroyRecord", function(assert) {
QUnit.test("destroyRecord", async assert => {
const store = createStore();
return store.find("widget", 123).then(function(w) {
store.destroyRecord("widget", w).then(function(result) {
assert.ok(result);
});
});
const widget = await store.find("widget", 123);
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 w = store.createRecord("widget", { name: "hello" });
store.destroyRecord("widget", w).then(function(result) {
assert.ok(result);
});
const widget = store.createRecord("widget", { name: "hello" });
assert.ok(await store.destroyRecord("widget", widget));
});
QUnit.test("find embedded", async assert => {