This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.js
104 lines (88 loc) · 2.69 KB
/
registry.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2012-2016 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
'use strict';
var mime, registry;
mime = require('../mime');
function Registry(mimes) {
/**
* Lookup the converter for a MIME type
*
* @param {string} type the MIME type
* @return a promise for the converter
*/
this.lookup = function lookup(type) {
var parsed;
parsed = typeof type === 'string' ? mime.parse(type) : type;
if (mimes[parsed.raw]) {
return mimes[parsed.raw];
}
if (mimes[parsed.type + parsed.suffix]) {
return mimes[parsed.type + parsed.suffix];
}
if (mimes[parsed.type]) {
return mimes[parsed.type];
}
if (mimes[parsed.suffix]) {
return mimes[parsed.suffix];
}
return Promise.reject(new Error('Unable to locate converter for mime "' + parsed.raw + '"'));
};
/**
* Create a late dispatched proxy to the target converter.
*
* Common when a converter is registered under multiple names and
* should be kept in sync if updated.
*
* @param {string} type mime converter to dispatch to
* @returns converter whose read/write methods target the desired mime converter
*/
this.delegate = function delegate(type) {
return {
read: function () {
var args = arguments;
return this.lookup(type).then(function (converter) {
return converter.read.apply(this, args);
}.bind(this));
}.bind(this),
write: function () {
var args = arguments;
return this.lookup(type).then(function (converter) {
return converter.write.apply(this, args);
}.bind(this));
}.bind(this)
};
};
/**
* Register a custom converter for a MIME type
*
* @param {string} type the MIME type
* @param converter the converter for the MIME type
* @return a promise for the converter
*/
this.register = function register(type, converter) {
mimes[type] = Promise.resolve(converter);
return mimes[type];
};
/**
* Create a child registry whoes registered converters remain local, while
* able to lookup converters from its parent.
*
* @returns child MIME registry
*/
this.child = function child() {
return new Registry(Object.create(mimes));
};
}
registry = new Registry({});
// include provided serializers
registry.register('application/hal', require('./type/application/hal'));
registry.register('application/json', require('./type/application/json'));
registry.register('application/x-www-form-urlencoded', require('./type/application/x-www-form-urlencoded'));
registry.register('multipart/form-data', require('./type/multipart/form-data'));
registry.register('text/plain', require('./type/text/plain'));
registry.register('+json', registry.delegate('application/json'));
module.exports = registry;