kastrick 1.0.0 → 1.0.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/lib/core/backendAPI.d.ts +54 -0
- package/lib/core/backendAPI.d.ts.map +1 -0
- package/lib/core/backendAPI.js +71 -0
- package/lib/core/backendAPI.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +16 -1
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/backendAPI.ts +102 -0
- package/src/index.ts +18 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend API Helper - Cleaner API handler definition
|
|
3
|
+
* Provides a simple interface for defining API endpoints
|
|
4
|
+
*/
|
|
5
|
+
export interface ApiContext {
|
|
6
|
+
params?: Record<string, any>;
|
|
7
|
+
query?: Record<string, any>;
|
|
8
|
+
body?: any;
|
|
9
|
+
}
|
|
10
|
+
export interface ApiHandlerResponse {
|
|
11
|
+
status?: number;
|
|
12
|
+
body?: any;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
export type RequestHandler = (req: any, context?: ApiContext) => ApiHandlerResponse | Promise<ApiHandlerResponse>;
|
|
16
|
+
export interface BackendAPIConfig {
|
|
17
|
+
GET?: RequestHandler;
|
|
18
|
+
POST?: RequestHandler;
|
|
19
|
+
PUT?: RequestHandler;
|
|
20
|
+
DELETE?: RequestHandler;
|
|
21
|
+
PATCH?: RequestHandler;
|
|
22
|
+
HEAD?: RequestHandler;
|
|
23
|
+
OPTIONS?: RequestHandler;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a backend API handler with cleaner syntax
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* export const API = backendAPI({
|
|
30
|
+
* GET: (req, ctx) => ({
|
|
31
|
+
* status: 200,
|
|
32
|
+
* body: { message: "Hello World" }
|
|
33
|
+
* }),
|
|
34
|
+
* POST: (req, ctx) => ({
|
|
35
|
+
* status: 201,
|
|
36
|
+
* body: { created: true }
|
|
37
|
+
* })
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
export declare function backendAPI(config: BackendAPIConfig): BackendAPIConfig;
|
|
41
|
+
/**
|
|
42
|
+
* Export handlers individually (for direct function export pattern)
|
|
43
|
+
*/
|
|
44
|
+
export declare function createHandlers(config: BackendAPIConfig): any;
|
|
45
|
+
/**
|
|
46
|
+
* Validate and normalize API response
|
|
47
|
+
*/
|
|
48
|
+
export declare function normalizeResponse(response: any): ApiHandlerResponse;
|
|
49
|
+
/**
|
|
50
|
+
* Create context from Express request
|
|
51
|
+
*/
|
|
52
|
+
export declare function createContext(req: any, params?: any): ApiContext;
|
|
53
|
+
export default backendAPI;
|
|
54
|
+
//# sourceMappingURL=backendAPI.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backendAPI.d.ts","sourceRoot":"","sources":["../../src/core/backendAPI.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,UAAU;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,MAAM,MAAM,cAAc,GAAG,CAC3B,GAAG,EAAE,GAAG,EACR,OAAO,CAAC,EAAE,UAAU,KACjB,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAEtD,MAAM,WAAW,gBAAgB;IAC/B,GAAG,CAAC,EAAE,cAAc,CAAC;IACrB,IAAI,CAAC,EAAE,cAAc,CAAC;IACtB,GAAG,CAAC,EAAE,cAAc,CAAC;IACrB,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,KAAK,CAAC,EAAE,cAAc,CAAC;IACvB,IAAI,CAAC,EAAE,cAAc,CAAC;IACtB,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,gBAAgB,CAErE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,gBAAgB,OAUtD;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,GAAG,GACZ,kBAAkB,CAiBpB;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,UAAU,CAMhE;AAED,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Backend API Helper - Cleaner API handler definition
|
|
4
|
+
* Provides a simple interface for defining API endpoints
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.backendAPI = backendAPI;
|
|
8
|
+
exports.createHandlers = createHandlers;
|
|
9
|
+
exports.normalizeResponse = normalizeResponse;
|
|
10
|
+
exports.createContext = createContext;
|
|
11
|
+
/**
|
|
12
|
+
* Create a backend API handler with cleaner syntax
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* export const API = backendAPI({
|
|
16
|
+
* GET: (req, ctx) => ({
|
|
17
|
+
* status: 200,
|
|
18
|
+
* body: { message: "Hello World" }
|
|
19
|
+
* }),
|
|
20
|
+
* POST: (req, ctx) => ({
|
|
21
|
+
* status: 201,
|
|
22
|
+
* body: { created: true }
|
|
23
|
+
* })
|
|
24
|
+
* });
|
|
25
|
+
*/
|
|
26
|
+
function backendAPI(config) {
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Export handlers individually (for direct function export pattern)
|
|
31
|
+
*/
|
|
32
|
+
function createHandlers(config) {
|
|
33
|
+
const handlers = {};
|
|
34
|
+
for (const [method, handler] of Object.entries(config)) {
|
|
35
|
+
if (handler) {
|
|
36
|
+
handlers[method] = handler;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return handlers;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validate and normalize API response
|
|
43
|
+
*/
|
|
44
|
+
function normalizeResponse(response) {
|
|
45
|
+
if (!response) {
|
|
46
|
+
return { status: 200, body: {} };
|
|
47
|
+
}
|
|
48
|
+
if (typeof response === "object" && "status" in response) {
|
|
49
|
+
return {
|
|
50
|
+
status: response.status || 200,
|
|
51
|
+
body: response.body !== undefined ? response.body : response,
|
|
52
|
+
headers: response.headers,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
status: 200,
|
|
57
|
+
body: response,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create context from Express request
|
|
62
|
+
*/
|
|
63
|
+
function createContext(req, params) {
|
|
64
|
+
return {
|
|
65
|
+
params: params || req.params || {},
|
|
66
|
+
query: req.query || {},
|
|
67
|
+
body: req.body,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
exports.default = backendAPI;
|
|
71
|
+
//# sourceMappingURL=backendAPI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backendAPI.js","sourceRoot":"","sources":["../../src/core/backendAPI.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AA4CH,gCAEC;AAKD,wCAUC;AAKD,8CAmBC;AAKD,sCAMC;AAnED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,UAAU,CAAC,MAAwB;IACjD,OAAO,MAAM,CAAC;AAChB,CAAC;A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
|
|
9
9
|
request<T>(endpoint: string, options?: RequestInit): Promise<ApiResponse<T>>;
|
|
10
10
|
}
|
|
11
|
+
export { backendAPI, createHandlers, normalizeResponse, createContext, type RequestHandler, type ApiContext, type ApiHandlerResponse, type BackendAPIConfig, } from './core/backendAPI';
|
|
12
|
+
export { Compiler, type Manifest, type ManifestRoute } from './core/compiler';
|
|
13
|
+
export { ExpressServer } from './server/expressServer';
|
|
14
|
+
export { Router } from './core/router';
|
|
15
|
+
export { Scanner, type ApiFile } from './core/scanner';
|
|
11
16
|
export default ApiHandler;
|
|
12
17
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW,CAAC,CAAC;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,UAAU;IACT,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,MAAM;IAE7B,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;CAuBnF;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW,CAAC,CAAC;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,UAAU;IACT,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,MAAM;IAE7B,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;CAuBnF;AAGD,OAAO,EACL,UAAU,EACV,cAAc,EACd,iBAAiB,EACjB,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,GACtB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,QAAQ,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEvD,eAAe,UAAU,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Kastrick API Logic Package
|
|
3
3
|
// Main entry point
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.ApiHandler = void 0;
|
|
5
|
+
exports.Scanner = exports.Router = exports.ExpressServer = exports.Compiler = exports.createContext = exports.normalizeResponse = exports.createHandlers = exports.backendAPI = exports.ApiHandler = void 0;
|
|
6
6
|
class ApiHandler {
|
|
7
7
|
constructor(baseUrl) {
|
|
8
8
|
this.baseUrl = baseUrl;
|
|
@@ -31,5 +31,20 @@ class ApiHandler {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
exports.ApiHandler = ApiHandler;
|
|
34
|
+
// Export backend API helpers
|
|
35
|
+
var backendAPI_1 = require("./core/backendAPI");
|
|
36
|
+
Object.defineProperty(exports, "backendAPI", { enumerable: true, get: function () { return backendAPI_1.backendAPI; } });
|
|
37
|
+
Object.defineProperty(exports, "createHandlers", { enumerable: true, get: function () { return backendAPI_1.createHandlers; } });
|
|
38
|
+
Object.defineProperty(exports, "normalizeResponse", { enumerable: true, get: function () { return backendAPI_1.normalizeResponse; } });
|
|
39
|
+
Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return backendAPI_1.createContext; } });
|
|
40
|
+
// Export server and compiler classes
|
|
41
|
+
var compiler_1 = require("./core/compiler");
|
|
42
|
+
Object.defineProperty(exports, "Compiler", { enumerable: true, get: function () { return compiler_1.Compiler; } });
|
|
43
|
+
var expressServer_1 = require("./server/expressServer");
|
|
44
|
+
Object.defineProperty(exports, "ExpressServer", { enumerable: true, get: function () { return expressServer_1.ExpressServer; } });
|
|
45
|
+
var router_1 = require("./core/router");
|
|
46
|
+
Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return router_1.Router; } });
|
|
47
|
+
var scanner_1 = require("./core/scanner");
|
|
48
|
+
Object.defineProperty(exports, "Scanner", { enumerable: true, get: function () { return scanner_1.Scanner; } });
|
|
34
49
|
exports.default = ApiHandler;
|
|
35
50
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,6BAA6B;AAC7B,mBAAmB;;;AAQnB,MAAa,UAAU;IACrB,YAAoB,OAAe;QAAf,YAAO,GAAP,OAAO,CAAQ;IAAG,CAAC;IAEvC,KAAK,CAAC,OAAO,CAAI,QAAgB,EAAE,OAAqB;QACtD,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,GAAG,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;YACpE,MAAM,IAAI,GAAQ,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YAExC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,OAAO;oBACL,OAAO,EAAE,KAAK;oBACd,KAAK,EAAE,CAAC,IAAI,EAAE,KAAK,IAAI,gBAAgB,CAAW;iBACnD,CAAC;YACJ,CAAC;YAED,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,IA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AA;AACV,4GAAA,cAAc,OAAA;AACd,+GAAA,iBAAiB,OAAA;AACjB,2GAAA,aAAa,OAAA;AAOf,qCAAqC;AACrC,4CAA8E;AAArE,oGAAA,QAAQ,OAAA;AACjB,wDAAuD;AAA9C,8GAAA,aAAa,OAAA;AACtB,wCAAuC;AAA9B,gGAAA,MAAM,OAAA;AACf,0CAAuD;AAA9C,kGAAA,OAAO,OAAA;AAEhB,kBAAe,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend API Helper - Cleaner API handler definition
|
|
3
|
+
* Provides a simple interface for defining API endpoints
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface ApiContext {
|
|
7
|
+
params?: Record<string, any>;
|
|
8
|
+
query?: Record<string, any>;
|
|
9
|
+
body?: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ApiHandlerResponse {
|
|
13
|
+
status?: number;
|
|
14
|
+
body?: any;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type RequestHandler = (
|
|
19
|
+
req: any,
|
|
20
|
+
context?: ApiContext
|
|
21
|
+
) => ApiHandlerResponse | Promise<ApiHandlerResponse>;
|
|
22
|
+
|
|
23
|
+
export interface BackendAPIConfig {
|
|
24
|
+
GET?: RequestHandler;
|
|
25
|
+
POST?: RequestHandler;
|
|
26
|
+
PUT?: RequestHandler;
|
|
27
|
+
DELETE?: RequestHandler;
|
|
28
|
+
PATCH?: RequestHandler;
|
|
29
|
+
HEAD?: RequestHandler;
|
|
30
|
+
OPTIONS?: RequestHandler;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create a backend API handler with cleaner syntax
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* export const API = backendAPI({
|
|
38
|
+
* GET: (req, ctx) => ({
|
|
39
|
+
* status: 200,
|
|
40
|
+
* body: { message: "Hello World" }
|
|
41
|
+
* }),
|
|
42
|
+
* POST: (req, ctx) => ({
|
|
43
|
+
* status: 201,
|
|
44
|
+
* body: { created: true }
|
|
45
|
+
* })
|
|
46
|
+
* });
|
|
47
|
+
*/
|
|
48
|
+
export function backendAPI(config: BackendAPIConfig): BackendAPIConfig {
|
|
49
|
+
return config;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Export handlers individually (for direct function export pattern)
|
|
54
|
+
*/
|
|
55
|
+
export function createHandlers(config: BackendAPIConfig) {
|
|
56
|
+
const handlers: any = {};
|
|
57
|
+
|
|
58
|
+
for (const [method, handler] of Object.entries(config)) {
|
|
59
|
+
if (handler) {
|
|
60
|
+
handlers[method] = handler;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return handlers;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Validate and normalize API response
|
|
69
|
+
*/
|
|
70
|
+
export function normalizeResponse(
|
|
71
|
+
response: any
|
|
72
|
+
): ApiHandlerResponse {
|
|
73
|
+
if (!response) {
|
|
74
|
+
return { status: 200, body: {} };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof response === "object" && "status" in response) {
|
|
78
|
+
return {
|
|
79
|
+
status: response.status || 200,
|
|
80
|
+
body: response.body !== undefined ? response.body : response,
|
|
81
|
+
headers: response.headers,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
status: 200,
|
|
87
|
+
body: response,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Create context from Express request
|
|
93
|
+
*/
|
|
94
|
+
export function createContext(req: any, params?: any): ApiContext {
|
|
95
|
+
return {
|
|
96
|
+
params: params || req.params || {},
|
|
97
|
+
query: req.query || {},
|
|
98
|
+
body: req.body,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export default backendAPI;
|
package/src/index.ts
CHANGED
|
@@ -35,4 +35,22 @@ export class ApiHandler {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
// Export backend API helpers
|
|
39
|
+
export {
|
|
40
|
+
backendAPI,
|
|
41
|
+
createHandlers,
|
|
42
|
+
normalizeResponse,
|
|
43
|
+
createContext,
|
|
44
|
+
type RequestHandler,
|
|
45
|
+
type ApiContext,
|
|
46
|
+
type ApiHandlerResponse,
|
|
47
|
+
type BackendAPIConfig,
|
|
48
|
+
} from './core/backendAPI';
|
|
49
|
+
|
|
50
|
+
// Export server and compiler classes
|
|
51
|
+
export { Compiler, type Manifest, type ManifestRoute } from './core/compiler';
|
|
52
|
+
export { ExpressServer } from './server/expressServer';
|
|
53
|
+
export { Router } from './core/router';
|
|
54
|
+
export { Scanner, type ApiFile } from './core/scanner';
|
|
55
|
+
|
|
38
56
|
export default ApiHandler;
|