schultek/jaspr

feat: Ability to custom headers on render.

theachoem opened this issue · 2 comments

Description

Ability to override headers on server before response back to user. I think it could look something like this:

return serveApp((request, render) async {
  Map<String, String> headers = await setUserSession(request.headers);

  return render(
    headers: headers, <----
    server.Document(
      styles: styles,
      base: '/',
      body: App(),
    ),
  );
})

Once headers are passed to render, render can take those headers to construct and return a Response.

return Response(
  200,
  headers: headers,
  body: body,
)

Additional Context

What I requested above could be done through middleware.

My actual problem is on each page, when I request an API, token can be expired which needed to re-authenticate and set new the token to cookies / headers.

class HomeView extends StatefulComponent {
  const HomeView({super.key});

  @override
  State<HomeView> createState() => HomeViewState();
}

class HomeViewState extends State<HomeView> with PreloadStateMixin {
  Response? response;

  @override
  Future<void> preloadState() {
    response = await Dio().get('/api/v2/products').catchError((e) {
      if (expired(e)) {
        reauthenticate();
        setCookiesToHeadersBeforeResponseToUser(); <------ Is this possible?
      }
    });
  }

  @override
  Iterable<Component> build(BuildContext context) sync* {
    yield text(response)
  }
}

I no longer need this, serverpod cookie works perfectly with jaspr for this.