smr/src/lua/hooks.lua

86 lines
2.8 KiB
Lua

--[[ md
@name lua/hooks
Global functions that smr exposes that can be detoured by addons
]]
--[[ md
@name doc/detouring
# Detouring
In Lua, functions are given a name, but more generally, they values on a table
, perhaps the global table `_G`, and their names are the keys on the table.
When you want to modify a function that exists either in smr or in other addons
you can **detour** the function by saving a reference to the original function,
and then creating a new function that calls the original, maybe after doing
other work, or modifying the arguments. For example:
local pages = require("pages")
-- Get notified when the index page is rendered to the user
local oldindex = pages.index
function pages.index(...)
print("Index page is getting rendered!")
oldindex(...)
end
]]
local api = {}
--[[ md
@name lua/hooks
## pre_request
Called before any request processing. Returning true "traps" the request, and
does not continue calling smr logic. Well-behaved addons should check for
"true" from the detoured function, and return true immediately if the check
succeeds.
@param req {{http_request}} - The request about to be processed
@returns boolean - If true, further processing is not done on this request.
]]
api.pre_request = function(req) end
-- Called after smr request processing. Returning true "traps" the request.
-- Well-behaved addons should check for true from the detoured function, and
-- immediately return true if the check succeeds. This will not stop smr from
-- responding to the request, since by this time http_request_response() has
-- already been called.
api.post_request = function(req) end
-- Called during startup of the worker process
-- calling error() in this function will prevent kore from starting.
-- Return value is ignored.
api.worker_init = function() end
-- Called during shutdown of the worker process
-- Failures in this function cause other addon hooks to this function to be skipped.
-- Return value is ignored.
api.worker_shutdown = function() end
-- The following are tables and their options:
-- "buttonspec" - specifies a button to display on the front end, has the fields:
-- .endpoint - the url to go to when the button is pressed
-- .method - the HTTP method to use to call the endpoint
-- .fields - key/value pairs to send as arguments when calling the endpoint
-- These are usually "hidden" fields for a form.
-- .text - The text that displays on the button
-- Called to display configuration as html.
api.get = {
-- returns an array of buttonspec, displayed at the top of a story,
-- only for the logged in owner of a story.
page_owner = function(env) return {} end,
-- returns an array of buttonspec, displayed at the bottom of a story
page_reader = function(env) return {} end,
}
-- Called when the /_api endpoint is accessed
api.call = function() end
return api