@arkyn/server 3.0.1-beta.21 → 3.0.1-beta.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api/arkynLogRequest.ts +0 -118
- package/src/api/deleteRequest.ts +0 -22
- package/src/api/getRequest.ts +0 -20
- package/src/api/makeRequest.ts +0 -118
- package/src/api/patchRequest.ts +0 -22
- package/src/api/postRequest.ts +0 -22
- package/src/api/putRequest.ts +0 -22
- package/src/config/apiInstance.ts +0 -148
- package/src/config/arkynLogInstance.ts +0 -70
- package/src/http/badResponses/badGateway.ts +0 -63
- package/src/http/badResponses/badRequest.ts +0 -63
- package/src/http/badResponses/conflict.ts +0 -63
- package/src/http/badResponses/forbidden.ts +0 -63
- package/src/http/badResponses/notFound.ts +0 -63
- package/src/http/badResponses/notImplemented.ts +0 -63
- package/src/http/badResponses/serverError.ts +0 -63
- package/src/http/badResponses/unauthorized.ts +0 -63
- package/src/http/badResponses/unprocessableEntity.ts +0 -79
- package/src/http/successResponses/created.ts +0 -64
- package/src/http/successResponses/found.ts +0 -67
- package/src/http/successResponses/noContent.ts +0 -42
- package/src/http/successResponses/success.ts +0 -64
- package/src/http/successResponses/updated.ts +0 -64
- package/src/index.ts +0 -31
- package/src/mapper/arkynLogRequestMapper.ts +0 -73
- package/src/services/decodeErrorMessageFromRequest.ts +0 -36
- package/src/services/decodeRequestBody.ts +0 -43
- package/src/services/errorHandler.ts +0 -99
- package/src/services/formParse.ts +0 -86
- package/src/services/getCaller.ts +0 -82
- package/src/services/getScopedParams.ts +0 -43
- package/src/services/httpDebug.ts +0 -61
- package/src/services/measureRouteExecution.ts +0 -31
- package/src/services/schemaValidator.ts +0 -66
- package/src/types/ApiResponseDTO.ts +0 -19
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -5
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
function measureRouteExecution<T = unknown>(
|
|
2
|
-
handler: (props: any) => Promise<T>
|
|
3
|
-
) {
|
|
4
|
-
return async function measuredHandler(props: any): Promise<T> {
|
|
5
|
-
const startTime = performance.now();
|
|
6
|
-
|
|
7
|
-
try {
|
|
8
|
-
const result = await handler(props);
|
|
9
|
-
|
|
10
|
-
const url = new URL(props.request.url);
|
|
11
|
-
const endTime = performance.now();
|
|
12
|
-
|
|
13
|
-
const duration = (endTime - startTime).toFixed(2);
|
|
14
|
-
|
|
15
|
-
console.log({
|
|
16
|
-
domain: url.hostname,
|
|
17
|
-
pathname: url.pathname,
|
|
18
|
-
method: props.request.method,
|
|
19
|
-
duration,
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return result as T;
|
|
23
|
-
} catch (error) {
|
|
24
|
-
const endTime = performance.now();
|
|
25
|
-
console.error("");
|
|
26
|
-
throw error;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export { measureRouteExecution };
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { ZodType, z } from "zod";
|
|
2
|
-
|
|
3
|
-
import { ServerError } from "../http/badResponses/serverError";
|
|
4
|
-
import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
|
|
5
|
-
import { formParse } from "./formParse";
|
|
6
|
-
import { getCaller } from "./getCaller";
|
|
7
|
-
import { httpDebug } from "./httpDebug";
|
|
8
|
-
|
|
9
|
-
function formatErrorMessage(error: z.ZodError) {
|
|
10
|
-
const title = "Error validating:";
|
|
11
|
-
const lines = error.issues.map(
|
|
12
|
-
({ path, message }) => `-> ${path.join(".")}: ${message}`
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
return [title, ...lines].join("\n");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class SchemaValidator<T extends ZodType> {
|
|
19
|
-
functionName: string;
|
|
20
|
-
callerInfo: string;
|
|
21
|
-
|
|
22
|
-
constructor(readonly schema: T) {
|
|
23
|
-
const { callerInfo, functionName } = getCaller();
|
|
24
|
-
this.callerInfo = callerInfo;
|
|
25
|
-
this.functionName = functionName;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
isValid(data: any): boolean {
|
|
29
|
-
return this.schema.safeParse(data).success;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
safeValidate(data: any): z.ZodSafeParseResult<z.infer<T>> {
|
|
33
|
-
return this.schema.safeParse(data);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
validate(data: any): z.infer<T> {
|
|
37
|
-
try {
|
|
38
|
-
return this.schema.parse(data);
|
|
39
|
-
} catch (error: any) {
|
|
40
|
-
throw new ServerError(formatErrorMessage(error));
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
formValidate(data: any, message?: string): z.infer<T> {
|
|
45
|
-
const formParsed = formParse([data, this.schema]);
|
|
46
|
-
|
|
47
|
-
if (!formParsed.success) {
|
|
48
|
-
httpDebug("UnprocessableEntity", formParsed);
|
|
49
|
-
const firstErrorKey = Object.keys(formParsed.fieldErrors)[0];
|
|
50
|
-
|
|
51
|
-
throw new UnprocessableEntity(
|
|
52
|
-
{
|
|
53
|
-
fields: formParsed.fields,
|
|
54
|
-
fieldErrors: formParsed.fieldErrors,
|
|
55
|
-
data: { scrollTo: firstErrorKey },
|
|
56
|
-
message,
|
|
57
|
-
},
|
|
58
|
-
false
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return formParsed.data as z.infer<T>;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export { SchemaValidator };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
type ApiSuccessResponse<T = any> = {
|
|
2
|
-
success: true;
|
|
3
|
-
status: number;
|
|
4
|
-
message: string;
|
|
5
|
-
response: T;
|
|
6
|
-
cause: null;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
type ApiFailedResponse = {
|
|
10
|
-
success: false;
|
|
11
|
-
status: number;
|
|
12
|
-
message: string;
|
|
13
|
-
response: any;
|
|
14
|
-
cause: string | Error | null;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
type ApiResponseDTO<T = any> = ApiSuccessResponse<T> | ApiFailedResponse;
|
|
18
|
-
|
|
19
|
-
export type { ApiResponseDTO };
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"allowSyntheticDefaultImports": true,
|
|
4
|
-
"declaration": true,
|
|
5
|
-
"declarationDir": "./dist",
|
|
6
|
-
"declarationMap": true,
|
|
7
|
-
"isolatedModules": true,
|
|
8
|
-
"lib": ["DOM", "DOM.Iterable", "ES2022"],
|
|
9
|
-
"module": "ESNext",
|
|
10
|
-
"moduleResolution": "node",
|
|
11
|
-
"outDir": "./dist",
|
|
12
|
-
"preserveWatchOutput": true,
|
|
13
|
-
"skipLibCheck": true,
|
|
14
|
-
"strict": true,
|
|
15
|
-
"target": "ESNext",
|
|
16
|
-
"types": ["bun-types"],
|
|
17
|
-
"verbatimModuleSyntax": true
|
|
18
|
-
},
|
|
19
|
-
"exclude": ["dist", "node_modules", "**/__test__"],
|
|
20
|
-
"include": ["src/**/*.ts", "src/**/*.ts"]
|
|
21
|
-
}
|