Skip to content

Common JSON Responses

Alice Zoë Bevan–McGregor edited this page Feb 15, 2016 · 3 revisions

The front-end RPC handler knows how to handle the following types of JSON responses, out of the box.

Actions

Fetch URL

This has different variants depending on desired history management behaviour.

{
	// "go" implies history push, "swap" history replacement, "fetch" has no history.
	"action": "go" | "swap" | "fetch",
	"url": String,  // URL to request.
	"message": undefined | String,  // Optional message to display when redirecting.
	"verb": undefined | String,  // HTTP verb to use during request.
	"data": undefined | Object,  // Additional key-value data to include in the request.
}

A typical example, an action to delete a user:

{
	"action": "fetch",
	"url": "/user/amcgregor",
	"verb": "delete",
	"data": {"signature": "signature"}
}

Confirm With User

{
	"action": "confirm",  // Fixed value.
	"title": undefined | String,  // Optional modal title to override default.
	"ref": undefined | String,
	"message": String,  // Principal confirmation message to display.
	"dismiss": String,
	"actions": undefined | [  // Populate as required.
			{"label": String, "ref": undefined | String, action ...},
		]
}

Continuing the example from earlier, this would be a "confirm user deletion" dialog:

{
	"action": "confirm",
	"title": "Are you sure?",
	"message": "Are you sure you wish to delete this user?",
	"dismiss": "Cancel",
	"actions": [{
			"label": "Delete",
			"ref": "delete-user",
			"action": "fetch",
			"url": "/user/amcgregor",
			"verb": "delete",
			"data": {"signature": "signature"}
		}]
}

Success/Failure Response

Typically used to confirm some action was taken.

{
	"success": Boolean,
	"scope": undefined | String,
	"policy": undefined | "keep" | "replace" | "push",
	"level": undefined | "success" | "info" | "warning" | "danger",
	"precedence": undefined | "normal" | "sticky" | "modal",
	"ref": undefined | String,
	"title": undefined | String,
	"message": String,
	"actions": undefined | [
			{"label": String, "ref": undefined | String, ...},
		],
}

The field level defaults to "success" if success is true, and defaults to "danger" if success is false. The precedence field defaults to "normal" if undefined. Messages marked sticky will persist until dismissed. If no scope is given, the message is global, otherwise use the first notification bucket with that name. If the policy is undefined it defaults to "push" if no scope is given, otherwise it defaults to "replace".

As an example, the following JSON:

{
	"success": true,
	"title": "Success",
	"message": "Successfully deleted user <code>amcgregor</code>.",
	"actions": [{
			"label": "Undo",
			"ref": "undo-delete-user",
			"action": "fetch",
			"url": "/user/amcgregor",
			"verb": "put",
			"data": {"$restore": "latest", "signature": "signature"}
		}]
}

Would be interpreted as this full message:

{
	"success": true,
	"scope": Message.scope.global,
	"policy": "push",
	"level": "success",
	"precedence": "normal",
	"title": "Success",
	"message": "Successfully deleted user <code>amcgregor</code>.",
	"actions": [{
			"label": "Undo",
			"ref": "undo-delete-user",
			"action": "fetch",
			"url": "/user/amcgregor",
			"verb": "put",
			"data": {"$restore": "latest", "signature": "signature"}
		}]
}
Clone this wiki locally