jaguar_cors 2.4.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 70

jaguar_cors

CORS interceptor for Jaguar.dart servers

Example

A complete example can be found here.

library main;

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

static const corsOptions = const CorsOptions(
  allowedOrigins: const ['http://mine.com'],
  allowAllHeaders: true,
  allowAllMethods: true);

Cors cors(Context ctx) => new Cors(corsOptions);

@Api(path: '/api')
class Routes extends Object with CorsHelper {
  @Route(methods: const ['GET', 'OPTIONS'])
  @WrapOne(cors)
  Response<String> get(Context ctx) {
    if(ctx.req.method != 'GET') return null;
    return Response.json('Hello foreigner!');
  }

  @Route(methods: const ['POST', 'OPTIONS'])
  @WrapOne(cors)
  Future<Response<String>> post(Context ctx) async {
    if(ctx.req.method != 'POST') return null;
    return Response.json(await ctx.req.bodyAsJson());
  }
}

main() async {
  final server = new Jaguar(port: 9000);
  server.addApi(reflect(new Routes()));
  server.log.onRecord.listen(print);
  await server.serve();
}

Docs

Configuring Cors interceptor

CorsOptions is used to configure Cors Interceptor.

  • allowedOrigins List<String>: A list of origins a cross-domain request can be executed from. Setting [allowAllOrigins] to [true] overrides [allowedOrigins] and allows all origins.
  • allowAllOrigins bool: Allows all origins. If set to true, overrides allowedOrigins option.
  • allowedMethods List<String>: A list of methods the client is allowed to use with cross-domain requests. Default value is simple methods (GET and POST).
  • allowAllMethods bool: Allows all methods. If set to true, overrides allowedMethods option.
  • allowedHeaders List<String>: A list of non simple headers the client is allowed to use with cross-domain requests.
  • allowAllHeaders bool: Allows all headers. If set to true, overrides allowedHeaders option.
  • exposeHeaders List<String>: Indicates which headers are safe to expose to the API of a CORS API specification
  • exposeAllHeaders bool: Exposes all headers. If set to true, overrides exposeHeaders option.
  • allowCredentials bool: Indicates whether the request can include user credentials like cookies, HTTP authentication or client side SSL certificates. The default is false.
  • maxAge int: Indicates how long (in seconds) the results of a preflight request can be cached. The default is 0 which stands for no max age.
  • vary bool:
  • allowNonCorsRequests bool: Should non-CORS requests be allowed?

See API documentation for more info.

Example

static const options = const CorsOptions(
      allowedOrigins: const ['http://mine.com'],
      // allowedHeaders: const ['X-Requested-With'],
      allowAllHeaders: true,
      allowAllMethods: true);

Wrapping the Cors interceptor

The Cors interceptor itself is simple to use. It accepts a CorsOptions object as configuration.

Note:

  1. Make sure that the route handler method/function accepts OPTIONS method.
  2. If same route handler method/function is used to handle both preflight and actual route, the actual route body is skipped during preflight requests
  @Route(methods: const ['GET', 'OPTIONS'])
  @WrapOne(cors)
  Response<String> get(Context ctx) {
    if(ctx.req.method != 'GET') return null;  // Skips for preflight requests
    return Response.json('Hello foreigner!');
  }

Example

	Cors cors(Context ctx) => new Cors(options);

  @Route(methods: const ['GET', 'OPTIONS'])
  @WrapOne(cors)
  Response<String> get(Context ctx) {
    if(ctx.req.method != 'GET') return null;
    return Response.json('Hello foreigner!');
  }

Changelog

0.5.3

  • Improved docs and README

0.5.2

  • Fixed allowHeaders bug

0.0.1

  • Initial version, created by Stagehand

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  jaguar_cors: ^2.4.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:jaguar_cors/jaguar_cors.dart';
  
Version Uploaded Documentation Archive
2.4.1 Sep 18, 2018 Go to the documentation of jaguar_cors 2.4.1 Download jaguar_cors 2.4.1 archive
2.1.2 Aug 8, 2018 Go to the documentation of jaguar_cors 2.1.2 Download jaguar_cors 2.1.2 archive
2.1.1 May 20, 2018 Go to the documentation of jaguar_cors 2.1.1 Download jaguar_cors 2.1.1 archive
1.3.1 Feb 2, 2018 Go to the documentation of jaguar_cors 1.3.1 Download jaguar_cors 1.3.1 archive
1.2.6 Jan 13, 2018 Go to the documentation of jaguar_cors 1.2.6 Download jaguar_cors 1.2.6 archive
1.1.5 Jan 11, 2018 Go to the documentation of jaguar_cors 1.1.5 Download jaguar_cors 1.1.5 archive
0.5.3 Sep 18, 2017 Go to the documentation of jaguar_cors 0.5.3 Download jaguar_cors 0.5.3 archive
0.5.2 Sep 18, 2017 Go to the documentation of jaguar_cors 0.5.2 Download jaguar_cors 0.5.2 archive
0.5.1 Jul 10, 2017 Go to the documentation of jaguar_cors 0.5.1 Download jaguar_cors 0.5.1 archive
0.5.0 Jun 6, 2017 Go to the documentation of jaguar_cors 0.5.0 Download jaguar_cors 0.5.0 archive

All 13 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
48
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, other

Primary library: package:jaguar_cors/jaguar_cors.dart with components: io.

Maintenance issues and suggestions

Fix analysis_options.yaml.

We were unable to parse analysis_options.yaml.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use jaguar_cors.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
jaguar ^2.4.4 2.4.5
Transitive dependencies
auth_header 2.1.2
charcode 1.1.2
collection 1.14.11
convert 2.0.2
crypto 2.0.6
http_server 0.9.8
jaguar_common 2.1.4
logging 0.11.3+2
mime 0.9.6+2
path 1.6.2
path_tree 2.2.2
stack_trace 1.9.3
typed_data 1.1.6
Dev dependencies
http any
jaguar_resty ^2.8.0
test any