Hooks¶
hooks: {
after_connect: async ({ connection, actor, access_token, params, client }) => {
const login = await vendorLogin(access_token);
return { values: { login }, account_label: `@${login}` }; // or { refuse: true, message: { en: "…" } }
},
revoke: async ({ connection, access_token, refresh_token, values }) => { /* end the grant */ },
webhook: async ({ connection, headers, body, client }) => {
await client.emitEvent({ eventType: "plugin.acme.tracker.ticket-opened", payload: { number: 7 } });
},
},
after_connectruns once a connection's flow has exchanged its code, when the flow setsafter_connect. When the account the person signed in with reaches several (an installation per organization, a site per company), answer{ choices: [{ values, account_label }, …] }: Initiative connects a lone choice at once, and otherwise lists the labels for the person to pick one, storing its values as a single answer's.- It also runs when an admin saves a static connection without a flow that
sets
after_connect: truebeside itsfields, before anything is stored:access_tokenis null andvaluesholds what they typed, secrets included. Check them at the vendor and answer the connection's managed values and an account label, or{ refuse: true }to store nothing. A refusal'smessage, by language, says what to fix, such as the permissions the vendor did not grant; Initiative shows it to the admin where they saved. This is where a plug-in makes the vendor's webhooks for the connection, with a secret of 32 random bytes kept in a managedsecretfield. - A flow with an
install_urlsigns the person in first, andafter_connectruns with noparams.installation_id: answer the installations they control aschoices, and none sends them to the install page. Back from it,after_connectruns again with theinstallation_idthe page returned. revokeruns when a connection whose flow saysrevoke: "hook"ends. A flow can instead have Initiative end the grant itself:rfc7009posts torevoke_url, andgithub_grantsends GitHub's "Delete an app authorization" (DELETEwith the client's credentials and the access token) to it.revokealso runs when a connection withafter_connect: truebeside itsfieldsis cleared or replaced, or a value it checked is not kept, with thevaluesit held, so you can remove what you made at the vendor.webhookruns once per community for each vendor delivery Initiative received, checked and routed;headersare the vendor'sx-headers, lowercased, andbodyis exactly what the vendor sent. Initiative checks a delivery underwebhooks.verify.secret: one vendor value,{vendor.<key>}, or a managedsecretfield of the routed connection,{<key>}. With a connection's secret, it routes the delivery first and checks it under the secret of each connection its route value names. A delivery whose body a community already has is not handed on again.- Each schedule's
runis called once per community each interval, withsince: when it last succeeded there, or null the first time.
A hook that throws is answered 500: a connection is then not recorded, and a delivery or a schedule is tried again.
After connecting¶
A static connection may have a setup to go through once it is made, and a read that says how it stands:
connections: {
store: {
scope: "static",
label: { en: "Store" },
fields: [{ key: "domain", type: "string", label: { en: "Store address" } }],
after_connect: true,
setup: { label: { en: "Sync the store" } },
summary: "sync-state",
},
},
hooks: {
setup: async ({ connection, values, answers }) => {
if (answers) {
if (!(await startSync(values, answers))) return { refuse: true, message: { en: "Choose where the stock goes." } };
return {};
}
if (await synced(values)) return { steps: [] };
return {
steps: [
{ title: { en: "What syncing does" }, text: { en: "The store's stock follows yours." } },
{
title: { en: "Locations" },
questions: [
{
key: "location-1",
label: { en: "Main Street" },
options: [
{ value: "new", label: { en: "A new location: Main Street" } },
{ value: "none", label: { en: "Nothing" } },
],
value: "new",
},
],
},
],
submit: { en: "Sync now" },
};
},
},
setupruns, asrevokedoes, with the connection and thevaluesit holds, secrets included. Withoutanswers, answer what to ask now: steps, each atitle, sometextandquestions, andsubmit, what the last step's button says; or{ steps: [] }when there is nothing to set up. A question is a choice among itsoptions, and itsvalueis the option suggested, one of them.- Initiative asks it once the connection is made (saved and checked, or a flow
completed), and when it answers steps, opens them as a wizard in its own
dialog over the plug-in's settings. While the connection is connected,
Initiative asks again when it draws the connection, and while there are
steps it shows a button with
setup.labelon it that opens the wizard. Only people who may manage the connection see any of it. - Submitting sends
answers, each question'skeyand thevalueof the option chosen. Act on them and answer{}, which closes the wizard, or{ refuse: true, message }, which the wizard shows. - The SDK answers 500 in place of an answer Initiative would not take, and
logs why: more than 8 steps, 64 questions in all or 50 options a question,
a key asked twice or an option offered twice, a suggestion that is not one
of its options, a title, label or
submitover 120 characters, a step'stextover 1000, a key or value over 64, or withanswers, anything but{}or a refusal. summarynames a read by its key, held to the rules a community summary is: a read, with returns, and no required parameter. Wherever Initiative shows the connection while it is connected, it reads the endpoint as the installation and draws each return under its label beneath the connection. It reads it again when the setup is submitted.
validateManifest refuses a setup on a connection that is not static or in
a declarative plug-in, which has no hook to ask, and a summary the community
summary's rules refuse.
The community's own hooks¶
Eight hooks are about the community's install rather than a connection: the
six below, and backup and restore (Backups). Each is
optional; a plug-in that defines none is answered 404, and Initiative stops
sending that hook.
hooks: {
installed: async ({ installation }) => { /* a first install, or a return */ },
uninstalled: async ({ installation }) => { /* keep what you hold: it may come back */ },
erase: async ({ installation }) => { /* delete everything about this community */ },
community_status: async ({ installation, status }) => { /* pause unless active */ },
plugin_status: async ({ installation, status }) => { /* your call */ },
entitlement: async ({ installation, version, values }) => { /* keep the newest */ },
},
installedruns when the community installs the plug-in, and again if it returns after an uninstall. It comes back under the sameinstallationand the same member references, so whatever you kept for it still fits.uninstalledruns when it removes the plug-in. Its references stand.eraseruns when the community asks for the plug-in's data to be deleted, or is itself purged. Delete everything you hold for it. Once the hook answers, Initiative forgets the references, and a later install starts afresh under new ones. A hook that throws is asked again.community_statusruns whenever the community's standing changes. The same value rides every call ascommunity_status, so a missed hook is caught up on the next call.plugin_statusruns whenever the plug-in's own state in the community changes:active,read_only,on_holdordeleted, as the deployment's staff or its billing service set it. What to keep, pause, archive or erase in each state is the plug-in's decision, and it may ignore any of them: Initiative manages no plug-in's data. A suspended plug-in is sent nothing at all, sosuspendednever arrives. Unlikecommunity_status, it does not ride every call.entitlementcarries billing's statement of what this community holds of the plug-in:values, whose keys are agreed between the plug-in and billing, and aversionthat rises with each statement. Statements can repeat or arrive out of order, so keep the highest version you have seen and ignore a lower one. A deployment with no billing never sends one. The SDK verifies each statement before your hook sees it: see Entitlement statements.