@nara-web/core 0.1.0 → 0.1.1
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/dist/App.d.ts +11 -29
- package/dist/App.d.ts.map +1 -1
- package/dist/App.js +24 -237
- package/dist/App.js.map +1 -1
- package/dist/BaseController.d.ts +5 -26
- package/dist/BaseController.d.ts.map +1 -1
- package/dist/BaseController.js +6 -56
- package/dist/BaseController.js.map +1 -1
- package/dist/adapters.d.ts +7 -0
- package/dist/adapters.d.ts.map +1 -0
- package/dist/{adapters/types.js → adapters.js} +1 -1
- package/dist/adapters.js.map +1 -0
- package/dist/errors.d.ts +7 -23
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +21 -73
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -16
- package/dist/index.js.map +1 -1
- package/dist/response.d.ts +6 -27
- package/dist/response.d.ts.map +1 -1
- package/dist/response.js +11 -54
- package/dist/response.js.map +1 -1
- package/dist/types.d.ts +9 -20
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/App.ts +67 -0
- package/src/BaseController.ts +15 -0
- package/src/adapters.ts +7 -0
- package/src/errors.ts +41 -0
- package/src/index.ts +38 -2
- package/src/response.ts +25 -0
- package/src/types.ts +9 -55
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/Router.d.ts +0 -32
- package/dist/Router.d.ts.map +0 -1
- package/dist/Router.js +0 -100
- package/dist/Router.js.map +0 -1
- package/dist/adapters/svelte.d.ts +0 -3
- package/dist/adapters/svelte.d.ts.map +0 -1
- package/dist/adapters/svelte.js +0 -18
- package/dist/adapters/svelte.js.map +0 -1
- package/dist/adapters/types.d.ts +0 -9
- package/dist/adapters/types.d.ts.map +0 -1
- package/dist/adapters/types.js.map +0 -1
- package/src/adapters/types.ts +0 -31
package/dist/errors.js
CHANGED
|
@@ -1,100 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.isHttpError = isHttpError;
|
|
5
|
-
exports.isValidationError = isValidationError;
|
|
3
|
+
exports.ValidationError = exports.BadRequestError = exports.ForbiddenError = exports.UnauthorizedError = exports.NotFoundError = exports.HttpError = void 0;
|
|
6
4
|
class HttpError extends Error {
|
|
7
|
-
constructor(
|
|
5
|
+
constructor(statusCode, message) {
|
|
8
6
|
super(message);
|
|
9
|
-
this.name = 'HttpError';
|
|
10
7
|
this.statusCode = statusCode;
|
|
11
|
-
this.
|
|
12
|
-
Error.captureStackTrace(this, this.constructor);
|
|
13
|
-
}
|
|
14
|
-
toJSON() {
|
|
15
|
-
return {
|
|
16
|
-
name: this.name,
|
|
17
|
-
message: this.message,
|
|
18
|
-
statusCode: this.statusCode,
|
|
19
|
-
code: this.code,
|
|
20
|
-
};
|
|
8
|
+
this.name = 'HttpError';
|
|
21
9
|
}
|
|
22
10
|
}
|
|
23
11
|
exports.HttpError = HttpError;
|
|
24
|
-
class ValidationError extends HttpError {
|
|
25
|
-
constructor(message = 'Validation failed', errors = {}) {
|
|
26
|
-
super(message, 422, 'VALIDATION_ERROR');
|
|
27
|
-
this.name = 'ValidationError';
|
|
28
|
-
this.errors = errors;
|
|
29
|
-
}
|
|
30
|
-
toJSON() {
|
|
31
|
-
return {
|
|
32
|
-
...super.toJSON(),
|
|
33
|
-
errors: this.errors,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
exports.ValidationError = ValidationError;
|
|
38
|
-
class AuthError extends HttpError {
|
|
39
|
-
constructor(message = 'Unauthorized') {
|
|
40
|
-
super(message, 401, 'AUTH_ERROR');
|
|
41
|
-
this.name = 'AuthError';
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.AuthError = AuthError;
|
|
45
12
|
class NotFoundError extends HttpError {
|
|
46
|
-
constructor(message = 'Not
|
|
47
|
-
super(
|
|
13
|
+
constructor(message = 'Not found') {
|
|
14
|
+
super(404, message);
|
|
48
15
|
this.name = 'NotFoundError';
|
|
49
16
|
}
|
|
50
17
|
}
|
|
51
18
|
exports.NotFoundError = NotFoundError;
|
|
19
|
+
class UnauthorizedError extends HttpError {
|
|
20
|
+
constructor(message = 'Unauthorized') {
|
|
21
|
+
super(401, message);
|
|
22
|
+
this.name = 'UnauthorizedError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
52
26
|
class ForbiddenError extends HttpError {
|
|
53
27
|
constructor(message = 'Forbidden') {
|
|
54
|
-
super(
|
|
28
|
+
super(403, message);
|
|
55
29
|
this.name = 'ForbiddenError';
|
|
56
30
|
}
|
|
57
31
|
}
|
|
58
32
|
exports.ForbiddenError = ForbiddenError;
|
|
59
33
|
class BadRequestError extends HttpError {
|
|
60
|
-
constructor(message = 'Bad
|
|
61
|
-
super(
|
|
34
|
+
constructor(message = 'Bad request') {
|
|
35
|
+
super(400, message);
|
|
62
36
|
this.name = 'BadRequestError';
|
|
63
37
|
}
|
|
64
38
|
}
|
|
65
39
|
exports.BadRequestError = BadRequestError;
|
|
66
|
-
class
|
|
67
|
-
constructor(message = '
|
|
68
|
-
super(
|
|
69
|
-
this.
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
exports.ConflictError = ConflictError;
|
|
73
|
-
class TooManyRequestsError extends HttpError {
|
|
74
|
-
constructor(message = 'Too Many Requests', retryAfter) {
|
|
75
|
-
super(message, 429, 'TOO_MANY_REQUESTS');
|
|
76
|
-
this.name = 'TooManyRequestsError';
|
|
77
|
-
this.retryAfter = retryAfter;
|
|
78
|
-
}
|
|
79
|
-
toJSON() {
|
|
80
|
-
return {
|
|
81
|
-
...super.toJSON(),
|
|
82
|
-
...(this.retryAfter && { retryAfter: this.retryAfter }),
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
exports.TooManyRequestsError = TooManyRequestsError;
|
|
87
|
-
class InternalError extends HttpError {
|
|
88
|
-
constructor(message = 'Internal Server Error') {
|
|
89
|
-
super(message, 500, 'INTERNAL_ERROR');
|
|
90
|
-
this.name = 'InternalError';
|
|
40
|
+
class ValidationError extends HttpError {
|
|
41
|
+
constructor(errors, message = 'Validation failed') {
|
|
42
|
+
super(422, message);
|
|
43
|
+
this.errors = errors;
|
|
44
|
+
this.name = 'ValidationError';
|
|
91
45
|
}
|
|
92
46
|
}
|
|
93
|
-
exports.
|
|
94
|
-
function isHttpError(error) {
|
|
95
|
-
return error instanceof HttpError;
|
|
96
|
-
}
|
|
97
|
-
function isValidationError(error) {
|
|
98
|
-
return error instanceof ValidationError;
|
|
99
|
-
}
|
|
47
|
+
exports.ValidationError = ValidationError;
|
|
100
48
|
//# sourceMappingURL=errors.js.map
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,SAAU,SAAQ,KAAK;IAClC,YAAmB,UAAkB,EAAE,OAAe;QACpD,KAAK,CAAC,OAAO,CAAC,CAAC;QADE,eAAU,GAAV,UAAU,CAAQ;QAEnC,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IAC1B,CAAC;CACF;AALD,8BAKC;AAED,MAAa,aAAc,SAAQ,SAAS;IAC1C,YAAY,OAAO,GAAG,WAAW;QAC/B,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AALD,sCAKC;AAED,MAAa,iBAAkB,SAAQ,SAAS;IAC9C,YAAY,OAAO,GAAG,cAAc;QAClC,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AALD,8CAKC;AAED,MAAa,cAAe,SAAQ,SAAS;IAC3C,YAAY,OAAO,GAAG,WAAW;QAC/B,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF;AALD,wCAKC;AAED,MAAa,eAAgB,SAAQ,SAAS;IAC5C,YAAY,OAAO,GAAG,aAAa;QACjC,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AALD,0CAKC;AAED,MAAa,eAAgB,SAAQ,SAAS;IAC5C,YAAmB,MAAgC,EAAE,OAAO,GAAG,mBAAmB;QAChF,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QADH,WAAM,GAAN,MAAM,CAA0B;QAEjD,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AALD,0CAKC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { NaraApp, createApp } from './App';
|
|
2
|
+
export type { AppOptions } from './App';
|
|
3
|
+
export { BaseController } from './BaseController';
|
|
4
|
+
export type { User, NaraRequest, NaraResponse, NaraHandler, NaraMiddleware } from './types';
|
|
5
|
+
export type { FrontendAdapter } from './adapters';
|
|
6
|
+
export { jsonSuccess, jsonError, jsonNotFound, jsonUnauthorized, jsonForbidden, jsonValidationError, } from './response';
|
|
7
|
+
export { HttpError, NotFoundError, UnauthorizedError, ForbiddenError, BadRequestError, ValidationError, } from './errors';
|
|
3
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAC3C,YAAY,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGlD,YAAY,EACV,IAAI,EACJ,WAAW,EACX,YAAY,EACZ,WAAW,EACX,cAAc,EACf,MAAM,SAAS,CAAC;AAGjB,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAGlD,OAAO,EACL,WAAW,EACX,SAAS,EACT,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,mBAAmB,GACpB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,SAAS,EACT,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,eAAe,EACf,eAAe,GAChB,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
exports.ValidationError = exports.BadRequestError = exports.ForbiddenError = exports.UnauthorizedError = exports.NotFoundError = exports.HttpError = exports.jsonValidationError = exports.jsonForbidden = exports.jsonUnauthorized = exports.jsonNotFound = exports.jsonError = exports.jsonSuccess = exports.BaseController = exports.createApp = exports.NaraApp = void 0;
|
|
4
|
+
var App_1 = require("./App");
|
|
5
|
+
Object.defineProperty(exports, "NaraApp", { enumerable: true, get: function () { return App_1.NaraApp; } });
|
|
6
|
+
Object.defineProperty(exports, "createApp", { enumerable: true, get: function () { return App_1.createApp; } });
|
|
7
|
+
var BaseController_1 = require("./BaseController");
|
|
8
|
+
Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return BaseController_1.BaseController; } });
|
|
9
|
+
var response_1 = require("./response");
|
|
10
|
+
Object.defineProperty(exports, "jsonSuccess", { enumerable: true, get: function () { return response_1.jsonSuccess; } });
|
|
11
|
+
Object.defineProperty(exports, "jsonError", { enumerable: true, get: function () { return response_1.jsonError; } });
|
|
12
|
+
Object.defineProperty(exports, "jsonNotFound", { enumerable: true, get: function () { return response_1.jsonNotFound; } });
|
|
13
|
+
Object.defineProperty(exports, "jsonUnauthorized", { enumerable: true, get: function () { return response_1.jsonUnauthorized; } });
|
|
14
|
+
Object.defineProperty(exports, "jsonForbidden", { enumerable: true, get: function () { return response_1.jsonForbidden; } });
|
|
15
|
+
Object.defineProperty(exports, "jsonValidationError", { enumerable: true, get: function () { return response_1.jsonValidationError; } });
|
|
16
|
+
var errors_1 = require("./errors");
|
|
17
|
+
Object.defineProperty(exports, "HttpError", { enumerable: true, get: function () { return errors_1.HttpError; } });
|
|
18
|
+
Object.defineProperty(exports, "NotFoundError", { enumerable: true, get: function () { return errors_1.NotFoundError; } });
|
|
19
|
+
Object.defineProperty(exports, "UnauthorizedError", { enumerable: true, get: function () { return errors_1.UnauthorizedError; } });
|
|
20
|
+
Object.defineProperty(exports, "ForbiddenError", { enumerable: true, get: function () { return errors_1.ForbiddenError; } });
|
|
21
|
+
Object.defineProperty(exports, "BadRequestError", { enumerable: true, get: function () { return errors_1.BadRequestError; } });
|
|
22
|
+
Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_1.ValidationError; } });
|
|
19
23
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,6BAA2C;AAAlC,8FAAA,OAAO,OAAA;AAAE,gGAAA,SAAS,OAAA;AAI3B,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAevB,uCAOoB;AANlB,uGAAA,WAAW,OAAA;AACX,qGAAA,SAAS,OAAA;AACT,wGAAA,YAAY,OAAA;AACZ,4GAAA,gBAAgB,OAAA;AAChB,yGAAA,aAAa,OAAA;AACb,+GAAA,mBAAmB,OAAA;AAIrB,mCAOkB;AANhB,mGAAA,SAAS,OAAA;AACT,uGAAA,aAAa,OAAA;AACb,2GAAA,iBAAiB,OAAA;AACjB,wGAAA,cAAc,OAAA;AACd,yGAAA,eAAe,OAAA;AACf,yGAAA,eAAe,OAAA"}
|
package/dist/response.d.ts
CHANGED
|
@@ -1,29 +1,8 @@
|
|
|
1
1
|
import type { NaraResponse } from './types';
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
message: string;
|
|
9
|
-
data?: T;
|
|
10
|
-
meta?: ResponseMeta;
|
|
11
|
-
}
|
|
12
|
-
export interface ApiErrorResponse {
|
|
13
|
-
success: false;
|
|
14
|
-
message: string;
|
|
15
|
-
code?: string;
|
|
16
|
-
errors?: Record<string, string[]>;
|
|
17
|
-
}
|
|
18
|
-
export type ApiResponse<T = unknown> = ApiSuccessResponse<T> | ApiErrorResponse;
|
|
19
|
-
export declare function jsonSuccess<T = unknown>(res: NaraResponse, message: string, data?: T, meta?: ResponseMeta, statusCode?: number): NaraResponse;
|
|
20
|
-
export declare function jsonError(res: NaraResponse, message: string, statusCode?: number, code?: string, errors?: Record<string, string[]>): NaraResponse;
|
|
21
|
-
export declare function jsonPaginated<T = unknown>(res: NaraResponse, message: string, data: T[], meta: PaginatedMeta): NaraResponse;
|
|
22
|
-
export declare function jsonCreated<T = unknown>(res: NaraResponse, message: string, data?: T): NaraResponse;
|
|
23
|
-
export declare function jsonNoContent(res: NaraResponse): NaraResponse;
|
|
24
|
-
export declare function jsonUnauthorized(res: NaraResponse, message?: string): NaraResponse;
|
|
25
|
-
export declare function jsonForbidden(res: NaraResponse, message?: string): NaraResponse;
|
|
26
|
-
export declare function jsonNotFound(res: NaraResponse, message?: string): NaraResponse;
|
|
27
|
-
export declare function jsonValidationError(res: NaraResponse, message?: string, errors?: Record<string, string[]>): NaraResponse;
|
|
28
|
-
export declare function jsonServerError(res: NaraResponse, message?: string): NaraResponse;
|
|
2
|
+
export declare function jsonSuccess(res: NaraResponse, data: any, message?: string): boolean;
|
|
3
|
+
export declare function jsonError(res: NaraResponse, message: string, status?: number): boolean;
|
|
4
|
+
export declare function jsonNotFound(res: NaraResponse, message?: string): boolean;
|
|
5
|
+
export declare function jsonUnauthorized(res: NaraResponse, message?: string): boolean;
|
|
6
|
+
export declare function jsonForbidden(res: NaraResponse, message?: string): boolean;
|
|
7
|
+
export declare function jsonValidationError(res: NaraResponse, errors: Record<string, string[]>): boolean;
|
|
29
8
|
//# sourceMappingURL=response.d.ts.map
|
package/dist/response.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,wBAAgB,WAAW,CAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,SAAY,WAE5E;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAM,WAEzE;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,SAAc,WAEpE;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,SAAiB,WAE3E;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,SAAc,WAErE;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,WAEtF"}
|
package/dist/response.js
CHANGED
|
@@ -2,69 +2,26 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jsonSuccess = jsonSuccess;
|
|
4
4
|
exports.jsonError = jsonError;
|
|
5
|
-
exports.
|
|
6
|
-
exports.jsonCreated = jsonCreated;
|
|
7
|
-
exports.jsonNoContent = jsonNoContent;
|
|
5
|
+
exports.jsonNotFound = jsonNotFound;
|
|
8
6
|
exports.jsonUnauthorized = jsonUnauthorized;
|
|
9
7
|
exports.jsonForbidden = jsonForbidden;
|
|
10
|
-
exports.jsonNotFound = jsonNotFound;
|
|
11
8
|
exports.jsonValidationError = jsonValidationError;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const response = {
|
|
15
|
-
success: true,
|
|
16
|
-
message,
|
|
17
|
-
};
|
|
18
|
-
if (data !== undefined) {
|
|
19
|
-
response.data = data;
|
|
20
|
-
}
|
|
21
|
-
if (meta !== undefined) {
|
|
22
|
-
response.meta = meta;
|
|
23
|
-
}
|
|
24
|
-
res.status(statusCode).json(response);
|
|
25
|
-
return res;
|
|
26
|
-
}
|
|
27
|
-
function jsonError(res, message, statusCode = 400, code, errors) {
|
|
28
|
-
const response = {
|
|
29
|
-
success: false,
|
|
30
|
-
message,
|
|
31
|
-
};
|
|
32
|
-
if (code !== undefined) {
|
|
33
|
-
response.code = code;
|
|
34
|
-
}
|
|
35
|
-
if (errors !== undefined) {
|
|
36
|
-
response.errors = errors;
|
|
37
|
-
}
|
|
38
|
-
res.status(statusCode).json(response);
|
|
39
|
-
return res;
|
|
9
|
+
function jsonSuccess(res, data, message = 'Success') {
|
|
10
|
+
return res.json({ success: true, message, data });
|
|
40
11
|
}
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
...meta,
|
|
44
|
-
totalPages: meta.totalPages ?? Math.ceil(meta.total / meta.limit),
|
|
45
|
-
};
|
|
46
|
-
return jsonSuccess(res, message, data, paginationMeta);
|
|
12
|
+
function jsonError(res, message, status = 400) {
|
|
13
|
+
return res.status(status).json({ success: false, message });
|
|
47
14
|
}
|
|
48
|
-
function
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
function jsonNoContent(res) {
|
|
52
|
-
res.status(204).send('');
|
|
53
|
-
return res;
|
|
15
|
+
function jsonNotFound(res, message = 'Not found') {
|
|
16
|
+
return res.status(404).json({ success: false, message });
|
|
54
17
|
}
|
|
55
18
|
function jsonUnauthorized(res, message = 'Unauthorized') {
|
|
56
|
-
return
|
|
19
|
+
return res.status(401).json({ success: false, message });
|
|
57
20
|
}
|
|
58
21
|
function jsonForbidden(res, message = 'Forbidden') {
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
function jsonNotFound(res, message = 'Not Found') {
|
|
62
|
-
return jsonError(res, message, 404, 'NOT_FOUND');
|
|
63
|
-
}
|
|
64
|
-
function jsonValidationError(res, message = 'Validation failed', errors) {
|
|
65
|
-
return jsonError(res, message, 422, 'VALIDATION_ERROR', errors);
|
|
22
|
+
return res.status(403).json({ success: false, message });
|
|
66
23
|
}
|
|
67
|
-
function
|
|
68
|
-
return
|
|
24
|
+
function jsonValidationError(res, errors) {
|
|
25
|
+
return res.status(422).json({ success: false, message: 'Validation failed', errors });
|
|
69
26
|
}
|
|
70
27
|
//# sourceMappingURL=response.js.map
|
package/dist/response.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":";;AAEA,kCAEC;AAED,8BAEC;AAED,oCAEC;AAED,4CAEC;AAED,sCAEC;AAED,kDAEC;AAtBD,SAAgB,WAAW,CAAC,GAAiB,EAAE,IAAS,EAAE,OAAO,GAAG,SAAS;IAC3E,OAAO,GAAG,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AACpD,CAAC;AAED,SAAgB,SAAS,CAAC,GAAiB,EAAE,OAAe,EAAE,MAAM,GAAG,GAAG;IACxE,OAAO,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;AAC9D,CAAC;AAED,SAAgB,YAAY,CAAC,GAAiB,EAAE,OAAO,GAAG,WAAW;IACnE,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,SAAgB,gBAAgB,CAAC,GAAiB,EAAE,OAAO,GAAG,cAAc;IAC1E,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,SAAgB,aAAa,CAAC,GAAiB,EAAE,OAAO,GAAG,WAAW;IACpE,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,SAAgB,mBAAmB,CAAC,GAAiB,EAAE,MAAgC;IACrF,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,EAAE,CAAC,CAAC;AACxF,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
|
-
import type { Request
|
|
1
|
+
import type { Request, Response, MiddlewareHandler } from 'hyper-express';
|
|
2
2
|
export interface User {
|
|
3
|
-
id:
|
|
4
|
-
name: string
|
|
3
|
+
id: number;
|
|
4
|
+
name: string;
|
|
5
5
|
email: string;
|
|
6
|
-
|
|
7
|
-
avatar: string | null;
|
|
8
|
-
is_admin: boolean;
|
|
9
|
-
is_verified: boolean;
|
|
10
|
-
created_at?: number;
|
|
11
|
-
updated_at?: number;
|
|
6
|
+
role?: string;
|
|
12
7
|
}
|
|
13
|
-
export interface NaraRequest extends
|
|
8
|
+
export interface NaraRequest extends Request {
|
|
14
9
|
user?: User;
|
|
15
|
-
share?: Record<string, unknown>;
|
|
16
10
|
}
|
|
17
|
-
export interface NaraResponse extends
|
|
18
|
-
|
|
19
|
-
inertia?(component: string, props?: Record<string, unknown>, viewProps?: Record<string, unknown>): Promise<unknown>;
|
|
20
|
-
flash(key: string, value: unknown): NaraResponse;
|
|
11
|
+
export interface NaraResponse extends Response {
|
|
12
|
+
inertia?: (component: string, props?: Record<string, any>) => void;
|
|
21
13
|
}
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
export type NaraMiddleware = (req: NaraRequest, res: NaraResponse, next: MiddlewareNext) => unknown | Promise<unknown>;
|
|
26
|
-
export type NaraHandler = (req: NaraRequest, res: NaraResponse) => unknown | Promise<unknown>;
|
|
14
|
+
export type NaraHandler = (req: NaraRequest, res: NaraResponse) => void | Promise<void>;
|
|
15
|
+
export type NaraMiddleware = MiddlewareHandler;
|
|
27
16
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE1E,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,WAAY,SAAQ,OAAO;IAC1C,IAAI,CAAC,EAAE,IAAI,CAAC;CACb;AAED,MAAM,WAAW,YAAa,SAAQ,QAAQ;IAC5C,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;CACpE;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACxF,MAAM,MAAM,cAAc,GAAG,iBAAiB,CAAC"}
|
package/package.json
CHANGED
package/src/App.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import HyperExpress from 'hyper-express';
|
|
2
|
+
import type { NaraRequest, NaraResponse, NaraHandler, NaraMiddleware } from './types';
|
|
3
|
+
import type { FrontendAdapter } from './adapters';
|
|
4
|
+
|
|
5
|
+
export interface AppOptions {
|
|
6
|
+
port?: number;
|
|
7
|
+
adapter?: FrontendAdapter;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class NaraApp {
|
|
11
|
+
private server: HyperExpress.Server;
|
|
12
|
+
private port: number;
|
|
13
|
+
private adapter?: FrontendAdapter;
|
|
14
|
+
|
|
15
|
+
constructor(options: AppOptions = {}) {
|
|
16
|
+
this.server = new HyperExpress.Server();
|
|
17
|
+
this.port = options.port || 3000;
|
|
18
|
+
this.adapter = options.adapter;
|
|
19
|
+
|
|
20
|
+
if (this.adapter) {
|
|
21
|
+
this.server.use(this.adapter.middleware());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
use(middleware: NaraMiddleware) {
|
|
26
|
+
this.server.use(middleware);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get(path: string, ...handlers: NaraHandler[]) {
|
|
31
|
+
this.server.get(path, ...handlers as any);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
post(path: string, ...handlers: NaraHandler[]) {
|
|
36
|
+
this.server.post(path, ...handlers as any);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
put(path: string, ...handlers: NaraHandler[]) {
|
|
41
|
+
this.server.put(path, ...handlers as any);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
patch(path: string, ...handlers: NaraHandler[]) {
|
|
46
|
+
this.server.patch(path, ...handlers as any);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
delete(path: string, ...handlers: NaraHandler[]) {
|
|
51
|
+
this.server.delete(path, ...handlers as any);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async start() {
|
|
56
|
+
await this.server.listen(this.port);
|
|
57
|
+
console.log(`🚀 Server running on http://localhost:${this.port}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
getServer() {
|
|
61
|
+
return this.server;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function createApp(options?: AppOptions): NaraApp {
|
|
66
|
+
return new NaraApp(options);
|
|
67
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NaraRequest, NaraResponse } from './types';
|
|
2
|
+
|
|
3
|
+
export class BaseController {
|
|
4
|
+
protected json(res: NaraResponse, data: any, status = 200) {
|
|
5
|
+
return res.status(status).json(data);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
protected success(res: NaraResponse, data: any, message = 'Success') {
|
|
9
|
+
return res.json({ success: true, message, data });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
protected error(res: NaraResponse, message: string, status = 400) {
|
|
13
|
+
return res.status(status).json({ success: false, message });
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/adapters.ts
ADDED
package/src/errors.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class HttpError extends Error {
|
|
2
|
+
constructor(public statusCode: number, message: string) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'HttpError';
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class NotFoundError extends HttpError {
|
|
9
|
+
constructor(message = 'Not found') {
|
|
10
|
+
super(404, message);
|
|
11
|
+
this.name = 'NotFoundError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class UnauthorizedError extends HttpError {
|
|
16
|
+
constructor(message = 'Unauthorized') {
|
|
17
|
+
super(401, message);
|
|
18
|
+
this.name = 'UnauthorizedError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class ForbiddenError extends HttpError {
|
|
23
|
+
constructor(message = 'Forbidden') {
|
|
24
|
+
super(403, message);
|
|
25
|
+
this.name = 'ForbiddenError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class BadRequestError extends HttpError {
|
|
30
|
+
constructor(message = 'Bad request') {
|
|
31
|
+
super(400, message);
|
|
32
|
+
this.name = 'BadRequestError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class ValidationError extends HttpError {
|
|
37
|
+
constructor(public errors: Record<string, string[]>, message = 'Validation failed') {
|
|
38
|
+
super(422, message);
|
|
39
|
+
this.name = 'ValidationError';
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
// App
|
|
2
|
+
export { NaraApp, createApp } from './App';
|
|
3
|
+
export type { AppOptions } from './App';
|
|
4
|
+
|
|
5
|
+
// Base Controller
|
|
6
|
+
export { BaseController } from './BaseController';
|
|
7
|
+
|
|
8
|
+
// Types
|
|
9
|
+
export type {
|
|
10
|
+
User,
|
|
11
|
+
NaraRequest,
|
|
12
|
+
NaraResponse,
|
|
13
|
+
NaraHandler,
|
|
14
|
+
NaraMiddleware
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
// Adapters
|
|
18
|
+
export type { FrontendAdapter } from './adapters';
|
|
19
|
+
|
|
20
|
+
// Response helpers
|
|
21
|
+
export {
|
|
22
|
+
jsonSuccess,
|
|
23
|
+
jsonError,
|
|
24
|
+
jsonNotFound,
|
|
25
|
+
jsonUnauthorized,
|
|
26
|
+
jsonForbidden,
|
|
27
|
+
jsonValidationError,
|
|
28
|
+
} from './response';
|
|
29
|
+
|
|
30
|
+
// Errors
|
|
31
|
+
export {
|
|
32
|
+
HttpError,
|
|
33
|
+
NotFoundError,
|
|
34
|
+
UnauthorizedError,
|
|
35
|
+
ForbiddenError,
|
|
36
|
+
BadRequestError,
|
|
37
|
+
ValidationError,
|
|
38
|
+
} from './errors';
|
package/src/response.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { NaraResponse } from './types';
|
|
2
|
+
|
|
3
|
+
export function jsonSuccess(res: NaraResponse, data: any, message = 'Success') {
|
|
4
|
+
return res.json({ success: true, message, data });
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function jsonError(res: NaraResponse, message: string, status = 400) {
|
|
8
|
+
return res.status(status).json({ success: false, message });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function jsonNotFound(res: NaraResponse, message = 'Not found') {
|
|
12
|
+
return res.status(404).json({ success: false, message });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function jsonUnauthorized(res: NaraResponse, message = 'Unauthorized') {
|
|
16
|
+
return res.status(401).json({ success: false, message });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function jsonForbidden(res: NaraResponse, message = 'Forbidden') {
|
|
20
|
+
return res.status(403).json({ success: false, message });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function jsonValidationError(res: NaraResponse, errors: Record<string, string[]>) {
|
|
24
|
+
return res.status(422).json({ success: false, message: 'Validation failed', errors });
|
|
25
|
+
}
|