UNPKG

3.39 kBJavaScriptView Raw
1'use strict';
2const _ = require('lodash'),
3 nodeUrl = require('url'),
4 replace = require('string-replace-async'),
5 h = require('highland'),
6 b64 = require('base-64'),
7 types = require('./types');
8
9/**
10 * add prefixes
11 * @param {object} dispatch
12 * @param {string} prefix
13 * @returns {Stream}
14 */
15function add(dispatch, prefix) {
16 const stringDispatch = JSON.stringify(dispatch);
17
18 let urlPrefix = prefix;
19
20 if (_.includes(prefix, 'http')) {
21 // make sure it's a uri
22 prefix = urlToUri(prefix);
23 }
24
25 return h(replace(stringDispatch, /"\/_?(components|uris|pages|lists|users|layouts)(\/[\w-\/]*)/g, (match, type, name) => {
26 if (type === 'uris') {
27 return Promise.resolve(`"${prefix}/_${type}/${b64.encode(prefix + name)}`);
28 } else {
29 return Promise.resolve(`"${prefix}/_${type}${name}`);
30 }
31 }).then((prefixedString) => replace(prefixedString, /"customUrl":"(.*)"/g, (match, uri) => Promise.resolve(`"customUrl":"${urlPrefix}${uri}"`)))).map(JSON.parse);
32}
33
34/**
35 * remove prefixes
36 * @param {object} dispatch
37 * @param {string} prefix
38 * @return {Stream}
39 */
40function remove(dispatch, prefix) {
41 const stringDispatch = JSON.stringify(dispatch);
42
43 let urlPrefix = prefix;
44
45 if (_.includes(prefix, 'http')) {
46 // make sure it's a uri
47 prefix = urlToUri(prefix);
48 }
49
50 return h(replace(stringDispatch, new RegExp(`"${prefix}\/_?(components|uris|pages|lists|users|layouts)/(.+?)"`, 'g'), (match, type, end) => {
51 if (type === 'uris') {
52 return Promise.resolve(`"/_${type}${b64.decode(end).replace(prefix, '')}"`);
53 } else {
54 return Promise.resolve(`"/_${type}/${end}"`);
55 }
56 }).then((unprefixedString) => replace(unprefixedString, /"customUrl":"(.*)"/g, (match, prefixedURI) => Promise.resolve(`"customUrl":"${prefixedURI.replace(urlPrefix, '')}"`)))).map(JSON.parse);
57}
58
59/**
60 * get site prefix from url
61 * note: only works on api routes
62 * @param {string} url
63 * @return {string}
64 */
65function getFromUrl(url) {
66 let type = _.find(types, (t) => _.includes(url, t));
67
68 if (type) {
69 return url.slice(0, url.indexOf(type));
70 } else {
71 throw new Error(`Unable to find site prefix for ${url}`);
72 }
73}
74
75/**
76 * convert uri to url, using prefix provided
77 * @param {string} prefix
78 * @param {string} uri
79 * @return {string}
80 */
81function uriToUrl(prefix, uri) {
82 let type = _.find(types, (t) => _.includes(uri, t)),
83 parts = uri.split(type),
84 path = parts[1];
85
86 return `${prefix}${type}${path}`;
87}
88
89/**
90 * convert url to uri
91 * and removes extension
92 * @param {string} url
93 * @return {string}
94 */
95function urlToUri(url) {
96 const parts = nodeUrl.parse(url);
97
98 let path;
99
100 if (parts.pathname === '/') {
101 path = '';
102 } else if (_.includes(parts.pathname, '.')) {
103 path = parts.pathname.slice(0, parts.pathname.indexOf('.'));
104 } else {
105 path = parts.pathname;
106 }
107
108 return parts.hostname + path;
109}
110
111/**
112 * get extension from url
113 * @param {string} url
114 * @return {string|null} e.g. '.json' or '.html'
115 */
116function getExt(url) {
117 const parts = nodeUrl.parse(url);
118
119 if (_.includes(parts.pathname, '.')) {
120 return parts.pathname.slice(parts.pathname.indexOf('.'));
121 } else {
122 return null;
123 }
124}
125
126module.exports.add = add;
127module.exports.remove = remove;
128module.exports.getFromUrl = getFromUrl;
129module.exports.uriToUrl = uriToUrl;
130module.exports.urlToUri = urlToUri;
131module.exports.getExt = getExt;