@forgehive/hive-sdk 0.0.2 → 0.0.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +84 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forgehive/hive-sdk",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -221,3 +221,87 @@ export const createHiveLogClient = (config: HiveLogClientConfig): HiveLogClient
221
221
  log('Creating HiveLogClient for project "%s"', config.projectName)
222
222
  return new HiveLogClient(config)
223
223
  }
224
+
225
+ // Configuration interface for HiveClient
226
+ export interface HiveClientConfig {
227
+ projectUuid: string
228
+ apiKey?: string
229
+ apiSecret?: string
230
+ host?: string
231
+ }
232
+
233
+ // Response types for invoke method
234
+ export interface InvokeResponse {
235
+ responsePayload: unknown
236
+ }
237
+
238
+ export interface InvokeError {
239
+ error: string
240
+ }
241
+
242
+ export type InvokeResult = InvokeResponse | InvokeError
243
+
244
+ // Type guard to check if invoke response is an error
245
+ export function isInvokeError(response: unknown): response is InvokeError {
246
+ return response !== null && typeof response === 'object' && 'error' in response
247
+ }
248
+
249
+ export class HiveClient {
250
+ private apiKey: string
251
+ private apiSecret: string
252
+ private host: string
253
+ private projectUuid: string
254
+
255
+ constructor(config: HiveClientConfig) {
256
+ const apiKey = config.apiKey || process.env.HIVE_API_KEY
257
+ const apiSecret = config.apiSecret || process.env.HIVE_API_SECRET
258
+ const host = config.host || process.env.HIVE_HOST || 'https://forgehive.dev'
259
+
260
+ if (!apiKey || !apiSecret) {
261
+ 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')
262
+ }
263
+
264
+ this.projectUuid = config.projectUuid
265
+ this.host = host
266
+ this.apiKey = apiKey
267
+ this.apiSecret = apiSecret
268
+
269
+ log('HiveClient initialized for project "%s" with host "%s"', config.projectUuid, host)
270
+ }
271
+
272
+ async invoke(taskName: string, payload: unknown): Promise<InvokeResult | null> {
273
+ try {
274
+ const invokeUrl = `${this.host}/api/project/${this.projectUuid}/task/${taskName}/invoke`
275
+ log('Invoking task "%s" at %s', taskName, invokeUrl)
276
+
277
+ const authToken = `${this.apiKey}:${this.apiSecret}`
278
+
279
+ const response = await axios.post(invokeUrl, {
280
+ payload
281
+ }, {
282
+ headers: {
283
+ Authorization: `Bearer ${authToken}`,
284
+ 'Content-Type': 'application/json'
285
+ }
286
+ })
287
+
288
+ log('Success: Invoked task "%s"', taskName)
289
+ return response.data as InvokeResult
290
+ } catch (e) {
291
+ const error = e as Error
292
+ log('Error: Failed to invoke task "%s": %s', taskName, error.message)
293
+
294
+ // Check if it's an axios error with response data
295
+ if (axios.isAxiosError(error) && error.response?.data) {
296
+ return error.response.data as InvokeError
297
+ }
298
+
299
+ return { error: error.message }
300
+ }
301
+ }
302
+ }
303
+
304
+ export const createHiveClient = (config: HiveClientConfig): HiveClient => {
305
+ log('Creating HiveClient for project "%s"', config.projectUuid)
306
+ return new HiveClient(config)
307
+ }