forked from mirrors/homebox
feat: items-editor (#5)
* format readme * update logo * format html * add logo to docs * repository for document and document tokens * add attachments type and repository * autogenerate types via scripts * use autogenerated types * attachment type updates * add insured and quantity fields for items * implement HasID interface for entities * implement label updates for items * implement service update method * WIP item update client side actions * check err on attachment * finish types for basic items editor * remove unused var * house keeping
This commit is contained in:
parent
fbc364dcd2
commit
95ab14b866
125 changed files with 15626 additions and 1791 deletions
30
frontend/lib/api/base/base-api.test.ts
Normal file
30
frontend/lib/api/base/base-api.test.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { describe, expect, test } from "vitest";
|
||||
import { hasKey, parseDate } from "./base-api";
|
||||
|
||||
describe("hasKey works as expected", () => {
|
||||
test("hasKey returns true if the key exists", () => {
|
||||
const obj = { createdAt: "2021-01-01" };
|
||||
expect(hasKey(obj, "createdAt")).toBe(true);
|
||||
});
|
||||
|
||||
test("hasKey returns false if the key does not exist", () => {
|
||||
const obj = { createdAt: "2021-01-01" };
|
||||
expect(hasKey(obj, "updatedAt")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseDate should work as expected", () => {
|
||||
test("parseDate should set defaults", () => {
|
||||
const obj = { createdAt: "2021-01-01", updatedAt: "2021-01-01" };
|
||||
const result = parseDate(obj);
|
||||
expect(result.createdAt).toBeInstanceOf(Date);
|
||||
expect(result.updatedAt).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
test("parseDate should set passed in types", () => {
|
||||
const obj = { key1: "2021-01-01", key2: "2021-01-01" };
|
||||
const result = parseDate(obj, ["key1", "key2"]);
|
||||
expect(result.key1).toBeInstanceOf(Date);
|
||||
expect(result.key2).toBeInstanceOf(Date);
|
||||
});
|
||||
});
|
|
@ -8,10 +8,50 @@ import { Requests } from "../../requests";
|
|||
// TDeleteResult = void
|
||||
// >
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export class BaseAPI {
|
||||
http: Requests;
|
||||
|
||||
constructor(requests: Requests) {
|
||||
this.http = requests;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
dropFields<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
|
||||
delete result[key];
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue