miragejs / discuss

Ask questions, get help, and share what you've built with Mirage
MIT License
2 stars 0 forks source link

APIs I want to change #11

Open samselikoff opened 5 years ago

samselikoff commented 5 years ago

Wanted to start collecting some APIs I'm consider changing/breaking, just because they feel very outdated.

  1. Route handlers should have a single object as their sole argument, and schema and request should be destructurable properties from it.

    // before
    this.get('/users', (schema, request) => {
      // logic
    });
    
    // after
    this.get('/users', ({ schema, request }) => {
      // logic
    });

    We should also add db and perhaps other things.

  2. We have two route handler "helpers" that you currently access off of a route handler's this context: serialize and normalizedRequestAttrs. This is super confusing because it prevents you from being able to use fat arrow functions.

    // whoops! won't work.
    this.get('/users', (schema, request) => {
      let json = this.serialize(schema.users.all())
    });
    
    // now it works.
    this.get('/users', function(schema, request) {
      let json = this.serialize(schema.users.all())
    });

    This sucks. But now that we have destructuring we have a much better way to make these helpers available - injection!

    // wew \o/
    this.get('/users', ({ schema, request, serialize }) => {
      let json = serialize(schema.users.all())
    });

    No more this, just grab the helper from the argument if you need it! Now you can use fat arrows all the time.

  3. The Response API is kinda awkward and feels like a big step to take just to customize the response code. We have a weird shorthand version that can do it but it might be better to revisit this. If we make change 2, we could possibly inject a response function that could be used more easily.

bobisjan commented 5 years ago

👋, I like the proposals introduced here 👏.

I would suggest to make request API to be more fetch friendly to match with Request class as much as possible), eq. having request.headers to be an instance of Headers class will simplify dealing with headers like case insensitivity.

samselikoff commented 4 years ago

FYI: Transferred this to our Discuss repo, our new home for more open-ended conversations about Mirage!

If things become more concrete + actionable we can create a tracking issue in the main repo.