I guess it builds now?
This commit is contained in:
parent
ac9c21d58e
commit
a06c8a3883
@ -16,14 +16,20 @@
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export interface FieldErrorResponse {
|
||||||
|
code: number;
|
||||||
|
message: string;
|
||||||
|
errors: Record<string, ObjectErrorContent>;
|
||||||
|
}
|
||||||
|
|
||||||
export function FieldErrors(
|
export type ErrorContent = { code: string; message: string };
|
||||||
fields: Record<string, { code?: string; message: string }>,
|
export type ObjectErrorContent = { _errors: ErrorContent[] };
|
||||||
) {
|
|
||||||
|
export function FieldErrors(fields: Record<string, { code?: string; message: string }>) {
|
||||||
return new FieldError(
|
return new FieldError(
|
||||||
50035,
|
50035,
|
||||||
"Invalid Form Body",
|
"Invalid Form Body",
|
||||||
fields.map(({ message, code }) => ({
|
fields.map<ErrorContent, ObjectErrorContent>(({ message, code }) => ({
|
||||||
_errors: [
|
_errors: [
|
||||||
{
|
{
|
||||||
message,
|
message,
|
||||||
|
|||||||
@ -6,10 +6,10 @@ import assert from "node:assert/strict";
|
|||||||
|
|
||||||
describe("Object extensions", () => {
|
describe("Object extensions", () => {
|
||||||
it("forEach", async () => {
|
it("forEach", async () => {
|
||||||
const obj = { a: 1, b: 2, c: 3 };
|
const obj: { [index:string]: number } = { a: 1, b: 2, c: 3 };
|
||||||
const keys: string[] = [];
|
const keys: string[] = [];
|
||||||
const values: number[] = [];
|
const values: number[] = [];
|
||||||
obj.forEach((value, key) => {
|
obj.forEach<number>((value, key, _) => {
|
||||||
keys.push(key);
|
keys.push(key);
|
||||||
values.push(value);
|
values.push(value);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,31 +1,34 @@
|
|||||||
declare global {
|
declare global {
|
||||||
interface Object {
|
interface Object {
|
||||||
forEach(callback: (value: unknown, key: string, object: unknown) => void): void;
|
forEach<T>(callback: (value: T, key: string, object: { [index: string]: T }) => void): void;
|
||||||
map<T>(callback: (value: unknown, key: string, object: unknown) => T): object;
|
map<SV, TV>(callback: (value: SV, key: string, object: { [index: string]: SV }) => TV): { [index: string]: TV };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function objectForEach<T>(obj: never, callback: (value: T, key: string, object: unknown) => void): void {
|
export function objectForEach<T>(obj: { [index: string]: T }, callback: (value: T, key: string, object: { [index: string]: T }) => void): void {
|
||||||
Object.keys(obj).forEach((key) => {
|
Object.keys(obj).forEach((key) => {
|
||||||
callback(obj[key], key, obj);
|
callback(obj[key], key, obj);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function objectMap<T>(obj: object, callback: (value: unknown, key: string, object: unknown) => T): T[] {
|
export function objectMap<SV, TV>(srcObj: { [index: string]: SV }, callback: (value: SV, key: string, object: { [index: string]: SV }) => TV): { [index: string]: TV } {
|
||||||
return Object.keys(obj).map((key) => {
|
if (typeof callback !== "function") throw new TypeError(`${callback} is not a function`);
|
||||||
return callback(obj[key], key, obj);
|
const obj: { [index: string]: TV } = {};
|
||||||
|
Object.keys(srcObj).forEach((key) => {
|
||||||
|
obj[key] = callback(srcObj[key], key, srcObj);
|
||||||
});
|
});
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Object.prototype.forEach)
|
if (!Object.prototype.forEach)
|
||||||
Object.defineProperty(Object.prototype, "forEach", {
|
Object.defineProperty(Object.prototype, "forEach", {
|
||||||
value: objectForEach,
|
value: objectForEach,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true
|
writable: true,
|
||||||
});
|
});
|
||||||
if (!Object.prototype.map)
|
if (!Object.prototype.map)
|
||||||
Object.defineProperty(Object.prototype, "map", {
|
Object.defineProperty(Object.prototype, "map", {
|
||||||
value: objectMap,
|
value: objectMap,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true
|
writable: true,
|
||||||
});
|
});
|
||||||
@ -2,7 +2,7 @@ import express, { Application, NextFunction, Request, Response, Router } from "e
|
|||||||
import { traverseDirectory } from "./Utils";
|
import { traverseDirectory } from "./Utils";
|
||||||
import { Server as HTTPServer } from "http";
|
import { Server as HTTPServer } from "http";
|
||||||
import { HTTPError } from "./HTTPError";
|
import { HTTPError } from "./HTTPError";
|
||||||
import "express-async-errors";
|
// import "express-async-errors";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
// import helmet from "helmet";
|
// import helmet from "helmet";
|
||||||
import http from "http";
|
import http from "http";
|
||||||
|
|||||||
Reference in New Issue
Block a user