-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(core): Add
importWalletSetupFile
function
- Loading branch information
1 parent
4c83abf
commit 1bd65d2
Showing
5 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,32 @@ | ||
import { z } from 'zod' | ||
import type { WalletSetupFunction } from '../defineWalletSetup' | ||
|
||
// TODO: Add hash length validation. | ||
const WalletSetupModule = z.object({ | ||
default: z.object({ | ||
hash: z.string(), | ||
fn: z.function().returns(z.promise(z.void())) | ||
}) | ||
}) | ||
|
||
export async function importWalletSetupFile(walletSetupFilePath: string) { | ||
const walletSetupModule = await import(walletSetupFilePath) | ||
|
||
const result = WalletSetupModule.safeParse(walletSetupModule) | ||
if (!result.success) { | ||
throw new Error( | ||
[ | ||
`[ImportWalletSetupFile] Invalid wallet setup function at ${walletSetupFilePath}`, | ||
'Remember that all wallet setup files must export the wallet setup function as a default export!' | ||
].join('\n') | ||
) | ||
} | ||
|
||
const { hash, fn } = result.data.default | ||
|
||
// TODO: Can we somehow validate this function type with Zod? | ||
return { | ||
hash, | ||
fn: fn as WalletSetupFunction | ||
} | ||
} |
This file contains 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,52 @@ | ||
import { afterAll, describe, expect, it, vi } from 'vitest' | ||
import { importWalletSetupFile } from '../../src/utils/importWalletSetupFile' | ||
|
||
vi.mock('./valid.setup.ts', async () => { | ||
return { | ||
default: { | ||
hash: 'bca3f22838f317ed9e87', | ||
fn: async () => { | ||
return | ||
} | ||
} | ||
} | ||
}) | ||
|
||
vi.mock('./invalid.setup.ts', async () => { | ||
return { | ||
default: { | ||
duck: 'Quack! 🦆' | ||
} | ||
} | ||
}) | ||
|
||
describe('importWalletSetupFile', () => { | ||
afterAll(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
// This error is from `await import`. | ||
it('throws if the target file does not exist', async () => { | ||
const nonExistentFilePath = './non-existent-file.ts' | ||
await expect(importWalletSetupFile(nonExistentFilePath)).rejects.toThrowError( | ||
`Failed to load url ${nonExistentFilePath}` | ||
) | ||
}) | ||
|
||
it('throws if the target file is not a valid wallet setup file', async () => { | ||
const invalidFilePath = './invalid.setup.ts' | ||
await expect(importWalletSetupFile(invalidFilePath)).rejects.toThrowError( | ||
`[ImportWalletSetupFile] Invalid wallet setup function at ${invalidFilePath}` | ||
) | ||
}) | ||
|
||
it('returns the hash and function of a valid wallet setup file', async () => { | ||
const validFilePath = './valid.setup.ts' | ||
const result = await importWalletSetupFile(validFilePath) | ||
|
||
expect(result).toEqual({ | ||
hash: 'bca3f22838f317ed9e87', | ||
fn: expect.any(Function) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.