-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement export functionality #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,4 @@ src-tauri/gen/ | |
| # Local security scan artifacts | ||
| .secrets.baseline | ||
| .secretscan-venv/ | ||
| coverage/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| legacy-peer-deps=true |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import type { SecretItem } from '../../types'; | ||
| import { buildSecretMetadata, exportSecretMetadata } from './secretsExport'; | ||
|
|
||
| function makeSecret(overrides?: Partial<SecretItem>): SecretItem { | ||
| return { | ||
| id: 'id-1', | ||
| name: 'secret-a', | ||
| enabled: true, | ||
| created: '2025-01-01T00:00:00Z', | ||
| updated: '2025-01-02T00:00:00Z', | ||
| expires: null, | ||
| notBefore: null, | ||
| contentType: 'text/plain', | ||
| tags: { env: 'dev' }, | ||
| managed: null, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('secretsExport', () => { | ||
| it('builds metadata rows without secret values', () => { | ||
| const out = buildSecretMetadata([ | ||
| makeSecret(), | ||
| makeSecret({ name: 'secret-b', tags: null, contentType: null }), | ||
| ]); | ||
|
|
||
| expect(out).toEqual([ | ||
| { | ||
| name: 'secret-a', | ||
| enabled: true, | ||
| created: '2025-01-01T00:00:00Z', | ||
| updated: '2025-01-02T00:00:00Z', | ||
| expires: null, | ||
| contentType: 'text/plain', | ||
| tags: '{"env":"dev"}', | ||
| }, | ||
| { | ||
| name: 'secret-b', | ||
| enabled: true, | ||
| created: '2025-01-01T00:00:00Z', | ||
| updated: '2025-01-02T00:00:00Z', | ||
| expires: null, | ||
| contentType: null, | ||
| tags: '', | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('exports and downloads when primary path succeeds', async () => { | ||
| const exportItems = vi.fn<(...args: [string, 'json' | 'csv']) => Promise<string>>(); | ||
| exportItems.mockResolvedValue('payload-json'); | ||
| const download = vi.fn<(content: string, format: 'json' | 'csv') => void>(); | ||
| const writeClipboard = vi.fn<(content: string) => Promise<void>>(); | ||
| writeClipboard.mockResolvedValue(); | ||
| const onError = vi.fn<(error: unknown) => void>(); | ||
| const onSuccess = vi.fn<(mode: 'download' | 'clipboard') => void>(); | ||
|
|
||
| await exportSecretMetadata([makeSecret()], 'json', { | ||
| exportItems, | ||
| download, | ||
| writeClipboard, | ||
| onError, | ||
| onSuccess, | ||
| }); | ||
|
|
||
| expect(exportItems).toHaveBeenCalledTimes(1); | ||
| expect(exportItems).toHaveBeenCalledWith( | ||
| JSON.stringify([ | ||
| { | ||
| name: 'secret-a', | ||
| enabled: true, | ||
| created: '2025-01-01T00:00:00Z', | ||
| updated: '2025-01-02T00:00:00Z', | ||
| expires: null, | ||
| contentType: 'text/plain', | ||
| tags: '{"env":"dev"}', | ||
| }, | ||
| ]), | ||
| 'json', | ||
| ); | ||
| expect(download).toHaveBeenCalledWith('payload-json', 'json'); | ||
| expect(writeClipboard).not.toHaveBeenCalled(); | ||
| expect(onError).not.toHaveBeenCalled(); | ||
| expect(onSuccess).toHaveBeenCalledWith('download'); | ||
| }); | ||
|
|
||
| it('falls back to clipboard when download fails', async () => { | ||
| const exportItems = vi.fn<(...args: [string, 'json' | 'csv']) => Promise<string>>(); | ||
| exportItems.mockResolvedValue('payload-csv'); | ||
| const download = vi.fn<(content: string, format: 'json' | 'csv') => void>(); | ||
| download.mockImplementation(() => { | ||
| throw new Error('download blocked'); | ||
| }); | ||
| const writeClipboard = vi.fn<(content: string) => Promise<void>>(); | ||
| writeClipboard.mockResolvedValue(); | ||
| const onError = vi.fn<(error: unknown) => void>(); | ||
| const onSuccess = vi.fn<(mode: 'download' | 'clipboard') => void>(); | ||
|
|
||
| await exportSecretMetadata([makeSecret()], 'csv', { | ||
| exportItems, | ||
| download, | ||
| writeClipboard, | ||
| onError, | ||
| onSuccess, | ||
| }); | ||
|
|
||
| expect(download).toHaveBeenCalledWith('payload-csv', 'csv'); | ||
| expect(writeClipboard).toHaveBeenCalledWith('payload-csv'); | ||
| expect(onError).not.toHaveBeenCalled(); | ||
| expect(onSuccess).toHaveBeenCalledWith('clipboard'); | ||
| }); | ||
|
|
||
| it('reports error when both download and clipboard are unavailable', async () => { | ||
| const exportItems = vi.fn<(...args: [string, 'json' | 'csv']) => Promise<string>>(); | ||
| exportItems.mockResolvedValue('payload-json'); | ||
| const download = vi.fn<(content: string, format: 'json' | 'csv') => void>(); | ||
| download.mockImplementation(() => { | ||
| throw new Error('download blocked'); | ||
| }); | ||
| const onError = vi.fn<(error: unknown) => void>(); | ||
|
|
||
| await exportSecretMetadata([makeSecret()], 'json', { | ||
| exportItems, | ||
| download, | ||
| onError, | ||
| }); | ||
|
|
||
| expect(onError).toHaveBeenCalledTimes(1); | ||
| expect(String(onError.mock.calls[0][0])).toContain('Unable to download or copy export.'); | ||
| }); | ||
|
|
||
| it('reports backend export errors', async () => { | ||
| const exportItems = vi.fn<(...args: [string, 'json' | 'csv']) => Promise<string>>(); | ||
| exportItems.mockRejectedValue(new Error('backend failed')); | ||
| const download = vi.fn<(content: string, format: 'json' | 'csv') => void>(); | ||
| const onError = vi.fn<(error: unknown) => void>(); | ||
|
|
||
| await exportSecretMetadata([makeSecret()], 'json', { | ||
| exportItems, | ||
| download, | ||
| onError, | ||
| }); | ||
|
|
||
| expect(download).not.toHaveBeenCalled(); | ||
| expect(onError).toHaveBeenCalledTimes(1); | ||
| expect(String(onError.mock.calls[0][0])).toContain('backend failed'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { SecretItem } from '../../types'; | ||
|
|
||
| export type ExportFormat = 'json' | 'csv'; | ||
|
|
||
| type ExportItemsFn = (itemsJson: string, format: ExportFormat) => Promise<string>; | ||
| type DownloadFn = (content: string, format: ExportFormat) => void; | ||
| type ClipboardFn = (content: string) => Promise<void>; | ||
| type ErrorFn = (error: unknown) => void; | ||
| type SuccessFn = (mode: 'download' | 'clipboard') => void; | ||
|
|
||
| export type SecretExportMetadata = { | ||
| name: string; | ||
| enabled: boolean; | ||
| created: string | null; | ||
| updated: string | null; | ||
| expires: string | null; | ||
| contentType: string | null; | ||
| tags: string; | ||
| }; | ||
|
|
||
| export function buildSecretMetadata(items: SecretItem[]): SecretExportMetadata[] { | ||
| return items.map(({ name, enabled, created, updated, expires, contentType, tags }) => ({ | ||
| name, | ||
| enabled, | ||
| created, | ||
| updated, | ||
| expires, | ||
| contentType, | ||
| tags: tags ? JSON.stringify(tags) : '', | ||
| })); | ||
| } | ||
|
|
||
| export async function exportSecretMetadata( | ||
| items: SecretItem[], | ||
| format: ExportFormat, | ||
| deps: { | ||
| exportItems: ExportItemsFn; | ||
| download: DownloadFn; | ||
| writeClipboard?: ClipboardFn; | ||
| onError?: ErrorFn; | ||
| onSuccess?: SuccessFn; | ||
| }, | ||
| ): Promise<void> { | ||
| const { exportItems, download, writeClipboard, onError, onSuccess } = deps; | ||
|
|
||
| try { | ||
| const metadata = buildSecretMetadata(items); | ||
| const result = await exportItems(JSON.stringify(metadata), format); | ||
|
|
||
| try { | ||
| download(result, format); | ||
| onSuccess?.('download'); | ||
| } catch { | ||
| if (!writeClipboard) { | ||
| throw new Error('Unable to download or copy export.'); | ||
| } | ||
| await writeClipboard(result); | ||
| onSuccess?.('clipboard'); | ||
| } | ||
| } catch (error) { | ||
| onError?.(error); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
downloadExportfunction is defined inside theSecretsListcomponent, which causes it to be recreated on every render. Since this function does not depend on any component props or state, it can be moved outside the component's scope. This is a React best practice that improves performance by avoiding unnecessary function re-creations and makes the component's rendering logic cleaner.