Skip to content

Add "status" request helper#894

Merged
255kb merged 5 commits intomockoon:feature/894-status-helperfrom
dashie:main
Jan 11, 2023
Merged

Add "status" request helper#894
255kb merged 5 commits intomockoon:feature/894-status-helperfrom
dashie:main

Conversation

@dashie
Copy link
Contributor

@dashie dashie commented Dec 17, 2022

Technical implementation details

I want to propose this new helper. The impact is very little and it is simple to be used, but can be very useful in many cases.
The idea is to be able to set the response status inside the template.

Imagine a route like this:

test/:id

and a data bucket with name testdata and the following content:

{
  "data": {
    "001": {
      "id": "001",
      "name": "Item 1"
    },
    "002": {
      "id": "002",
      "name": "Item 2"
    }
  }
}

now using a template like this:

{{setVar 'selection' (concat 'data.' (urlParam 'id'))}}
{{#if (dataRaw 'testdata' @selection true)}}
{{data 'testdata' @selection}}
{{else}}
{{status 404}}
{
  "error": "NOT_EXISTS"
}
{{/if}}

we have a routes where we can return 200 with data if the data exists in the bucket

GET /test/001

or 404 if the data does not exist.

GET /test/003

To be able to check the path selection I've also added an optional flag on dataRaw helper, that return an empty string "" when the selection path does not match any property.

If you accept this proposal I can finish it with tests and docs :P

Checklist

  • data migration added (@mockoon/commons)
  • data migration automated tests added (@mockoon/commons)
  • CLI automated tests added (@mockoon/cli)
  • desktop automated tests added (@mockoon/desktop)

@dashie dashie requested a review from 255kb as a code owner December 17, 2022 10:46
@255kb
Copy link
Member

255kb commented Dec 20, 2022

Thank you for opening this PR.
I have mixed feelings regarding this feature. As stated in #893, CRUD endpoints will have this kind of behavior. They will be linked to a data bucket and return a 404 when something is missing. The implementation will be similar to what json-server does.
Introducing this status helper is clever, but I have the feeling that it's a bit redundant with the CRUD routes (future) implementation.
Anyway, the feature is kind of hidden in a helper and it's not adding more complexity to the UI or anything like this.
So, I guess it can be added 😄.

I will review and probably add some remarks. I saw that the linter is failing. Maybe correct this first so we can run all the tests.

@255kb 255kb added the feature label Dec 20, 2022
@255kb
Copy link
Member

255kb commented Jan 11, 2023

I realized that this helper is not compatible with the data buckets as they are generated during server start, or during the first call.
I will finalize this PR, and add some documentation.
It will be part of the release in a few days.

@255kb 255kb changed the base branch from main to feature/894-status-helper January 11, 2023 09:42
@255kb 255kb merged commit 03942f4 into mockoon:feature/894-status-helper Jan 11, 2023
255kb pushed a commit to mockoon/mockoon.com that referenced this pull request Jan 11, 2023
255kb pushed a commit that referenced this pull request Jan 11, 2023
* Add "status" request helper (#894)

* Add "status" request helper

* test added

* test null condition flag on dataRaw helper

* fix minor lint issue

* set status code from template on sendFile

* Remove data helper modifications from PR

* Move response helpers in separated file, use express response locals

* fix test

Co-authored-by: Dashie <dashie@users.noreply.github.com>
255kb pushed a commit to mockoon/mockoon.com that referenced this pull request Jan 12, 2023
- Remove old docs and add images for latest version
- Update release changelog
- Add doc for mockoon/mockoon#873
- Add details about faker.js seeding
- Add folders doc
- Add serverless infos
- Add doc for mockoon/mockoon#894
- Add docs for query params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants