CORS interceptor for Jaguar.dart servers
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();
}
CorsOptions is used to configure Cors
Interceptor.
List<String>
: A list of origins a cross-domain request can be executed from. Setting [allowAllOrigins]
to [true] overrides [allowedOrigins] and allows all origins.bool
: Allows all origins. If set to true
, overrides allowedOrigins option. List<String>
: A list of methods the client is allowed to use with cross-domain requests. Default
value is simple methods (GET
and POST
).bool
: Allows all methods. If set to true
, overrides allowedMethods option. List<String>
: A list of non simple headers the client is allowed to use with cross-domain requests.bool
: Allows all headers. If set to true
, overrides allowedHeaders option. List<String>
: Indicates which headers are safe to expose to the API of a CORS API specificationbool
: Exposes all headers. If set to true
, overrides exposeHeaders option. bool
: Indicates whether the request can include user credentials like cookies, HTTP authentication
or client side SSL certificates. The default is false
.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.bool
: bool
: Should non-CORS requests be allowed?See API documentation for more info.
static const options = const CorsOptions(
allowedOrigins: const ['http://mine.com'],
// allowedHeaders: const ['X-Requested-With'],
allowAllHeaders: true,
allowAllMethods: true);
The Cors
interceptor itself is simple to use. It accepts a CorsOptions
object as configuration.
Note:
@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!');
}
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!');
}
cors
method.allowHeaders
bugAdd this to your package's pubspec.yaml file:
dependencies:
jaguar_cors: ^2.4.4
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.
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 |
|
|
2.4.2 | Jan 9, 2019 |
|
|
2.4.1 | Sep 18, 2018 |
|
|
2.1.2 | Aug 8, 2018 |
|
|
2.1.1 | May 20, 2018 |
|
|
1.3.1 | Feb 2, 2018 |
|
|
1.2.6 | Jan 13, 2018 |
|
|
1.1.5 | Jan 11, 2018 |
|
|
0.5.3 | Sep 18, 2017 |
|
|
0.5.2 | Sep 18, 2017 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
50
|
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]
|
72
|
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, other
Primary library:
package:jaguar_cors/jaguar_cors.dart
with components:io
.
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
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.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
jaguar | ^2.4.4 | 2.4.26 | |
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.8 | ||
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 |