rajt
Version:
A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.
15 lines (12 loc) • 437 B
text/typescript
import { readFileSync } from 'node:fs'
import { dirname, join } from 'pathe'
export default function jsonImport<T = any>(filePath: string, defaultValue: T = {} as T): T {
const __dirname = dirname(new URL(import.meta.url).pathname)
try {
const fullPath = join(__dirname, filePath)
const fileContent = readFileSync(fullPath, 'utf-8')
return JSON.parse(fileContent) as T
} catch (error) {
return defaultValue
}
}