mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 18:26:00 +00:00
SYNC: Merge pull request #5 from dbgate/feature/firestore
This commit is contained in:
38
packages/web/src/utility/parseFileAsJson.js
Normal file
38
packages/web/src/utility/parseFileAsJson.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @template [T = any]
|
||||
* @typedef {Object} FileParseResultSuccess
|
||||
* @property {true} success
|
||||
* @property {T} data
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} FileParseResultError
|
||||
* @property {false} success
|
||||
* @property {string} error
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template [T = any]
|
||||
* @typedef {FileParseResultSuccess<T> | FileParseResultError} FileParseResult
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template [T = any]
|
||||
* @param {File} file
|
||||
* @returns {Promise<FileParseResult<T>>}
|
||||
*/
|
||||
export async function parseFileAsJson(file) {
|
||||
try {
|
||||
const text = await file.text();
|
||||
const data = JSON.parse(text);
|
||||
return {
|
||||
success: true,
|
||||
data,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown parsing error',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user