@apiverve/advice 1.1.10 → 1.1.13
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/LICENSE +1 -1
- package/README.md +8 -7
- package/index.d.ts +8 -1
- package/index.js +113 -13
- package/package.json +21 -7
- package/examples/basic.js +0 -34
- package/tmp/build.dat +0 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2025 APIVerve, and EvlarSoft LLC
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -5,8 +5,9 @@ Advice Generator is a simple tool for generating random pieces of advice. It ret
|
|
|
5
5
|

|
|
6
6
|

|
|
7
7
|

|
|
8
|
+
[](https://www.npmjs.com/package/@apiverve/advice)
|
|
8
9
|
|
|
9
|
-
This is a Javascript Wrapper for the [Advice Generator API](https://apiverve.com/marketplace/advice)
|
|
10
|
+
This is a Javascript Wrapper for the [Advice Generator API](https://apiverve.com/marketplace/advice?utm_source=npm&utm_medium=readme)
|
|
10
11
|
|
|
11
12
|
---
|
|
12
13
|
|
|
@@ -27,15 +28,15 @@ yarn add @apiverve/advice
|
|
|
27
28
|
## Configuration
|
|
28
29
|
|
|
29
30
|
Before using the Advice Generator API client, you have to setup your account and obtain your API Key.
|
|
30
|
-
You can get it by signing up at [https://apiverve.com](https://apiverve.com)
|
|
31
|
+
You can get it by signing up at [https://apiverve.com](https://apiverve.com?utm_source=npm&utm_medium=readme)
|
|
31
32
|
|
|
32
33
|
---
|
|
33
34
|
|
|
34
35
|
## Quick Start
|
|
35
36
|
|
|
36
|
-
[Get started with the Quick Start Guide](https://docs.apiverve.com/quickstart)
|
|
37
|
+
[Get started with the Quick Start Guide](https://docs.apiverve.com/quickstart?utm_source=npm&utm_medium=readme)
|
|
37
38
|
|
|
38
|
-
The Advice Generator API documentation is found here: [https://docs.apiverve.com/ref/advice](https://docs.apiverve.com/ref/advice).
|
|
39
|
+
The Advice Generator API documentation is found here: [https://docs.apiverve.com/ref/advice](https://docs.apiverve.com/ref/advice?utm_source=npm&utm_medium=readme).
|
|
39
40
|
You can find parameters, example responses, and status codes documented here.
|
|
40
41
|
|
|
41
42
|
### Setup
|
|
@@ -126,7 +127,7 @@ async function makeRequest() {
|
|
|
126
127
|
|
|
127
128
|
## Customer Support
|
|
128
129
|
|
|
129
|
-
Need any assistance? [Get in touch with Customer Support](https://apiverve.com/contact).
|
|
130
|
+
Need any assistance? [Get in touch with Customer Support](https://apiverve.com/contact?utm_source=npm&utm_medium=readme).
|
|
130
131
|
|
|
131
132
|
---
|
|
132
133
|
|
|
@@ -138,14 +139,14 @@ Stay up to date by following [@apiverveHQ](https://twitter.com/apiverveHQ) on Tw
|
|
|
138
139
|
|
|
139
140
|
## Legal
|
|
140
141
|
|
|
141
|
-
All usage of the APIVerve website, API, and services is subject to the [APIVerve Terms of Service](https://apiverve.com/terms)
|
|
142
|
+
All usage of the APIVerve website, API, and services is subject to the [APIVerve Terms of Service](https://apiverve.com/terms?utm_source=npm&utm_medium=readme), [Privacy Policy](https://apiverve.com/privacy?utm_source=npm&utm_medium=readme), and [Refund Policy](https://apiverve.com/refund?utm_source=npm&utm_medium=readme).
|
|
142
143
|
|
|
143
144
|
---
|
|
144
145
|
|
|
145
146
|
## License
|
|
146
147
|
Licensed under the The MIT License (MIT)
|
|
147
148
|
|
|
148
|
-
Copyright (©)
|
|
149
|
+
Copyright (©) 2026 APIVerve, and EvlarSoft LLC
|
|
149
150
|
|
|
150
151
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
151
152
|
|
package/index.d.ts
CHANGED
|
@@ -7,10 +7,17 @@ declare module '@apiverve/advice' {
|
|
|
7
7
|
export interface adviceResponse {
|
|
8
8
|
status: string;
|
|
9
9
|
error: string | null;
|
|
10
|
-
data:
|
|
10
|
+
data: AdviceGeneratorData;
|
|
11
11
|
code?: number;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
|
|
15
|
+
interface AdviceGeneratorData {
|
|
16
|
+
id: string;
|
|
17
|
+
advice: string;
|
|
18
|
+
lang: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
export default class adviceWrapper {
|
|
15
22
|
constructor(options: adviceOptions);
|
|
16
23
|
|
package/index.js
CHANGED
|
@@ -34,6 +34,102 @@ class adviceWrapper {
|
|
|
34
34
|
|
|
35
35
|
// secure is deprecated, all requests must be made over HTTPS
|
|
36
36
|
this.baseURL = 'https://api.apiverve.com/v1/advice';
|
|
37
|
+
|
|
38
|
+
// Validation rules for parameters (generated from schema)
|
|
39
|
+
this.validationRules = {};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Validate query parameters against schema rules
|
|
44
|
+
* @param {Object} query - The query parameters to validate
|
|
45
|
+
* @throws {Error} - If validation fails
|
|
46
|
+
*/
|
|
47
|
+
validateParams(query) {
|
|
48
|
+
const errors = [];
|
|
49
|
+
|
|
50
|
+
for (const [paramName, rules] of Object.entries(this.validationRules)) {
|
|
51
|
+
const value = query[paramName];
|
|
52
|
+
|
|
53
|
+
// Check required
|
|
54
|
+
if (rules.required && (value === undefined || value === null || value === '')) {
|
|
55
|
+
errors.push(`Required parameter [${paramName}] is missing.`);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Skip validation if value is not provided and not required
|
|
60
|
+
if (value === undefined || value === null) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Type validation
|
|
65
|
+
if (rules.type === 'integer' || rules.type === 'number') {
|
|
66
|
+
const numValue = Number(value);
|
|
67
|
+
if (isNaN(numValue)) {
|
|
68
|
+
errors.push(`Parameter [${paramName}] must be a valid ${rules.type}.`);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (rules.type === 'integer' && !Number.isInteger(numValue)) {
|
|
73
|
+
errors.push(`Parameter [${paramName}] must be an integer.`);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Min/max validation for numbers
|
|
78
|
+
if (rules.min !== undefined && numValue < rules.min) {
|
|
79
|
+
errors.push(`Parameter [${paramName}] must be at least ${rules.min}.`);
|
|
80
|
+
}
|
|
81
|
+
if (rules.max !== undefined && numValue > rules.max) {
|
|
82
|
+
errors.push(`Parameter [${paramName}] must be at most ${rules.max}.`);
|
|
83
|
+
}
|
|
84
|
+
} else if (rules.type === 'string') {
|
|
85
|
+
if (typeof value !== 'string') {
|
|
86
|
+
errors.push(`Parameter [${paramName}] must be a string.`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Length validation for strings
|
|
91
|
+
if (rules.minLength !== undefined && value.length < rules.minLength) {
|
|
92
|
+
errors.push(`Parameter [${paramName}] must be at least ${rules.minLength} characters.`);
|
|
93
|
+
}
|
|
94
|
+
if (rules.maxLength !== undefined && value.length > rules.maxLength) {
|
|
95
|
+
errors.push(`Parameter [${paramName}] must be at most ${rules.maxLength} characters.`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Format validation
|
|
99
|
+
if (rules.format) {
|
|
100
|
+
const formatPatterns = {
|
|
101
|
+
'email': /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
102
|
+
'url': /^https?:\/\/.+/i,
|
|
103
|
+
'ip': /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
|
|
104
|
+
'date': /^\d{4}-\d{2}-\d{2}$/,
|
|
105
|
+
'hexColor': /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
if (formatPatterns[rules.format] && !formatPatterns[rules.format].test(value)) {
|
|
109
|
+
errors.push(`Parameter [${paramName}] must be a valid ${rules.format}.`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} else if (rules.type === 'boolean') {
|
|
113
|
+
if (typeof value !== 'boolean' && value !== 'true' && value !== 'false') {
|
|
114
|
+
errors.push(`Parameter [${paramName}] must be a boolean.`);
|
|
115
|
+
}
|
|
116
|
+
} else if (rules.type === 'array') {
|
|
117
|
+
if (!Array.isArray(value)) {
|
|
118
|
+
errors.push(`Parameter [${paramName}] must be an array.`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Enum validation
|
|
123
|
+
if (rules.enum && Array.isArray(rules.enum)) {
|
|
124
|
+
if (!rules.enum.includes(value)) {
|
|
125
|
+
errors.push(`Parameter [${paramName}] must be one of: ${rules.enum.join(', ')}.`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (errors.length > 0) {
|
|
131
|
+
throw new Error(`Validation failed: ${errors.join(' ')} See documentation: https://docs.apiverve.com/ref/advice`);
|
|
132
|
+
}
|
|
37
133
|
}
|
|
38
134
|
|
|
39
135
|
async execute(query, callback) {
|
|
@@ -58,14 +154,8 @@ class adviceWrapper {
|
|
|
58
154
|
}
|
|
59
155
|
}
|
|
60
156
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (var i = 0; i < requiredParams.length; i++) {
|
|
64
|
-
if (!query[requiredParams[i]]) {
|
|
65
|
-
throw new Error(`Required parameter [${requiredParams[i]}] is missing. See documentation: https://docs.apiverve.com/ref/advice`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
157
|
+
// Validate parameters against schema rules
|
|
158
|
+
this.validateParams(query);
|
|
69
159
|
|
|
70
160
|
const method = 'GET';
|
|
71
161
|
const url = method === 'POST' ? this.baseURL : this.constructURL(query);
|
|
@@ -86,20 +176,29 @@ class adviceWrapper {
|
|
|
86
176
|
if (callback) callback(null, data);
|
|
87
177
|
return data;
|
|
88
178
|
} catch (error) {
|
|
179
|
+
let apiError;
|
|
180
|
+
|
|
89
181
|
if (error.response && error.response.data) {
|
|
90
|
-
|
|
91
|
-
|
|
182
|
+
apiError = error.response.data;
|
|
183
|
+
} else if (error.message) {
|
|
184
|
+
apiError = { error: error.message, status: 'error' };
|
|
92
185
|
} else {
|
|
93
|
-
|
|
94
|
-
throw error;
|
|
186
|
+
apiError = { error: 'An unknown error occurred', status: 'error' };
|
|
95
187
|
}
|
|
188
|
+
|
|
189
|
+
if (callback) {
|
|
190
|
+
callback(apiError, null);
|
|
191
|
+
return; // Don't throw if callback is provided
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
throw apiError;
|
|
96
195
|
}
|
|
97
196
|
}
|
|
98
197
|
|
|
99
198
|
constructURL(query) {
|
|
100
199
|
let url = this.baseURL;
|
|
101
200
|
|
|
102
|
-
if(query && typeof query === 'object')
|
|
201
|
+
if(query && typeof query === 'object')
|
|
103
202
|
{
|
|
104
203
|
if (Object.keys(query).length > 0) {
|
|
105
204
|
const queryString = Object.keys(query)
|
|
@@ -110,6 +209,7 @@ class adviceWrapper {
|
|
|
110
209
|
}
|
|
111
210
|
return url;
|
|
112
211
|
}
|
|
212
|
+
|
|
113
213
|
}
|
|
114
214
|
|
|
115
215
|
module.exports = adviceWrapper;
|
package/package.json
CHANGED
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apiverve/advice",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.13",
|
|
4
4
|
"description": "Advice Generator is a simple tool for generating random pieces of advice. It returns a random piece of advice.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=14.0.0"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"index.d.ts",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
7
16
|
"scripts": {
|
|
8
17
|
"test": "mocha",
|
|
9
18
|
"example": "node examples/basic.js"
|
|
10
19
|
},
|
|
11
20
|
"repository": {
|
|
12
21
|
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/apiverve/advice-
|
|
22
|
+
"url": "git+https://github.com/apiverve/advice-api.git",
|
|
23
|
+
"directory": "npm"
|
|
14
24
|
},
|
|
15
25
|
"keywords": [
|
|
16
|
-
"
|
|
26
|
+
""
|
|
17
27
|
],
|
|
18
|
-
"author":
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "APIVerve",
|
|
30
|
+
"email": "hello@apiverve.com",
|
|
31
|
+
"url": "https://apiverve.com"
|
|
32
|
+
},
|
|
19
33
|
"license": "MIT",
|
|
20
34
|
"bugs": {
|
|
21
|
-
"url": "https://github.com/apiverve/advice-
|
|
35
|
+
"url": "https://github.com/apiverve/advice-api/issues"
|
|
22
36
|
},
|
|
23
|
-
"homepage": "https://apiverve.com/marketplace/advice?utm_source=npm",
|
|
37
|
+
"homepage": "https://apiverve.com/marketplace/advice?utm_source=npm&utm_medium=homepage",
|
|
24
38
|
"devDependencies": {
|
|
25
39
|
"mocha": "^11.0.1",
|
|
26
40
|
"chai": "^5.1.2",
|
|
@@ -29,6 +43,6 @@
|
|
|
29
43
|
"dependencies": {
|
|
30
44
|
"node-fetch": "^3.3.2",
|
|
31
45
|
"promise": "^8.3.0",
|
|
32
|
-
"axios": "1.
|
|
46
|
+
"axios": "1.13.2"
|
|
33
47
|
}
|
|
34
48
|
}
|
package/examples/basic.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Basic Example - Advice Generator API
|
|
3
|
-
*
|
|
4
|
-
* This example demonstrates how to use the Advice Generator API.
|
|
5
|
-
* Make sure to set your API key in the .env file or replace '[YOUR_API_KEY]' below.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
require('dotenv').config();
|
|
9
|
-
const adviceAPI = require('../index.js');
|
|
10
|
-
|
|
11
|
-
// Initialize the API client
|
|
12
|
-
const api = new adviceAPI({
|
|
13
|
-
api_key: process.env.API_KEY || '[YOUR_API_KEY]'
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
// Example query
|
|
17
|
-
// This API does not require a Query
|
|
18
|
-
|
|
19
|
-
// Make the API request
|
|
20
|
-
async function main() {
|
|
21
|
-
try {
|
|
22
|
-
console.log('Making request to Advice Generator API...\n');
|
|
23
|
-
const data = await api.execute(query);
|
|
24
|
-
console.log('Response:');
|
|
25
|
-
console.log(JSON.stringify(data, null, 2));
|
|
26
|
-
} catch (error) {
|
|
27
|
-
console.error('Error:', error.message);
|
|
28
|
-
if (error.response && error.response.data) {
|
|
29
|
-
console.error('API Error:', error.response.data);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
main();
|
package/tmp/build.dat
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
#
|