Skip to content

Commit

Permalink
libs: blueos: Use path access on bag-of-holding to simplify queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Feb 1, 2024
1 parent c1438ae commit f222fb4
Showing 1 changed file with 5 additions and 34 deletions.
39 changes: 5 additions & 34 deletions src/libs/blueos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@ import ky from 'ky'
/* eslint-disable @typescript-eslint/no-explicit-any */
export const getBagOfHoldingFromVehicle = async (
vehicleAddress: string,
bagName: string
): Promise<Record<string, any>> => {
bagPath: string
): Promise<Record<string, any> | any> => {
try {
return await ky.get(`http://${vehicleAddress}/bag/v1.0/get/${bagName}`, { timeout: 5000 }).json()
return await ky.get(`http://${vehicleAddress}/bag/v1.0/get/${bagPath}`, { timeout: 5000, retry: 0 }).json()
} catch (error) {
throw new Error(`Could not get bag of holdings for ${bagName}. ${error}`)
}
}

export const getCockpitStorageFromVehicle = async (vehicleAddress: string): Promise<Record<string, any>> => {
try {
return await getBagOfHoldingFromVehicle(vehicleAddress, 'cockpit')
} catch (error) {
throw new Error(`Could not get Cockpit's storage data from vehicle. ${error}`)
}
}

export const getKeyDataFromCockpitVehicleStorage = async (
vehicleAddress: string,
storageKey: string
): Promise<Record<string, any> | undefined> => {
const cockpitVehicleStorage = await getCockpitStorageFromVehicle(vehicleAddress)
return cockpitVehicleStorage[storageKey]
return await getBagOfHoldingFromVehicle(vehicleAddress, `cockpit/${storageKey}`)
}

export const setBagOfHoldingOnVehicle = async (
Expand All @@ -40,32 +31,12 @@ export const setBagOfHoldingOnVehicle = async (
}
}

export const setCockpitStorageOnVehicle = async (
vehicleAddress: string,
storageData: Record<string, any> | any
): Promise<void> => {
try {
await setBagOfHoldingOnVehicle(vehicleAddress, 'cockpit', storageData)
} catch (error) {
throw new Error(`Could not set Cockpit's storage data on vehicle. ${error}`)
}
}

export const setKeyDataOnCockpitVehicleStorage = async (
vehicleAddress: string,
storageKey: string,
storageData: Record<string, any> | any
): Promise<void> => {
let previousVehicleStorage: Record<string, any> = {}
try {
previousVehicleStorage = await getCockpitStorageFromVehicle(vehicleAddress)
} catch (error) {
console.error(error)
}
const newVehicleStorage = previousVehicleStorage
newVehicleStorage[storageKey] = storageData

await setCockpitStorageOnVehicle(vehicleAddress, newVehicleStorage)
await setBagOfHoldingOnVehicle(vehicleAddress, `cockpit/${storageKey}`, storageData)
}

/* eslint-disable jsdoc/require-jsdoc */
Expand Down

0 comments on commit f222fb4

Please sign in to comment.