Skip to content

Commit

Permalink
Update distributed files
Browse files Browse the repository at this point in the history
  • Loading branch information
saltie2193 committed Nov 29, 2022
1 parent 6497138 commit 1ba2f50
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 70 deletions.
47 changes: 25 additions & 22 deletions dist/DataResponse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,22 @@ interface DataResponseInterface<T> {
succeeded: () => boolean;
msg?: string;
}
/**
* _Convenient data status handling_
*/
declare class DataResponse<T> implements DataResponseInterface<T> {
data: T;
status: DataStatus;
msg?: string;
/**
* Creates new {@link DataResponse} from an old instance with new {@link DataStatus}.
* Data and message will be coppied from the old instance to the new instance.
* Data and message will be copied from the old instance to the new instance.
*
* @param {DataResponse<T>} resp - Old {@link DataResponse} instance.
* @param {DataStatus} status - New {@link DataStatus} to use.
* @returns {DataResponse<T>} New {@link DataResponse} instance.
*/
static fromDataResponse<T>(resp: DataResponse<T>, status: DataStatus): DataResponse<T>;
constructor(data: T, status?: DataStatus, msg?: string);
succeeded(): boolean;
/**
* Checks if the {@link DataResponse} is empty.
* If the field `data` is `null` or `undefined` the object is an instance of {@link EmptyResponse}.
* Otherwise its an instance of {@link DataResponse}.
*/
isEmpty(): this is DataResponse<null>;
/**
* Creates a new EmptyResponse without data, with given status and msg.
* If a message is provided it will be logged as a warning.
Expand All @@ -43,13 +38,12 @@ declare class DataResponse<T> implements DataResponseInterface<T> {
*/
static empty(status?: DataStatus, msg?: string): EmptyResponse;
/**
* Creates a new {@link DataResponse} with status {@link DataStatus.OK}.
* Creates new {@link EmptyResponse} with {@link DataStatus.NOT_FOUND}.
*
* @param data - Data of the created {@link DataResponse}.
* @param {string} msg - Message of the created {@link DataResponse}.
* @returns {DataResponse} New {@link DataResponse}.
* @param {string} msg - Message of the created {@link EmptyResponse}.
* @returns {EmptyResponse} New {@link EmptyResponse}.
*/
static ok<T>(data: T, msg?: string): DataResponse<T>;
static notFound(msg?: string): EmptyResponse;
/**
* Creates new {@link EmptyResponse} with {@link DataStatus.Error}.
*
Expand All @@ -58,19 +52,20 @@ declare class DataResponse<T> implements DataResponseInterface<T> {
*/
static error(msg?: string): EmptyResponse;
/**
* Creates new {@link EmptyResponse} with {@link DataStatus.NOT_FOUND}.
* Creates new {@link EmptyResponse} with {@link DataStatus.API_ERROR}.
*
* @param {string} msg - Messag of the created {@link EmptyResponse}.
* @param {string} msg - Message of the created {@link EmptyResponse}.
* @returns {EmptyResponse} New {@link EmptyResponse}.
*/
static notFound(msg?: string): EmptyResponse;
static apiError(msg?: string): EmptyResponse;
/**
* Creates new {@link EmptyResponse} with {@link DataStatus.API_ERROR}.
* Creates a new {@link DataResponse} with status {@link DataStatus.OK}.
*
* @param {string} msg - Messag of the created {@link EmptyResponse}.
* @returns {EmptyResponse} New {@link EmptyResponse}.
* @param data - Data of the created {@link DataResponse}.
* @param {string} msg - Message of the created {@link DataResponse}.
* @returns {DataResponse} New {@link DataResponse}.
*/
static apiError(msg?: string): EmptyResponse;
static ok<T>(data: T, msg?: string): DataResponse<T>;
/**
* Creates new {@link DataResponse} with status {@link DataStatus.CACHED}.
* If the passed data is already a {@link DataResponse} a new {@link DataResponse} will be created.
Expand All @@ -80,8 +75,16 @@ declare class DataResponse<T> implements DataResponseInterface<T> {
* @returns
*/
static cached<T>(data: T): DataResponse<T>;
constructor(data: T, status?: DataStatus, msg?: string);
succeeded(): boolean;
/**
* Checks if the {@link DataResponse} is empty.
* If the field `data` is `null` or `undefined` the object is an instance of {@link EmptyResponse}.
* Otherwise its an instance of {@link DataResponse}.
*/
isEmpty(): this is DataResponse<null>;
/**
* Check if given {@link DataStatus} is interpreted as a successfull response.
* Check if given {@link DataStatus} is interpreted as a successful response.
* @param {DataStatus} status
* @returns {boolean}
*/
Expand Down
71 changes: 37 additions & 34 deletions dist/DataResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ var DataStatus;
DataStatus["NOT_FOUND"] = "not found";
DataStatus["API_ERROR"] = "api error";
})(DataStatus || (DataStatus = {}));
/**
* _Convenient data status handling_
*/
class DataResponse {
data;
status;
msg;
/**
* Creates new {@link DataResponse} from an old instance with new {@link DataStatus}.
* Data and message will be coppied from the old instance to the new instance.
* Data and message will be copied from the old instance to the new instance.
*
* @param {DataResponse<T>} resp - Old {@link DataResponse} instance.
* @param {DataStatus} status - New {@link DataStatus} to use.
Expand All @@ -22,23 +25,6 @@ class DataResponse {
static fromDataResponse(resp, status) {
return new DataResponse(resp.data, status, resp.msg);
}
constructor(data, status = DataStatus.OK, msg) {
this.data = data;
this.status = status;
if (msg)
this.msg = msg;
}
succeeded() {
return DataResponse.isSuccess(this.status);
}
/**
* Checks if the {@link DataResponse} is empty.
* If the field `data` is `null` or `undefined` the object is an instance of {@link EmptyResponse}.
* Otherwise its an instance of {@link DataResponse}.
*/
isEmpty() {
return DataResponse.isEmpty(this);
}
/**
* Creates a new EmptyResponse without data, with given status and msg.
* If a message is provided it will be logged as a warning.
Expand All @@ -53,14 +39,13 @@ class DataResponse {
return new DataResponse(null, status, msg);
}
/**
* Creates a new {@link DataResponse} with status {@link DataStatus.OK}.
* Creates new {@link EmptyResponse} with {@link DataStatus.NOT_FOUND}.
*
* @param data - Data of the created {@link DataResponse}.
* @param {string} msg - Message of the created {@link DataResponse}.
* @returns {DataResponse} New {@link DataResponse}.
* @param {string} msg - Message of the created {@link EmptyResponse}.
* @returns {EmptyResponse} New {@link EmptyResponse}.
*/
static ok(data, msg) {
return new DataResponse(data, DataStatus.OK, msg);
static notFound(msg) {
return DataResponse.empty(DataStatus.NOT_FOUND, msg);
}
/**
* Creates new {@link EmptyResponse} with {@link DataStatus.Error}.
Expand All @@ -72,22 +57,23 @@ class DataResponse {
return DataResponse.empty(DataStatus.ERROR, msg);
}
/**
* Creates new {@link EmptyResponse} with {@link DataStatus.NOT_FOUND}.
* Creates new {@link EmptyResponse} with {@link DataStatus.API_ERROR}.
*
* @param {string} msg - Messag of the created {@link EmptyResponse}.
* @param {string} msg - Message of the created {@link EmptyResponse}.
* @returns {EmptyResponse} New {@link EmptyResponse}.
*/
static notFound(msg) {
return DataResponse.empty(DataStatus.NOT_FOUND, msg);
static apiError(msg) {
return DataResponse.empty(DataStatus.API_ERROR, msg);
}
/**
* Creates new {@link EmptyResponse} with {@link DataStatus.API_ERROR}.
* Creates a new {@link DataResponse} with status {@link DataStatus.OK}.
*
* @param {string} msg - Messag of the created {@link EmptyResponse}.
* @returns {EmptyResponse} New {@link EmptyResponse}.
* @param data - Data of the created {@link DataResponse}.
* @param {string} msg - Message of the created {@link DataResponse}.
* @returns {DataResponse} New {@link DataResponse}.
*/
static apiError(msg) {
return DataResponse.empty(DataStatus.API_ERROR, msg);
static ok(data, msg) {
return new DataResponse(data, DataStatus.OK, msg);
}
/**
* Creates new {@link DataResponse} with status {@link DataStatus.CACHED}.
Expand All @@ -103,8 +89,25 @@ class DataResponse {
}
return new DataResponse(data, DataStatus.CACHED);
}
constructor(data, status = DataStatus.OK, msg) {
this.data = data;
this.status = status;
if (msg)
this.msg = msg;
}
succeeded() {
return DataResponse.isSuccess(this.status);
}
/**
* Checks if the {@link DataResponse} is empty.
* If the field `data` is `null` or `undefined` the object is an instance of {@link EmptyResponse}.
* Otherwise its an instance of {@link DataResponse}.
*/
isEmpty() {
return DataResponse.isEmpty(this);
}
/**
* Check if given {@link DataStatus} is interpreted as a successfull response.
* Check if given {@link DataStatus} is interpreted as a successful response.
* @param {DataStatus} status
* @returns {boolean}
*/
Expand Down
14 changes: 7 additions & 7 deletions dist/FileManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ declare class NewFileManager implements FileManager {
* 2. Checks if the given file is stored in the iCloud and downloads it if necessary.
* 3. Uses the given function to read the file and returns the result.
*
* @param {string} filePath - Path to file, relative to the basedor of the {@link NewFileManager}.
* @param {string} filePath - Path to file, relative to the basedir of the {@link NewFileManager}.
* @param {(path: string) => T} fn - Function to read the given file.
* @returns {DataResponse<T>}
*/
Expand Down Expand Up @@ -133,7 +133,7 @@ declare class NewFileManager implements FileManager {
* Note that it is always safe to call `downloadFileFromiCloud(filePath)`, even if the file is stored locally on the device.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns
*/
readJSON(filePath: string, autoExtension?: boolean): Record<string, unknown> | unknown[] | number | string | null;
Expand All @@ -147,7 +147,7 @@ declare class NewFileManager implements FileManager {
* If the file does not exists or downloading fails, an {@link EmptyResponse} will be returned.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns
*/
readJSONS(filePath: string, autoExtension?: boolean): Promise<EmptyResponse | DataResponse<Record<string, unknown> | unknown[] | number | string | null>>;
Expand All @@ -161,10 +161,10 @@ declare class NewFileManager implements FileManager {
* Use `fileExists(filePath)` to check if a file exists and `downloadFileFromiCloud(filePath)` to download the file.
* Note that it is always safe to call `downloadFileFromiCloud(filePath)`, even if the file is stored locally on the device.
*
* If the content of the file is not a JSON dictonary `null` will be returned.
* If the content of the file is not a JSON dictionary `null` will be returned.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns
*/
readJSONDict(filePath: string, autoExtension?: boolean): Record<string, unknown> | null;
Expand All @@ -180,7 +180,7 @@ declare class NewFileManager implements FileManager {
* If the content of the file cannot be read as a JSON dictionary an {@link EmptyResponse} will be returned.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns A {@link DataResponse} containing the content of the file as {@link Record}. An {@link EmptyResponse} if the file does not exist or the content cannot be read as JSON dictionary.
*/
readJSONDictS(filePath: string, autoExtension?: boolean): Promise<EmptyResponse | DataResponse<Record<string, unknown>>>;
Expand Down Expand Up @@ -220,7 +220,7 @@ declare class NewFileManager implements FileManager {
*
* Writes the content to the specified file path on disk.
* If the file does not already exist, it will be created.
* If the file already exists the contents of the file will beoverwritten with the new content.
* If the file already exists the contents of the file will be overwritten with the new content.
* @param filePath - Path of file to write to, relative to the basedir of the {@link NewFileManager}.
* @param content - Content to write to disk, relative to the basedir of the {@link NewFileManager}.
*/
Expand Down
14 changes: 7 additions & 7 deletions dist/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class NewFileManager {
* 2. Checks if the given file is stored in the iCloud and downloads it if necessary.
* 3. Uses the given function to read the file and returns the result.
*
* @param {string} filePath - Path to file, relative to the basedor of the {@link NewFileManager}.
* @param {string} filePath - Path to file, relative to the basedir of the {@link NewFileManager}.
* @param {(path: string) => T} fn - Function to read the given file.
* @returns {DataResponse<T>}
*/
Expand Down Expand Up @@ -273,7 +273,7 @@ class NewFileManager {
* Note that it is always safe to call `downloadFileFromiCloud(filePath)`, even if the file is stored locally on the device.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns
*/
readJSON(filePath, autoExtension = true) {
Expand All @@ -293,7 +293,7 @@ class NewFileManager {
* If the file does not exists or downloading fails, an {@link EmptyResponse} will be returned.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns
*/
async readJSONS(filePath, autoExtension = true) {
Expand All @@ -315,10 +315,10 @@ class NewFileManager {
* Use `fileExists(filePath)` to check if a file exists and `downloadFileFromiCloud(filePath)` to download the file.
* Note that it is always safe to call `downloadFileFromiCloud(filePath)`, even if the file is stored locally on the device.
*
* If the content of the file is not a JSON dictonary `null` will be returned.
* If the content of the file is not a JSON dictionary `null` will be returned.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns
*/
readJSONDict(filePath, autoExtension = true) {
Expand All @@ -341,7 +341,7 @@ class NewFileManager {
* If the content of the file cannot be read as a JSON dictionary an {@link EmptyResponse} will be returned.
*
* @param filePath - Path of the file to read, relatively to the basedir of the {@link NewFileManager}.
* @param autoExtension - Automatcally add '.json' extension to the file path.
* @param autoExtension - Wether to automatically add '.json' extension to the file path.
* @returns A {@link DataResponse} containing the content of the file as {@link Record}. An {@link EmptyResponse} if the file does not exist or the content cannot be read as JSON dictionary.
*/
async readJSONDictS(filePath, autoExtension = true) {
Expand Down Expand Up @@ -399,7 +399,7 @@ class NewFileManager {
*
* Writes the content to the specified file path on disk.
* If the file does not already exist, it will be created.
* If the file already exists the contents of the file will beoverwritten with the new content.
* If the file already exists the contents of the file will be overwritten with the new content.
* @param filePath - Path of file to write to, relative to the basedir of the {@link NewFileManager}.
* @param content - Content to write to disk, relative to the basedir of the {@link NewFileManager}.
*/
Expand Down

0 comments on commit 1ba2f50

Please sign in to comment.