@closedloop-ai/cli 0.2.0
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/dist/__tests__/cli.test.js +37 -0
- package/dist/__tests__/cli.test.js.map +1 -0
- package/dist/__tests__/config.test.js +33 -0
- package/dist/__tests__/config.test.js.map +1 -0
- package/dist/__tests__/errors.test.js +109 -0
- package/dist/__tests__/errors.test.js.map +1 -0
- package/dist/__tests__/setup.js +20 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/__tests__/validation.test.js +124 -0
- package/dist/__tests__/validation.test.js.map +1 -0
- package/dist/api/auth.js +27 -0
- package/dist/api/auth.js.map +1 -0
- package/dist/api/feedback.js +70 -0
- package/dist/api/feedback.js.map +1 -0
- package/dist/api/input.js +60 -0
- package/dist/api/input.js.map +1 -0
- package/dist/api/usage.js +49 -0
- package/dist/api/usage.js.map +1 -0
- package/dist/cli.js +156 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/config.js +186 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/feedback.js +234 -0
- package/dist/commands/feedback.js.map +1 -0
- package/dist/commands/input.js +370 -0
- package/dist/commands/input.js.map +1 -0
- package/dist/commands/list.js +81 -0
- package/dist/commands/list.js.map +1 -0
- package/dist/commands/status.js +93 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/submit.js +225 -0
- package/dist/commands/submit.js.map +1 -0
- package/dist/commands/usage.js +168 -0
- package/dist/commands/usage.js.map +1 -0
- package/dist/config/constants.js +37 -0
- package/dist/config/constants.js.map +1 -0
- package/dist/config/index.js +84 -0
- package/dist/config/index.js.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/config.js +55 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/errors.js +70 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/validation.js +133 -0
- package/dist/utils/validation.js.map +1 -0
- package/jest.config.js +14 -0
- package/package.json +42 -0
- package/src/__tests__/cli.test.ts +44 -0
- package/src/__tests__/config.test.ts +38 -0
- package/src/__tests__/errors.test.ts +132 -0
- package/src/__tests__/setup.ts +21 -0
- package/src/__tests__/validation.test.ts +156 -0
- package/src/api/auth.ts +21 -0
- package/src/api/feedback.ts +64 -0
- package/src/api/input.ts +55 -0
- package/src/api/usage.ts +45 -0
- package/src/cli.ts +158 -0
- package/src/commands/config.ts +192 -0
- package/src/commands/feedback.ts +250 -0
- package/src/commands/input.ts +389 -0
- package/src/commands/list.ts +84 -0
- package/src/commands/status.ts +96 -0
- package/src/commands/submit.ts +214 -0
- package/src/config/constants.ts +36 -0
- package/src/config/index.ts +59 -0
- package/src/types/index.ts +119 -0
- package/src/utils/errors.ts +98 -0
- package/src/utils/validation.ts +166 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export class CliError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
message: string,
|
|
4
|
+
public code: string,
|
|
5
|
+
public exitCode: number = 1
|
|
6
|
+
) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'CliError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const ERROR_CODES = {
|
|
13
|
+
NO_API_KEY: 'NO_API_KEY',
|
|
14
|
+
INVALID_API_KEY: 'INVALID_API_KEY',
|
|
15
|
+
INVALID_INPUT: 'INVALID_INPUT',
|
|
16
|
+
API_ERROR: 'API_ERROR',
|
|
17
|
+
VALIDATION_ERROR: 'VALIDATION_ERROR',
|
|
18
|
+
CONNECTION_ERROR: 'CONNECTION_ERROR',
|
|
19
|
+
PROCESSING_ERROR: 'PROCESSING_ERROR',
|
|
20
|
+
NOT_FOUND: 'NOT_FOUND',
|
|
21
|
+
UNAUTHORIZED: 'UNAUTHORIZED',
|
|
22
|
+
RATE_LIMITED: 'RATE_LIMITED',
|
|
23
|
+
SERVER_ERROR: 'SERVER_ERROR',
|
|
24
|
+
UNKNOWN_ERROR: 'UNKNOWN_ERROR'
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
export function handleApiError(error: any): CliError {
|
|
28
|
+
if (error.response) {
|
|
29
|
+
const status = error.response.status;
|
|
30
|
+
const data = error.response.data;
|
|
31
|
+
|
|
32
|
+
switch (status) {
|
|
33
|
+
case 401:
|
|
34
|
+
return new CliError(
|
|
35
|
+
'Invalid API key. Please check your configuration.',
|
|
36
|
+
ERROR_CODES.UNAUTHORIZED,
|
|
37
|
+
1
|
|
38
|
+
);
|
|
39
|
+
case 404:
|
|
40
|
+
return new CliError(
|
|
41
|
+
'Resource not found. Please check the ID and try again.',
|
|
42
|
+
ERROR_CODES.NOT_FOUND,
|
|
43
|
+
1
|
|
44
|
+
);
|
|
45
|
+
case 429:
|
|
46
|
+
return new CliError(
|
|
47
|
+
'Rate limit exceeded. Please wait a moment and try again.',
|
|
48
|
+
ERROR_CODES.RATE_LIMITED,
|
|
49
|
+
1
|
|
50
|
+
);
|
|
51
|
+
case 500:
|
|
52
|
+
case 502:
|
|
53
|
+
case 503:
|
|
54
|
+
case 504:
|
|
55
|
+
return new CliError(
|
|
56
|
+
'Server error. Please try again later.',
|
|
57
|
+
ERROR_CODES.SERVER_ERROR,
|
|
58
|
+
1
|
|
59
|
+
);
|
|
60
|
+
default:
|
|
61
|
+
return new CliError(
|
|
62
|
+
data?.message || `API error (${status})`,
|
|
63
|
+
ERROR_CODES.API_ERROR,
|
|
64
|
+
1
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
} else if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
|
|
68
|
+
return new CliError(
|
|
69
|
+
'Cannot connect to ClosedLoop AI servers. Please check your internet connection.',
|
|
70
|
+
ERROR_CODES.CONNECTION_ERROR,
|
|
71
|
+
1
|
|
72
|
+
);
|
|
73
|
+
} else if (error.code === 'ETIMEDOUT') {
|
|
74
|
+
return new CliError(
|
|
75
|
+
'Request timed out. Please try again.',
|
|
76
|
+
ERROR_CODES.CONNECTION_ERROR,
|
|
77
|
+
1
|
|
78
|
+
);
|
|
79
|
+
} else {
|
|
80
|
+
return new CliError(
|
|
81
|
+
error.message || 'An unknown error occurred',
|
|
82
|
+
ERROR_CODES.UNKNOWN_ERROR,
|
|
83
|
+
1
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function handleValidationError(message: string): CliError {
|
|
89
|
+
return new CliError(message, ERROR_CODES.VALIDATION_ERROR, 1);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function handleInputError(message: string): CliError {
|
|
93
|
+
return new CliError(message, ERROR_CODES.INVALID_INPUT, 1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function handleProcessingError(message: string): CliError {
|
|
97
|
+
return new CliError(message, ERROR_CODES.PROCESSING_ERROR, 1);
|
|
98
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { CliError, handleValidationError, handleInputError, ERROR_CODES } from './errors';
|
|
2
|
+
import { CONFIG } from '../config/constants';
|
|
3
|
+
|
|
4
|
+
// UUID validation
|
|
5
|
+
export function validateUuid(id: string): boolean {
|
|
6
|
+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
7
|
+
return uuidRegex.test(id);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Content validation
|
|
11
|
+
export function validateInputContent(content: string): string {
|
|
12
|
+
if (!content || content.trim().length === 0) {
|
|
13
|
+
throw handleInputError('Content cannot be empty');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (content.length > CONFIG.MAX_CONTENT_LENGTH) {
|
|
17
|
+
throw handleInputError(`Content too long (max ${CONFIG.MAX_CONTENT_LENGTH} characters)`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return content.trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// API key validation
|
|
24
|
+
export function validateApiKey(apiKey: string): string {
|
|
25
|
+
if (!apiKey || apiKey.trim().length === 0 || apiKey.length < CONFIG.MIN_API_KEY_LENGTH) {
|
|
26
|
+
throw handleValidationError('Invalid API key. Get your free API key at https://closedloop.sh');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return apiKey.trim();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Email validation
|
|
33
|
+
export function validateEmail(email: string): string {
|
|
34
|
+
if (!email || email.trim().length === 0) {
|
|
35
|
+
throw handleValidationError('Email cannot be empty');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const trimmedEmail = email.trim();
|
|
39
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
40
|
+
if (!emailRegex.test(trimmedEmail)) {
|
|
41
|
+
throw handleValidationError('Invalid email format');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return trimmedEmail.toLowerCase();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// URL validation
|
|
48
|
+
export function validateUrl(url: string): string {
|
|
49
|
+
if (!url || url.trim().length === 0) {
|
|
50
|
+
throw handleValidationError('URL cannot be empty');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const urlObj = new URL(url);
|
|
55
|
+
if (!['http:', 'https:'].includes(urlObj.protocol)) {
|
|
56
|
+
throw handleValidationError('URL must use HTTP or HTTPS protocol');
|
|
57
|
+
}
|
|
58
|
+
return urlObj.toString();
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw handleValidationError('Invalid URL format');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Pagination validation
|
|
65
|
+
export function validatePagination(page?: number, limit?: number): { page: number; limit: number } {
|
|
66
|
+
const validatedPage = Math.max(1, page || 1);
|
|
67
|
+
const validatedLimit = Math.min(
|
|
68
|
+
Math.max(1, limit || CONFIG.DEFAULT_PAGE_SIZE),
|
|
69
|
+
CONFIG.MAX_PAGE_SIZE
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return { page: validatedPage, limit: validatedLimit };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Title validation
|
|
76
|
+
export function validateTitle(title: string): string {
|
|
77
|
+
if (!title || title.trim().length === 0) {
|
|
78
|
+
throw handleValidationError('Title cannot be empty');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (title.length > 200) {
|
|
82
|
+
throw handleValidationError('Title too long (max 200 characters)');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return title.trim();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Customer ID validation
|
|
89
|
+
export function validateCustomerId(customerId: string): string {
|
|
90
|
+
if (!customerId || customerId.trim().length === 0) {
|
|
91
|
+
throw handleValidationError('Customer ID cannot be empty');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (customerId.length > 100) {
|
|
95
|
+
throw handleValidationError('Customer ID too long (max 100 characters)');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return customerId.trim();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Name validation
|
|
102
|
+
export function validateName(name: string): string {
|
|
103
|
+
if (!name || name.trim().length === 0) {
|
|
104
|
+
throw handleValidationError('Name cannot be empty');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (name.length > 100) {
|
|
108
|
+
throw handleValidationError('Name too long (max 100 characters)');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return name.trim();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Sanitize input to prevent XSS
|
|
115
|
+
export function sanitizeInput(input: string): string {
|
|
116
|
+
return input
|
|
117
|
+
.replace(/[<>]/g, '') // Remove < and >
|
|
118
|
+
.replace(/javascript:/gi, '') // Remove javascript: protocol
|
|
119
|
+
.replace(/on\w+=/gi, '') // Remove event handlers
|
|
120
|
+
.trim();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Validate and sanitize all input data
|
|
124
|
+
export function validateAndSanitizeInput(data: {
|
|
125
|
+
content: string;
|
|
126
|
+
title?: string;
|
|
127
|
+
source_url?: string;
|
|
128
|
+
customer_id?: string;
|
|
129
|
+
reporter_name?: string;
|
|
130
|
+
reporter_email?: string;
|
|
131
|
+
}): {
|
|
132
|
+
content: string;
|
|
133
|
+
title?: string;
|
|
134
|
+
source_url?: string;
|
|
135
|
+
customer_id?: string;
|
|
136
|
+
reporter_name?: string;
|
|
137
|
+
reporter_email?: string;
|
|
138
|
+
} {
|
|
139
|
+
const validated: any = {};
|
|
140
|
+
|
|
141
|
+
// Required fields
|
|
142
|
+
validated.content = sanitizeInput(validateInputContent(data.content));
|
|
143
|
+
|
|
144
|
+
// Optional fields
|
|
145
|
+
if (data.title) {
|
|
146
|
+
validated.title = sanitizeInput(validateTitle(data.title));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (data.source_url) {
|
|
150
|
+
validated.source_url = validateUrl(data.source_url);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (data.customer_id) {
|
|
154
|
+
validated.customer_id = sanitizeInput(validateCustomerId(data.customer_id));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (data.reporter_name) {
|
|
158
|
+
validated.reporter_name = sanitizeInput(validateName(data.reporter_name));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (data.reporter_email) {
|
|
162
|
+
validated.reporter_email = validateEmail(data.reporter_email);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return validated;
|
|
166
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"declaration": false,
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"removeComments": false,
|
|
15
|
+
"moduleResolution": "node",
|
|
16
|
+
"resolveJsonModule": true
|
|
17
|
+
},
|
|
18
|
+
"include": [
|
|
19
|
+
"src/**/*"
|
|
20
|
+
],
|
|
21
|
+
"exclude": [
|
|
22
|
+
"node_modules",
|
|
23
|
+
"dist"
|
|
24
|
+
]
|
|
25
|
+
}
|