2022-09-09 22:46:53 +00:00
|
|
|
import { Requests } from "../../requests";
|
2022-08-31 00:06:57 +00:00
|
|
|
// <
|
|
|
|
// TGetResult,
|
|
|
|
// TPostData,
|
|
|
|
// TPostResult,
|
|
|
|
// TPutData = TPostData,
|
|
|
|
// TPutResult = TPostResult,
|
|
|
|
// TDeleteResult = void
|
|
|
|
// >
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
type BaseApiType = {
|
|
|
|
createdAt: string;
|
|
|
|
updatedAt: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function hasKey(obj: object, key: string): obj is Required<BaseApiType> {
|
|
|
|
return typeof obj[key] === "string";
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseDate<T>(obj: T, keys: Array<keyof T> = []): T {
|
|
|
|
const result = { ...obj };
|
|
|
|
[...keys, "createdAt", "updatedAt"].forEach(key => {
|
|
|
|
// @ts-ignore - we are checking for the key above
|
|
|
|
if (hasKey(result, key)) {
|
|
|
|
// @ts-ignore - we are guarding against this above
|
|
|
|
result[key] = new Date(result[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:06:57 +00:00
|
|
|
export class BaseAPI {
|
2022-08-31 05:22:10 +00:00
|
|
|
http: Requests;
|
2022-08-31 00:06:57 +00:00
|
|
|
|
2022-08-31 05:22:10 +00:00
|
|
|
constructor(requests: Requests) {
|
|
|
|
this.http = requests;
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dropFields will remove any fields that are specified in the fields array
|
|
|
|
* additionally, it will remove the `createdAt` and `updatedAt` fields if they
|
|
|
|
* are present. This is useful for when you want to send a subset of fields to
|
|
|
|
* the server like when performing an update.
|
|
|
|
*/
|
2022-10-09 17:23:21 +00:00
|
|
|
protected dropFields<T>(obj: T, keys: Array<keyof T> = []): T {
|
2022-09-12 22:47:27 +00:00
|
|
|
const result = { ...obj };
|
|
|
|
[...keys, "createdAt", "updatedAt"].forEach(key => {
|
|
|
|
// @ts-ignore - we are checking for the key above
|
|
|
|
if (hasKey(result, key)) {
|
|
|
|
// @ts-ignore - we are guarding against this above
|
|
|
|
delete result[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2022-08-31 00:06:57 +00:00
|
|
|
}
|