2022-09-09 22:46:53 +00:00
|
|
|
import { Requests } from "../../requests";
|
2023-07-23 04:11:29 +00:00
|
|
|
import { route } from ".";
|
2022-08-31 00:06:57 +00:00
|
|
|
|
2023-01-01 21:50:48 +00:00
|
|
|
const ZERO_DATE = "0001-01-01T00:00:00Z";
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
type BaseApiType = {
|
|
|
|
createdAt: string;
|
|
|
|
updatedAt: string;
|
2023-02-13 19:43:09 +00:00
|
|
|
|
2022-11-01 02:43:30 +00:00
|
|
|
[key: string]: any;
|
2022-09-12 22:47:27 +00:00
|
|
|
};
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
export function hasKey(obj: Record<string, any>, key: string): obj is Required<BaseApiType> {
|
|
|
|
return key in obj ? typeof obj[key] === "string" : false;
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseDate<T>(obj: T, keys: Array<keyof T> = []): T {
|
|
|
|
const result = { ...obj };
|
|
|
|
[...keys, "createdAt", "updatedAt"].forEach(key => {
|
2023-02-15 17:40:35 +00:00
|
|
|
// @ts-expect-error - TS doesn't know that we're checking for the key above
|
2022-09-12 22:47:27 +00:00
|
|
|
if (hasKey(result, key)) {
|
2023-02-15 17:40:35 +00:00
|
|
|
const value = result[key] as string;
|
|
|
|
|
|
|
|
if (value === undefined || value === "" || value.startsWith(ZERO_DATE)) {
|
2023-01-01 21:50:48 +00:00
|
|
|
const dt = new Date();
|
|
|
|
dt.setFullYear(1);
|
|
|
|
|
|
|
|
result[key] = dt;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:40:35 +00:00
|
|
|
// Possible Formats
|
|
|
|
// Date Only: YYYY-MM-DD
|
|
|
|
// Timestamp: 0001-01-01T00:00:00Z
|
|
|
|
|
|
|
|
// Parse timestamps with default date
|
|
|
|
if (value.includes("T")) {
|
|
|
|
result[key] = new Date(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse dates with default time
|
|
|
|
const split = value.split("-");
|
|
|
|
|
|
|
|
if (split.length !== 3) {
|
|
|
|
console.log(`Invalid date format: ${value}`);
|
|
|
|
throw new Error(`Invalid date format: ${value}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [year, month, day] = split;
|
|
|
|
|
|
|
|
const dt = new Date();
|
|
|
|
|
|
|
|
dt.setFullYear(parseInt(year, 10));
|
|
|
|
dt.setMonth(parseInt(month, 10) - 1);
|
|
|
|
dt.setDate(parseInt(day, 10));
|
|
|
|
|
|
|
|
result[key] = dt;
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:06:57 +00:00
|
|
|
export class BaseAPI {
|
2022-08-31 05:22:10 +00:00
|
|
|
http: Requests;
|
2022-12-03 19:55:00 +00:00
|
|
|
attachmentToken: string;
|
2022-08-31 00:06:57 +00:00
|
|
|
|
2022-12-03 19:55:00 +00:00
|
|
|
constructor(requests: Requests, attachmentToken = "") {
|
2022-08-31 05:22:10 +00:00
|
|
|
this.http = requests;
|
2022-12-03 19:55:00 +00:00
|
|
|
this.attachmentToken = attachmentToken;
|
|
|
|
}
|
|
|
|
|
2023-07-23 04:11:29 +00:00
|
|
|
// if an attachmentToken is present, it will be added to URL as a query param
|
2022-12-03 19:55:00 +00:00
|
|
|
// this is done with a simple appending of the query param to the URL. If your
|
|
|
|
// URL already has a query param, this will not work.
|
|
|
|
authURL(url: string): string {
|
|
|
|
if (this.attachmentToken) {
|
2023-07-23 04:11:29 +00:00
|
|
|
return route(url, { access_token: this.attachmentToken });
|
2022-12-03 19:55:00 +00:00
|
|
|
}
|
|
|
|
return url;
|
2022-08-31 05:22:10 +00:00
|
|
|
}
|
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
|
|
|
}
|