jaguar_mux 0.5.0 copy "jaguar_mux: ^0.5.0" to clipboard
jaguar_mux: ^0.5.0 copied to clipboard

outdatedDart 1 only

A isomorphic fluent route muxer for Jaguar

jaguar_mux #

An isomorphic fluent route muxer for Jaguar

Example #

library jaguar_mux.example.simple.server;

import 'dart:async';
import 'package:jaguar/jaguar.dart';
import 'package:jaguar_mux/jaguar_mux.dart';

class SimpleInterceptor extends Interceptor {
  final String name;

  SimpleInterceptor(this.name);

  String pre(Context ctx) => 'input';

  void post(Context ctx, Response resp) => print('post $name');
}

Map versionRoute(Context ctx) => {'v': 0.1};

int getUserById(Context ctx) => ctx.pathParams.getInt('id');

Response<String> getQueryParams(Context ctx) {
  String input = ctx.getInput(SimpleInterceptor);
  String hello = ctx.req.headers.value('hello');
  String name = ctx.queryParams['name'];
  String password = ctx.queryParams['password'];

  return Response.json({
    'n': name,
    'p': password,
    'h': hello,
    'i': input,
  });
}

Future<Response<String>> decodeJsonMap(Context ctx) async {
  Map body = await ctx.req.bodyAsJsonMap();
  return Response.json(body);
}

class ExceptionHandler1 extends ExceptionHandler<Exception> {
  const ExceptionHandler1();

  Response<String> onRouteException(
      Request request, Exception e, StackTrace trace) {
    return Response.json({
      "Message": e.toString(),
    });
  }
}

main() async {
  final muxer = new MuxBuilder();

  muxer.get('/encode/maptojson', (Context ctx) => {'name': 'jaguar'});

  muxer.get('/wrap/simpleinterceptor',
      (Context ctx) => Response.json({'name': 'jaguar'}));

  muxer.get('/user/:id', getUserById);

  muxer.get('/version', versionRoute);

  muxer
      .get('/query/params', getQueryParams)
      .wrap((Context ctx) => new SimpleInterceptor('hello'));

  muxer.post('/decode/json/map', decodeJsonMap);

  muxer.get('/exception', (Context ctx) {
    throw new Exception("Holy fuck!");
  }).onException(const ExceptionHandler1());

  Mux mux = muxer.build();

  Jaguar configuration = new Jaguar();
  configuration.addApi(mux);

  await configuration.serve();
}
0
likes
5
points
23
downloads

Publisher

unverified uploader

Weekly Downloads

A isomorphic fluent route muxer for Jaguar

Homepage

License

unknown (license)

Dependencies

jaguar, jaguar_reflect

More

Packages that depend on jaguar_mux