@apiverve/truthtable 1.1.12
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 +21 -0
- package/README.md +1709 -0
- package/examples/basic.js +38 -0
- package/index.d.ts +47 -0
- package/index.js +124 -0
- package/package.json +35 -0
- package/tmp/build.dat +1 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic Example - Truth Table Generator API
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use the Truth Table 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 truthtableAPI = require('../index.js');
|
|
10
|
+
|
|
11
|
+
// Initialize the API client
|
|
12
|
+
const api = new truthtableAPI({
|
|
13
|
+
api_key: process.env.API_KEY || '[YOUR_API_KEY]'
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Example query
|
|
17
|
+
var query = {
|
|
18
|
+
expression: "A AND B OR C"
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Make the API request using callback
|
|
22
|
+
console.log('Making request to Truth Table Generator API...\n');
|
|
23
|
+
|
|
24
|
+
api.execute(query, function (error, data) {
|
|
25
|
+
if (error) {
|
|
26
|
+
console.error('Error occurred:');
|
|
27
|
+
if (error.error) {
|
|
28
|
+
console.error('Message:', error.error);
|
|
29
|
+
console.error('Status:', error.status);
|
|
30
|
+
} else {
|
|
31
|
+
console.error(JSON.stringify(error, null, 2));
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
console.log('Response:');
|
|
37
|
+
console.log(JSON.stringify(data, null, 2));
|
|
38
|
+
});
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
declare module '@apiverve/truthtable' {
|
|
2
|
+
export interface truthtableOptions {
|
|
3
|
+
api_key: string;
|
|
4
|
+
secure?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface truthtableResponse {
|
|
8
|
+
status: string;
|
|
9
|
+
error: string | null;
|
|
10
|
+
data: TruthTableGeneratorData;
|
|
11
|
+
code?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
interface TruthTableGeneratorData {
|
|
16
|
+
expression: string;
|
|
17
|
+
normalized: string;
|
|
18
|
+
variables: string[];
|
|
19
|
+
variableCount: number;
|
|
20
|
+
rowCount: number;
|
|
21
|
+
rows: Row[];
|
|
22
|
+
asciiTable: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface Row {
|
|
26
|
+
inputs: Inputs;
|
|
27
|
+
result: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Inputs {
|
|
31
|
+
a: number;
|
|
32
|
+
b: number;
|
|
33
|
+
c: number;
|
|
34
|
+
d: number;
|
|
35
|
+
n: number;
|
|
36
|
+
o: number;
|
|
37
|
+
r: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default class truthtableWrapper {
|
|
41
|
+
constructor(options: truthtableOptions);
|
|
42
|
+
|
|
43
|
+
execute(callback: (error: any, data: truthtableResponse | null) => void): Promise<truthtableResponse>;
|
|
44
|
+
execute(query: Record<string, any>, callback: (error: any, data: truthtableResponse | null) => void): Promise<truthtableResponse>;
|
|
45
|
+
execute(query?: Record<string, any>): Promise<truthtableResponse>;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
|
|
3
|
+
class truthtableWrapper {
|
|
4
|
+
|
|
5
|
+
constructor(options) {
|
|
6
|
+
if (!options || typeof options !== 'object') {
|
|
7
|
+
throw new Error('Options object must be provided. See documentation: https://docs.apiverve.com/ref/truthtable');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { api_key, secure = true } = options;
|
|
11
|
+
|
|
12
|
+
if (!api_key || typeof api_key !== 'string') {
|
|
13
|
+
throw new Error('API key must be provided as a non-empty string. Get your API key at: https://apiverve.com');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Validate API key format (GUID or alphanumeric with hyphens)
|
|
17
|
+
const apiKeyPattern = /^[a-zA-Z0-9-]+$/;
|
|
18
|
+
if (!apiKeyPattern.test(api_key)) {
|
|
19
|
+
throw new Error('Invalid API key format. API key must be alphanumeric and may contain hyphens. Get your API key at: https://apiverve.com');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Check minimum length (GUIDs are typically 36 chars with hyphens, or 32 without)
|
|
23
|
+
const trimmedKey = api_key.replace(/-/g, '');
|
|
24
|
+
if (trimmedKey.length < 32) {
|
|
25
|
+
throw new Error('Invalid API key. API key appears to be too short. Get your API key at: https://apiverve.com');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof secure !== 'boolean') {
|
|
29
|
+
throw new Error('Secure parameter must be a boolean value.');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.APIKey = api_key;
|
|
33
|
+
this.IsSecure = secure;
|
|
34
|
+
|
|
35
|
+
// secure is deprecated, all requests must be made over HTTPS
|
|
36
|
+
this.baseURL = 'https://api.apiverve.com/v1/truthtable';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async execute(query, callback) {
|
|
40
|
+
// Handle different argument patterns
|
|
41
|
+
if(arguments.length === 0) {
|
|
42
|
+
// execute() - no args
|
|
43
|
+
query = {};
|
|
44
|
+
callback = null;
|
|
45
|
+
} else if(arguments.length === 1) {
|
|
46
|
+
if (typeof query === 'function') {
|
|
47
|
+
// execute(callback)
|
|
48
|
+
callback = query;
|
|
49
|
+
query = {};
|
|
50
|
+
} else {
|
|
51
|
+
// execute(query)
|
|
52
|
+
callback = null;
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
// execute(query, callback)
|
|
56
|
+
if (!query || typeof query !== 'object') {
|
|
57
|
+
throw new Error('Query parameters must be provided as an object.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
var requiredParams = ["expression"];
|
|
62
|
+
if (requiredParams.length > 0) {
|
|
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/truthtable`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const method = 'GET';
|
|
71
|
+
const url = method === 'POST' ? this.baseURL : this.constructURL(query);
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const response = await axios({
|
|
75
|
+
method,
|
|
76
|
+
url,
|
|
77
|
+
headers: {
|
|
78
|
+
'Content-Type': 'application/json',
|
|
79
|
+
'x-api-key': this.APIKey,
|
|
80
|
+
'auth-mode': 'npm-package'
|
|
81
|
+
},
|
|
82
|
+
data: method === 'POST' ? query : undefined
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const data = response.data;
|
|
86
|
+
if (callback) callback(null, data);
|
|
87
|
+
return data;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
let apiError;
|
|
90
|
+
|
|
91
|
+
if (error.response && error.response.data) {
|
|
92
|
+
apiError = error.response.data;
|
|
93
|
+
} else if (error.message) {
|
|
94
|
+
apiError = { error: error.message, status: 'error' };
|
|
95
|
+
} else {
|
|
96
|
+
apiError = { error: 'An unknown error occurred', status: 'error' };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (callback) {
|
|
100
|
+
callback(apiError, null);
|
|
101
|
+
return; // Don't throw if callback is provided
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
throw apiError;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
constructURL(query) {
|
|
109
|
+
let url = this.baseURL;
|
|
110
|
+
|
|
111
|
+
if(query && typeof query === 'object')
|
|
112
|
+
{
|
|
113
|
+
if (Object.keys(query).length > 0) {
|
|
114
|
+
const queryString = Object.keys(query)
|
|
115
|
+
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`)
|
|
116
|
+
.join('&');
|
|
117
|
+
url += `?${queryString}`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return url;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = truthtableWrapper;
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@apiverve/truthtable",
|
|
3
|
+
"version": "1.1.12",
|
|
4
|
+
"description": "Truth Table Generator creates complete truth tables for boolean expressions with support for AND, OR, NOT, XOR, NAND, and NOR operators.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha",
|
|
9
|
+
"example": "node examples/basic.js"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/apiverve/truthtable-api.git",
|
|
14
|
+
"directory": "npm"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"truth table", "boolean logic", "logic gates", "AND", "OR"
|
|
18
|
+
],
|
|
19
|
+
"author": "APIVerve <hello@apiverve.com> (http://apiverve.com/)",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/apiverve/truthtable-api/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://apiverve.com/marketplace/truthtable?utm_source=npm",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"mocha": "^11.0.1",
|
|
27
|
+
"chai": "^5.1.2",
|
|
28
|
+
"dotenv": "^16.4.7"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"node-fetch": "^3.3.2",
|
|
32
|
+
"promise": "^8.3.0",
|
|
33
|
+
"axios": "1.13.2"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/tmp/build.dat
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#
|