1 | /*
|
2 | * Copyright (c) 2012 VMware, Inc. All Rights Reserved.
|
3 | *
|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5 | * of this software and associated documentation files (the "Software"), to
|
6 | * deal in the Software without restriction, including without limitation the
|
7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
8 | * sell copies of the Software, and to permit persons to whom the Software is
|
9 | * furnished to do so, subject to the following conditions:
|
10 | *
|
11 | * The above copyright notice and this permission notice shall be included in
|
12 | * all copies or substantial portions of the Software.
|
13 | *
|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
20 | * IN THE SOFTWARE.
|
21 | */
|
22 |
|
23 | (function (define) {
|
24 | ;
|
25 |
|
26 | define(function (require) {
|
27 |
|
28 | var client, errorCode, mime, entity, pathPrefix, when, plugin;
|
29 |
|
30 | client = require('../rest');
|
31 | errorCode = require('./interceptor/errorCode');
|
32 | mime = require('./interceptor/mime');
|
33 | entity = require('./interceptor/entity');
|
34 | pathPrefix = require('./interceptor/pathPrefix');
|
35 | when = require('when');
|
36 |
|
37 |
|
38 | function parseConfig(name, refObj) {
|
39 | return {
|
40 | prefix: name,
|
41 | mime: refObj.mime,
|
42 | accept: refObj.accept,
|
43 | errorCode: refObj.errorCode,
|
44 | entity: refObj.entity
|
45 | };
|
46 | }
|
47 |
|
48 | /**
|
49 | * Builds the rest client for the provided config
|
50 | *
|
51 | * @param client the client to wrap
|
52 | * @param config configuration for client interceptors
|
53 | */
|
54 | function buildClient(client, config) {
|
55 | return when(client, function (client) {
|
56 | if (config.errorCode !== false) {
|
57 | client = errorCode(client, { code: config.errorCode });
|
58 | }
|
59 | if (config.mime !== false) {
|
60 | client = mime(client, { mime: config.mime || 'application/x-www-form-urlencoded', accept: config.accept });
|
61 | }
|
62 | if (config.entity !== false) {
|
63 | client = entity(client);
|
64 | }
|
65 | client = pathPrefix(client, { prefix: config.prefix });
|
66 | return client;
|
67 | });
|
68 | }
|
69 |
|
70 | /**
|
71 | * Resolves a 'rest' client for the specified path and scopes, e.g. client!url/to/resource
|
72 | *
|
73 | * @param resolver
|
74 | * @param name
|
75 | * @param refObj
|
76 | * @param wire
|
77 | */
|
78 | function resolveClient(resolver, name, refObj /*, wire */) {
|
79 | var config, client;
|
80 |
|
81 | config = parseConfig(name, refObj);
|
82 | client = buildClient(refObj.client, config);
|
83 |
|
84 | when(client, resolver.resolve, resolver.reject);
|
85 | }
|
86 |
|
87 | /**
|
88 | * The plugin instance. Can be the same for all wiring runs
|
89 | */
|
90 | plugin = {
|
91 | resolvers: {
|
92 | client: resolveClient
|
93 | }
|
94 | };
|
95 |
|
96 | return {
|
97 | wire$plugin: function restPlugin(/* ready, destroyed, options */) {
|
98 | return plugin;
|
99 | }
|
100 | };
|
101 |
|
102 | });
|
103 |
|
104 | }(
|
105 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
|
106 | // Boilerplate for AMD and Node
|
107 | ));
|