Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support named parameters in URLs #163

Open
danascheider opened this issue Mar 7, 2023 · 2 comments
Open

Support named parameters in URLs #163

danascheider opened this issue Mar 7, 2023 · 2 comments
Labels
feature request New feature or request

Comments

@danascheider
Copy link

danascheider commented Mar 7, 2023

Hi team, I have a feature request that I'd be able to help implement if you're interested and can point me in the right direction as far as where to start. I need to be able to support named parameters in URLs:

Default.parameters = {
  mockData: [
    {
      url: 'http://localhost:3000/games/:id',
      method: 'DELETE',
      status: 204,
      response: null,
    },
  ],
}

This is a simple example that could potentially also be solved with a wildcard, but for more complex cases I'll need to be able to access the value of this parameter in a response function.

Is this functionality you'd be interested in seeing added? I may be a little out of my comfort zone contributing something like this but I'm happy to take a stab at it if you can suggest where to get started. I love your tool and I want to be able to keep using it!

@weeklyTea
Copy link

Would be really nice to have this feature!
Then code of stories can be improved from this:

const mockUsers = generateMockUsers(10) // generating 10 users.
const mockSubscriptions = generateSubscriptions(mockUsers, 4) // generating 4 subscriptions for every user

Default.parameters = {
  mockData: [
    ...(mockUsers.map(user => {
      const subscriptions = mockSubscriptions[user.id]
      return [
        {
          url: `/users/${user.id}`,
          method: 'GET',
          status: 200,
          response: user,
        },
        ...subscriptions.map(sub => ({
          url: `/users/${user.id}/subscription/${sub.id}`,
          method: 'GET',
          status: 200,
          response: sub,
        }))
      ]
    }).flat()),
  ]
}

to this:

const mockUsers = generateMockUsers(10) // generating 10 users.
const mockSubscriptions = generateSubscriptions(mockUsers, 4) // generating 4 subscriptions for every user

Default.parameters = {
  mockData: [
    {
      url: `/users/:userId`,
      method: 'GET',
      status: 200,
      response: request => {
        const userId = request.pathParams.userId
        return mockUsers.find(user => user.id === userId)
      },
    },
    {
      url: `/users/:userId/subscriptions/:subId`,
      method: 'GET',
      status: 200,
      response: request => {
        const userId = request.pathParams.userId
        const subId = request.pathParams.subId

        return mockSubscriptions[userId].find(sub => sub.id === subId)
      },
    }
  ]
}

@nutboltu nutboltu added the feature request New feature or request label Sep 20, 2023
@sr258
Copy link

sr258 commented Nov 20, 2023

As far as I can see, this feature is already partly implemented. You can use named parameters in URLs, however these parameters are not passed to the response function, which would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants