@tryghost/url-utils 1.1.4 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +8 -67
  2. package/package.json +5 -3
package/lib/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  // Contains all path information to be used throughout the codebase.
2
2
  const _ = require('lodash');
3
- const {URL} = require('url');
4
3
  const utils = require('./utils');
5
4
 
6
5
  // similar to Object.assign but will not override defaults if a source value is undefined
@@ -16,8 +15,9 @@ module.exports = class UrlUtils {
16
15
  /**
17
16
  * Initialization method to pass in URL configurations
18
17
  * @param {Object} options
19
- * @param {String} options.url Ghost instance blog URL
20
- * @param {String} options.adminUrl Ghost instance admin URL
18
+ * @param {Function} options.getSubdir
19
+ * @param {Function} options.getSiteUrl
20
+ * @param {Function} options.getAdminUrl Ghost instance admin URL
21
21
  * @param {Object} options.apiVersions configuration object which has defined `all` property which is an array of keys for other available properties
22
22
  * @param {('v2' | 'v3' | 'v4' | 'canary')} [options.defaultApiVersion] default API version which is one of the values from options.apiVersions
23
23
  * @param {('content' | 'admin')} [options.defaultApiType] default API type to be used and is one of the values from options.apiVersions
@@ -28,8 +28,6 @@ module.exports = class UrlUtils {
28
28
  */
29
29
  constructor(options = {}) {
30
30
  const defaultOptions = {
31
- url: null,
32
- adminUrl: null,
33
31
  apiVersions: null,
34
32
  slugs: null,
35
33
  redirectCacheMaxAge: null,
@@ -47,6 +45,10 @@ module.exports = class UrlUtils {
47
45
  type: this._config.defaultApiType,
48
46
  apiVersions: this._config.apiVersions
49
47
  };
48
+
49
+ this.getSubdir = options.getSubdir;
50
+ this.getSiteUrl = options.getSiteUrl;
51
+ this.getAdminUrl = options.getAdminUrl;
50
52
  }
51
53
 
52
54
  /**
@@ -73,48 +75,6 @@ module.exports = class UrlUtils {
73
75
  return utils.getVersionPath(_options);
74
76
  }
75
77
 
76
- /**
77
- * Returns the base URL of the site as set in the config.
78
- *
79
- * Secure:
80
- * If the request is secure, we want to force returning the site url as https.
81
- * Imagine Ghost runs with http, but nginx allows SSL connections.
82
- *
83
- * @param {boolean} secure
84
- * @return {string} URL returns the url as defined in config, but always with a trailing `/`
85
- */
86
- getSiteUrl(secure = false) {
87
- let siteUrl = this._config.url;
88
-
89
- if (secure) {
90
- siteUrl = this._config.url.replace('http://', 'https://');
91
- }
92
-
93
- if (!siteUrl.match(/\/$/)) {
94
- siteUrl += '/';
95
- }
96
-
97
- return siteUrl;
98
- }
99
-
100
- /**
101
- * Returns a subdirectory URL, if defined so in the config.
102
- * @return {string} URL a subdirectory if configured.
103
- */
104
- getSubdir() {
105
- // Parse local path location
106
- let {pathname} = new URL(this._config.url);
107
- let subdir;
108
-
109
- // Remove trailing slash
110
- if (pathname !== '/') {
111
- pathname = pathname.replace(/\/$/, '');
112
- }
113
-
114
- subdir = pathname === '/' ? '' : pathname;
115
- return subdir;
116
- }
117
-
118
78
  getProtectedSlugs() {
119
79
  var subDir = this.getSubdir();
120
80
 
@@ -134,26 +94,6 @@ module.exports = class UrlUtils {
134
94
  return utils.urlJoin(parts, {rootUrl: this.getSiteUrl()});
135
95
  }
136
96
 
137
- /**
138
- * admin:url is optional
139
- */
140
- getAdminUrl() {
141
- let adminUrl = this._config.adminUrl;
142
- const subDir = this.getSubdir();
143
-
144
- if (!adminUrl) {
145
- return;
146
- }
147
-
148
- if (!adminUrl.match(/\/$/)) {
149
- adminUrl += '/';
150
- }
151
-
152
- adminUrl = this.urlJoin(adminUrl, subDir, '/');
153
- adminUrl = utils.deduplicateSubdirectory(adminUrl, this.getSiteUrl());
154
- return adminUrl;
155
- }
156
-
157
97
  // ## createUrl
158
98
  // Simple url creation from a given path
159
99
  // Ensures that our urls contain the subdirectory if there is one
@@ -278,6 +218,7 @@ module.exports = class UrlUtils {
278
218
  } else if (context === 'api') {
279
219
  urlPath = this.getAdminUrl() || this.getSiteUrl();
280
220
  let apiPath = this.getApiPath();
221
+
281
222
  // CASE: with or without protocol? If your blog url (or admin url) is configured to http, it's still possible that e.g. nginx allows both https+http.
282
223
  // So it depends how you serve your blog. The main focus here is to avoid cors problems.
283
224
  // @TODO: rename cors
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryghost/url-utils",
3
- "version": "1.1.4",
3
+ "version": "2.0.3",
4
4
  "repository": "https://github.com/TryGhost/SDK/tree/master/packages/url-utils",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
7
7
  "main": "lib/index.js",
8
8
  "scripts": {
9
9
  "dev": "echo \"Implement me!\"",
10
- "test": "NODE_ENV=testing mocha './test/**/*.test.js'",
10
+ "test": "NODE_ENV=testing c8 --reporter text --reporter cobertura mocha './test/**/*.test.js'",
11
11
  "lint": "eslint . --ext .js --cache",
12
12
  "posttest": "yarn lint"
13
13
  },
@@ -18,6 +18,8 @@
18
18
  "access": "public"
19
19
  },
20
20
  "devDependencies": {
21
+ "@tryghost/config-url-helpers": "0.1.3",
22
+ "c8": "7.10.0",
21
23
  "mocha": "7.2.0",
22
24
  "rewire": "5.0.0",
23
25
  "should": "13.2.3",
@@ -31,5 +33,5 @@
31
33
  "remark-footnotes": "^1.0.0",
32
34
  "unist-util-visit": "^2.0.0"
33
35
  },
34
- "gitHead": "8fb46408bde1a7431beb2ed0db9ed5d01708656e"
36
+ "gitHead": "93b42d8e27b3d8bd73fe17e200b5e9afe09dffa2"
35
37
  }