@forgehive/hive-sdk 0.0.3 → 0.0.4
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/index.d.ts +23 -0
- package/dist/index.js +53 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -64,3 +64,26 @@ export declare class HiveLogClient {
|
|
|
64
64
|
setQuality(taskName: string, uuid: string, quality: Quality): Promise<boolean>;
|
|
65
65
|
}
|
|
66
66
|
export declare const createHiveLogClient: (config: HiveLogClientConfig) => HiveLogClient;
|
|
67
|
+
export interface HiveClientConfig {
|
|
68
|
+
projectUuid: string;
|
|
69
|
+
apiKey?: string;
|
|
70
|
+
apiSecret?: string;
|
|
71
|
+
host?: string;
|
|
72
|
+
}
|
|
73
|
+
export interface InvokeResponse {
|
|
74
|
+
responsePayload: unknown;
|
|
75
|
+
}
|
|
76
|
+
export interface InvokeError {
|
|
77
|
+
error: string;
|
|
78
|
+
}
|
|
79
|
+
export type InvokeResult = InvokeResponse | InvokeError;
|
|
80
|
+
export declare function isInvokeError(response: unknown): response is InvokeError;
|
|
81
|
+
export declare class HiveClient {
|
|
82
|
+
private apiKey;
|
|
83
|
+
private apiSecret;
|
|
84
|
+
private host;
|
|
85
|
+
private projectUuid;
|
|
86
|
+
constructor(config: HiveClientConfig);
|
|
87
|
+
invoke(taskName: string, payload: unknown): Promise<InvokeResult | null>;
|
|
88
|
+
}
|
|
89
|
+
export declare const createHiveClient: (config: HiveClientConfig) => HiveClient;
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createHiveLogClient = exports.HiveLogClient = void 0;
|
|
6
|
+
exports.createHiveClient = exports.HiveClient = exports.createHiveLogClient = exports.HiveLogClient = void 0;
|
|
7
7
|
exports.isApiError = isApiError;
|
|
8
|
+
exports.isInvokeError = isInvokeError;
|
|
8
9
|
const axios_1 = __importDefault(require("axios"));
|
|
9
10
|
const debug_1 = __importDefault(require("debug"));
|
|
10
11
|
const log = (0, debug_1.default)('hive-sdk');
|
|
@@ -142,3 +143,54 @@ const createHiveLogClient = (config) => {
|
|
|
142
143
|
return new HiveLogClient(config);
|
|
143
144
|
};
|
|
144
145
|
exports.createHiveLogClient = createHiveLogClient;
|
|
146
|
+
// Type guard to check if invoke response is an error
|
|
147
|
+
function isInvokeError(response) {
|
|
148
|
+
return response !== null && typeof response === 'object' && 'error' in response;
|
|
149
|
+
}
|
|
150
|
+
class HiveClient {
|
|
151
|
+
constructor(config) {
|
|
152
|
+
const apiKey = config.apiKey || process.env.HIVE_API_KEY;
|
|
153
|
+
const apiSecret = config.apiSecret || process.env.HIVE_API_SECRET;
|
|
154
|
+
const host = config.host || process.env.HIVE_HOST || 'https://forgehive.dev';
|
|
155
|
+
if (!apiKey || !apiSecret) {
|
|
156
|
+
throw new Error('Missing Hive API credentials. Please provide apiKey and apiSecret, or set HIVE_API_KEY and HIVE_API_SECRET environment variables. Get them at https://forgehive.dev');
|
|
157
|
+
}
|
|
158
|
+
this.projectUuid = config.projectUuid;
|
|
159
|
+
this.host = host;
|
|
160
|
+
this.apiKey = apiKey;
|
|
161
|
+
this.apiSecret = apiSecret;
|
|
162
|
+
log('HiveClient initialized for project "%s" with host "%s"', config.projectUuid, host);
|
|
163
|
+
}
|
|
164
|
+
async invoke(taskName, payload) {
|
|
165
|
+
try {
|
|
166
|
+
const invokeUrl = `${this.host}/api/project/${this.projectUuid}/task/${taskName}/invoke`;
|
|
167
|
+
log('Invoking task "%s" at %s', taskName, invokeUrl);
|
|
168
|
+
const authToken = `${this.apiKey}:${this.apiSecret}`;
|
|
169
|
+
const response = await axios_1.default.post(invokeUrl, {
|
|
170
|
+
payload
|
|
171
|
+
}, {
|
|
172
|
+
headers: {
|
|
173
|
+
Authorization: `Bearer ${authToken}`,
|
|
174
|
+
'Content-Type': 'application/json'
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
log('Success: Invoked task "%s"', taskName);
|
|
178
|
+
return response.data;
|
|
179
|
+
}
|
|
180
|
+
catch (e) {
|
|
181
|
+
const error = e;
|
|
182
|
+
log('Error: Failed to invoke task "%s": %s', taskName, error.message);
|
|
183
|
+
// Check if it's an axios error with response data
|
|
184
|
+
if (axios_1.default.isAxiosError(error) && error.response?.data) {
|
|
185
|
+
return error.response.data;
|
|
186
|
+
}
|
|
187
|
+
return { error: error.message };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
exports.HiveClient = HiveClient;
|
|
192
|
+
const createHiveClient = (config) => {
|
|
193
|
+
log('Creating HiveClient for project "%s"', config.projectUuid);
|
|
194
|
+
return new HiveClient(config);
|
|
195
|
+
};
|
|
196
|
+
exports.createHiveClient = createHiveClient;
|