diff --git a/internal/collections/collections.go b/internal/collections/collections.go index 76f3702..c8dc241 100644 --- a/internal/collections/collections.go +++ b/internal/collections/collections.go @@ -7,7 +7,11 @@ var Synced = []string{"entries", "closed_days", "closed_weeks", "settings_histor // ReadOnly collections are pulled by clients but only ever written by the server. var ReadOnly = []string{"closed_days", "closed_weeks"} -// DataFields lists, per synced collection, the record fields a client may write through the push endpoint. +// DataFields lists, per synced collection, the record fields that hold the data +// of a row, in contrast to the sync fields every collection shares (owner, rev, +// client_updated_at and deleted). Only the fields of the writable collections +// reach the records through the push endpoint; the read-only collections are +// written by the server alone. var DataFields = map[string][]string{ "entries": {"start_time", "end_time", "auto_stopped", "note", "day_key"}, "closed_days": {"day_key", "worked_ms", "kind", "closed_at"}, diff --git a/ui.go b/ui.go index 080a020..a7664d9 100644 --- a/ui.go +++ b/ui.go @@ -4,6 +4,7 @@ import ( "embed" "io/fs" "mime" + "net/http" "github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/core" @@ -12,13 +13,32 @@ import ( //go:embed all:web/build var webBuild embed.FS +// apiMethods are the methods the catch-all for unknown API routes covers. HEAD is +// left out because it would conflict with every GET route of the router; GET +// patterns answer HEAD requests anyway. +var apiMethods = []string{ + http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodOptions, +} + func registerUI(app core.App) { _ = mime.AddExtensionType(".webmanifest", "application/manifest+json") sub, err := fs.Sub(webBuild, "web/build") if err != nil { panic(err) } + notFound := func(e *core.RequestEvent) error { + return e.NotFoundError("Not found.", nil) + } app.OnServe().BindFunc(func(se *core.ServeEvent) error { + // Without this the SPA catch-all below answers unknown API routes with + // the index page and status 200, which the client SDK cannot parse. + // The routes registered before keep precedence because they are more + // specific than this catch-all. + for _, method := range apiMethods { + if !se.Router.HasRoute(method, "/api/{path...}") { + se.Router.Route(method, "/api/{path...}", notFound) + } + } if !se.Router.HasRoute("GET", "/{path...}") { se.Router.GET("/{path...}", apis.Static(sub, true)) } diff --git a/ui_test.go b/ui_test.go new file mode 100644 index 0000000..98f4234 --- /dev/null +++ b/ui_test.go @@ -0,0 +1,62 @@ +package main + +import ( + "net/http" + "testing" + + "github.com/pocketbase/pocketbase/tests" + + "github.com/wotra/wotra/internal/tracking" +) + +func TestUnknownApiRoutesAnswerJSON(t *testing.T) { + const notFound = `{"data":{},"message":"Not found.","status":404}` + cases := []struct { + name string + method string + url string + status int + content []string + absent []string + }{ + {name: "GET", method: http.MethodGet, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}}, + {name: "POST", method: http.MethodPost, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}}, + {name: "PUT", method: http.MethodPut, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}}, + {name: "PATCH", method: http.MethodPatch, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}}, + {name: "DELETE", method: http.MethodDelete, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}}, + {name: "OPTIONS", method: http.MethodOptions, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}}, + {name: "HEAD", method: http.MethodHead, url: "/api/does-not-exist", status: http.StatusNotFound}, + {name: "nested path", method: http.MethodGet, url: "/api/does-not-exist/children", status: http.StatusNotFound, content: []string{notFound}}, + // The routes registered before keep precedence over the catch-all. + {name: "PocketBase route", method: http.MethodGet, url: "/api/health", status: http.StatusOK, + content: []string{`"message":"API is healthy."`}, absent: []string{`"Not found."`}}, + {name: "collection route", method: http.MethodGet, url: "/api/collections/entries/records", status: http.StatusOK, + absent: []string{`"Not found."`}}, + {name: "app route", method: http.MethodGet, url: "/api/export/entries.csv", status: http.StatusUnauthorized, + content: []string{`"status":401`}, absent: []string{`"Not found."`}}, + // A method the app route does not have is still an unknown route. + {name: "app route with another method", method: http.MethodGet, url: "/api/days/2026-05-11/close", status: http.StatusNotFound, + content: []string{notFound}}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + app, err := tests.NewTestApp() + if err != nil { + t.Fatal(err) + } + t.Cleanup(app.Cleanup) + tracking.Register(app) + registerUI(app) + (&tests.ApiScenario{ + Name: c.name, + Method: c.method, + URL: c.url, + ExpectedStatus: c.status, + ExpectedContent: c.content, + NotExpectedContent: c.absent, + TestAppFactory: func(testing.TB) *tests.TestApp { return app }, + DisableTestAppCleanup: true, + }).Test(t) + }) + } +}