mockoon / commons-server Goto Github PK
View Code? Open in Web Editor NEWMockoon's commons library. Used in Mockoon desktop application and CLI.
Home Page: https://mockoon.com
License: MIT License
Mockoon's commons library. Used in Mockoon desktop application and CLI.
Home Page: https://mockoon.com
License: MIT License
Is your feature request related to a problem? Please describe.
I wanted to return an absolute path on a response, ideally it starts with the baseUrl where my current mockAPI is served on. The sample request response looks like this:
GET: http://localhost:3001/api/users/123
{
"id": "123"
"name": "Foobar",
"imageUrl": "http://localhost:3001/api/images/123"
}
Currently, there's no way to achieve this.
Describe the solution you'd like
Adding a baseUrl
will enable us to do this:
GET: /users/:userId
{
"id": "{{urlParam 'userId'}}"
"name": "Foobar",
"imageUrl": "{{baseUrl}}/images/{{urlParam 'userId'}}"
}
Describe alternatives you've considered
Tried using hostname etc..
Additional context
Discussion here: mockoon/mockoon#466
Describe the bug
when using the dateTimeShift template, the queryParam helper does not work inside it
To Reproduce
create GET route with endpoint '/api/setExpiry' and response body:
{
"ExpiryDate": "{{dateTimeShift date=(now 'yyyy-MM-dd') format='yyyy-MM-dd' months=(queryParam 'NoOfMonths')}}T11:59:15.043Z",
}
then run query: 'localhost:3000/api/setExpiry?NoOfMonths=24'
The response is the current date with no time shift.
Expected behavior
The response should be time shifted by 24 months.
something like "test": "{{#repeat (queryParam 'NoOfMonths') comma=true}}test{{/repeat}}" works as expected
Mockoon version:
v1.17.0
OS / OS version:
Windows 11
Is your feature request related to a problem? Please describe.
There is currently no way to define the number of digits after the decimal point.
Describe the solution you'd like
Implements Number.toFixed()
as a helper
An example: https://github.com/helpers/handlebars-helpers/blob/master/lib/number.js#L153
Describe the bug
When you start the npm task test, it will only run the tests inside the spec files which are inside a subfolder of test (like test/templating-helpers/helpers.spec.ts but not the spec files which are directly inside the test folder
To Reproduce
npm run test and check the executed tests
Expected behavior (solution)
All tests should be executed. An easy fix would be to create subfolders for the utils.spec.ts and the response-rules-interpreter.spec.ts
Mockoon version:
Server Version 2.10.0
OS / OS version:
Tested on Mac OS
Describe the bug
When proxying a call containing a file (multipart/form-data
) the proxied payload is wrongly converted to UTF-8 string content.
To Reproduce
Proxy to a server able to receive file upload, and call it through Mockoon's proxy with multipart/form-data
content type and a file. Uploaded file should be broken.
Expected behavior
Uploaded file should be intact.
Mockoon version:
all
OS / OS version:
all
Is your feature request related to a problem? Please describe.
Body
, queryParam
helpers doesn't work with each
. It is impossible to iterate on a body's or query string's array due to the stringification. It requires the use of another iterator like repeat
+ @index
.
Describe the solution you'd like
Stop stringifying arrays and objects by default and use the existing stringify
option when stringification is wanted. This would allow objects and arrays to be reused in combination with each
and potentially new helpers like length
, etc.
Describe alternatives you've considered
None seems available
Additional context
This is a small breaking change for the templating system. Some investigation/testing is needed to evaluate how much breaking changes we are talking about.
Is your feature request related to a problem? Please describe.
We have a floor and a ceil helpers, but no round
helper.
Describe the solution you'd like
Add a round
helper using Math.round()
.
Describe the bug
Fetching body's root array inside a loop doesn't work well with numbers as index 0 returns the full body.
To Reproduce
Call an endpoint with:
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Doe"
}
]
Body:
{{#repeat 2}}
{{body @index}}
{{/repeat}}
Receive this:
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Doe"
}
],
{
"id": 2,
"name": "Doe"
}
Expected behavior
It's working with concat, which is the expected behavior:
{{#repeat 2 }}
{{body (concat '' @index)}}
{{/repeat}}
Is your feature request related to a problem? Please describe.
We currently use a home made function to check if the header name does not contain any prohibited character. TestHeaderValidity
method https://github.com/mockoon/commons/blob/main/src/libs/utils.ts#L58 used here: https://github.com/mockoon/commons-server/blob/main/src/libs/server.ts#L695
Describe the solution you'd like
It may be more correct to rely on https://nodejs.org/dist/latest-v16.x/docs/api/http.html#httpvalidateheadernamename or silently fail.
Describe the bug
Helper switch
doesn't work with variable @index
from helper repeat
. It returns error:
Error while serving the content: (lookupProperty(...) || (data && lookupProperty(...)) || alias2).call is not a function
To Reproduce
[
{{#repeat 4}}
{
"id": {{@index}},
"name":
{{#switch (@index)}}
{{#case 0}}"John"{{/case}}
{{#case 1}}"Jack"{{/case}}
{{#default}}"Peter"{{/default}}
{{/switch}}
}
{{/repeat}}
]
Expected behavior
It should return follow response:
[
{
"id": 0,
"name": "John"
},
{
"id": 1,
"name": "Jack"
},
{
"id": 2,
"name": "Peter"
},
{
"id": 3,
"name": "Peter"
}
]
Mockoon version:
1.16.0
OS / OS version:
Windows 11
Describe the bug
Having empty rules with "AND" operator serves the corresponding response even if another response's rules match
To Reproduce
Expected behavior
We should get the second one
Mockoon version:
all
OS / OS version:
all
Describe the bug
Helpers returning SafeString like queryParam
doesn't work with dateTimeShift
helper and returns the following error:
"Error while serving the content: dateToShift.getDate is not a function"
To Reproduce
Try to serve {{dateTimeShift date=(queryParam 'date') days=1}}
Expected behavior
Helper should convert the SafeString to a string to create a date.
Mockoon version:
all
OS / OS version:
all
Is your feature request related to a problem? Please describe.
Using intensive/long helpers ({{faker 'random.alphaNumeric' 999999999}}
) block the main AND renderer processes. It prevents other calls to complete during the compute time.
Describe the solution you'd like
Template parsing should be done in a separated worker thread (see for example: https://blog.logrocket.com/node-js-multithreading-what-are-worker-threads-and-why-do-they-matter-48ab102f8b10/)
Is your feature request related to a problem? Please describe.
We cannot split a string with the current templating helpers
Describe the solution you'd like
Create a split
helper.
An example: https://github.com/helpers/handlebars-helpers/blob/master/lib/string.js#L547
It should split by default on spaces, delimiter could also be provided as second arg.
Describe the bug
All Faker.js images mock hosted on S3 returns a 403.
This is due to Faker.js maintainer's decision to stop working on the lib for free. S3 access was probably cut.
https://github.com/Marak/faker.js/issues/1054
Multiple solution: recreate these helpers in Mockoon, fork Faker.js and update the code, propose help to Faker.js maintainer.
To Reproduce
Use a faker helper like image.avatar
Expected behavior
Images should be available.
Mockoon version:
'>= 1.9.0
OS / OS version:
All
Describe the bug
When using split
in combination with a SafeString (e.g. from queryParam
), it will return an empty string.
To Reproduce
Call with /route?param1=123,456,789
{{#each (split (queryParam 'param1') ',')}} item{{this}}, {{/each}}
Expected behavior
We should get item123, item456, item789,
Mockoon version:
All
OS / OS version:
All
Sometimes you need more control of your response data following Handlebar Helpers are an good addition for this:
This can be achieved by adding simple Handlebars Functions
Describe the bug
Using the header
template helper without header name returns an error "name argument is required to req.get".
To Reproduce
Use the helper like this: {{header}}
or {{header ''}}
Expected behavior
the helper should fail silently and return an empty string if the name parameter is not present.
Describe the bug
bodyRaw
returns a string when no path is provided: https://github.com/mockoon/commons-server/blob/main/src/libs/templating-helpers/request-helpers.ts#L67
To Reproduce
Try to use bodyRaw
in an each
, with an entering request body array:
{{#each (bodyRaw)}}
{{this}}
{{/each}}
Expected behavior
The parsed JS object (request.parsedBody
) should be returned instead
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.