forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime.js
101 lines (86 loc) · 3.07 KB
/
mime.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
/*
* Copyright 2012-2014 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
(function (define) {
'use strict';
define(function (require) {
var interceptor, registry, plainText, when;
interceptor = require('../interceptor');
registry = require('../mime/registry');
when = require('when');
plainText = registry.lookup('text/plain');
/**
* MIME type support for request and response entities. Entities are
* (de)serialized using the converter for the MIME type.
*
* Request entities are converted using the desired converter and the
* 'Accept' request header prefers this MIME.
*
* Response entities are converted based on the Content-Type response header.
*
* @param {Client} [client] client to wrap
* @param {string} [config.mime='text/plain'] MIME type to encode the request
* entity
* @param {string} [config.accept] Accept header for the request
* @param {Client} [config.client=<request.originator>] client passed to the
* serializer, defaults to the client originating the request
* @param {Registry} [config.registry] MIME registry, defaults to the root
* registry
*
* @returns {Client}
*/
return interceptor({
init: function (config) {
config.registry = config.registry || registry;
return config;
},
request: function (request, config) {
var mime, headers;
headers = request.headers || (request.headers = {});
mime = headers['Content-Type'] = headers['Content-Type'] || config.mime || 'text/plain';
headers.Accept = headers.Accept || config.accept || mime + ', application/json;q=0.8, text/plain;q=0.5, */*;q=0.2';
if (!('entity' in request)) {
return request;
}
return config.registry.lookup(mime).then(function (serializer) {
var client = config.client || request.originator;
return when.attempt(serializer.write, request.entity, { client: client, request: request })
.otherwise(function() {
throw 'mime-serialization';
})
.then(function(entity) {
request.entity = entity;
return request;
});
}, function () {
throw 'mime-unknown';
});
},
response: function (response, config) {
if (!(response.headers && response.headers['Content-Type'] && response.entity)) {
return response;
}
var mime = response.headers['Content-Type'];
return config.registry.lookup(mime).otherwise(function () { return plainText; }).then(function (serializer) {
var client = config.client || response.request && response.request.originator;
return when.attempt(serializer.read, response.entity, { client: client, response: response })
.otherwise(function (e) {
response.error = 'mime-deserialization';
response.cause = e;
throw response;
})
.then(function (entity) {
response.entity = entity;
return response;
});
});
}
});
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));