@codexsploitx/schemaapi 1.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.
Files changed (66) hide show
  1. package/.prettierignore +5 -0
  2. package/.prettierrc +7 -0
  3. package/bin/schemaapi +302 -0
  4. package/build.md +246 -0
  5. package/dist/core/contract.d.ts +4 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/schemaapi.cjs.js +13 -0
  8. package/dist/schemaapi.cjs.js.map +1 -0
  9. package/dist/schemaapi.esm.js +11 -0
  10. package/dist/schemaapi.esm.js.map +1 -0
  11. package/dist/schemaapi.umd.js +19 -0
  12. package/dist/schemaapi.umd.js.map +1 -0
  13. package/docs/adapters/deno.md +51 -0
  14. package/docs/adapters/express.md +67 -0
  15. package/docs/adapters/fastify.md +64 -0
  16. package/docs/adapters/hapi.md +67 -0
  17. package/docs/adapters/koa.md +61 -0
  18. package/docs/adapters/nest.md +66 -0
  19. package/docs/adapters/next.md +66 -0
  20. package/docs/adapters/remix.md +72 -0
  21. package/docs/cli.md +18 -0
  22. package/docs/consepts.md +18 -0
  23. package/docs/getting_started.md +149 -0
  24. package/docs/sdk.md +25 -0
  25. package/docs/validation.md +228 -0
  26. package/docs/versioning.md +28 -0
  27. package/eslint.config.mjs +34 -0
  28. package/estructure.md +55 -0
  29. package/libreria.md +319 -0
  30. package/package.json +61 -0
  31. package/readme.md +89 -0
  32. package/resumen.md +188 -0
  33. package/rollup.config.js +19 -0
  34. package/src/adapters/deno.ts +139 -0
  35. package/src/adapters/express.ts +134 -0
  36. package/src/adapters/fastify.ts +133 -0
  37. package/src/adapters/hapi.ts +140 -0
  38. package/src/adapters/index.ts +9 -0
  39. package/src/adapters/koa.ts +128 -0
  40. package/src/adapters/nest.ts +122 -0
  41. package/src/adapters/next.ts +175 -0
  42. package/src/adapters/remix.ts +145 -0
  43. package/src/adapters/ws.ts +132 -0
  44. package/src/core/client.ts +104 -0
  45. package/src/core/contract.ts +534 -0
  46. package/src/core/versioning.test.ts +174 -0
  47. package/src/docs.ts +535 -0
  48. package/src/index.ts +5 -0
  49. package/src/playground.test.ts +98 -0
  50. package/src/playground.ts +13 -0
  51. package/src/sdk.ts +17 -0
  52. package/tests/adapters.deno.test.ts +70 -0
  53. package/tests/adapters.express.test.ts +67 -0
  54. package/tests/adapters.fastify.test.ts +63 -0
  55. package/tests/adapters.hapi.test.ts +66 -0
  56. package/tests/adapters.koa.test.ts +58 -0
  57. package/tests/adapters.nest.test.ts +85 -0
  58. package/tests/adapters.next.test.ts +39 -0
  59. package/tests/adapters.remix.test.ts +52 -0
  60. package/tests/adapters.ws.test.ts +91 -0
  61. package/tests/cli.test.ts +156 -0
  62. package/tests/client.test.ts +110 -0
  63. package/tests/contract.handle.test.ts +267 -0
  64. package/tests/docs.test.ts +96 -0
  65. package/tests/sdk.test.ts +34 -0
  66. package/tsconfig.json +15 -0
@@ -0,0 +1,145 @@
1
+ import type { createContract } from "../core/contract";
2
+ import { buildErrorPayload } from "../core/contract";
3
+
4
+ type AnyContract = ReturnType<typeof createContract>;
5
+
6
+ type RemixParams = Record<string, string | undefined>;
7
+
8
+ type MethodSchemaLike = {
9
+ media?: {
10
+ kind?: string;
11
+ contentTypes?: string[];
12
+ maxSize?: number;
13
+ };
14
+ };
15
+
16
+ type DownloadResult =
17
+ | {
18
+ data: unknown;
19
+ contentType?: string;
20
+ filename?: string;
21
+ }
22
+ | unknown;
23
+
24
+ export function createRouteHandlers(
25
+ contract: AnyContract,
26
+ handlers: Record<
27
+ string,
28
+ (ctx: Record<string, unknown>) => unknown | Promise<unknown>
29
+ >,
30
+ routePattern: string
31
+ ) {
32
+ const schema = contract.schema as Record<string, Record<string, unknown>>;
33
+ const methods = schema[routePattern] as
34
+ | Record<string, MethodSchemaLike>
35
+ | undefined;
36
+
37
+ if (!methods) {
38
+ const notFound = () => {
39
+ throw new Response("Not Found in Contract", { status: 404 });
40
+ };
41
+ return { loader: notFound, action: notFound };
42
+ }
43
+
44
+ const handleRequest = async (request: Request, params: RemixParams) => {
45
+ const method = request.method;
46
+ const endpoint = `${method} ${routePattern}`;
47
+ const implementation = handlers[endpoint];
48
+ const methodSchema = methods && methods[method];
49
+
50
+ if (!implementation) {
51
+ throw new Response("Method Not Implemented", { status: 501 });
52
+ }
53
+
54
+ const wrapped = contract.handle(endpoint, implementation);
55
+
56
+ try {
57
+ let body = undefined;
58
+ // Parse body only for non-GET/HEAD methods
59
+ if (method !== "GET" && method !== "HEAD") {
60
+ try {
61
+ body = await request.json();
62
+ } catch {
63
+ // Si falla json, tal vez es FormData? Remix usa mucho FormData.
64
+ // Por simplicidad, aquí asumimos JSON como el estándar de SchemaApi.
65
+ // Si se requiere FormData, se debería procesar antes o aquí.
66
+ }
67
+ }
68
+
69
+ const url = new URL(request.url);
70
+ const context: Record<string, unknown> = {
71
+ params: params || {},
72
+ query: Object.fromEntries(url.searchParams.entries()),
73
+ body,
74
+ headers: Object.fromEntries(request.headers.entries()),
75
+ };
76
+
77
+ const result = await wrapped(context);
78
+ const media = methodSchema?.media;
79
+ if (media && media.kind === "download") {
80
+ const download = result as DownloadResult;
81
+ const hasData =
82
+ download &&
83
+ typeof download === "object" &&
84
+ Object.prototype.hasOwnProperty.call(download, "data");
85
+ const data = hasData
86
+ ? (download as { data: unknown }).data
87
+ : download;
88
+ const contentType =
89
+ download &&
90
+ typeof download === "object" &&
91
+ "contentType" in download &&
92
+ typeof (download as { contentType?: unknown }).contentType ===
93
+ "string"
94
+ ? (download as { contentType?: string }).contentType
95
+ : "application/octet-stream";
96
+ const filename =
97
+ download &&
98
+ typeof download === "object" &&
99
+ "filename" in download &&
100
+ typeof (download as { filename?: unknown }).filename === "string"
101
+ ? (download as { filename?: string }).filename
102
+ : undefined;
103
+
104
+ const headers: Record<string, string> = {
105
+ "Content-Type": String(contentType),
106
+ };
107
+ if (filename) {
108
+ headers["Content-Disposition"] = `attachment; filename="${filename}"`;
109
+ }
110
+
111
+ return new Response(data as BodyInit, {
112
+ headers,
113
+ });
114
+ }
115
+
116
+ return new Response(JSON.stringify(result), {
117
+ headers: { "Content-Type": "application/json" },
118
+ });
119
+ } catch (err) {
120
+ const payload = buildErrorPayload(err);
121
+ return new Response(JSON.stringify(payload), {
122
+ status: payload.status,
123
+ headers: { "Content-Type": "application/json" },
124
+ });
125
+ }
126
+ };
127
+
128
+ const loader = async (args: { request: Request; params: RemixParams }) => {
129
+ if (methods["GET"]) {
130
+ return handleRequest(args.request, args.params);
131
+ }
132
+ // Si no hay GET definido pero se llama al loader
133
+ throw new Response("Method Not Allowed", { status: 405 });
134
+ };
135
+
136
+ const action = async (args: { request: Request; params: RemixParams }) => {
137
+ const method = args.request.method;
138
+ if (methods[method]) {
139
+ return handleRequest(args.request, args.params);
140
+ }
141
+ throw new Response("Method Not Allowed", { status: 405 });
142
+ };
143
+
144
+ return { loader, action };
145
+ }
@@ -0,0 +1,132 @@
1
+ import { WebSocketServer, WebSocket } from "ws";
2
+ import type { IncomingMessage } from "http";
3
+ import type { createContract } from "../core/contract";
4
+
5
+ type AnyContract = ReturnType<typeof createContract>;
6
+
7
+ type WsSchemaDefinition = {
8
+ params?: unknown;
9
+ query?: unknown;
10
+ serverMessages?: {
11
+ parse: (value: unknown) => unknown;
12
+ };
13
+ clientMessages?: {
14
+ parse: (value: unknown) => unknown;
15
+ };
16
+ };
17
+
18
+ type WsHandlerContext = {
19
+ params: Record<string, string>;
20
+ query: Record<string, string>;
21
+ headers: IncomingMessage["headers"];
22
+ ws: WebSocket;
23
+ send: (data: unknown) => void;
24
+ onMessage: (cb: (data: unknown) => void) => void;
25
+ };
26
+
27
+ export function handleContract(
28
+ wss: WebSocketServer,
29
+ contract: AnyContract,
30
+ handlers: Record<string, (ctx: WsHandlerContext) => void | Promise<void>>
31
+ ) {
32
+ const schema = contract.schema as Record<string, Record<string, unknown>>;
33
+
34
+ wss.on("connection", async (ws: WebSocket, req: IncomingMessage) => {
35
+ const url = req.url ? new URL(req.url, "http://localhost") : null;
36
+ if (!url) {
37
+ ws.close(1002, "Invalid URL");
38
+ return;
39
+ }
40
+
41
+ const path = url.pathname;
42
+ let matched = false;
43
+
44
+ for (const routePattern of Object.keys(schema)) {
45
+ const methods = schema[routePattern] as Record<string, unknown>;
46
+ const wsDef = methods["WS"] as WsSchemaDefinition | undefined;
47
+ if (!wsDef) continue;
48
+
49
+ // Match path
50
+ const regexPattern = routePattern.replace(/:[a-zA-Z0-9_]+/g, "([^/]+)");
51
+ const regex = new RegExp(`^${regexPattern}$`);
52
+ const match = path.match(regex);
53
+
54
+ if (match) {
55
+ matched = true;
56
+ const implementation = handlers[`WS ${routePattern}`];
57
+ if (!implementation) {
58
+ ws.close(1011, "Handler not found");
59
+ return;
60
+ }
61
+
62
+ // Extract params
63
+ const paramNames = (routePattern.match(/:[a-zA-Z0-9_]+/g) || []).map(
64
+ (p) => p.substring(1)
65
+ );
66
+ const params: Record<string, string> = {};
67
+ paramNames.forEach((name, index) => {
68
+ params[name] = match[index + 1];
69
+ });
70
+
71
+ const query = Object.fromEntries(url.searchParams.entries());
72
+
73
+ const context: WsHandlerContext = {
74
+ params,
75
+ query,
76
+ headers: req.headers,
77
+ ws,
78
+ send: (data: unknown) => {
79
+ if (wsDef.serverMessages) {
80
+ try {
81
+ wsDef.serverMessages.parse(data);
82
+ } catch (e) {
83
+ console.error("Server message validation failed", e);
84
+ }
85
+ }
86
+ ws.send(JSON.stringify(data));
87
+ },
88
+ onMessage: (cb: (data: unknown) => void) => {
89
+ ws.on("message", (raw) => {
90
+ try {
91
+ const json = JSON.parse(raw.toString());
92
+ if (wsDef.clientMessages) {
93
+ try {
94
+ const parsed = wsDef.clientMessages.parse(json);
95
+ cb(parsed);
96
+ } catch (e) {
97
+ console.error("Client message validation failed", e);
98
+ ws.send(
99
+ JSON.stringify({
100
+ error: "Invalid Message Schema",
101
+ details: e,
102
+ })
103
+ );
104
+ }
105
+ } else {
106
+ cb(json);
107
+ }
108
+ } catch (e) {
109
+ console.error("Invalid JSON", e);
110
+ }
111
+ });
112
+ },
113
+ };
114
+
115
+ try {
116
+ await implementation(context);
117
+ } catch (e) {
118
+ console.error("Error in WS handler", e);
119
+ ws.close(1011, "Internal Error");
120
+ }
121
+
122
+ break;
123
+ }
124
+ }
125
+
126
+ if (!matched) {
127
+ ws.close(4404, "Not Found");
128
+ }
129
+ });
130
+
131
+ return wss;
132
+ }
@@ -0,0 +1,104 @@
1
+ // Definimos tipos básicos para simular la estructura del cliente
2
+ // En una implementación real, estos tipos se inferirían recursivamente del esquema T
3
+
4
+ type WebSocketLike = {
5
+ close: (code?: number, reason?: string) => void;
6
+ };
7
+
8
+ type WebSocketConstructor = new (url: string) => WebSocketLike;
9
+
10
+ type ClientConfig = {
11
+ baseUrl: string;
12
+ fetch?: typeof fetch;
13
+ WebSocket?: WebSocketConstructor;
14
+ };
15
+
16
+ export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
17
+
18
+ export type ExtractSchema<TContract> = TContract;
19
+
20
+ export type InferResponse<TContract, Endpoint extends string> = unknown;
21
+
22
+ type RequestParams = {
23
+ id?: string;
24
+ body?: unknown;
25
+ headers?: Record<string, string>;
26
+ [key: string]: unknown;
27
+ };
28
+
29
+ export function createClient<T>(contract: { schema: T }, config: ClientConfig) {
30
+ const { baseUrl, fetch: customFetch } = config;
31
+ const fetchFn = customFetch || globalThis.fetch;
32
+
33
+ return new Proxy({} as Record<string, unknown>, {
34
+ get(_target, resource: string) {
35
+ return new Proxy({} as Record<string, unknown>, {
36
+ get(_target, operation: string) {
37
+ // operation podría ser 'get', 'post', 'put', 'delete', 'ws', etc.
38
+ // resource sería 'users', 'posts', etc.
39
+
40
+ return async (params: RequestParams = {}) => {
41
+ const method = operation.toUpperCase();
42
+ let path = `/${resource}`;
43
+
44
+ // Si hay params.id, asumimos que va en la URL
45
+ if (params.id) {
46
+ path += `/${params.id}`;
47
+ }
48
+
49
+ // Manejo de WebSocket
50
+ if (method === "WS") {
51
+ const wsUrlStr = baseUrl.replace(/^http/, "ws");
52
+ const url = new URL(`${wsUrlStr}${path}`);
53
+
54
+ // Agregar query params
55
+ Object.keys(params).forEach(key => {
56
+ if (key !== 'id' && key !== 'headers' && key !== 'body') {
57
+ url.searchParams.append(key, String(params[key]));
58
+ }
59
+ });
60
+
61
+ type GlobalWithWebSocket = typeof globalThis & {
62
+ WebSocket?: WebSocketConstructor;
63
+ };
64
+ const globalWithWebSocket = globalThis as GlobalWithWebSocket;
65
+ const WebSocketCtor =
66
+ config.WebSocket || globalWithWebSocket.WebSocket;
67
+ if (!WebSocketCtor) {
68
+ throw new Error("WebSocket implementation not found. Please provide it in config.");
69
+ }
70
+
71
+ return new WebSocketCtor(url.toString());
72
+ }
73
+
74
+ const url = new URL(`${baseUrl}${path}`);
75
+
76
+ // Agregar query params si existen (excluyendo id y body)
77
+ Object.keys(params).forEach(key => {
78
+ if (key !== 'id' && key !== 'body' && key !== 'headers') {
79
+ url.searchParams.append(key, String(params[key]));
80
+ }
81
+ });
82
+
83
+ const headers = {
84
+ "Content-Type": "application/json",
85
+ ...(params.headers || {})
86
+ };
87
+
88
+ const response = await fetchFn(url.toString(), {
89
+ method,
90
+ headers,
91
+ body: params.body ? JSON.stringify(params.body) : undefined,
92
+ });
93
+
94
+ if (!response.ok) {
95
+ throw new Error(`Request failed: ${response.status} ${response.statusText}`);
96
+ }
97
+
98
+ return response.json();
99
+ };
100
+ }
101
+ });
102
+ }
103
+ });
104
+ }