swiftyper-node 1.0.1 → 1.0.2

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/swiftyper.js CHANGED
@@ -1,159 +1,159 @@
1
- 'use strict';
2
-
3
- const resources = require('./resources');
4
-
5
- const DEFAULT_HOST = 'api.swiftyper.sk';
6
- const DEFAULT_PORT = '443';
7
- const DEFAULT_BASE_PATH = '/v1/';
8
-
9
- const DEFAULT_TIMEOUT = 8000;
10
-
11
- const MAX_NETWORK_RETRY_DELAY_SEC = 2;
12
- const INITIAL_NETWORK_RETRY_DELAY_SEC = 0.5;
13
-
14
- const ALLOWED_CONFIG_PROPERTIES = [
15
- 'maxNetworkRetries',
16
- 'timeout',
17
- 'host',
18
- 'port',
19
- 'protocol',
20
- ];
21
-
22
- const EventEmitter = require('events').EventEmitter;
23
- const utils = require('./utils');
24
-
25
- Swiftyper.SwiftyperResource = require('./SwiftyperResource');
26
- Swiftyper.resources = resources;
27
-
28
- function Swiftyper(key, config = {}) {
29
- if (!(this instanceof Swiftyper)) {
30
- return new Swiftyper(key, config);
31
- }
32
-
33
- const props = this._getPropsFromConfig(config);
34
-
35
- Object.defineProperty(this, '_emitter', {
36
- value: new EventEmitter(),
37
- enumerable: false,
38
- configurable: false,
39
- writable: false,
40
- });
41
-
42
- this.on = this._emitter.on.bind(this._emitter);
43
- this.once = this._emitter.once.bind(this._emitter);
44
- this.off = this._emitter.removeListener.bind(this._emitter);
45
-
46
- if (
47
- props.protocol &&
48
- props.protocol !== 'https' &&
49
- (!props.host || /\.swiftyper\.(cz|pl|sk)/.test(props.host))
50
- ) {
51
- throw new Error(
52
- 'The `https` protocol must be used when sending requests to Swiftyper'
53
- );
54
- }
55
-
56
- this._api = {
57
- auth: null,
58
- host: props.host || DEFAULT_HOST,
59
- port: props.port || DEFAULT_PORT,
60
- protocol: props.protocol || 'https',
61
- basePath: DEFAULT_BASE_PATH,
62
- timeout: utils.validateInteger('timeout', props.timeout, DEFAULT_TIMEOUT),
63
- maxNetworkRetries: utils.validateInteger(
64
- 'maxNetworkRetries',
65
- props.maxNetworkRetries,
66
- 0
67
- ),
68
- };
69
-
70
- this._prepResources();
71
- this._setApiKey(key);
72
-
73
- this.errors = require('./Error');
74
-
75
- this.SwiftyperResource = Swiftyper.SwiftyperResource;
76
- }
77
-
78
- Swiftyper.errors = require('./Error');
79
-
80
- Swiftyper.prototype = {
81
- /**
82
- * @private
83
- */
84
- _setApiKey(key) {
85
- if (key) {
86
- this._setApiField('auth', key);
87
- }
88
- },
89
-
90
- /**
91
- * @private
92
- */
93
- _setApiField(key, value) {
94
- this._api[key] = value;
95
- },
96
-
97
- /**
98
- * @private
99
- */
100
- getApiField(key) {
101
- return this._api[key];
102
- },
103
-
104
- getMaxNetworkRetries() {
105
- return this.getApiField('maxNetworkRetries');
106
- },
107
-
108
- getMaxNetworkRetryDelay() {
109
- return MAX_NETWORK_RETRY_DELAY_SEC;
110
- },
111
-
112
- getInitialNetworkRetryDelay() {
113
- return INITIAL_NETWORK_RETRY_DELAY_SEC;
114
- },
115
-
116
- /**
117
- * @private
118
- */
119
- _prepResources() {
120
- for (const name in resources) {
121
- this[utils.pascalToCamelCase(name)] = new resources[name](this);
122
- }
123
- },
124
-
125
- /**
126
- * @private
127
- */
128
- _getPropsFromConfig(config) {
129
- if (!config) {
130
- return {};
131
- }
132
-
133
- const isObject = config === Object(config) && !Array.isArray(config);
134
-
135
- if (!isObject) {
136
- throw new Error('Config must be an object');
137
- }
138
-
139
- const values = Object.keys(config).filter(
140
- (value) => !ALLOWED_CONFIG_PROPERTIES.includes(value)
141
- );
142
-
143
- if (values.length > 0) {
144
- throw new Error(
145
- `Config object may only contain the following: ${ALLOWED_CONFIG_PROPERTIES.join(
146
- ', '
147
- )}`
148
- );
149
- }
150
-
151
- return config;
152
- },
153
- };
154
-
155
- module.exports = Swiftyper;
156
-
157
- module.exports.Swiftyper = Swiftyper;
158
-
159
- module.exports.default = Swiftyper;
1
+ 'use strict';
2
+
3
+ const resources = require('./resources');
4
+
5
+ const DEFAULT_HOST = 'api.swiftyper.sk';
6
+ const DEFAULT_PORT = '443';
7
+ const DEFAULT_BASE_PATH = '/v1/';
8
+
9
+ const DEFAULT_TIMEOUT = 8000;
10
+
11
+ const MAX_NETWORK_RETRY_DELAY_SEC = 2;
12
+ const INITIAL_NETWORK_RETRY_DELAY_SEC = 0.5;
13
+
14
+ const ALLOWED_CONFIG_PROPERTIES = [
15
+ 'maxNetworkRetries',
16
+ 'timeout',
17
+ 'host',
18
+ 'port',
19
+ 'protocol',
20
+ ];
21
+
22
+ const EventEmitter = require('events').EventEmitter;
23
+ const utils = require('./utils');
24
+
25
+ Swiftyper.SwiftyperResource = require('./SwiftyperResource');
26
+ Swiftyper.resources = resources;
27
+
28
+ function Swiftyper(key, config = {}) {
29
+ if (!(this instanceof Swiftyper)) {
30
+ return new Swiftyper(key, config);
31
+ }
32
+
33
+ const props = this._getPropsFromConfig(config);
34
+
35
+ Object.defineProperty(this, '_emitter', {
36
+ value: new EventEmitter(),
37
+ enumerable: false,
38
+ configurable: false,
39
+ writable: false,
40
+ });
41
+
42
+ this.on = this._emitter.on.bind(this._emitter);
43
+ this.once = this._emitter.once.bind(this._emitter);
44
+ this.off = this._emitter.removeListener.bind(this._emitter);
45
+
46
+ if (
47
+ props.protocol &&
48
+ props.protocol !== 'https' &&
49
+ (!props.host || /\.swiftyper\.(cz|pl|sk)/.test(props.host))
50
+ ) {
51
+ throw new Error(
52
+ 'The `https` protocol must be used when sending requests to Swiftyper'
53
+ );
54
+ }
55
+
56
+ this._api = {
57
+ auth: null,
58
+ host: props.host || DEFAULT_HOST,
59
+ port: props.port || DEFAULT_PORT,
60
+ protocol: props.protocol || 'https',
61
+ basePath: DEFAULT_BASE_PATH,
62
+ timeout: utils.validateInteger('timeout', props.timeout, DEFAULT_TIMEOUT),
63
+ maxNetworkRetries: utils.validateInteger(
64
+ 'maxNetworkRetries',
65
+ props.maxNetworkRetries,
66
+ 0
67
+ ),
68
+ };
69
+
70
+ this._prepResources();
71
+ this._setApiKey(key);
72
+
73
+ this.errors = require('./Error');
74
+
75
+ this.SwiftyperResource = Swiftyper.SwiftyperResource;
76
+ }
77
+
78
+ Swiftyper.errors = require('./Error');
79
+
80
+ Swiftyper.prototype = {
81
+ /**
82
+ * @private
83
+ */
84
+ _setApiKey(key) {
85
+ if (key) {
86
+ this._setApiField('auth', key);
87
+ }
88
+ },
89
+
90
+ /**
91
+ * @private
92
+ */
93
+ _setApiField(key, value) {
94
+ this._api[key] = value;
95
+ },
96
+
97
+ /**
98
+ * @private
99
+ */
100
+ getApiField(key) {
101
+ return this._api[key];
102
+ },
103
+
104
+ getMaxNetworkRetries() {
105
+ return this.getApiField('maxNetworkRetries');
106
+ },
107
+
108
+ getMaxNetworkRetryDelay() {
109
+ return MAX_NETWORK_RETRY_DELAY_SEC;
110
+ },
111
+
112
+ getInitialNetworkRetryDelay() {
113
+ return INITIAL_NETWORK_RETRY_DELAY_SEC;
114
+ },
115
+
116
+ /**
117
+ * @private
118
+ */
119
+ _prepResources() {
120
+ for (const name in resources) {
121
+ this[utils.pascalToCamelCase(name)] = new resources[name](this);
122
+ }
123
+ },
124
+
125
+ /**
126
+ * @private
127
+ */
128
+ _getPropsFromConfig(config) {
129
+ if (!config) {
130
+ return {};
131
+ }
132
+
133
+ const isObject = config === Object(config) && !Array.isArray(config);
134
+
135
+ if (!isObject) {
136
+ throw new Error('Config must be an object');
137
+ }
138
+
139
+ const values = Object.keys(config).filter(
140
+ (value) => !ALLOWED_CONFIG_PROPERTIES.includes(value)
141
+ );
142
+
143
+ if (values.length > 0) {
144
+ throw new Error(
145
+ `Config object may only contain the following: ${ALLOWED_CONFIG_PROPERTIES.join(
146
+ ', '
147
+ )}`
148
+ );
149
+ }
150
+
151
+ return config;
152
+ },
153
+ };
154
+
155
+ module.exports = Swiftyper;
156
+
157
+ module.exports.Swiftyper = Swiftyper;
158
+
159
+ module.exports.default = Swiftyper;