From 4023f6b73866ea346c28c0c1144a3788ce006d0c Mon Sep 17 00:00:00 2001 From: Aakash Taneja Date: Thu, 4 Sep 2025 12:51:46 +0530 Subject: [PATCH 1/2] Bump version to 0.4.2 and add delete-file command in cli and package with tests --- README.md | 4 ++- package-lock.json | 4 +-- package.json | 2 +- src/Commands/delete-file.ts | 25 ++++++++++++++++ src/Commands/index.ts | 9 +++++- src/Lighthouse/deleteFile/index.ts | 40 +++++++++++++++++++++++++ src/Lighthouse/index.ts | 3 ++ src/Lighthouse/tests/deleteFile.test.ts | 29 ++++++++++++++++++ 8 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 src/Commands/delete-file.ts create mode 100644 src/Lighthouse/deleteFile/index.ts create mode 100644 src/Lighthouse/tests/deleteFile.test.ts diff --git a/README.md b/README.md index e60ebbfe..ab27c044 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Lighthouse +# Lighthouse Lighthouse is a permanent decentralized file storage protocol that allows the ability to pay once and store forever. While traditionally, users need to repeatedly keep track and pay for their storage after every fixed amount of time, Lighthouse manages this for them and makes sure that user files are stored forever. The aim is to move users from a rent-based cost model where they are renting their own files on cloud storage to a permanent ownership model. It is built on top of IPFS, Filecoin, and Polygon. It uses the existing miner network and storage capacity of the filecoin network. @@ -35,6 +35,7 @@ lighthouse-web3 deal-status # Get filecoin deal status of a # File management lighthouse-web3 get-uploads # Get details of files uploaded +lighthouse-web3 delete-file # Delete a file # Sharing and access control lighthouse-web3 share-file
# Share access to another user @@ -80,3 +81,4 @@ This project is tested with [BrowserStack](https://www.browserstack.com/). ## License [MIT](https://choosealicense.com/licenses/mit/) +``` diff --git a/package-lock.json b/package-lock.json index f51e2383..96cbbb2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.4.1", + "version": "0.4.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lighthouse-web3/sdk", - "version": "0.4.1", + "version": "0.4.2", "license": "MIT", "dependencies": { "@lighthouse-web3/kavach": "^0.1.9", diff --git a/package.json b/package.json index 1a28d5a5..f319fdbb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.4.1", + "version": "0.4.2", "description": "NPM package and CLI tool to interact with lighthouse protocol", "main": "./dist/Lighthouse/index.js", "types": "./dist/Lighthouse/index.d.ts", diff --git a/src/Commands/delete-file.ts b/src/Commands/delete-file.ts new file mode 100644 index 00000000..58026e0e --- /dev/null +++ b/src/Commands/delete-file.ts @@ -0,0 +1,25 @@ +import { yellow, red, green } from 'kleur' +import { config } from './utils/getNetwork' +import lighthouse from '../Lighthouse' + +export default async function (fileId: string) { + try { + if (!config.get('LIGHTHOUSE_GLOBAL_API_KEY')) { + throw new Error('Please create api-key first: use api-key command') + } + + if (!fileId) { + throw new Error('Please provide a file ID to delete.') + } + + const response = await lighthouse.deleteFile( + config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string, + fileId + ) + + console.log(green('Success: ') + yellow(response.data.message)) + } catch (error: any) { + console.log(red(error.message)) + process.exit(0) + } +} diff --git a/src/Commands/index.ts b/src/Commands/index.ts index 34acd0a0..b1cfb13e 100644 --- a/src/Commands/index.ts +++ b/src/Commands/index.ts @@ -9,6 +9,7 @@ import apiKey from './api-key' import balance from './balance' import shareFile from './share-file' import getUploads from './get-uploads' +import deleteFile from './delete-file' import dealStatus from './deal-status' import decryptFile from './decrypt-file' import createWallet from './create-wallet' @@ -71,7 +72,7 @@ Command.prototype.helpInformation = function (context: any) { } widgets.addHelpText('before', 'Welcome to lighthouse-web3') -widgets.version('0.4.1') +widgets.version('0.4.2') widgets .command('wallet') @@ -158,6 +159,12 @@ widgets .description('Get details of file uploaded') .action(getUploads) +widgets + .command('delete-file') + .description('Delete a file') + .argument('', 'File ID') + .action(deleteFile) + widgets.addHelpText( 'after', '\r\nExample:' + diff --git a/src/Lighthouse/deleteFile/index.ts b/src/Lighthouse/deleteFile/index.ts new file mode 100644 index 00000000..bde4d23c --- /dev/null +++ b/src/Lighthouse/deleteFile/index.ts @@ -0,0 +1,40 @@ +import { lighthouseConfig } from '../../lighthouse.config' + +export type deleteFileResponseType = { + data: { + message: string + } +} + +export default async ( + authToken: string, + fileId: string +): Promise => { + try { + const response = await fetch( + `${lighthouseConfig.lighthouseAPI}/api/user/delete_file?id=${fileId}`, + { + method: 'DELETE', + headers: { + Authorization: `Bearer ${authToken}`, + 'Content-Type': 'application/json', + }, + } + ) + + if (!response.ok) { + throw new Error(`Request failed with status code ${response.status}`) + } + + const result = (await response.json()) as any + /* + Example response: + { + "message": "File deleted successfully." + } + */ + return { data: result } + } catch (error: any) { + throw new Error(error.message) + } +} diff --git a/src/Lighthouse/index.ts b/src/Lighthouse/index.ts index 1ab28067..1edaa211 100644 --- a/src/Lighthouse/index.ts +++ b/src/Lighthouse/index.ts @@ -5,6 +5,7 @@ import dealStatus from './dealStatus' import getUploads from './getUploads' import getFileInfo from './getFileInfo' import createWallet from './createWallet' +import deleteFile from './deleteFile' // Pay per deal import fund from './payPerDeal/fund' @@ -64,6 +65,7 @@ export { getAllKeys, removeKey, posdi, + deleteFile, } export default { @@ -94,4 +96,5 @@ export default { getAllKeys, removeKey, posdi, + deleteFile, } diff --git a/src/Lighthouse/tests/deleteFile.test.ts b/src/Lighthouse/tests/deleteFile.test.ts new file mode 100644 index 00000000..694e08bf --- /dev/null +++ b/src/Lighthouse/tests/deleteFile.test.ts @@ -0,0 +1,29 @@ +import lighthouse from '..' +import 'dotenv/config' + +describe('deleteFile', () => { + const apiKey = process.env.TEST_API_KEY as string + const fileId = process.env.TEST_FILE_ID as string + + it('should delete a file with correct API key and file ID', async () => { + const res = await lighthouse.deleteFile(apiKey, fileId) + expect(res.data.message).toBe('File deleted successfully.') + }, 60000) + + it('should not delete a file with invalid API key', async () => { + try { + await lighthouse.deleteFile('invalid.APIKey', fileId) + } catch (error: any) { + expect(error.message).toBe('Request failed with status code 401') + } + }, 60000) + + it('should not delete a file with invalid file ID', async () => { + try { + await lighthouse.deleteFile(apiKey, 'invalid-file-id') + } catch (error: any) { + // The error message may vary depending on API response + expect(error.message).toMatch(/Request failed with status code/i) + } + }, 60000) +}) From f03da24adcf51bde4376928885cd10ce379a94fd Mon Sep 17 00:00:00 2001 From: Aakash Taneja Date: Tue, 9 Sep 2025 15:18:42 +0530 Subject: [PATCH 2/2] updated test case for deletefile function --- src/Lighthouse/tests/deleteFile.test.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Lighthouse/tests/deleteFile.test.ts b/src/Lighthouse/tests/deleteFile.test.ts index 694e08bf..f72968e3 100644 --- a/src/Lighthouse/tests/deleteFile.test.ts +++ b/src/Lighthouse/tests/deleteFile.test.ts @@ -6,7 +6,14 @@ describe('deleteFile', () => { const fileId = process.env.TEST_FILE_ID as string it('should delete a file with correct API key and file ID', async () => { - const res = await lighthouse.deleteFile(apiKey, fileId) + const uploadsRes = await lighthouse.getUploads(apiKey) + const fileList = uploadsRes.data.fileList + expect(fileList.length).toBeGreaterThan(0) + + const firstFileId = fileList[0].id + expect(typeof firstFileId).toBe('string') + + const res = await lighthouse.deleteFile(apiKey, firstFileId) expect(res.data.message).toBe('File deleted successfully.') }, 60000)