lukeed / freshie

(WIP) A fresh take on building universal applications with support for pluggable frontends and backends.
MIT License
155 stars 2 forks source link

Add `freshie/request` import #9

Closed lukeed closed 4 years ago

lukeed commented 4 years ago

Much like freshie/router -> navaid, re-export httpie as freshie/request or freshie/http -- no changes.

Benefit is that it'll include a universal HTTP client regardless of the SSR choice. The resolve.mainFields are already configured correctly to enable this.

Finally, if you don't use freshie/request, it won't be included at all. There are no internal assumptions made about what/how you're preloading. Opt-in usage would look like this:

import { send } from 'freshie/request';

export async function preload(req, context) {
  let res = await send('GET', '...', ...);
  return { ... }
}

See httpie@next for available methods

Another benefit to this is that because httpie throws (normalized) errors on 4xx responses, you don't have to deal with preload errors by default~! Your 4xx or xxx error template will automatically step in and send the correct error output. However, if you don't want this, you just handle the error manually, as you normally would (and can't avoid in other frameworks) anyway:

import { send } from 'freshie/request';

export async function preload(req, context) {
  try {
    let res = await send('GET', '...', '...');
    return { success: true };
  } catch (err) {
    // send different props to current view
    // instead of deferring to Error page output
    return { success: false };
  }
}