Datapages actions

From romshark/datapages · View interactive version

Datapages actions is an AI agent skill from the GitHub repository romshark/datapages. Write Datapages action handlers (GET, POST, PUT, PATCH, DELETE, QUERY): file responses, parameters, return values, Datastar signals, SSE patching, HTTP error status codes and the RecoverError hook.

How to use this skill

Skill file

Actions

Read datapages first for the build loop, hard rules and naming conventions.

Define a page action as a value-receiver method on its page type. Define an action not tied to a page as a method on *App. Give each action 1 route doc comment. A page action route must be below its page route. For example, /login/submit is valid for PageLogin at /login. An action route cannot end in a {name...} wildcard. Pass that value in a query parameter or a signal.

// POSTSubmit is /login/submit
func (PageLogin) POSTSubmit(r *http.Request) error { return nil }

// POSTSignOut is /sign-out/{$}
func (*App) POSTSignOut(r *http.Request) error { return nil }

GETXXX is a file action, not the page's GET handler. A browser loads it by

URL from a link, image or download. It must return datapages.File and may take

only *http.Request, Session, Path and Query. The generated href package

builds its URL. A GET action skips CSRF checks and must not change server state.

Use a QUERYXXX action for a read that sends data in the body, such as a search with a large filter. It skips the CSRF check because QUERY is a safe method. Never change state in it.

Parameters

Parameters may appear in any order because the generator matches them by type. Names are unrestricted except for stateID. Read wrapped values from .Values.

| type | what |

| ---- | ---- |

| *http.Request | required |

| datapages.SSE | patches the stream of the calling page; page actions only, not *App actions |

| Session | the current session, see datapages-sessions |

| datapages.Path[struct{...}] | route variables, path:"id" tags |

| datapages.Query[struct{...}] | query parameters, query:"p" tags |

| datapages.Signals[struct{...}] | signals sent by the client, json:"v" tags |

| datapages.State[StateX] | per-tab server state, see datapages-state |

| stateID string | tab event address; requires State[T], see datapages-state |

| datapages.PageCacheWriter | writes the offline page cache, see datapages-offline |

| datapages.Dispatcher[EventX] | publishes EventX, see datapages-events |

The name in a Signals field's json tag must match [A-Za-z_][A-Za-z0-9_]* and must not contain __. json:"-" is invalid. Omit a field to exclude it. Nested structs define signal paths.

Return values

A non-GET action may return only error. Other supported return types are datapages.Component, datapages.Head, datapages.DocumentAttrs, datapages.Redirect, datapages.NewSession[Data], datapages.CloseSession and datapages.File. Return values may appear in any order. datapages.Head and datapages.DocumentAttrs require datapages.Component. Datastar morphs a returned body into the open document, including the attributes of <html>, so return the same datapages.DocumentAttrs as the page's GET.

) (redirect datapages.Redirect, err error) {
	return datapages.Redirect{URL: href.PageIndex()}, nil
}

Redirect.Status defaults to 302. Datastar requests ignore it because they cannot follow an HTTP redirect. They navigate by assigning window.location.

Do not combine datapages.SSE with session changes. An sse parameter causes the response headers to be sent before the handler runs, so the handler cannot set or delete the session cookie. The generator rejects newSession or closeSession with sse. A redirect still works because it uses the stream. A returned datapages.Component is rejected with sse too: send it with sse.PatchElement instead.

An action's SSE response uses datapages.WithSSECompression, not the page's StreamConfig. Configure it with a method named StreamConfig plus the action's method name, such as StreamConfigPOSTSave, on the same page or App; see datapages-events.

Files

A named GET action serves a file at its route:

// GETImage is /images/{name}
func (a *App) GETImage(
	r *http.Request,
	path datapages.Path[struct {
		Name string `path:"name"`
	}],
) (datapages.File, error) {
	// a.images opens through an os.Root: the name is unescaped
	// and can hold "/" and "..".
	f, contentType, err := a.images.Open(path.Values.Name)
	switch {
	case errors.Is(err, fs.ErrNotExist):
		return datapages.File{}, datapages.ErrNotFound
	case err != nil:
		return datapages.File{}, err
	}
	return datapages.File{Type: contentType, Body: f}, nil
}

A path value holds its segment unescaped: %2F arrives as / and %2E%2E as ... Open a file through an fs.FS or an os.Root, or look the value up as a key. Never join it into a file path.

Any action may return datapages.File. The file is the complete response, so it

may be accompanied only by error, which is optional. A file action cannot take

datapages.SSE or datapages.PageCacheWriter.

Body is an io.ReadSeekCloser, which Datapages closes after serving. Content held in memory needs a Close that does nothing: a bytes.Reader has none, and io.NopCloser drops Seek.

type memBody struct{ *bytes.Reader }

func (memBody) Close() error { return nil }

Do not use datapages.File to render application HTML. Return HTML as a Templ

datapages.Component. File bytes bypass Templ's contextual escaping and the

Datapages template linter.

Serve a file a visitor uploaded with Disposition: datapages.FileDisposition{Download: true}, or show inline only types from an allowlist. The type of an upload is what the uploading browser reported, and text/html or image/svg+xml runs script on the application's origin when shown. X-Content-Type-Options: nosniff, which Datapages always sends, does not prevent that.

Datapages leaves datapages.File responses uncompressed to preserve Content-Length, ranges and ETag. For smaller transfers, serve a precompressed archive such as export.csv.gz with Type: "application/gzip", or negotiate Accept-Encoding. A negotiated representation needs Content-Encoding in Header, its own ETag and Vary: Accept-Encoding.

The zero Cache sends Cache-Control: no-cache, which makes the browser ask before every reuse. Set MaxAge and Immutable only for a URL whose bytes never change, and Private for a file only its visitor may see. Without MaxAge or NoStore, Datapages adds no-cache, also next to Private. NoStore also keeps the file out of the offline worker's cache. A handler that takes Session sends Vary: Cookie, which keeps one visitor from getting the copy a browser or CDN stored for another. Serve a file that is the same for every visitor from a handler without Session, which a CDN can share.

SSE

| method | effect |

| ------ | ------ |

| Context() | context of the SSE stream |

| PatchElement(c) | morph by element id |

| PatchElementAt(c, sel, mode) | target a selector; datapages.PatchModeInner, ...Replace, ...Prepend, ...Append, ...Before, ...After |

| RemoveElement(sel) | remove matching elements |

| PatchSignals(v) | set client signals from JSON |

| PatchSignalsIfMissing(v) | set only signals that do not exist |

| ExecuteScript(js) | run JS in the browser |

| Redirect(url) | client-side navigation |

| Prefetch(urls...) | speculation rules hint |

A selector must not contain a line break. Prefer 1 complete fragment over several small targeted patches. ...Prepend and ...Append cannot recover missed events. See the delivery rules in datapages-events.

Errors

return datapages.ErrBadRequest                            // 400
return datapages.ErrForbidden                             // 403
return datapages.ErrNotFound                              // 404
return datapages.ErrConflict                              // 409
return fmt.Errorf("%w: %w", datapages.ErrNotFound, err)   // 404, keeps err

Any other error returns 500. The response body is the standard status text. A page load gets PageError404 for 404 and PageError500 for 500 instead when the app defines them. A Datastar request gets status 200 when the app defines RecoverError, and a handler with sse sends 200 before it runs. Wrap at most 1 sentinel. If an error contains several sentinels, the first of ErrBadRequest, ErrForbidden, ErrNotFound and ErrConflict sets the status.

RecoverError

A Datastar request shows an HTTP error only in the console. Define this hook to patch an error message into the page.

func (*App) RecoverError(err error, sse datapages.SSE) error {
	return sse.PatchElement(errorToast(err))
}

Only Datastar requests call this hook. It receives the errors of actions and StreamOpen, including sentinels, and the panics of On handlers. Datapages only logs an error that an On handler or StreamClose returns. Use errors.Is to identify sentinels. The response has status 200 whatever the error. For a page load, Datapages renders PageError404 for a 404 and PageError500 for a 500 if the app defines them. Otherwise it writes the status and its standard text if the response has not started. The hook writes an event stream, so do not use it as a page response.

A panic in a GET, action, StreamOpen or On handler becomes a datapages.PanicError. It contains the panic value and stack. Use errors.As to inspect it. Datapages logs the stack before it calls the hook, then ends the request.

StreamClose runs after the response completes. Datapages logs its panics but does not call the hook. If the hook returns an error, Datapages logs that error with the original one and does not change the response. Writing an HTTP error at that point would append plain text to the open SSE stream.

<!-- written by datapages sha256:5819cd4a70aa7a5d -->

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.