♻️ Answer unknown API routes with JSON and fix stale comments
The SPA catch-all answered unknown /api paths with the index page and status 200, so the client SDK failed to parse the body. A catch-all per HTTP method answers them with a JSON 404 in the error shape of PocketBase; the routes registered before keep precedence. The comment on DataFields no longer claims the fields of the read-only collections were client writable.
This commit is contained in:
@@ -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.
|
// ReadOnly collections are pulled by clients but only ever written by the server.
|
||||||
var ReadOnly = []string{"closed_days", "closed_weeks"}
|
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{
|
var DataFields = map[string][]string{
|
||||||
"entries": {"start_time", "end_time", "auto_stopped", "note", "day_key"},
|
"entries": {"start_time", "end_time", "auto_stopped", "note", "day_key"},
|
||||||
"closed_days": {"day_key", "worked_ms", "kind", "closed_at"},
|
"closed_days": {"day_key", "worked_ms", "kind", "closed_at"},
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"mime"
|
"mime"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/apis"
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
@@ -12,13 +13,32 @@ import (
|
|||||||
//go:embed all:web/build
|
//go:embed all:web/build
|
||||||
var webBuild embed.FS
|
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) {
|
func registerUI(app core.App) {
|
||||||
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
|
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
|
||||||
sub, err := fs.Sub(webBuild, "web/build")
|
sub, err := fs.Sub(webBuild, "web/build")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
notFound := func(e *core.RequestEvent) error {
|
||||||
|
return e.NotFoundError("Not found.", nil)
|
||||||
|
}
|
||||||
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
|
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...}") {
|
if !se.Router.HasRoute("GET", "/{path...}") {
|
||||||
se.Router.GET("/{path...}", apis.Static(sub, true))
|
se.Router.GET("/{path...}", apis.Static(sub, true))
|
||||||
}
|
}
|
||||||
|
|||||||
+62
@@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user