forked from traverson/traverson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverson.js
77 lines (67 loc) · 2.34 KB
/
traverson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
var minilog = require('./browser/lib/shim/log.js')
, errorModule = require('./lib/errors')
, errors = errorModule.errors
, createError = errorModule.createError
, mediaTypes = require('./lib/media_types')
, Builder = require('./lib/builder')
, mediaTypeRegistry = require('./lib/media_type_registry');
// activate this line to enable logging
if (process.env.TRAVERSON_LOGGING) {
require('./browser/lib/shim/log.js').enable();
}
// export builder for traverson-angular
exports._Builder = Builder;
/**
* Creates a new request builder instance.
*/
exports.newRequest = function newRequest() {
return new Builder();
};
/**
* Creates a new request builder instance with the given root URL.
*/
exports.from = function from(url) {
var builder = new Builder();
builder.from(url);
return builder;
};
// Provided for backward compatibility with pre-1.0.0 versions.
// The preferred way is to use newRequest() or from() to create a request
// builder and either set the media type explicitly by calling json() on the
// request builder instance - or use content negotiation.
exports.json = {
from: function(url) {
var builder = new Builder();
builder.from(url);
builder.setMediaType(mediaTypes.JSON);
return builder;
}
},
// Provided for backward compatibility with pre-1.0.0 versions.
// The preferred way is to use newRequest() or from() to create a request
// builder and then either set the media type explicitly by calling jsonHal() on
// the request builder instance - or use content negotiation.
exports.jsonHal = {
from: function(url) {
if (!mediaTypeRegistry.get(mediaTypes.JSON_HAL)) {
throw createError('JSON HAL adapter is not registered. From version ' +
'1.0.0 on, Traverson has no longer built-in support for ' +
'application/hal+json. HAL support was moved to a separate, optional ' +
'plug-in. See https://github.com/traverson/traverson-hal',
errors.UnsupportedMediaType
);
}
var builder = new Builder();
builder.from(url);
builder.setMediaType(mediaTypes.JSON_HAL);
return builder;
}
};
// expose media type registry so that media type plug-ins can register
// themselves
exports.registerMediaType = mediaTypeRegistry.register;
// re-export media type constants
exports.mediaTypes = mediaTypes;
// re-export error names
exports.errors = errors;