jaguar_cors 2.4.4

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 67

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 #

2.4.2 #

  • Added cors method.

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.4

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.4 Jan 9, 2019 Go to the documentation of jaguar_cors 2.4.4 Download jaguar_cors 2.4.4 archive
2.4.2 Jan 9, 2019 Go to the documentation of jaguar_cors 2.4.2 Download jaguar_cors 2.4.2 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

All 15 versions...

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

We analyzed this package on Apr 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, other

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

Health suggestions

Fix lib/src/options.dart. (-5.36 points)

Analysis of lib/src/options.dart reported 11 hints, including:

line 76 col 28: Use = to separate a named parameter from its default value.

line 77 col 26: Use = to separate a named parameter from its default value.

line 78 col 28: Use = to separate a named parameter from its default value.

line 79 col 27: Use = to separate a named parameter from its default value.

line 80 col 26: Use = to separate a named parameter from its default value.

Fix lib/src/jaguar_cors.dart. (-2.48 points)

Analysis of lib/src/jaguar_cors.dart reported 5 hints:

line 98 col 9: Use isEmpty instead of length

line 122 col 9: Use isEmpty instead of length

line 162 col 18: Use isNotEmpty instead of length

line 169 col 18: Use isNotEmpty instead of length

line 182 col 18: Use isNotEmpty instead of length

Maintenance suggestions

Homepage URL is insecure. (-5 points)

Update the homepage field and use a secure (https) URL.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and jaguar_cors.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
jaguar ^2.4.4 2.4.36
Transitive dependencies
auth_header 2.1.2
charcode 1.1.2
collection 1.14.11
convert 2.1.1
crypto 2.0.6
http_server 0.9.8+1
jaguar_common 2.1.4
jaguar_serializer 2.2.12
logging 0.11.3+2
meta 1.1.7
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