1 |
|
2 | var async, formats, json, jsonSchemaV4, represent, selectFormat, serializers;
|
3 |
|
4 | async = require('async');
|
5 |
|
6 | serializers = require('./serializers');
|
7 |
|
8 | selectFormat = require('./formatselection').selectFormat;
|
9 |
|
10 | jsonSchemaV4 = {
|
11 | lib: require('./formats/jsonschema-v4'),
|
12 | serialize: serializers.json
|
13 | };
|
14 |
|
15 | json = {
|
16 | lib: require('./formats/json'),
|
17 | serialize: serializers.json
|
18 | };
|
19 |
|
20 | formats = {
|
21 | 'application/schema+json': jsonSchemaV4,
|
22 | 'application/schema+json; profile="http://json-schema.org/schema"': jsonSchemaV4,
|
23 | 'application/schema+json; profile="http://json-schema.org/draft-04/schema"': jsonSchemaV4,
|
24 | 'application/json': json
|
25 | };
|
26 |
|
27 | represent = function(_arg, cb) {
|
28 | var ast, availableContentTypes, contentType;
|
29 | ast = _arg.ast, contentType = _arg.contentType;
|
30 | if (ast == null) {
|
31 | ast = {};
|
32 | }
|
33 | if (contentType == null) {
|
34 | contentType = 'application/schema+json';
|
35 | }
|
36 | availableContentTypes = Object.keys(formats);
|
37 | return selectFormat(contentType, availableContentTypes, function(err, selectedContentType) {
|
38 | var lib, serialize, _ref;
|
39 | if (err) {
|
40 | return cb(err);
|
41 | }
|
42 | if (!selectedContentType) {
|
43 | return cb(new Error("Content-Type '" + contentType + "' is not implemented."));
|
44 | }
|
45 | _ref = formats[selectedContentType], lib = _ref.lib, serialize = _ref.serialize;
|
46 | return async.waterfall([
|
47 | function(next) {
|
48 | return lib.transform(ast, next);
|
49 | }, function(obj, next) {
|
50 | return serialize(obj, next);
|
51 | }
|
52 | ], function(err, repr) {
|
53 | return cb(err, repr, selectedContentType);
|
54 | });
|
55 | });
|
56 | };
|
57 |
|
58 | module.exports = {
|
59 | represent: represent
|
60 | };
|