json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Intercepted PUT request and subsequent sorting

Open jasonpaulmichel opened this issue 4 years ago • 0 comments

I'm mocking our production server which for some legacy reasons are using a PUT request rather than a POST when creating a specific new piece of data.

I am intercepting that PUT request and changing the req.method to a POST and providing a mocked req.body. The data is being stored to the in memory database and can be sorted on some fields but for some reason is not being sorted on the updated_on field or title field.

Here's what I have:

  if (req.method === 'PUT' && req.originalUrl === '/v2/messages/announcements/') {
    req.method = 'POST';
    req.body = {
      id: 101,
      title: 'ZZ New Message',
      content: 'ZZ New Message Sentence',
      created_on: new Date().toISOString(),
      updated_on: new Date().toISOString(),
    };
  }
  next();
});```

I can sort by `content` and `id` not `created_on` or `updated_on`.

jasonpaulmichel avatar Oct 13 '21 15:10 jasonpaulmichel