[GH-ISSUE #1308] [Question] Map REST API when resource could be a path #1302

Open
opened 2026-03-03 19:50:15 +03:00 by kerem · 7 comments
Owner

Originally created by @esdrasdl on GitHub (Jul 25, 2022).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1308

Originally assigned to: @NghiaTranUIT on GitHub.

Proxyman version?

3.7.0

macOS Version?

12.04

Expected behavior

I would like to do the following map using Map Local (Directory).

  • mydomain.com/path/to/resource/resources
    response: any json, could be a list of resources
  • mydomain.com/path/to/resource/resources/item
    response: any json

Even the mac doesn't allow a folder and file with the same name I tried to create a file as item?param=1 but it didn't work. How can I achieve this behavior?

Originally created by @esdrasdl on GitHub (Jul 25, 2022). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1308 Originally assigned to: @NghiaTranUIT on GitHub. ### Proxyman version? 3.7.0 ### macOS Version? 12.04 ### Expected behavior I would like to do the following map using Map Local (Directory). - mydomain.com/path/to/resource/resources response: any json, could be a list of resources - mydomain.com/path/to/resource/resources/item response: any json Even the mac doesn't allow a folder and file with the same name I tried to create a file as `item?param=1` but it didn't work. How can I achieve this behavior?
Author
Owner

@NghiaTranUIT commented on GitHub (Jul 26, 2022):

Proxyman has supported the resource path without the extension. Ref: https://github.com/ProxymanApp/Proxyman/issues/842

Here is a sample config:

  1. Create a Map Local Rule, URL = "https://mydomain.com"
  2. Map Directory to ~/Desktop/local_folder

Map Local Resolve path:

  • https://mydomain.com/path/to/resource/resources -> File: ~/Desktop/local_folder/path/to/resource/resources
    => The last resource path is a File without extension.
  • https://mydomain.com/path/to/resource/item -> File: ~/Desktop/local_folder/path/to/resource/item
    => item is a File without extension.

If it doesn't work, it'd be great if you can share your map local configuration 👍

<!-- gh-comment-id:1195044682 --> @NghiaTranUIT commented on GitHub (Jul 26, 2022): Proxyman has supported the resource path without the extension. Ref: https://github.com/ProxymanApp/Proxyman/issues/842 Here is a sample config: 1. Create a Map Local Rule, URL = "https://mydomain.com" 2. Map Directory to `~/Desktop/local_folder` Map Local Resolve path: - `https://mydomain.com/path/to/resource/resources` -> File: `~/Desktop/local_folder/path/to/resource/resources` => The last `resource` path is a File without extension. - `https://mydomain.com/path/to/resource/item` -> File: `~/Desktop/local_folder/path/to/resource/item` => `item` is a File without extension. ---------- If it doesn't work, it'd be great if you can share your map local configuration 👍
Author
Owner

@esdrasdl commented on GitHub (Jul 26, 2022):

In your example, you setup two different paths, that works as expected
~/Desktop/local_folder/path/to/resource/resources
~/Desktop/local_folder/path/to/resource/item

But my case is a little bit different:
~/Desktop/local_folder/path/to/resource/resources
~/Desktop/local_folder/path/to/resource/resources/item

But I could solve it using a script:

async function onResponse(context, url, request, response) {
  console.log(response);
  if (request.path == "/path/to/resource/resources") {
    response.bodyFilePath = "~/Desktop/local_folder/path/to/resource/resources.json"
  }

  return response;
}

Which leads me to another question
I would like to share this script with my coworkers. But to setup the file path I need to setup relative path beginning in my user folder (~/). So every time my coworkes would like to use the script, they need change the filePath. It could be solved using environment variable. What is the roadmap to add Environment Variable to proxyman?
With environment variable I can improve the script like that:

async function onResponse(context, url, request, response) {
  console.log(response);
  if (request.path == "/path/to/resource/resources") {
    response.bodyFilePath = $USER_PATH+"/path/to/resource/resources.json"
  }

  return response;
}
<!-- gh-comment-id:1195435223 --> @esdrasdl commented on GitHub (Jul 26, 2022): In your example, you setup two different paths, that works as expected ~/Desktop/local_folder/path/to/resource/resources ~/Desktop/local_folder/path/to/resource/item But my case is a little bit different: ~/Desktop/local_folder/path/to/resource/resources ~/Desktop/local_folder/path/to/resource/resources/item But I could solve it using a script: ``` async function onResponse(context, url, request, response) { console.log(response); if (request.path == "/path/to/resource/resources") { response.bodyFilePath = "~/Desktop/local_folder/path/to/resource/resources.json" } return response; } ``` Which leads me to another question I would like to share this script with my coworkers. But to setup the file path I need to setup relative path beginning in my user folder (~/). So every time my coworkes would like to use the script, they need change the filePath. It could be solved using environment variable. What is the roadmap to add Environment Variable to proxyman? With environment variable I can improve the script like that: ``` async function onResponse(context, url, request, response) { console.log(response); if (request.path == "/path/to/resource/resources") { response.bodyFilePath = $USER_PATH+"/path/to/resource/resources.json" } return response; } ```
Author
Owner

@NghiaTranUIT commented on GitHub (Jul 26, 2022):

Nice, it can be easily solved by using Scripting.

$USER_PATH is not supported yet. However, ~ is already working. It will expand on the different user names on runtime.
So, the following code will work on your coworker too.

response.bodyFilePath = "~/Desktop/local_folder/path/to/resource/resources.json"

So every time my coworkes would like to use the script, they need change the filePath.

I'm not sure why your coworkers need to change the file path?

<!-- gh-comment-id:1195631704 --> @NghiaTranUIT commented on GitHub (Jul 26, 2022): Nice, it can be easily solved by using Scripting. `$USER_PATH` is not supported yet. However, `~` is already working. It will expand on the different user names on runtime. So, the following code will work on your coworker too. ``` response.bodyFilePath = "~/Desktop/local_folder/path/to/resource/resources.json" ``` > So every time my coworkes would like to use the script, they need change the filePath. I'm not sure why your coworkers need to change the file path?
Author
Owner

@esdrasdl commented on GitHub (Jul 26, 2022):

I want to put all my mocks in a Github project. Usually every developer creates your own folder to handle his projects.

Let's says that I have project called proxyman-mapper. Inside that project/folder I put the json on this path:
"proxyman-mapper/path/to/resource/resources.json". Everybody who wants to use must clone this repository. Someone could have ~/git/proxyman-mapper and the other one could use ~/git/projects/p-mapper. Everybody needs to change the path to your current location and be aware to not commit that change if he wants to commit anything.

With environment variable I can export the script together with this project and my coworkers only need to create a variable that fits their path.

<!-- gh-comment-id:1195883518 --> @esdrasdl commented on GitHub (Jul 26, 2022): I want to put all my mocks in a Github project. Usually every developer creates your own folder to handle his projects. Let's says that I have project called proxyman-mapper. Inside that project/folder I put the json on this path: "proxyman-mapper/path/to/resource/resources.json". Everybody who wants to use must clone this repository. Someone could have ~/git/proxyman-mapper and the other one could use ~/git/projects/p-mapper. Everybody needs to change the path to your current location and be aware to not commit that change if he wants to commit anything. With environment variable I can export the script together with this project and my coworkers only need to create a variable that fits their path.
Author
Owner

@NghiaTranUIT commented on GitHub (Jul 27, 2022):

Thanks. I understand your problem @esdrasdl 👍

I will add logic to get the $USER_PATH from env on runtime, and send you a Beta build soon 👍

<!-- gh-comment-id:1196169977 --> @NghiaTranUIT commented on GitHub (Jul 27, 2022): Thanks. I understand your problem @esdrasdl 👍 I will add logic to get the `$USER_PATH` from env on runtime, and send you a Beta build soon 👍
Author
Owner

@NghiaTranUIT commented on GitHub (Jul 28, 2022):

Hey @esdrasdl let's try this beta build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.7.0_Support_Scripting_with_ENV.dmg

Changelog

  • Proxyman now can access system env as we've discussed 👍
  • Proxyman will load the system env if it's enabled. It's disabled by default.
  • You can define a env on ~/.zshrc: , like export USER_PATH=MyPath/proxyman/file-mapper
  • If you change the env, please reload to get the new value.

Screenshot

Screen_Shot_2022-07-28_at_09_14_11

<!-- gh-comment-id:1197579796 --> @NghiaTranUIT commented on GitHub (Jul 28, 2022): Hey @esdrasdl let's try this beta build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.7.0_Support_Scripting_with_ENV.dmg ### Changelog - Proxyman now can access system env as we've discussed 👍 - Proxyman will load the system env if it's enabled. It's disabled by default. - You can define a env on `~/.zshrc`: , like `export USER_PATH=MyPath/proxyman/file-mapper` - If you change the env, please reload to get the new value. ### Screenshot ![Screen_Shot_2022-07-28_at_09_14_11](https://user-images.githubusercontent.com/5878421/181407082-6c674696-367f-4821-aeda-266897ea6fbc.jpg)
Author
Owner

@NghiaTranUIT commented on GitHub (Jul 31, 2022):

Here is a v2 for this feature: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.7.0_Support_ENV_for_scripting_v2.dmg

  • Show the correct error message when the ENV is missing.
<!-- gh-comment-id:1200343838 --> @NghiaTranUIT commented on GitHub (Jul 31, 2022): Here is a v2 for this feature: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.7.0_Support_ENV_for_scripting_v2.dmg - Show the correct error message when the ENV is missing.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Proxyman#1302
No description provided.