Skip to content

Commit 25d5f41

Browse files
authored
Merge pull request #136 from lighthouse-web3/v0.4.2
Delete function in CLI
2 parents 8fa5441 + f03da24 commit 25d5f41

File tree

8 files changed

+118
-5
lines changed

8 files changed

+118
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Lighthouse <img src="https://img.shields.io/badge/v0.4.1-green"/>
1+
# Lighthouse <img src="https://img.shields.io/badge/v0.4.2-green"/>
22

33
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.
44

@@ -35,6 +35,7 @@ lighthouse-web3 deal-status <cid> # Get filecoin deal status of a
3535

3636
# File management
3737
lighthouse-web3 get-uploads # Get details of files uploaded
38+
lighthouse-web3 delete-file <fileID> # Delete a file
3839

3940
# Sharing and access control
4041
lighthouse-web3 share-file <cid> <address> # Share access to another user
@@ -80,3 +81,4 @@ This project is tested with [BrowserStack](https://www.browserstack.com/).
8081
## License
8182

8283
[MIT](https://choosealicense.com/licenses/mit/)
84+
```

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lighthouse-web3/sdk",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "NPM package and CLI tool to interact with lighthouse protocol",
55
"main": "./dist/Lighthouse/index.js",
66
"types": "./dist/Lighthouse/index.d.ts",

src/Commands/delete-file.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { yellow, red, green } from 'kleur'
2+
import { config } from './utils/getNetwork'
3+
import lighthouse from '../Lighthouse'
4+
5+
export default async function (fileId: string) {
6+
try {
7+
if (!config.get('LIGHTHOUSE_GLOBAL_API_KEY')) {
8+
throw new Error('Please create api-key first: use api-key command')
9+
}
10+
11+
if (!fileId) {
12+
throw new Error('Please provide a file ID to delete.')
13+
}
14+
15+
const response = await lighthouse.deleteFile(
16+
config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string,
17+
fileId
18+
)
19+
20+
console.log(green('Success: ') + yellow(response.data.message))
21+
} catch (error: any) {
22+
console.log(red(error.message))
23+
process.exit(0)
24+
}
25+
}

src/Commands/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import apiKey from './api-key'
99
import balance from './balance'
1010
import shareFile from './share-file'
1111
import getUploads from './get-uploads'
12+
import deleteFile from './delete-file'
1213
import dealStatus from './deal-status'
1314
import decryptFile from './decrypt-file'
1415
import createWallet from './create-wallet'
@@ -71,7 +72,7 @@ Command.prototype.helpInformation = function (context: any) {
7172
}
7273

7374
widgets.addHelpText('before', 'Welcome to lighthouse-web3')
74-
widgets.version('0.4.1')
75+
widgets.version('0.4.2')
7576

7677
widgets
7778
.command('wallet')
@@ -158,6 +159,12 @@ widgets
158159
.description('Get details of file uploaded')
159160
.action(getUploads)
160161

162+
widgets
163+
.command('delete-file')
164+
.description('Delete a file')
165+
.argument('<fileID>', 'File ID')
166+
.action(deleteFile)
167+
161168
widgets.addHelpText(
162169
'after',
163170
'\r\nExample:' +

src/Lighthouse/deleteFile/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { lighthouseConfig } from '../../lighthouse.config'
2+
3+
export type deleteFileResponseType = {
4+
data: {
5+
message: string
6+
}
7+
}
8+
9+
export default async (
10+
authToken: string,
11+
fileId: string
12+
): Promise<deleteFileResponseType> => {
13+
try {
14+
const response = await fetch(
15+
`${lighthouseConfig.lighthouseAPI}/api/user/delete_file?id=${fileId}`,
16+
{
17+
method: 'DELETE',
18+
headers: {
19+
Authorization: `Bearer ${authToken}`,
20+
'Content-Type': 'application/json',
21+
},
22+
}
23+
)
24+
25+
if (!response.ok) {
26+
throw new Error(`Request failed with status code ${response.status}`)
27+
}
28+
29+
const result = (await response.json()) as any
30+
/*
31+
Example response:
32+
{
33+
"message": "File deleted successfully."
34+
}
35+
*/
36+
return { data: result }
37+
} catch (error: any) {
38+
throw new Error(error.message)
39+
}
40+
}

src/Lighthouse/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dealStatus from './dealStatus'
55
import getUploads from './getUploads'
66
import getFileInfo from './getFileInfo'
77
import createWallet from './createWallet'
8+
import deleteFile from './deleteFile'
89

910
// Pay per deal
1011
import fund from './payPerDeal/fund'
@@ -64,6 +65,7 @@ export {
6465
getAllKeys,
6566
removeKey,
6667
posdi,
68+
deleteFile,
6769
}
6870

6971
export default {
@@ -94,4 +96,5 @@ export default {
9496
getAllKeys,
9597
removeKey,
9698
posdi,
99+
deleteFile,
97100
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import lighthouse from '..'
2+
import 'dotenv/config'
3+
4+
describe('deleteFile', () => {
5+
const apiKey = process.env.TEST_API_KEY as string
6+
const fileId = process.env.TEST_FILE_ID as string
7+
8+
it('should delete a file with correct API key and file ID', async () => {
9+
const uploadsRes = await lighthouse.getUploads(apiKey)
10+
const fileList = uploadsRes.data.fileList
11+
expect(fileList.length).toBeGreaterThan(0)
12+
13+
const firstFileId = fileList[0].id
14+
expect(typeof firstFileId).toBe('string')
15+
16+
const res = await lighthouse.deleteFile(apiKey, firstFileId)
17+
expect(res.data.message).toBe('File deleted successfully.')
18+
}, 60000)
19+
20+
it('should not delete a file with invalid API key', async () => {
21+
try {
22+
await lighthouse.deleteFile('invalid.APIKey', fileId)
23+
} catch (error: any) {
24+
expect(error.message).toBe('Request failed with status code 401')
25+
}
26+
}, 60000)
27+
28+
it('should not delete a file with invalid file ID', async () => {
29+
try {
30+
await lighthouse.deleteFile(apiKey, 'invalid-file-id')
31+
} catch (error: any) {
32+
// The error message may vary depending on API response
33+
expect(error.message).toMatch(/Request failed with status code/i)
34+
}
35+
}, 60000)
36+
})

0 commit comments

Comments
 (0)