ForgeTotal.js 5
en

Total.js 5

Controller callbacks, manual authorization and conditional responses

Total.js 5 · total5@0.0.19 total5reference Executed against Total.js 5

These are ordinary route-controller methods, distinct from the action context. $.jsonstring() takes already-serialized JSON. $.respond() selects the method named by $.response.output, defaulting to JSON. $.done(true) forwards the callback result inside the success envelope; another argument selects a fixed value. $.successful(fn) calls your function only when there is no error.

await $.authorize() resolves with the authorization result; an anonymous result must still be handled by your code. For conditional responses pass a UTC header string to $.notmodified(): in 0.0.19 comparison is exact string equality. $.httpcache() sets headers but $.json() overwrites cache policy, so the example sends a raw buffer after setting them. Use public caching only for public representations. $.custom() transfers response ownership to your handler; finish the Node response yourself.

definitions/auth.js
AUTH(function($) {
	if ($.headers['x-demo-user'] === 'alice')
		$.success({ id: 'alice' });
	else
		$.invalid();
});
controllers/completion.js
exports.install = function() {
	ROUTE('GET /serialized/', $ => $.jsonstring('{"ok":true}'));
	ROUTE('GET /respond/', function($) { $.response.output = 'plain'; $.respond('hello'); });
	ROUTE('GET /done/', $ => $.done(true)(null, { id: 7 }));
	ROUTE('GET /successful/', $ => $.successful(value => $.json({ value: value }))(null, 9));
	ROUTE('GET /callback-error/', $ => $.successful(() => $.json({ wrong: true }))('failure'));
	ROUTE('GET /manual-auth/', async function($) {
		var user = await $.authorize();
		if (user)
			$.json({ id: $.user.id });
		else
			$.invalid(401);
	});
	ROUTE('GET /conditional/', function($) {
		var modified = 'Mon, 05 Jan 2026 00:00:00 GMT';
		if ($.notmodified(modified))
			return;
		$.httpcache(modified);
		$.binary(Buffer.from('public version 1'), 'text/plain');
	});
	ROUTE('GET /custom/', function($) {
		$.custom();
		$.res.writeHead(202, { 'content-type': 'text/plain' });
		$.res.end('accepted');
	});
};