Skip to content

Mock API: matching, import and Studio fixes - #336

Merged
bryantgillespie merged 1 commit into
robl/mock-api-extensionfrom
bry/mock-api-improvements
Nov 6, 2026
Merged

bryantgillespie merged 1 commit into
robl/mock-api-extensionfrom
bry/mock-api-improvements

Conversation

@bryantgillespie

@bryantgillespie bryantgillespie commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up fixes for #333, from review. They don't block release (#335 is the one that does). Rob, take or leave any of these.

API and matching

  • A param now beats a trailing wildcard, whatever the order (engine.ts). Before, /files/* listed above /files/:id answered /files/1, even though the README says the most specific endpoint answers.
  • The endpoint Query field is read the same way as the request query (parseRequiredQuery now uses URLSearchParams). Two cases that never matched before:
    • name=Jane+Doe (+ is now read as a space).
    • token=abc== (the value was cut at the second =).
  • Import keeps a=x%26b%3Dy and a=x&b=y apart (endpointKey). They used to get the same key, so a merge import could overwrite the wrong endpoint.
  • Copy import no longer gives two collections the same prefix. Example: you have foo locally and the file has foo and foo-2. Both used to target foo-2, the unique constraint failed and the whole import rolled back.
  • The mock body is sent as written instead of being parsed and re-serialized. That keeps large IDs exact (12345678901234567890) and keeps the author's formatting.
  • Schema setup runs on init('app.after') instead of at hook load, so it no longer runs (and logs errors) in CLI commands like bootstrap and migrate. Checked on Directus 12: the collections are created at startup.
  • README notes that the request log is in memory and kept per instance.

Studio

  • Request log
    • Polling is sequential and stops reliably on unmount. Before, the interval leaked if you left the page during the first load.
    • One error toast per run of failures instead of a sticky toast every 3s.
    • Clear log handles errors.
    • Row keys are unique.
  • Collection page
    • Switching collections clears the endpoint list and ignores late responses, so you no longer see the previous collection's endpoints.
    • Closing the settings drawer discards its edits.
  • Endpoint page
    • The collection comes from the endpoint record, not the URL. A wrong collection ID in the URL redirects to the right one.
    • Late fetches are ignored when you switch endpoints.
    • The full URL is built from the API base URL instead of looking for an admin path segment.
  • Save as copy
    • It's blocked while response or active-response changes are unsaved. They used to be silently dropped.
    • The copy keeps the response order and picks the active response by position, not by name.
  • Import
    • The preview waits until typing stops and ignores out-of-order responses.
    • Replace asks for confirmation when it will delete collections.
    • Newly imported collections are selected for export.
  • Test request only sends to /mock/... paths. Before, it would send any method to any project path with your admin session.
  • Smaller fixes
    • The Delete buttons show a loading state, so a double click no longer sends two deletes.
    • Settings shows an error if it can't load.
    • The setup error clears on a later successful load. Removed the unused ready ref.
    • Home only redirects if you're still on the module root.

Screenshots

Replace import now asks before deleting:

replace import
Full video

replace confirm

Test request rejects paths outside /mock/. In the same screenshot, /files/:id answers over /files/* even though the wildcard is listed first:

test request blocked

Testing

  • New unit tests: wildcard vs param ranking, required query parsing, endpointKey encoding and order, and Copy prefix allocation.
  • pnpm test (38), pnpm validate, eslint and build all pass.
  • Ran it on a local Directus 12 (SQLite):
    • Schema created at startup.
    • Wildcard vs param, Jane+Doe query matching and the large-number body checked over HTTP.
    • Replace import confirm, Test request path check, and the redirect from a wrong collection ID checked in the browser.

Not done

  • Save as copy still makes several requests. If a later one fails, a partial copy is left behind. Fixing that needs a server-side copy endpoint.
  • No unsaved-changes guard on the "Creating collection" form, and no beforeunload warning on the endpoint page.
  • This branch doesn't include Mock API: escape response tokens so bodies stay valid JSON #335. The two touch different functions in engine.ts.

@robluton robluton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎸

@bryantgillespie
bryantgillespie merged commit 682eb81 into robl/mock-api-extension Nov 6, 2026
@bryantgillespie
bryantgillespie deleted the bry/mock-api-improvements branch November 6, 2026 17:26
bryantgillespie added a commit that referenced this pull request Nov 6, 2026
* move mock-api extension into directus labs

* fix misaligned text and add space below header bar

* Escape mock response tokens so bodies stay valid JSON (#335)

* Fix Mock API matching, import and Studio edge cases (#336)

---------

Co-authored-by: Bryant Gillespie <bryant@directus.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants