@polygonlabs/servercore 0.0.0
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/README.md +15 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.js +3 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/response_handler.d.ts +13 -0
- package/dist/api/response_handler.js +22 -0
- package/dist/api/response_handler.js.map +1 -0
- package/dist/api/zod_utils.d.ts +7 -0
- package/dist/api/zod_utils.js +42 -0
- package/dist/api/zod_utils.js.map +1 -0
- package/dist/constants/error_codes.d.ts +29 -0
- package/dist/constants/error_codes.js +34 -0
- package/dist/constants/error_codes.js.map +1 -0
- package/dist/constants/http_success_codes.d.ts +5 -0
- package/dist/constants/http_success_codes.js +7 -0
- package/dist/constants/http_success_codes.js.map +1 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.js +3 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/database/db_interface.d.ts +13 -0
- package/dist/database/db_interface.js +1 -0
- package/dist/database/db_interface.js.map +1 -0
- package/dist/database/index.d.ts +2 -0
- package/dist/database/index.js +2 -0
- package/dist/database/index.js.map +1 -0
- package/dist/errors/api_errors.d.ts +32 -0
- package/dist/errors/api_errors.js +94 -0
- package/dist/errors/api_errors.js.map +1 -0
- package/dist/errors/base_error.d.ts +22 -0
- package/dist/errors/base_error.js +30 -0
- package/dist/errors/base_error.js.map +1 -0
- package/dist/errors/consumer_errors.d.ts +13 -0
- package/dist/errors/consumer_errors.js +18 -0
- package/dist/errors/consumer_errors.js.map +1 -0
- package/dist/errors/database_errors.d.ts +13 -0
- package/dist/errors/database_errors.js +18 -0
- package/dist/errors/database_errors.js.map +1 -0
- package/dist/errors/external_dependency_error.d.ts +15 -0
- package/dist/errors/external_dependency_error.js +29 -0
- package/dist/errors/external_dependency_error.js.map +1 -0
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/index.js +6 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/index.d.ts +3 -0
- package/dist/logger/index.js +2 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/logger.d.ts +52 -0
- package/dist/logger/logger.js +125 -0
- package/dist/logger/logger.js.map +1 -0
- package/dist/types/database.d.ts +23 -0
- package/dist/types/database.js +1 -0
- package/dist/types/database.js.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/logger_config.d.ts +18 -0
- package/dist/types/logger_config.js +2 -0
- package/dist/types/logger_config.js.map +1 -0
- package/package.json +44 -0
package/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# servercore
|
2
|
+
|
3
|
+
To install dependencies:
|
4
|
+
|
5
|
+
```bash
|
6
|
+
bun install
|
7
|
+
```
|
8
|
+
|
9
|
+
To run:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
bun run index.ts
|
13
|
+
```
|
14
|
+
|
15
|
+
This project was created using `bun init` in bun v1.2.4. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
@@ -0,0 +1,7 @@
|
|
1
|
+
export { handleError, handleResponse } from './response_handler.js';
|
2
|
+
export { validateBody, validateParams, validateQuery } from './zod_utils.js';
|
3
|
+
import '../errors/api_errors.js';
|
4
|
+
import '../errors/base_error.js';
|
5
|
+
import '../errors/database_errors.js';
|
6
|
+
import '../errors/external_dependency_error.js';
|
7
|
+
import 'zod';
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/api/index.ts"],"sourcesContent":["export * from \"./response_handler\";\nexport * from \"./zod_utils\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { ApiError } from '../errors/api_errors.js';
|
2
|
+
import { DatabaseError } from '../errors/database_errors.js';
|
3
|
+
import { ExternalDependencyError } from '../errors/external_dependency_error.js';
|
4
|
+
import '../errors/base_error.js';
|
5
|
+
|
6
|
+
type ResponseContext = {
|
7
|
+
status: (statusCode: number) => ResponseContext;
|
8
|
+
json: (body: any) => any;
|
9
|
+
};
|
10
|
+
declare const handleResponse: (c: ResponseContext, data: any) => any;
|
11
|
+
declare const handleError: (c: ResponseContext, error: ApiError | ExternalDependencyError | DatabaseError) => any;
|
12
|
+
|
13
|
+
export { handleError, handleResponse };
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { ApiError, DatabaseError, ExternalDependencyError } from "../errors";
|
2
|
+
import { httpResposneCodes, errorCodes } from "../constants";
|
3
|
+
const handleResponse = (c, data) => {
|
4
|
+
return c.status(httpResposneCodes.OK_RESPONSE).json({
|
5
|
+
status: "success",
|
6
|
+
data
|
7
|
+
});
|
8
|
+
};
|
9
|
+
const handleError = (c, error) => {
|
10
|
+
return c.status(error.code ?? errorCodes.api.INTERNAL_SERVER_ERROR).json({
|
11
|
+
status: "error",
|
12
|
+
message: error.message,
|
13
|
+
name: error.name,
|
14
|
+
code: error.code,
|
15
|
+
details: error.context
|
16
|
+
});
|
17
|
+
};
|
18
|
+
export {
|
19
|
+
handleError,
|
20
|
+
handleResponse
|
21
|
+
};
|
22
|
+
//# sourceMappingURL=response_handler.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/api/response_handler.ts"],"sourcesContent":["import { ApiError, DatabaseError, ExternalDependencyError } from \"../errors\"; // Define your error types\nimport { httpResposneCodes, errorCodes } from \"../constants\"; // Define your success codes\n\ntype ResponseContext = {\n // Here we allow the user of this utility to pass the response methods depending on the API framework.\n // For example, in Express, this would be `res` (response) object.\n status: (statusCode: number) => ResponseContext;\n json: (body: any) => any; // Define how to send JSON responses\n};\n\nexport const handleResponse = (c: ResponseContext, data: any) => {\n // This method returns a success response with the status and data.\n return c\n .status(httpResposneCodes.OK_RESPONSE) // Use success code from httpResposneCodes\n .json({\n status: \"success\",\n data: data,\n });\n};\n\nexport const handleError = (\n c: ResponseContext,\n error: ApiError | ExternalDependencyError | DatabaseError\n) => {\n // This method returns an error response with the error details.\n return c\n .status(error.code ?? errorCodes.api.INTERNAL_SERVER_ERROR) // Use error code or default to internal server error\n .json({\n status: \"error\",\n message: error.message,\n name: error.name,\n code: error.code,\n details: error.context,\n });\n};\n"],"mappings":"AAAA,SAAS,UAAU,eAAe,+BAA+B;AACjE,SAAS,mBAAmB,kBAAkB;AASvC,MAAM,iBAAiB,CAAC,GAAoB,SAAc;AAE7D,SAAO,EACF,OAAO,kBAAkB,WAAW,EACpC,KAAK;AAAA,IACF,QAAQ;AAAA,IACR;AAAA,EACJ,CAAC;AACT;AAEO,MAAM,cAAc,CACvB,GACA,UACC;AAED,SAAO,EACF,OAAO,MAAM,QAAQ,WAAW,IAAI,qBAAqB,EACzD,KAAK;AAAA,IACF,QAAQ;AAAA,IACR,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,IACZ,MAAM,MAAM;AAAA,IACZ,SAAS,MAAM;AAAA,EACnB,CAAC;AACT;","names":[]}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { z } from 'zod';
|
2
|
+
|
3
|
+
declare const validateBody: <T extends z.ZodTypeAny>(schema: T, data: unknown) => z.infer<T>;
|
4
|
+
declare const validateQuery: <T extends z.ZodTypeAny>(schema: T, query: Record<string, unknown>) => z.infer<T>;
|
5
|
+
declare const validateParams: <T extends z.ZodTypeAny>(schema: T, params: Record<string, string>) => z.infer<T>;
|
6
|
+
|
7
|
+
export { validateBody, validateParams, validateQuery };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
const validateBody = (schema, data) => {
|
3
|
+
const result = schema.safeParse(data);
|
4
|
+
if (!result.success) {
|
5
|
+
throw new Error(
|
6
|
+
`Invalid body: ${JSON.stringify(result.error.format(), null, 2)}`
|
7
|
+
);
|
8
|
+
}
|
9
|
+
return result.data;
|
10
|
+
};
|
11
|
+
const validateQuery = (schema, query) => {
|
12
|
+
const result = schema.safeParse(query);
|
13
|
+
if (!result.success) {
|
14
|
+
throw new Error(
|
15
|
+
`Invalid query params: ${JSON.stringify(
|
16
|
+
result.error.format(),
|
17
|
+
null,
|
18
|
+
2
|
19
|
+
)}`
|
20
|
+
);
|
21
|
+
}
|
22
|
+
return result.data;
|
23
|
+
};
|
24
|
+
const validateParams = (schema, params) => {
|
25
|
+
const result = schema.safeParse(params);
|
26
|
+
if (!result.success) {
|
27
|
+
throw new Error(
|
28
|
+
`Invalid path params: ${JSON.stringify(
|
29
|
+
result.error.format(),
|
30
|
+
null,
|
31
|
+
2
|
32
|
+
)}`
|
33
|
+
);
|
34
|
+
}
|
35
|
+
return result.data;
|
36
|
+
};
|
37
|
+
export {
|
38
|
+
validateBody,
|
39
|
+
validateParams,
|
40
|
+
validateQuery
|
41
|
+
};
|
42
|
+
//# sourceMappingURL=zod_utils.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/api/zod_utils.ts"],"sourcesContent":["import { z } from \"zod\";\n\n// Utility function for validating the request body\nexport const validateBody = <T extends z.ZodTypeAny>(\n schema: T,\n data: unknown\n): z.infer<T> => {\n const result = schema.safeParse(data);\n if (!result.success) {\n throw new Error(\n `Invalid body: ${JSON.stringify(result.error.format(), null, 2)}`\n );\n }\n return result.data;\n};\n\n// Utility function for validating query parameters\nexport const validateQuery = <T extends z.ZodTypeAny>(\n schema: T,\n query: Record<string, unknown>\n): z.infer<T> => {\n const result = schema.safeParse(query);\n if (!result.success) {\n throw new Error(\n `Invalid query params: ${JSON.stringify(\n result.error.format(),\n null,\n 2\n )}`\n );\n }\n return result.data;\n};\n\n// Utility function for validating path parameters\nexport const validateParams = <T extends z.ZodTypeAny>(\n schema: T,\n params: Record<string, string>\n): z.infer<T> => {\n const result = schema.safeParse(params);\n if (!result.success) {\n throw new Error(\n `Invalid path params: ${JSON.stringify(\n result.error.format(),\n null,\n 2\n )}`\n );\n }\n return result.data;\n};\n"],"mappings":"AAAA,SAAS,SAAS;AAGX,MAAM,eAAe,CACxB,QACA,SACa;AACb,QAAM,SAAS,OAAO,UAAU,IAAI;AACpC,MAAI,CAAC,OAAO,SAAS;AACjB,UAAM,IAAI;AAAA,MACN,iBAAiB,KAAK,UAAU,OAAO,MAAM,OAAO,GAAG,MAAM,CAAC,CAAC;AAAA,IACnE;AAAA,EACJ;AACA,SAAO,OAAO;AAClB;AAGO,MAAM,gBAAgB,CACzB,QACA,UACa;AACb,QAAM,SAAS,OAAO,UAAU,KAAK;AACrC,MAAI,CAAC,OAAO,SAAS;AACjB,UAAM,IAAI;AAAA,MACN,yBAAyB,KAAK;AAAA,QAC1B,OAAO,MAAM,OAAO;AAAA,QACpB;AAAA,QACA;AAAA,MACJ,CAAC;AAAA,IACL;AAAA,EACJ;AACA,SAAO,OAAO;AAClB;AAGO,MAAM,iBAAiB,CAC1B,QACA,WACa;AACb,QAAM,SAAS,OAAO,UAAU,MAAM;AACtC,MAAI,CAAC,OAAO,SAAS;AACjB,UAAM,IAAI;AAAA,MACN,wBAAwB,KAAK;AAAA,QACzB,OAAO,MAAM,OAAO;AAAA,QACpB;AAAA,QACA;AAAA,MACJ,CAAC;AAAA,IACL;AAAA,EACJ;AACA,SAAO,OAAO;AAClB;","names":[]}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
declare const errorCodes: {
|
2
|
+
base: {
|
3
|
+
BASE_ERROR: number;
|
4
|
+
};
|
5
|
+
consumer: {
|
6
|
+
UNKNOWN_CONSUMER_ERR: number;
|
7
|
+
};
|
8
|
+
datastore: {
|
9
|
+
UNKNOWN_DATASTORE_ERR: number;
|
10
|
+
DATASTORE_AUTH_ERR: number;
|
11
|
+
DATASTORE_READ_ERROR: number;
|
12
|
+
DATASTORE_WRITE_ERROR: number;
|
13
|
+
};
|
14
|
+
external: {
|
15
|
+
UNKNOWN_EXTERNAL_DEPENDENCY_ERROR: number;
|
16
|
+
};
|
17
|
+
api: {
|
18
|
+
BAD_REQUEST: number;
|
19
|
+
UNAUTHORIZED: number;
|
20
|
+
FORBIDDEN: number;
|
21
|
+
NOT_FOUND: number;
|
22
|
+
TOO_MANY_REQUESTS: number;
|
23
|
+
INTERNAL_SERVER_ERROR: number;
|
24
|
+
GATEWAY_ERROR: number;
|
25
|
+
TIMEOUT_ERROR: number;
|
26
|
+
};
|
27
|
+
};
|
28
|
+
|
29
|
+
export { errorCodes };
|
@@ -0,0 +1,34 @@
|
|
1
|
+
const errorCodes = {
|
2
|
+
// Base error identifier
|
3
|
+
base: { BASE_ERROR: 100 },
|
4
|
+
// Consumer related error codes
|
5
|
+
consumer: {
|
6
|
+
UNKNOWN_CONSUMER_ERR: 1e3
|
7
|
+
},
|
8
|
+
// Datastore related error codes
|
9
|
+
datastore: {
|
10
|
+
UNKNOWN_DATASTORE_ERR: 2e3,
|
11
|
+
DATASTORE_AUTH_ERR: 2001,
|
12
|
+
DATASTORE_READ_ERROR: 2002,
|
13
|
+
DATASTORE_WRITE_ERROR: 2003
|
14
|
+
},
|
15
|
+
// External dependencies errors codes
|
16
|
+
external: {
|
17
|
+
UNKNOWN_EXTERNAL_DEPENDENCY_ERROR: 3e3
|
18
|
+
},
|
19
|
+
// API related error codes
|
20
|
+
api: {
|
21
|
+
BAD_REQUEST: 400,
|
22
|
+
UNAUTHORIZED: 401,
|
23
|
+
FORBIDDEN: 403,
|
24
|
+
NOT_FOUND: 404,
|
25
|
+
TOO_MANY_REQUESTS: 429,
|
26
|
+
INTERNAL_SERVER_ERROR: 500,
|
27
|
+
GATEWAY_ERROR: 502,
|
28
|
+
TIMEOUT_ERROR: 504
|
29
|
+
}
|
30
|
+
};
|
31
|
+
export {
|
32
|
+
errorCodes
|
33
|
+
};
|
34
|
+
//# sourceMappingURL=error_codes.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/constants/error_codes.ts"],"sourcesContent":["export const errorCodes = {\n // Base error identifier\n base: { BASE_ERROR: 100 },\n\n // Consumer related error codes\n consumer: {\n UNKNOWN_CONSUMER_ERR: 1000,\n },\n\n // Datastore related error codes\n datastore: {\n UNKNOWN_DATASTORE_ERR: 2000,\n DATASTORE_AUTH_ERR: 2001,\n DATASTORE_READ_ERROR: 2002,\n DATASTORE_WRITE_ERROR: 2003,\n },\n\n // External dependencies errors codes\n external: {\n UNKNOWN_EXTERNAL_DEPENDENCY_ERROR: 3000,\n },\n\n // API related error codes\n api: {\n BAD_REQUEST: 400,\n UNAUTHORIZED: 401,\n FORBIDDEN: 403,\n NOT_FOUND: 404,\n TOO_MANY_REQUESTS: 429,\n INTERNAL_SERVER_ERROR: 500,\n GATEWAY_ERROR: 502,\n TIMEOUT_ERROR: 504,\n },\n};\n"],"mappings":"AAAO,MAAM,aAAa;AAAA;AAAA,EAEtB,MAAM,EAAE,YAAY,IAAI;AAAA;AAAA,EAGxB,UAAU;AAAA,IACN,sBAAsB;AAAA,EAC1B;AAAA;AAAA,EAGA,WAAW;AAAA,IACP,uBAAuB;AAAA,IACvB,oBAAoB;AAAA,IACpB,sBAAsB;AAAA,IACtB,uBAAuB;AAAA,EAC3B;AAAA;AAAA,EAGA,UAAU;AAAA,IACN,mCAAmC;AAAA,EACvC;AAAA;AAAA,EAGA,KAAK;AAAA,IACD,aAAa;AAAA,IACb,cAAc;AAAA,IACd,WAAW;AAAA,IACX,WAAW;AAAA,IACX,mBAAmB;AAAA,IACnB,uBAAuB;AAAA,IACvB,eAAe;AAAA,IACf,eAAe;AAAA,EACnB;AACJ;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/constants/http_success_codes.ts"],"sourcesContent":["export const httpResposneCodes = {\n OK_RESPONSE: 200,\n};\n"],"mappings":"AAAO,MAAM,oBAAoB;AAAA,EAC7B,aAAa;AACjB;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/constants/index.ts"],"sourcesContent":["export * from \"./error_codes\";\nexport * from \"./http_success_codes\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { IQueryFilterOperationParams, IDocumentConditionalModifications, IQueryOrderOperationParams } from '../types/database.js';
|
2
|
+
|
3
|
+
interface Database {
|
4
|
+
connect(): Promise<void>;
|
5
|
+
disconnect(): Promise<void>;
|
6
|
+
addDocuments(collectionPaths: string[], docDatas: any[], docIds?: string[]): Promise<void>;
|
7
|
+
updateDocuments(collectionPaths: string[], docDatas: any[], docIds: string[]): Promise<void>;
|
8
|
+
conditionalUpdateDocuments(collectionPaths: string[], docDatas: any[], docIds: string[], conditions: IQueryFilterOperationParams[], conditionModifications: IDocumentConditionalModifications[]): Promise<void>;
|
9
|
+
getDocuments(collectionPath: string, filter?: IQueryFilterOperationParams[] | undefined, limit?: number | undefined, order?: IQueryOrderOperationParams[] | undefined, startAfterCursor?: string | number | undefined): Promise<any[]>;
|
10
|
+
getDocument(collectionId: string, docId: string): Promise<any | null>;
|
11
|
+
}
|
12
|
+
|
13
|
+
export type { Database };
|
@@ -0,0 +1 @@
|
|
1
|
+
//# sourceMappingURL=db_interface.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/database/index.ts"],"sourcesContent":["export * from \"./db_interface\";\n"],"mappings":"AAAA,cAAc;","names":[]}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { BaseError } from './base_error.js';
|
2
|
+
|
3
|
+
declare class ApiError extends BaseError {
|
4
|
+
constructor(message: string, { name, code, isFatal, origin, context, }?: {
|
5
|
+
name?: string;
|
6
|
+
code?: number;
|
7
|
+
isFatal?: boolean;
|
8
|
+
origin?: string;
|
9
|
+
context?: Record<string, any>;
|
10
|
+
});
|
11
|
+
}
|
12
|
+
declare class UnauthorizedError extends ApiError {
|
13
|
+
constructor(message?: string, context?: Record<string, any>, origin?: string);
|
14
|
+
}
|
15
|
+
declare class ForbiddenError extends ApiError {
|
16
|
+
constructor(message?: string, context?: Record<string, any>, origin?: string);
|
17
|
+
}
|
18
|
+
declare class BadRequestError extends ApiError {
|
19
|
+
readonly validationErrors: Record<string, string[]>;
|
20
|
+
constructor(message?: string, validationErrors?: Record<string, any>, context?: Record<string, any>, origin?: string);
|
21
|
+
}
|
22
|
+
declare class NotFoundError extends ApiError {
|
23
|
+
constructor(entity?: string, identifier?: string | number, context?: Record<string, any>, origin?: string);
|
24
|
+
}
|
25
|
+
declare class RateLimitError extends ApiError {
|
26
|
+
constructor(message?: string, context?: Record<string, any>, origin?: string);
|
27
|
+
}
|
28
|
+
declare class TimeoutError extends ApiError {
|
29
|
+
constructor(operation: string, timeoutMs: number, context?: Record<string, any>, origin?: string);
|
30
|
+
}
|
31
|
+
|
32
|
+
export { ApiError, BadRequestError, ForbiddenError, NotFoundError, RateLimitError, TimeoutError, UnauthorizedError };
|
@@ -0,0 +1,94 @@
|
|
1
|
+
import { BaseError } from "./base_error";
|
2
|
+
import { errorCodes } from "../constants";
|
3
|
+
class ApiError extends BaseError {
|
4
|
+
constructor(message, {
|
5
|
+
name = "INTERNAL_SERVER_ERROR",
|
6
|
+
code = errorCodes.api.INTERNAL_SERVER_ERROR,
|
7
|
+
isFatal = true,
|
8
|
+
origin = "api_errors",
|
9
|
+
context = {}
|
10
|
+
} = {}) {
|
11
|
+
super(name, code, message, isFatal, origin, context);
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
class UnauthorizedError extends ApiError {
|
16
|
+
constructor(message = "Invalid auth credentials", context = {}, origin = "api_errors") {
|
17
|
+
super(message, {
|
18
|
+
name: "UNAUTHORIZED",
|
19
|
+
code: errorCodes.api.UNAUTHORIZED,
|
20
|
+
isFatal: true,
|
21
|
+
origin,
|
22
|
+
context
|
23
|
+
});
|
24
|
+
}
|
25
|
+
}
|
26
|
+
class ForbiddenError extends ApiError {
|
27
|
+
constructor(message = "You do not have permission to perform this action", context = {}, origin = "api_errors") {
|
28
|
+
super(message, {
|
29
|
+
name: "FORBIDDEN",
|
30
|
+
code: errorCodes.api.FORBIDDEN,
|
31
|
+
isFatal: true,
|
32
|
+
origin,
|
33
|
+
context
|
34
|
+
});
|
35
|
+
}
|
36
|
+
}
|
37
|
+
class BadRequestError extends ApiError {
|
38
|
+
validationErrors;
|
39
|
+
constructor(message = "Malformed or invalid request", validationErrors = {}, context = {}, origin = "api_errors") {
|
40
|
+
super(message, {
|
41
|
+
name: "BAD_REQUEST",
|
42
|
+
code: errorCodes.api.BAD_REQUEST,
|
43
|
+
isFatal: true,
|
44
|
+
origin,
|
45
|
+
context
|
46
|
+
});
|
47
|
+
this.validationErrors = validationErrors;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
class NotFoundError extends ApiError {
|
51
|
+
constructor(entity = "Path", identifier, context = {}, origin = "api_errors") {
|
52
|
+
const message = identifier ? `${entity} with identifier ${identifier} not found` : `${entity} not found`;
|
53
|
+
super(message, {
|
54
|
+
name: "NOT_FOUND",
|
55
|
+
code: errorCodes.api.NOT_FOUND,
|
56
|
+
isFatal: true,
|
57
|
+
origin,
|
58
|
+
context: { entity, identifier, ...context }
|
59
|
+
});
|
60
|
+
}
|
61
|
+
}
|
62
|
+
class RateLimitError extends ApiError {
|
63
|
+
constructor(message = "Rate limit exceeded", context = {}, origin = "api_errors") {
|
64
|
+
super(message, {
|
65
|
+
name: "RATE_LIMIT",
|
66
|
+
code: errorCodes.api.TOO_MANY_REQUESTS,
|
67
|
+
isFatal: true,
|
68
|
+
origin,
|
69
|
+
context
|
70
|
+
});
|
71
|
+
}
|
72
|
+
}
|
73
|
+
class TimeoutError extends ApiError {
|
74
|
+
constructor(operation, timeoutMs, context = {}, origin = "api_errors") {
|
75
|
+
super(`Operation '${operation}' timed out after ${timeoutMs}ms`, {
|
76
|
+
name: "TIMEOUT",
|
77
|
+
code: errorCodes.api.TIMEOUT_ERROR,
|
78
|
+
// Gateway Timeout
|
79
|
+
isFatal: true,
|
80
|
+
origin,
|
81
|
+
context: { operation, timeoutMs, ...context }
|
82
|
+
});
|
83
|
+
}
|
84
|
+
}
|
85
|
+
export {
|
86
|
+
ApiError,
|
87
|
+
BadRequestError,
|
88
|
+
ForbiddenError,
|
89
|
+
NotFoundError,
|
90
|
+
RateLimitError,
|
91
|
+
TimeoutError,
|
92
|
+
UnauthorizedError
|
93
|
+
};
|
94
|
+
//# sourceMappingURL=api_errors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/errors/api_errors.ts"],"sourcesContent":["import { BaseError } from \"./base_error\";\nimport { errorCodes } from \"../constants\";\n\nexport class ApiError extends BaseError {\n constructor(\n message: string,\n {\n name = \"INTERNAL_SERVER_ERROR\",\n code = errorCodes.api.INTERNAL_SERVER_ERROR,\n isFatal = true,\n origin = \"api_errors\",\n context = {},\n }: {\n name?: string;\n code?: number;\n isFatal?: boolean;\n origin?: string;\n context?: Record<string, any>;\n } = {}\n ) {\n super(name, code, message, isFatal, origin, context);\n Error.captureStackTrace(this, this.constructor);\n }\n}\n\nexport class UnauthorizedError extends ApiError {\n constructor(\n message: string = \"Invalid auth credentials\",\n context: Record<string, any> = {},\n origin: string = \"api_errors\"\n ) {\n super(message, {\n name: \"UNAUTHORIZED\",\n code: errorCodes.api.UNAUTHORIZED,\n isFatal: true,\n origin,\n context,\n });\n }\n}\n\nexport class ForbiddenError extends ApiError {\n constructor(\n message: string = \"You do not have permission to perform this action\",\n context: Record<string, any> = {},\n origin: string = \"api_errors\"\n ) {\n super(message, {\n name: \"FORBIDDEN\",\n code: errorCodes.api.FORBIDDEN,\n isFatal: true,\n origin,\n context,\n });\n }\n}\n\nexport class BadRequestError extends ApiError {\n public readonly validationErrors: Record<string, string[]>;\n\n constructor(\n message: string = \"Malformed or invalid request\",\n validationErrors: Record<string, any> = {},\n context: Record<string, any> = {},\n origin: string = \"api_errors\"\n ) {\n super(message, {\n name: \"BAD_REQUEST\",\n code: errorCodes.api.BAD_REQUEST,\n isFatal: true,\n origin,\n context,\n });\n this.validationErrors = validationErrors;\n }\n}\n\nexport class NotFoundError extends ApiError {\n constructor(\n entity: string = \"Path\",\n identifier?: string | number,\n context: Record<string, any> = {},\n origin: string = \"api_errors\"\n ) {\n const message = identifier\n ? `${entity} with identifier ${identifier} not found`\n : `${entity} not found`;\n\n super(message, {\n name: \"NOT_FOUND\",\n code: errorCodes.api.NOT_FOUND,\n isFatal: true,\n origin,\n context: { entity, identifier, ...context },\n });\n }\n}\n\nexport class RateLimitError extends ApiError {\n constructor(\n message: string = \"Rate limit exceeded\",\n context: Record<string, any> = {},\n origin: string = \"api_errors\"\n ) {\n super(message, {\n name: \"RATE_LIMIT\",\n code: errorCodes.api.TOO_MANY_REQUESTS,\n isFatal: true,\n origin,\n context,\n });\n }\n}\n\nexport class TimeoutError extends ApiError {\n constructor(\n operation: string,\n timeoutMs: number,\n context: Record<string, any> = {},\n origin: string = \"api_errors\"\n ) {\n super(`Operation '${operation}' timed out after ${timeoutMs}ms`, {\n name: \"TIMEOUT\",\n code: errorCodes.api.TIMEOUT_ERROR, // Gateway Timeout\n isFatal: true,\n origin,\n context: { operation, timeoutMs, ...context },\n });\n }\n}\n"],"mappings":"AAAA,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAEpB,MAAM,iBAAiB,UAAU;AAAA,EACpC,YACI,SACA;AAAA,IACI,OAAO;AAAA,IACP,OAAO,WAAW,IAAI;AAAA,IACtB,UAAU;AAAA,IACV,SAAS;AAAA,IACT,UAAU,CAAC;AAAA,EACf,IAMI,CAAC,GACP;AACE,UAAM,MAAM,MAAM,SAAS,SAAS,QAAQ,OAAO;AACnD,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAClD;AACJ;AAEO,MAAM,0BAA0B,SAAS;AAAA,EAC5C,YACI,UAAkB,4BAClB,UAA+B,CAAC,GAChC,SAAiB,cACnB;AACE,UAAM,SAAS;AAAA,MACX,MAAM;AAAA,MACN,MAAM,WAAW,IAAI;AAAA,MACrB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;AAEO,MAAM,uBAAuB,SAAS;AAAA,EACzC,YACI,UAAkB,qDAClB,UAA+B,CAAC,GAChC,SAAiB,cACnB;AACE,UAAM,SAAS;AAAA,MACX,MAAM;AAAA,MACN,MAAM,WAAW,IAAI;AAAA,MACrB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;AAEO,MAAM,wBAAwB,SAAS;AAAA,EAC1B;AAAA,EAEhB,YACI,UAAkB,gCAClB,mBAAwC,CAAC,GACzC,UAA+B,CAAC,GAChC,SAAiB,cACnB;AACE,UAAM,SAAS;AAAA,MACX,MAAM;AAAA,MACN,MAAM,WAAW,IAAI;AAAA,MACrB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,IACJ,CAAC;AACD,SAAK,mBAAmB;AAAA,EAC5B;AACJ;AAEO,MAAM,sBAAsB,SAAS;AAAA,EACxC,YACI,SAAiB,QACjB,YACA,UAA+B,CAAC,GAChC,SAAiB,cACnB;AACE,UAAM,UAAU,aACV,GAAG,MAAM,oBAAoB,UAAU,eACvC,GAAG,MAAM;AAEf,UAAM,SAAS;AAAA,MACX,MAAM;AAAA,MACN,MAAM,WAAW,IAAI;AAAA,MACrB,SAAS;AAAA,MACT;AAAA,MACA,SAAS,EAAE,QAAQ,YAAY,GAAG,QAAQ;AAAA,IAC9C,CAAC;AAAA,EACL;AACJ;AAEO,MAAM,uBAAuB,SAAS;AAAA,EACzC,YACI,UAAkB,uBAClB,UAA+B,CAAC,GAChC,SAAiB,cACnB;AACE,UAAM,SAAS;AAAA,MACX,MAAM;AAAA,MACN,MAAM,WAAW,IAAI;AAAA,MACrB,SAAS;AAAA,MACT;AAAA,MACA;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;AAEO,MAAM,qBAAqB,SAAS;AAAA,EACvC,YACI,WACA,WACA,UAA+B,CAAC,GAChC,SAAiB,cACnB;AACE,UAAM,cAAc,SAAS,qBAAqB,SAAS,MAAM;AAAA,MAC7D,MAAM;AAAA,MACN,MAAM,WAAW,IAAI;AAAA;AAAA,MACrB,SAAS;AAAA,MACT;AAAA,MACA,SAAS,EAAE,WAAW,WAAW,GAAG,QAAQ;AAAA,IAChD,CAAC;AAAA,EACL;AACJ;","names":[]}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
/**
|
2
|
+
* BaseError used within the micro services that guarantees we don't loose the stack trace.
|
3
|
+
*/
|
4
|
+
declare class BaseError extends Error {
|
5
|
+
readonly name: string;
|
6
|
+
readonly code: number;
|
7
|
+
readonly isFatal: boolean;
|
8
|
+
readonly origin: string;
|
9
|
+
readonly context: Record<string, any>;
|
10
|
+
/**
|
11
|
+
* @param name {string} - The error name
|
12
|
+
* @param code {number} - The error code
|
13
|
+
* @param isFatal {boolean} - Flag to know if it is a fatal error
|
14
|
+
* @param message {string} - The actual error message
|
15
|
+
* @param origin {string} - The point this error originated
|
16
|
+
* @param context Record<string, any> - The stack trace
|
17
|
+
*/
|
18
|
+
constructor(name: string, code: number, message?: string, isFatal?: boolean, origin?: string, context?: Record<string, any>);
|
19
|
+
identifier: number;
|
20
|
+
}
|
21
|
+
|
22
|
+
export { BaseError };
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { errorCodes } from "../constants";
|
2
|
+
class BaseError extends Error {
|
3
|
+
name;
|
4
|
+
code;
|
5
|
+
isFatal;
|
6
|
+
origin;
|
7
|
+
context;
|
8
|
+
/**
|
9
|
+
* @param name {string} - The error name
|
10
|
+
* @param code {number} - The error code
|
11
|
+
* @param isFatal {boolean} - Flag to know if it is a fatal error
|
12
|
+
* @param message {string} - The actual error message
|
13
|
+
* @param origin {string} - The point this error originated
|
14
|
+
* @param context Record<string, any> - The stack trace
|
15
|
+
*/
|
16
|
+
constructor(name, code, message = "Unknown error", isFatal, origin, context) {
|
17
|
+
super(message);
|
18
|
+
this.name = name;
|
19
|
+
this.code = code;
|
20
|
+
this.isFatal = isFatal ?? true;
|
21
|
+
this.origin = origin ?? "base_error";
|
22
|
+
this.context = context ?? {};
|
23
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
24
|
+
}
|
25
|
+
identifier = errorCodes.base.BASE_ERROR;
|
26
|
+
}
|
27
|
+
export {
|
28
|
+
BaseError
|
29
|
+
};
|
30
|
+
//# sourceMappingURL=base_error.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/errors/base_error.ts"],"sourcesContent":["import { errorCodes } from \"../constants\";\n\n/**\n * BaseError used within the micro services that guarantees we don't loose the stack trace.\n */\nexport class BaseError extends Error {\n public readonly name: string;\n public readonly code: number;\n public readonly isFatal: boolean;\n public readonly origin: string;\n public readonly context: Record<string, any>;\n /**\n * @param name {string} - The error name\n * @param code {number} - The error code\n * @param isFatal {boolean} - Flag to know if it is a fatal error\n * @param message {string} - The actual error message\n * @param origin {string} - The point this error originated\n * @param context Record<string, any> - The stack trace\n */\n constructor(\n name: string,\n code: number,\n message: string = \"Unknown error\",\n isFatal?: boolean,\n origin?: string,\n context?: Record<string, any>\n ) {\n super(message);\n this.name = name;\n this.code = code;\n this.isFatal = isFatal ?? true;\n this.origin = origin ?? \"base_error\";\n this.context = context ?? {};\n Object.setPrototypeOf(this, new.target.prototype);\n }\n identifier: number = errorCodes.base.BASE_ERROR;\n}\n"],"mappings":"AAAA,SAAS,kBAAkB;AAKpB,MAAM,kBAAkB,MAAM;AAAA,EACjB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAShB,YACI,MACA,MACA,UAAkB,iBAClB,SACA,QACA,SACF;AACE,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,UAAU,WAAW;AAC1B,SAAK,SAAS,UAAU;AACxB,SAAK,UAAU,WAAW,CAAC;AAC3B,WAAO,eAAe,MAAM,WAAW,SAAS;AAAA,EACpD;AAAA,EACA,aAAqB,WAAW,KAAK;AACzC;","names":[]}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { BaseError } from './base_error.js';
|
2
|
+
|
3
|
+
declare class ConsumerError extends BaseError {
|
4
|
+
constructor(message: string, { name, code, isFatal, origin, context, }?: {
|
5
|
+
name?: string;
|
6
|
+
code?: number;
|
7
|
+
isFatal?: boolean;
|
8
|
+
origin?: string;
|
9
|
+
context?: Record<string, any>;
|
10
|
+
});
|
11
|
+
}
|
12
|
+
|
13
|
+
export { ConsumerError };
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { BaseError } from "./base_error";
|
2
|
+
import { errorCodes } from "../constants";
|
3
|
+
class ConsumerError extends BaseError {
|
4
|
+
constructor(message, {
|
5
|
+
name = "CONSUMER_ERROR",
|
6
|
+
code = errorCodes.consumer.UNKNOWN_CONSUMER_ERR,
|
7
|
+
isFatal = true,
|
8
|
+
origin = "consumer_errors",
|
9
|
+
context = {}
|
10
|
+
} = {}) {
|
11
|
+
super(name, code, message, isFatal, origin, context);
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
export {
|
16
|
+
ConsumerError
|
17
|
+
};
|
18
|
+
//# sourceMappingURL=consumer_errors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/errors/consumer_errors.ts"],"sourcesContent":["import { BaseError } from \"./base_error\";\nimport { errorCodes } from \"../constants\";\n\nexport class ConsumerError extends BaseError {\n constructor(\n message: string,\n {\n name = \"CONSUMER_ERROR\",\n code = errorCodes.consumer.UNKNOWN_CONSUMER_ERR,\n isFatal = true,\n origin = \"consumer_errors\",\n context = {},\n }: {\n name?: string;\n code?: number;\n isFatal?: boolean;\n origin?: string;\n context?: Record<string, any>;\n } = {}\n ) {\n super(name, code, message, isFatal, origin, context);\n Error.captureStackTrace(this, this.constructor);\n }\n}\n"],"mappings":"AAAA,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAEpB,MAAM,sBAAsB,UAAU;AAAA,EACzC,YACI,SACA;AAAA,IACI,OAAO;AAAA,IACP,OAAO,WAAW,SAAS;AAAA,IAC3B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,UAAU,CAAC;AAAA,EACf,IAMI,CAAC,GACP;AACE,UAAM,MAAM,MAAM,SAAS,SAAS,QAAQ,OAAO;AACnD,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAClD;AACJ;","names":[]}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { BaseError } from './base_error.js';
|
2
|
+
|
3
|
+
declare class DatabaseError extends BaseError {
|
4
|
+
constructor(message: string, originalError?: Error, { name, code, isFatal, origin, context, }?: {
|
5
|
+
name?: string;
|
6
|
+
code?: number;
|
7
|
+
isFatal?: boolean;
|
8
|
+
origin?: string;
|
9
|
+
context?: Record<string, any>;
|
10
|
+
});
|
11
|
+
}
|
12
|
+
|
13
|
+
export { DatabaseError };
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { BaseError } from "./base_error";
|
2
|
+
import { errorCodes } from "../constants";
|
3
|
+
class DatabaseError extends BaseError {
|
4
|
+
constructor(message, originalError, {
|
5
|
+
name = "CONSUMER_ERROR",
|
6
|
+
code = errorCodes.consumer.UNKNOWN_CONSUMER_ERR,
|
7
|
+
isFatal = true,
|
8
|
+
origin = "databse_errors",
|
9
|
+
context = {}
|
10
|
+
} = {}) {
|
11
|
+
super(name, code, message, isFatal, origin, context);
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
export {
|
16
|
+
DatabaseError
|
17
|
+
};
|
18
|
+
//# sourceMappingURL=database_errors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/errors/database_errors.ts"],"sourcesContent":["import { BaseError } from \"./base_error\";\nimport { errorCodes } from \"../constants\";\n\nexport class DatabaseError extends BaseError {\n constructor(\n message: string,\n originalError?: Error,\n {\n name = \"CONSUMER_ERROR\",\n code = errorCodes.consumer.UNKNOWN_CONSUMER_ERR,\n isFatal = true,\n origin = \"databse_errors\",\n context = {},\n }: {\n name?: string;\n code?: number;\n isFatal?: boolean;\n origin?: string;\n context?: Record<string, any>;\n } = {}\n ) {\n super(name, code, message, isFatal, origin, context);\n Error.captureStackTrace(this, this.constructor);\n }\n}\n"],"mappings":"AAAA,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAEpB,MAAM,sBAAsB,UAAU;AAAA,EACzC,YACI,SACA,eACA;AAAA,IACI,OAAO;AAAA,IACP,OAAO,WAAW,SAAS;AAAA,IAC3B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,UAAU,CAAC;AAAA,EACf,IAMI,CAAC,GACP;AACE,UAAM,MAAM,MAAM,SAAS,SAAS,QAAQ,OAAO;AACnD,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAClD;AACJ;","names":[]}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { BaseError } from './base_error.js';
|
2
|
+
|
3
|
+
declare class ExternalDependencyError extends BaseError {
|
4
|
+
readonly apiName: string;
|
5
|
+
readonly externalCode?: string | number;
|
6
|
+
readonly rawError?: any;
|
7
|
+
constructor(apiName: string, message: string, { externalCode, rawError, context, origin, }?: {
|
8
|
+
externalCode?: string | number;
|
9
|
+
rawError?: any;
|
10
|
+
context?: Record<string, any>;
|
11
|
+
origin?: string;
|
12
|
+
});
|
13
|
+
}
|
14
|
+
|
15
|
+
export { ExternalDependencyError };
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { BaseError } from "./base_error";
|
2
|
+
import { errorCodes } from "../constants";
|
3
|
+
class ExternalDependencyError extends BaseError {
|
4
|
+
apiName;
|
5
|
+
externalCode;
|
6
|
+
rawError;
|
7
|
+
constructor(apiName, message, {
|
8
|
+
externalCode,
|
9
|
+
rawError,
|
10
|
+
context = {},
|
11
|
+
origin = "external_dependency_error"
|
12
|
+
} = {}) {
|
13
|
+
super(
|
14
|
+
"EXTERNAL_DEPENDENCY_ERROR",
|
15
|
+
errorCodes.external.UNKNOWN_EXTERNAL_DEPENDENCY_ERROR,
|
16
|
+
`${apiName} API error: ${message}`,
|
17
|
+
false,
|
18
|
+
origin,
|
19
|
+
{ apiName, externalCode, ...context }
|
20
|
+
);
|
21
|
+
this.apiName = apiName;
|
22
|
+
this.externalCode = externalCode;
|
23
|
+
this.rawError = rawError;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
export {
|
27
|
+
ExternalDependencyError
|
28
|
+
};
|
29
|
+
//# sourceMappingURL=external_dependency_error.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/errors/external_dependency_error.ts"],"sourcesContent":["import { BaseError } from \"./base_error\";\nimport { errorCodes } from \"../constants\";\n\nexport class ExternalDependencyError extends BaseError {\n public readonly apiName: string;\n\n public readonly externalCode?: string | number;\n\n public readonly rawError?: any;\n\n constructor(\n apiName: string,\n message: string,\n {\n externalCode,\n rawError,\n context = {},\n origin = \"external_dependency_error\",\n }: {\n externalCode?: string | number;\n rawError?: any;\n context?: Record<string, any>;\n origin?: string;\n } = {}\n ) {\n super(\n \"EXTERNAL_DEPENDENCY_ERROR\",\n errorCodes.external.UNKNOWN_EXTERNAL_DEPENDENCY_ERROR,\n `${apiName} API error: ${message}`,\n false,\n origin,\n { apiName, externalCode, ...context }\n );\n\n this.apiName = apiName;\n this.externalCode = externalCode;\n this.rawError = rawError;\n }\n}\n"],"mappings":"AAAA,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAEpB,MAAM,gCAAgC,UAAU;AAAA,EACnC;AAAA,EAEA;AAAA,EAEA;AAAA,EAEhB,YACI,SACA,SACA;AAAA,IACI;AAAA,IACA;AAAA,IACA,UAAU,CAAC;AAAA,IACX,SAAS;AAAA,EACb,IAKI,CAAC,GACP;AACE;AAAA,MACI;AAAA,MACA,WAAW,SAAS;AAAA,MACpB,GAAG,OAAO,eAAe,OAAO;AAAA,MAChC;AAAA,MACA;AAAA,MACA,EAAE,SAAS,cAAc,GAAG,QAAQ;AAAA,IACxC;AAEA,SAAK,UAAU;AACf,SAAK,eAAe;AACpB,SAAK,WAAW;AAAA,EACpB;AACJ;","names":[]}
|
@@ -0,0 +1,5 @@
|
|
1
|
+
export { ApiError, BadRequestError, ForbiddenError, NotFoundError, RateLimitError, TimeoutError, UnauthorizedError } from './api_errors.js';
|
2
|
+
export { DatabaseError } from './database_errors.js';
|
3
|
+
export { ExternalDependencyError } from './external_dependency_error.js';
|
4
|
+
export { BaseError } from './base_error.js';
|
5
|
+
export { ConsumerError } from './consumer_errors.js';
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/errors/index.ts"],"sourcesContent":["export * from \"./api_errors\";\nexport * from \"./database_errors\";\nexport * from \"./external_dependency_error\";\nexport * from \"./base_error\";\nexport * from \"./consumer_errors\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
export { handleError, handleResponse } from './api/response_handler.js';
|
2
|
+
export { validateBody, validateParams, validateQuery } from './api/zod_utils.js';
|
3
|
+
export { Logger } from './logger/logger.js';
|
4
|
+
export { errorCodes } from './constants/error_codes.js';
|
5
|
+
export { httpResposneCodes } from './constants/http_success_codes.js';
|
6
|
+
export { ILoggerConfig } from './types/logger_config.js';
|
7
|
+
export { IDocumentConditionalModifications, IQueryFilterOperationParams, IQueryOrderOperationParams, OrderByDirection, WhereFilterOp } from './types/database.js';
|
8
|
+
export { ApiError, BadRequestError, ForbiddenError, NotFoundError, RateLimitError, TimeoutError, UnauthorizedError } from './errors/api_errors.js';
|
9
|
+
export { DatabaseError } from './errors/database_errors.js';
|
10
|
+
export { ExternalDependencyError } from './errors/external_dependency_error.js';
|
11
|
+
export { BaseError } from './errors/base_error.js';
|
12
|
+
export { ConsumerError } from './errors/consumer_errors.js';
|
13
|
+
export { Database } from './database/db_interface.js';
|
14
|
+
import 'zod';
|
15
|
+
import 'winston';
|
package/dist/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from \"./api\";\nexport * from \"./logger\";\nexport * from \"./constants\";\nexport * from \"./types\";\nexport * from \"./errors\";\nexport * from \"./database\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/logger/index.ts"],"sourcesContent":["export * from \"./logger\";\n"],"mappings":"AAAA,cAAc;","names":[]}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { ILoggerConfig } from '../types/logger_config.js';
|
2
|
+
import 'winston';
|
3
|
+
|
4
|
+
declare class Logger {
|
5
|
+
/**
|
6
|
+
* @static
|
7
|
+
* Create method must first be called before using the logger. It creates a singleton, which will then
|
8
|
+
* be referred to throughout the application.
|
9
|
+
*/
|
10
|
+
static create(config: ILoggerConfig): void;
|
11
|
+
/**
|
12
|
+
* @static
|
13
|
+
* Method to log for level - "info", this should not be called if it has been custom levels are
|
14
|
+
* set which does not include "info"
|
15
|
+
*
|
16
|
+
* @param {string|object} message - String or object to log.
|
17
|
+
*/
|
18
|
+
static info(message: string | object): void;
|
19
|
+
/**
|
20
|
+
* @static
|
21
|
+
* Method to log for level - "debug", this should not be called if it has been custom levels are
|
22
|
+
* set which does not include "debug"
|
23
|
+
*
|
24
|
+
* @param {string|object} message - String or object to log.
|
25
|
+
*/
|
26
|
+
static debug(message: string | object): void;
|
27
|
+
/**
|
28
|
+
* @static
|
29
|
+
* Method to log for level - "error", this should not be called if it has been custom levels are
|
30
|
+
* set which does not include "error"
|
31
|
+
*
|
32
|
+
* @param {string|object} error - String or object to log.
|
33
|
+
*/
|
34
|
+
static error(error: string | object): void;
|
35
|
+
/**
|
36
|
+
* @static
|
37
|
+
* Method to log for level - "warn", this should not be called if it has been custom levels are
|
38
|
+
* set which does not include "warn"
|
39
|
+
*
|
40
|
+
* @param {string|object} message - String or object to log.
|
41
|
+
*/
|
42
|
+
static warn(message: string | object): void;
|
43
|
+
/**
|
44
|
+
* @static
|
45
|
+
* Method to log for any level, which should be used to log all custom levels that may be added.
|
46
|
+
*
|
47
|
+
* @param {string|object} message - String or object to log.
|
48
|
+
*/
|
49
|
+
static log(level: string, message: string | object): void;
|
50
|
+
}
|
51
|
+
|
52
|
+
export { Logger };
|
@@ -0,0 +1,125 @@
|
|
1
|
+
import winston from "winston";
|
2
|
+
import * as SentryImport from "winston-transport-sentry-node";
|
3
|
+
const Sentry = SentryImport.default;
|
4
|
+
let logger;
|
5
|
+
class Logger {
|
6
|
+
/**
|
7
|
+
* @static
|
8
|
+
* Create method must first be called before using the logger. It creates a singleton, which will then
|
9
|
+
* be referred to throughout the application.
|
10
|
+
*/
|
11
|
+
static create(config) {
|
12
|
+
if (!logger) {
|
13
|
+
logger = winston.createLogger(
|
14
|
+
Object.assign(
|
15
|
+
{
|
16
|
+
format: winston.format.combine(
|
17
|
+
winston.format.timestamp({
|
18
|
+
format: "YYYY-MM-DD HH:mm:ss:ms"
|
19
|
+
}),
|
20
|
+
winston.format.colorize({
|
21
|
+
all: true,
|
22
|
+
colors: {
|
23
|
+
error: "red",
|
24
|
+
warn: "yellow",
|
25
|
+
info: "green",
|
26
|
+
debug: "white"
|
27
|
+
}
|
28
|
+
}),
|
29
|
+
winston.format.printf(
|
30
|
+
(info) => `${info.timestamp} ${info.level}: ${info.message}`
|
31
|
+
)
|
32
|
+
),
|
33
|
+
transports: [
|
34
|
+
new winston.transports.Console({
|
35
|
+
level: config.console?.level || "info"
|
36
|
+
}),
|
37
|
+
new Sentry({
|
38
|
+
sentry: {
|
39
|
+
dsn: config.sentry?.dsn
|
40
|
+
},
|
41
|
+
level: config.sentry?.level || "error"
|
42
|
+
})
|
43
|
+
]
|
44
|
+
},
|
45
|
+
config.winston
|
46
|
+
)
|
47
|
+
);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* @static
|
52
|
+
* Method to log for level - "info", this should not be called if it has been custom levels are
|
53
|
+
* set which does not include "info"
|
54
|
+
*
|
55
|
+
* @param {string|object} message - String or object to log.
|
56
|
+
*/
|
57
|
+
static info(message) {
|
58
|
+
if (typeof message === "string") {
|
59
|
+
logger?.info(message);
|
60
|
+
} else {
|
61
|
+
logger?.info(JSON.stringify(message));
|
62
|
+
}
|
63
|
+
}
|
64
|
+
/**
|
65
|
+
* @static
|
66
|
+
* Method to log for level - "debug", this should not be called if it has been custom levels are
|
67
|
+
* set which does not include "debug"
|
68
|
+
*
|
69
|
+
* @param {string|object} message - String or object to log.
|
70
|
+
*/
|
71
|
+
static debug(message) {
|
72
|
+
if (typeof message === "string") {
|
73
|
+
logger?.debug(message);
|
74
|
+
} else {
|
75
|
+
logger?.debug(JSON.stringify(message));
|
76
|
+
}
|
77
|
+
}
|
78
|
+
/**
|
79
|
+
* @static
|
80
|
+
* Method to log for level - "error", this should not be called if it has been custom levels are
|
81
|
+
* set which does not include "error"
|
82
|
+
*
|
83
|
+
* @param {string|object} error - String or object to log.
|
84
|
+
*/
|
85
|
+
static error(error) {
|
86
|
+
if (typeof error === "string") {
|
87
|
+
logger?.error(error);
|
88
|
+
} else {
|
89
|
+
logger?.error(
|
90
|
+
`${error.message ? `${error.message} : ` : ""}${JSON.stringify(error)}`
|
91
|
+
);
|
92
|
+
}
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* @static
|
96
|
+
* Method to log for level - "warn", this should not be called if it has been custom levels are
|
97
|
+
* set which does not include "warn"
|
98
|
+
*
|
99
|
+
* @param {string|object} message - String or object to log.
|
100
|
+
*/
|
101
|
+
static warn(message) {
|
102
|
+
if (typeof message === "string") {
|
103
|
+
logger?.warn(message);
|
104
|
+
} else {
|
105
|
+
logger?.warn(JSON.stringify(message));
|
106
|
+
}
|
107
|
+
}
|
108
|
+
/**
|
109
|
+
* @static
|
110
|
+
* Method to log for any level, which should be used to log all custom levels that may be added.
|
111
|
+
*
|
112
|
+
* @param {string|object} message - String or object to log.
|
113
|
+
*/
|
114
|
+
static log(level, message) {
|
115
|
+
if (typeof message === "string") {
|
116
|
+
logger?.log(level, message);
|
117
|
+
} else {
|
118
|
+
logger?.log(level, JSON.stringify(message));
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
export {
|
123
|
+
Logger
|
124
|
+
};
|
125
|
+
//# sourceMappingURL=logger.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/logger/logger.ts"],"sourcesContent":["import winston from \"winston\";\nimport type { ILoggerConfig } from \"../types\";\nimport * as SentryImport from \"winston-transport-sentry-node\";\nconst Sentry = SentryImport.default;\n\nlet logger: winston.Logger;\n\nexport class Logger {\n /**\n * @static\n * Create method must first be called before using the logger. It creates a singleton, which will then\n * be referred to throughout the application.\n */\n static create(config: ILoggerConfig) {\n if (!logger) {\n logger = winston.createLogger(\n Object.assign(\n {\n format: winston.format.combine(\n winston.format.timestamp({\n format: \"YYYY-MM-DD HH:mm:ss:ms\",\n }),\n winston.format.colorize({\n all: true,\n colors: {\n error: \"red\",\n warn: \"yellow\",\n info: \"green\",\n debug: \"white\",\n },\n }),\n winston.format.printf(\n (info) =>\n `${info.timestamp} ${info.level}: ${info.message}`\n )\n ),\n transports: [\n new winston.transports.Console({\n level: config.console?.level || \"info\",\n }),\n new Sentry({\n sentry: {\n dsn: config.sentry?.dsn,\n },\n level: config.sentry?.level || \"error\",\n }),\n ],\n },\n config.winston\n )\n );\n }\n }\n\n /**\n * @static\n * Method to log for level - \"info\", this should not be called if it has been custom levels are\n * set which does not include \"info\"\n *\n * @param {string|object} message - String or object to log.\n */\n static info(message: string | object) {\n if (typeof message === \"string\") {\n logger?.info(message);\n } else {\n logger?.info(JSON.stringify(message));\n }\n }\n\n /**\n * @static\n * Method to log for level - \"debug\", this should not be called if it has been custom levels are\n * set which does not include \"debug\"\n *\n * @param {string|object} message - String or object to log.\n */\n static debug(message: string | object) {\n if (typeof message === \"string\") {\n logger?.debug(message);\n } else {\n logger?.debug(JSON.stringify(message));\n }\n }\n\n /**\n * @static\n * Method to log for level - \"error\", this should not be called if it has been custom levels are\n * set which does not include \"error\"\n *\n * @param {string|object} error - String or object to log.\n */\n static error(error: string | object) {\n if (typeof error === \"string\") {\n logger?.error(error);\n } else {\n logger?.error(\n `${\n (error as Error).message\n ? `${(error as Error).message} : `\n : \"\"\n }${JSON.stringify(error)}`\n );\n }\n }\n\n /**\n * @static\n * Method to log for level - \"warn\", this should not be called if it has been custom levels are\n * set which does not include \"warn\"\n *\n * @param {string|object} message - String or object to log.\n */\n static warn(message: string | object) {\n if (typeof message === \"string\") {\n logger?.warn(message);\n } else {\n logger?.warn(JSON.stringify(message));\n }\n }\n\n /**\n * @static\n * Method to log for any level, which should be used to log all custom levels that may be added.\n *\n * @param {string|object} message - String or object to log.\n */\n static log(level: string, message: string | object) {\n if (typeof message === \"string\") {\n logger?.log(level, message);\n } else {\n logger?.log(level, JSON.stringify(message));\n }\n }\n}\n"],"mappings":"AAAA,OAAO,aAAa;AAEpB,YAAY,kBAAkB;AAC9B,MAAM,SAAS,aAAa;AAE5B,IAAI;AAEG,MAAM,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMhB,OAAO,OAAO,QAAuB;AACjC,QAAI,CAAC,QAAQ;AACT,eAAS,QAAQ;AAAA,QACb,OAAO;AAAA,UACH;AAAA,YACI,QAAQ,QAAQ,OAAO;AAAA,cACnB,QAAQ,OAAO,UAAU;AAAA,gBACrB,QAAQ;AAAA,cACZ,CAAC;AAAA,cACD,QAAQ,OAAO,SAAS;AAAA,gBACpB,KAAK;AAAA,gBACL,QAAQ;AAAA,kBACJ,OAAO;AAAA,kBACP,MAAM;AAAA,kBACN,MAAM;AAAA,kBACN,OAAO;AAAA,gBACX;AAAA,cACJ,CAAC;AAAA,cACD,QAAQ,OAAO;AAAA,gBACX,CAAC,SACG,GAAG,KAAK,SAAS,IAAI,KAAK,KAAK,KAAK,KAAK,OAAO;AAAA,cACxD;AAAA,YACJ;AAAA,YACA,YAAY;AAAA,cACR,IAAI,QAAQ,WAAW,QAAQ;AAAA,gBAC3B,OAAO,OAAO,SAAS,SAAS;AAAA,cACpC,CAAC;AAAA,cACD,IAAI,OAAO;AAAA,gBACP,QAAQ;AAAA,kBACJ,KAAK,OAAO,QAAQ;AAAA,gBACxB;AAAA,gBACA,OAAO,OAAO,QAAQ,SAAS;AAAA,cACnC,CAAC;AAAA,YACL;AAAA,UACJ;AAAA,UACA,OAAO;AAAA,QACX;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,KAAK,SAA0B;AAClC,QAAI,OAAO,YAAY,UAAU;AAC7B,cAAQ,KAAK,OAAO;AAAA,IACxB,OAAO;AACH,cAAQ,KAAK,KAAK,UAAU,OAAO,CAAC;AAAA,IACxC;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,MAAM,SAA0B;AACnC,QAAI,OAAO,YAAY,UAAU;AAC7B,cAAQ,MAAM,OAAO;AAAA,IACzB,OAAO;AACH,cAAQ,MAAM,KAAK,UAAU,OAAO,CAAC;AAAA,IACzC;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,MAAM,OAAwB;AACjC,QAAI,OAAO,UAAU,UAAU;AAC3B,cAAQ,MAAM,KAAK;AAAA,IACvB,OAAO;AACH,cAAQ;AAAA,QACJ,GACK,MAAgB,UACX,GAAI,MAAgB,OAAO,QAC3B,EACV,GAAG,KAAK,UAAU,KAAK,CAAC;AAAA,MAC5B;AAAA,IACJ;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,KAAK,SAA0B;AAClC,QAAI,OAAO,YAAY,UAAU;AAC7B,cAAQ,KAAK,OAAO;AAAA,IACxB,OAAO;AACH,cAAQ,KAAK,KAAK,UAAU,OAAO,CAAC;AAAA,IACxC;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,IAAI,OAAe,SAA0B;AAChD,QAAI,OAAO,YAAY,UAAU;AAC7B,cAAQ,IAAI,OAAO,OAAO;AAAA,IAC9B,OAAO;AACH,cAAQ,IAAI,OAAO,KAAK,UAAU,OAAO,CAAC;AAAA,IAC9C;AAAA,EACJ;AACJ;","names":[]}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
type WhereFilterOp = "<" | "<=" | "==" | "!=" | ">=" | ">" | "array-contains" | "in" | "not-in" | "array-contains-any";
|
2
|
+
type OrderByDirection = "desc" | "asc";
|
3
|
+
interface IQueryOrderOperationParams {
|
4
|
+
field: string;
|
5
|
+
order: OrderByDirection;
|
6
|
+
}
|
7
|
+
interface IDocumentConditionalModifications {
|
8
|
+
field: string;
|
9
|
+
value: string | number | boolean;
|
10
|
+
defaultValue: string | number | boolean;
|
11
|
+
}
|
12
|
+
interface IQueryFilterOperationParams {
|
13
|
+
field: string;
|
14
|
+
operator: WhereFilterOp;
|
15
|
+
value: string | number | boolean | string[] | number[];
|
16
|
+
}
|
17
|
+
interface IQueryFilterOperationParams {
|
18
|
+
field: string;
|
19
|
+
operator: WhereFilterOp;
|
20
|
+
value: string | number | boolean | string[] | number[];
|
21
|
+
}
|
22
|
+
|
23
|
+
export type { IDocumentConditionalModifications, IQueryFilterOperationParams, IQueryOrderOperationParams, OrderByDirection, WhereFilterOp };
|
@@ -0,0 +1 @@
|
|
1
|
+
//# sourceMappingURL=database.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/types/index.ts"],"sourcesContent":["export * from \"./logger_config\";\nexport * from \"./database\";\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import winston from 'winston';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Logger configuration interface
|
5
|
+
*/
|
6
|
+
interface ILoggerConfig {
|
7
|
+
sentry?: {
|
8
|
+
dsn?: string;
|
9
|
+
level?: string;
|
10
|
+
environment?: string;
|
11
|
+
};
|
12
|
+
console?: {
|
13
|
+
level?: string;
|
14
|
+
};
|
15
|
+
winston?: winston.LoggerOptions;
|
16
|
+
}
|
17
|
+
|
18
|
+
export type { ILoggerConfig };
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/types/logger_config.ts"],"sourcesContent":["import winston from \"winston\";\n\n/**\n * Logger configuration interface\n */\nexport interface ILoggerConfig {\n sentry?: {\n dsn?: string;\n level?: string;\n environment?: string;\n };\n console?: {\n level?: string;\n };\n winston?: winston.LoggerOptions;\n}\n"],"mappings":"AAAA,OAAO,aAAa;","names":[]}
|
package/package.json
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
{
|
2
|
+
"name": "@polygonlabs/servercore",
|
3
|
+
"version": "0.0.0",
|
4
|
+
"publishConfig": {
|
5
|
+
"access": "public"
|
6
|
+
},
|
7
|
+
"main": "dist/index.js",
|
8
|
+
"types": "dist/index.d.ts",
|
9
|
+
"exports": {
|
10
|
+
".": {
|
11
|
+
"import": "./dist/index.js",
|
12
|
+
"types": "./dist/index.d.ts"
|
13
|
+
}
|
14
|
+
},
|
15
|
+
"scripts": {
|
16
|
+
"tests": "echo 'Hello unit tests'",
|
17
|
+
"tests:integration": "echo 'Hello integration tests'",
|
18
|
+
"build": "tsup"
|
19
|
+
},
|
20
|
+
"files": [
|
21
|
+
"./dist/**",
|
22
|
+
"README.md",
|
23
|
+
"LICENSE"
|
24
|
+
],
|
25
|
+
"type": "module",
|
26
|
+
"devDependencies": {
|
27
|
+
"@semantic-release/changelog": "^6.0.3",
|
28
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
29
|
+
"@semantic-release/git": "^10.0.1",
|
30
|
+
"@semantic-release/npm": "^12.0.1",
|
31
|
+
"@semantic-release/release-notes-generator": "^14.0.3",
|
32
|
+
"@types/bun": "latest",
|
33
|
+
"semantic-release": "^24.2.3",
|
34
|
+
"tsup": "^8.4.0"
|
35
|
+
},
|
36
|
+
"peerDependencies": {
|
37
|
+
"typescript": "^5"
|
38
|
+
},
|
39
|
+
"dependencies": {
|
40
|
+
"winston": "^3.17.0",
|
41
|
+
"winston-transport-sentry-node": "^3.0.0",
|
42
|
+
"zod": "^3.24.2"
|
43
|
+
}
|
44
|
+
}
|