UNPKG

1.31 kBJavaScriptView Raw
1/*
2 * Copyright 2012-2013 the original author or authors
3 * @license MIT, see LICENSE.txt for details
4 *
5 * @author Scott Andrews
6 */
7
8(function (define) {
9 'use strict';
10
11 define(function (require) {
12
13 var interceptor, UrlBuilder;
14
15 interceptor = require('../interceptor');
16 UrlBuilder = require('../UrlBuilder');
17
18 function startsWith(str, prefix) {
19 return str.indexOf(prefix) === 0;
20 }
21
22 function endsWith(str, suffix) {
23 return str.lastIndexOf(suffix) + suffix.length === str.length;
24 }
25
26 /**
27 * Prefixes the request path with a common value.
28 *
29 * @param {Client} [client] client to wrap
30 * @param {number} [config.prefix] path prefix
31 *
32 * @returns {Client}
33 */
34 return interceptor({
35 request: function (request, config) {
36 var path;
37
38 if (config.prefix && !(new UrlBuilder(request.path).isFullyQualified())) {
39 path = config.prefix;
40 if (request.path) {
41 if (!endsWith(path, '/') && !startsWith(request.path, '/')) {
42 // add missing '/' between path sections
43 path += '/';
44 }
45 path += request.path;
46 }
47 request.path = path;
48 }
49
50 return request;
51 }
52 });
53
54 });
55
56}(
57 typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
58 // Boilerplate for AMD and Node
59));