@tryghost/limit-service 1.2.9 → 1.2.10
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.
- package/lib/limit-service.js +10 -8
- package/lib/limit.js +11 -11
- package/package.json +2 -2
package/lib/limit-service.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
const camelCase = require('lodash/camelCase');
|
|
2
|
+
const has = require('lodash/has');
|
|
3
|
+
const {IncorrectUsageError} = require('@tryghost/errors');
|
|
4
|
+
|
|
1
5
|
const {MaxLimit, MaxPeriodicLimit, FlagLimit, AllowlistLimit} = require('./limit');
|
|
2
6
|
const config = require('./config');
|
|
3
|
-
const {IncorrectUsageError} = require('@tryghost/errors');
|
|
4
|
-
const _ = require('lodash');
|
|
5
7
|
|
|
6
8
|
const messages = {
|
|
7
9
|
missingErrorsConfig: `Config Missing: 'errors' is required.`,
|
|
@@ -36,18 +38,18 @@ class LimitService {
|
|
|
36
38
|
this.limits = {};
|
|
37
39
|
|
|
38
40
|
Object.keys(limits).forEach((name) => {
|
|
39
|
-
name =
|
|
41
|
+
name = camelCase(name);
|
|
40
42
|
|
|
41
43
|
// NOTE: config module acts as an allowlist of supported config names, where each key is a name of supported config
|
|
42
44
|
if (config[name]) {
|
|
43
45
|
/** @type LimitConfig */
|
|
44
46
|
let limitConfig = Object.assign({}, config[name], limits[name]);
|
|
45
47
|
|
|
46
|
-
if (
|
|
48
|
+
if (has(limitConfig, 'allowlist')) {
|
|
47
49
|
this.limits[name] = new AllowlistLimit({name, config: limitConfig, helpLink, errors});
|
|
48
|
-
} else if (
|
|
50
|
+
} else if (has(limitConfig, 'max')) {
|
|
49
51
|
this.limits[name] = new MaxLimit({name: name, config: limitConfig, helpLink, db, errors});
|
|
50
|
-
} else if (
|
|
52
|
+
} else if (has(limitConfig, 'maxPeriodic')) {
|
|
51
53
|
if (subscription === undefined) {
|
|
52
54
|
throw new IncorrectUsageError({
|
|
53
55
|
message: messages.noSubscriptionParameter
|
|
@@ -64,7 +66,7 @@ class LimitService {
|
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
isLimited(limitName) {
|
|
67
|
-
return !!this.limits[
|
|
69
|
+
return !!this.limits[camelCase(limitName)];
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
/**
|
|
@@ -147,7 +149,7 @@ class LimitService {
|
|
|
147
149
|
|
|
148
150
|
/**
|
|
149
151
|
* Checks if any of the configured limits acceded
|
|
150
|
-
*
|
|
152
|
+
*
|
|
151
153
|
* @param {Object} [options] - limit parameters
|
|
152
154
|
* @param {Object} [options.transacting] Transaction to run the count queries on (if required for the chosen limit)
|
|
153
155
|
* @returns {Promise<boolean>}
|
package/lib/limit.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
1
|
+
const lowerCase = require('lodash/lowerCase');
|
|
2
|
+
const template = require('lodash/template');
|
|
3
3
|
const {lastPeriodStart, SUPPORTED_INTERVALS} = require('./date-utils');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const interpolate = /{{([\s\S]+?)}}/g;
|
|
6
6
|
|
|
7
7
|
class Limit {
|
|
8
8
|
/**
|
|
@@ -65,7 +65,7 @@ class MaxLimit extends Limit {
|
|
|
65
65
|
this.currentCountQueryFn = config.currentCountQuery;
|
|
66
66
|
this.max = config.max;
|
|
67
67
|
this.formatter = config.formatter;
|
|
68
|
-
this.fallbackMessage = `This action would exceed the ${
|
|
68
|
+
this.fallbackMessage = `This action would exceed the ${lowerCase(this.name)} limit on your current plan.`;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
@@ -81,7 +81,7 @@ class MaxLimit extends Limit {
|
|
|
81
81
|
if (this.error) {
|
|
82
82
|
const formatter = this.formatter || Intl.NumberFormat().format;
|
|
83
83
|
try {
|
|
84
|
-
errorObj.message =
|
|
84
|
+
errorObj.message = template(this.error, {interpolate})(
|
|
85
85
|
{
|
|
86
86
|
max: formatter(this.max),
|
|
87
87
|
count: formatter(count),
|
|
@@ -101,7 +101,7 @@ class MaxLimit extends Limit {
|
|
|
101
101
|
/**
|
|
102
102
|
* @param {Object} [options]
|
|
103
103
|
* @param {Object} [options.transacting] Transaction to run the count query on
|
|
104
|
-
* @returns
|
|
104
|
+
* @returns
|
|
105
105
|
*/
|
|
106
106
|
async currentCountQuery(options = {}) {
|
|
107
107
|
return await this.currentCountQueryFn(options.transacting ?? this.db?.knex);
|
|
@@ -183,7 +183,7 @@ class MaxPeriodicLimit extends Limit {
|
|
|
183
183
|
this.maxPeriodic = config.maxPeriodic;
|
|
184
184
|
this.interval = config.interval;
|
|
185
185
|
this.startDate = config.startDate;
|
|
186
|
-
this.fallbackMessage = `This action would exceed the ${
|
|
186
|
+
this.fallbackMessage = `This action would exceed the ${lowerCase(this.name)} limit on your current plan.`;
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
generateError(count) {
|
|
@@ -193,7 +193,7 @@ class MaxPeriodicLimit extends Limit {
|
|
|
193
193
|
|
|
194
194
|
if (this.error) {
|
|
195
195
|
try {
|
|
196
|
-
errorObj.message =
|
|
196
|
+
errorObj.message = template(this.error, {interpolate})(
|
|
197
197
|
{
|
|
198
198
|
max: Intl.NumberFormat().format(this.maxPeriodic),
|
|
199
199
|
count: Intl.NumberFormat().format(count),
|
|
@@ -213,7 +213,7 @@ class MaxPeriodicLimit extends Limit {
|
|
|
213
213
|
/**
|
|
214
214
|
* @param {Object} [options]
|
|
215
215
|
* @param {Object} [options.transacting] Transaction to run the count query on
|
|
216
|
-
* @returns
|
|
216
|
+
* @returns
|
|
217
217
|
*/
|
|
218
218
|
async currentCountQuery(options = {}) {
|
|
219
219
|
const lastPeriodStartDate = lastPeriodStart(this.startDate, this.interval);
|
|
@@ -271,7 +271,7 @@ class FlagLimit extends Limit {
|
|
|
271
271
|
super({name, error: config.error || '', helpLink, db, errors});
|
|
272
272
|
|
|
273
273
|
this.disabled = config.disabled;
|
|
274
|
-
this.fallbackMessage = `Your plan does not support ${
|
|
274
|
+
this.fallbackMessage = `Your plan does not support ${lowerCase(this.name)}. Please upgrade to enable ${lowerCase(this.name)}.`;
|
|
275
275
|
}
|
|
276
276
|
|
|
277
277
|
generateError() {
|
|
@@ -323,7 +323,7 @@ class AllowlistLimit extends Limit {
|
|
|
323
323
|
}
|
|
324
324
|
|
|
325
325
|
this.allowlist = config.allowlist;
|
|
326
|
-
this.fallbackMessage = `This action would exceed the ${
|
|
326
|
+
this.fallbackMessage = `This action would exceed the ${lowerCase(this.name)} limit on your current plan.`;
|
|
327
327
|
}
|
|
328
328
|
|
|
329
329
|
generateError() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/limit-service",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10",
|
|
4
4
|
"repository": "https://github.com/TryGhost/SDK/tree/main/packages/limit-service",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"lodash": "^4.17.21",
|
|
31
31
|
"luxon": "^1.26.0"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "2ca4cf6106e708da5d7df4fd73ea2d9024ddd8ef"
|
|
34
34
|
}
|