@apiverve/codedetector 1.1.9 → 1.1.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/README.md CHANGED
@@ -1,5 +1,4 @@
1
- Code Detector API
2
- ============
1
+ # Code Detector API
3
2
 
4
3
  Code Detector is a simple tool for detecting the language of code in text. It returns details such as extension, language, family, and more.
5
4
 
@@ -7,66 +6,60 @@ Code Detector is a simple tool for detecting the language of code in text. It re
7
6
  ![Code Climate](https://img.shields.io/badge/maintainability-B-purple)
8
7
  ![Prod Ready](https://img.shields.io/badge/production-ready-blue)
9
8
 
10
- This is a Javascript Wrapper for the [Code Detector API](https://apiverve.com/marketplace/api/codedetector)
9
+ This is a Javascript Wrapper for the [Code Detector API](https://apiverve.com/marketplace/codedetector)
11
10
 
12
11
  ---
13
12
 
14
13
  ## Installation
15
- npm install @apiverve/codedetector --save
14
+
15
+ Using npm:
16
+ ```shell
17
+ npm install @apiverve/codedetector
18
+ ```
19
+
20
+ Using yarn:
21
+ ```shell
22
+ yarn add @apiverve/codedetector
23
+ ```
16
24
 
17
25
  ---
18
26
 
19
27
  ## Configuration
20
28
 
21
- Before using the codedetector API client, you have to setup your account and obtain your API Key.
29
+ Before using the Code Detector API client, you have to setup your account and obtain your API Key.
22
30
  You can get it by signing up at [https://apiverve.com](https://apiverve.com)
23
31
 
24
32
  ---
25
33
 
26
- ## Usage
34
+ ## Quick Start
27
35
 
28
- The Code Detector API documentation is found here: [https://docs.apiverve.com/api/codedetector](https://docs.apiverve.com/api/codedetector).
36
+ [Get started with the Quick Start Guide](https://docs.apiverve.com/quickstart)
37
+
38
+ The Code Detector API documentation is found here: [https://docs.apiverve.com/ref/codedetector](https://docs.apiverve.com/ref/codedetector).
29
39
  You can find parameters, example responses, and status codes documented here.
30
40
 
31
41
  ### Setup
32
42
 
33
- ```
34
- var codedetectorAPI = require('@apiverve/codedetector');
35
- var api = new codedetectorAPI({
36
- api_key: [YOUR_API_KEY],
37
- secure: true //(Optional, defaults to true)
43
+ ```javascript
44
+ const codedetectorAPI = require('@apiverve/codedetector');
45
+ const api = new codedetectorAPI({
46
+ api_key: '[YOUR_API_KEY]'
38
47
  });
39
48
  ```
40
49
 
41
50
  ---
42
51
 
52
+ ## Usage
43
53
 
44
- ### Perform Request
45
- Using the API client, you can perform requests to the API.
54
+ ---
46
55
 
47
- ###### Define Query
56
+ ### Perform Request
48
57
 
49
- ```
50
- var query = a = 5
51
- b = 6
52
- c = 7
53
-
54
- # Uncomment below to take inputs from the user
55
- # a = float(input('Enter first side: '))
56
- # b = float(input('Enter second side: '))
57
- # c = float(input('Enter third side: '))
58
-
59
- # calculate the semi-perimeter
60
- s = (a + b + c) / 2
61
-
62
- # calculate the area
63
- area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
64
- print('The area of the triangle is %0.2f' %area);
65
- ```
58
+ Using the API is simple. All you have to do is make a request. The API will return a response with the data you requested.
66
59
 
67
- ###### Simple Request (using Callback)
60
+ ```javascript
61
+ var query = "a = 5\nb = 6\nc = 7\n\n# Uncomment below to take inputs from the user\n# a = float(input('Enter first side: '))\n# b = float(input('Enter second side: '))\n# c = float(input('Enter third side: '))\n\n# calculate the semi-perimeter\ns = (a + b + c) / 2\n\n# calculate the area\narea = (s*(s-a)*(s-b)*(s-c)) ** 0.5\nprint('The area of the triangle is %0.2f' %area)";
68
62
 
69
- ```
70
63
  api.execute(query, function (error, data) {
71
64
  if (error) {
72
65
  return console.error(error);
@@ -76,9 +69,48 @@ api.execute(query, function (error, data) {
76
69
  });
77
70
  ```
78
71
 
79
- ###### Example Response
72
+ ---
73
+
74
+ ### Using Promises
75
+
76
+ You can also use promises to make requests. The API returns a promise that you can use to handle the response.
77
+
78
+ ```javascript
79
+ var query = "a = 5\nb = 6\nc = 7\n\n# Uncomment below to take inputs from the user\n# a = float(input('Enter first side: '))\n# b = float(input('Enter second side: '))\n# c = float(input('Enter third side: '))\n\n# calculate the semi-perimeter\ns = (a + b + c) / 2\n\n# calculate the area\narea = (s*(s-a)*(s-b)*(s-c)) ** 0.5\nprint('The area of the triangle is %0.2f' %area)";
80
+
81
+ api.execute(query)
82
+ .then(data => {
83
+ console.log(data);
84
+ })
85
+ .catch(error => {
86
+ console.error(error);
87
+ });
88
+ ```
89
+
90
+ ---
91
+
92
+ ### Using Async/Await
93
+
94
+ You can also use async/await to make requests. The API returns a promise that you can use to handle the response.
95
+
96
+ ```javascript
97
+ async function makeRequest() {
98
+ var query = "a = 5\nb = 6\nc = 7\n\n# Uncomment below to take inputs from the user\n# a = float(input('Enter first side: '))\n# b = float(input('Enter second side: '))\n# c = float(input('Enter third side: '))\n\n# calculate the semi-perimeter\ns = (a + b + c) / 2\n\n# calculate the area\narea = (s*(s-a)*(s-b)*(s-c)) ** 0.5\nprint('The area of the triangle is %0.2f' %area)";
80
99
 
100
+ try {
101
+ const data = await api.execute(query);
102
+ console.log(data);
103
+ } catch (error) {
104
+ console.error(error);
105
+ }
106
+ }
81
107
  ```
108
+
109
+ ---
110
+
111
+ ## Example Response
112
+
113
+ ```json
82
114
  {
83
115
  "status": "ok",
84
116
  "error": null,
@@ -88,8 +120,7 @@ api.execute(query, function (error, data) {
88
120
  "current": "python",
89
121
  "readable": "Python Code",
90
122
  "extension": ".py"
91
- },
92
- "code": 200
123
+ }
93
124
  }
94
125
  ```
95
126
 
@@ -102,6 +133,7 @@ Need any assistance? [Get in touch with Customer Support](https://apiverve.com/c
102
133
  ---
103
134
 
104
135
  ## Updates
136
+
105
137
  Stay up to date by following [@apiverveHQ](https://twitter.com/apiverveHQ) on Twitter.
106
138
 
107
139
  ---
@@ -115,10 +147,10 @@ All usage of the APIVerve website, API, and services is subject to the [APIVerve
115
147
  ## License
116
148
  Licensed under the The MIT License (MIT)
117
149
 
118
- Copyright (©) 2025 APIVerve, and EvlarSoft LLC
150
+ Copyright (©) 2025 APIVerve, and Evlar LLC
119
151
 
120
152
  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:
121
153
 
122
154
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
123
155
 
124
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
156
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Basic Example - Code Detector API
3
+ *
4
+ * This example demonstrates how to use the Code Detector 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 codedetectorAPI = require('../index.js');
10
+
11
+ // Initialize the API client
12
+ const api = new codedetectorAPI({
13
+ api_key: process.env.API_KEY || '[YOUR_API_KEY]'
14
+ });
15
+
16
+ // Example query
17
+ var query = "a = 5\nb = 6\nc = 7\n\n# Uncomment below to take inputs from the user\n# a = float(input('Enter first side: '))\n# b = float(input('Enter second side: '))\n# c = float(input('Enter third side: '))\n\n# calculate the semi-perimeter\ns = (a + b + c) / 2\n\n# calculate the area\narea = (s*(s-a)*(s-b)*(s-c)) ** 0.5\nprint('The area of the triangle is %0.2f' %area)";
18
+
19
+ // Make the API request using callback
20
+ console.log('Making request to Code Detector API...\n');
21
+
22
+ api.execute(query, function (error, data) {
23
+ if (error) {
24
+ console.error('Error occurred:');
25
+ if (error.error) {
26
+ console.error('Message:', error.error);
27
+ console.error('Status:', error.status);
28
+ } else {
29
+ console.error(JSON.stringify(error, null, 2));
30
+ }
31
+ return;
32
+ }
33
+
34
+ console.log('Response:');
35
+ console.log(JSON.stringify(data, null, 2));
36
+ });
package/index.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ declare module '@apiverve/codedetector' {
2
+ export interface codedetectorOptions {
3
+ api_key: string;
4
+ secure?: boolean;
5
+ }
6
+
7
+ export interface codedetectorResponse {
8
+ status: string;
9
+ error: string | null;
10
+ data: any;
11
+ code?: number;
12
+ }
13
+
14
+ export default class codedetectorWrapper {
15
+ constructor(options: codedetectorOptions);
16
+
17
+ execute(callback: (error: any, data: codedetectorResponse | null) => void): Promise<codedetectorResponse>;
18
+ execute(query: Record<string, any>, callback: (error: any, data: codedetectorResponse | null) => void): Promise<codedetectorResponse>;
19
+ execute(query?: Record<string, any>): Promise<codedetectorResponse>;
20
+ }
21
+ }
package/index.js CHANGED
@@ -4,14 +4,27 @@ class codedetectorWrapper {
4
4
 
5
5
  constructor(options) {
6
6
  if (!options || typeof options !== 'object') {
7
- throw new Error('Options object must be provided.');
7
+ throw new Error('Options object must be provided. See documentation: https://docs.apiverve.com/ref/codedetector');
8
8
  }
9
9
 
10
10
  const { api_key, secure = true } = options;
11
11
 
12
12
  if (!api_key || typeof api_key !== 'string') {
13
- throw new Error('API key must be provided as a non-empty string.');
13
+ throw new Error('API key must be provided as a non-empty string. Get your API key at: https://apiverve.com');
14
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
+
15
28
  if (typeof secure !== 'boolean') {
16
29
  throw new Error('Secure parameter must be a boolean value.');
17
30
  }
@@ -24,20 +37,32 @@ class codedetectorWrapper {
24
37
  }
25
38
 
26
39
  async execute(query, callback) {
27
- if(arguments.length > 1) {
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)
28
56
  if (!query || typeof query !== 'object') {
29
57
  throw new Error('Query parameters must be provided as an object.');
30
58
  }
31
- } else {
32
- callback = query;
33
- query = {};
34
59
  }
35
60
 
36
61
  var requiredParams = ["text"];
37
62
  if (requiredParams.length > 0) {
38
63
  for (var i = 0; i < requiredParams.length; i++) {
39
64
  if (!query[requiredParams[i]]) {
40
- throw new Error(`Required parameter [${requiredParams[i]}] is missing.`);
65
+ throw new Error(`Required parameter [${requiredParams[i]}] is missing. See documentation: https://docs.apiverve.com/ref/codedetector`);
41
66
  }
42
67
  }
43
68
  }
@@ -58,16 +83,25 @@ class codedetectorWrapper {
58
83
  });
59
84
 
60
85
  const data = response.data;
61
- callback(null, data);
86
+ if (callback) callback(null, data);
62
87
  return data;
63
88
  } catch (error) {
64
- if (error.response.data) {
65
- callback(error.response.data, null);
66
- throw error.response.data;
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' };
67
95
  } else {
68
- callback(error, null);
69
- throw error;
70
- }
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;
71
105
  }
72
106
  }
73
107
 
package/package.json CHANGED
@@ -1,24 +1,26 @@
1
1
  {
2
2
  "name": "@apiverve/codedetector",
3
- "version": "1.1.9",
3
+ "version": "1.1.10",
4
4
  "description": "Code Detector is a simple tool for detecting the language of code in text. It returns details such as extension, language, family, and more.",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
- "test": "mocha"
8
+ "test": "mocha",
9
+ "example": "node examples/basic.js"
8
10
  },
9
11
  "repository": {
10
12
  "type": "git",
11
13
  "url": "git+https://github.com/apiverve/codedetector-API.git"
12
14
  },
13
15
  "keywords": [
14
- "code detector","code detection","code detect","code api","code tool"
16
+ "code detector", "code detection", "code detect", "code api", "code tool"
15
17
  ],
16
18
  "author": "APIVerve <hello@apiverve.com> (http://apiverve.com/)",
17
19
  "license": "MIT",
18
20
  "bugs": {
19
21
  "url": "https://github.com/apiverve/codedetector-API/issues"
20
22
  },
21
- "homepage": "https://apiverve.com/marketplace/api/codedetector?utm_source=npm",
23
+ "homepage": "https://apiverve.com/marketplace/codedetector?utm_source=npm",
22
24
  "devDependencies": {
23
25
  "mocha": "^11.0.1",
24
26
  "chai": "^5.1.2",
@@ -27,6 +29,6 @@
27
29
  "dependencies": {
28
30
  "node-fetch": "^3.3.2",
29
31
  "promise": "^8.3.0",
30
- "axios": "1.7.9"
32
+ "axios": "1.8.4"
31
33
  }
32
34
  }