typed-bridge 2.0.7 → 2.0.9
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/context.md +11 -11
- package/dist/demo/bridge/user/index.d.ts +3 -3
- package/dist/demo/bridge/user/types.d.ts +11 -11
- package/dist/demo/bridge/user/types.js +8 -8
- package/dist/helpers/index.d.ts +1 -1
- package/dist/helpers/index.js +5 -38
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -3
- package/dist/scripts/typedBridgeCleaner.js +19 -49
- package/package.json +4 -4
- package/readme.md +11 -11
package/context.md
CHANGED
|
@@ -102,32 +102,32 @@ createMiddleware('user.*', async (req, res) => {
|
|
|
102
102
|
|
|
103
103
|
---
|
|
104
104
|
|
|
105
|
-
## **How to Validate
|
|
105
|
+
## **How to Validate Zod**
|
|
106
106
|
|
|
107
107
|
```ts
|
|
108
108
|
// src/bridge/user/types.ts
|
|
109
|
-
import {
|
|
109
|
+
import { z } from 'zod'
|
|
110
110
|
|
|
111
111
|
export const fetch = {
|
|
112
|
-
args:
|
|
113
|
-
res:
|
|
114
|
-
id:
|
|
115
|
-
name:
|
|
116
|
-
email:
|
|
117
|
-
createdAt:
|
|
112
|
+
args: z.object({ id: z.number().min(1) }),
|
|
113
|
+
res: z.object({
|
|
114
|
+
id: z.number(),
|
|
115
|
+
name: z.string(),
|
|
116
|
+
email: z.string().email(),
|
|
117
|
+
createdAt: z.date()
|
|
118
118
|
})
|
|
119
119
|
}
|
|
120
120
|
```
|
|
121
121
|
|
|
122
122
|
```ts
|
|
123
123
|
// src/bridge/user/index.ts (validated version)
|
|
124
|
-
import {
|
|
124
|
+
import { z } from 'zod'
|
|
125
125
|
import * as types from './types.js'
|
|
126
126
|
|
|
127
127
|
export const fetch = async (
|
|
128
|
-
args:
|
|
128
|
+
args: z.infer<typeof types.fetch.args>,
|
|
129
129
|
context: { userId?: number }
|
|
130
|
-
): Promise
|
|
130
|
+
): Promise<z.infer<typeof types.fetch.res>> => {
|
|
131
131
|
args = types.fetch.args.parse(args)
|
|
132
132
|
const user = users.find(u => u.id === args.id)
|
|
133
133
|
if (!user) throw new Error(`User ${args.id} not found`)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { z } from 'zod';
|
|
2
2
|
import * as types from './types';
|
|
3
3
|
export interface User {
|
|
4
4
|
id: number;
|
|
@@ -6,9 +6,9 @@ export interface User {
|
|
|
6
6
|
email: string;
|
|
7
7
|
createdAt: Date;
|
|
8
8
|
}
|
|
9
|
-
export declare const fetch: (args:
|
|
9
|
+
export declare const fetch: (args: z.infer<typeof types.fetch.args>, context: {
|
|
10
10
|
id: number;
|
|
11
|
-
}) => Promise
|
|
11
|
+
}) => Promise<z.infer<typeof types.fetch.res>>;
|
|
12
12
|
export declare const update: (args: {
|
|
13
13
|
id: number;
|
|
14
14
|
name?: string;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { z } from 'zod';
|
|
2
2
|
export declare const fetch: {
|
|
3
|
-
args:
|
|
4
|
-
id:
|
|
5
|
-
}, "strip",
|
|
3
|
+
args: z.ZodObject<{
|
|
4
|
+
id: z.ZodNumber;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
6
|
id: number;
|
|
7
7
|
}, {
|
|
8
8
|
id: number;
|
|
9
9
|
}>;
|
|
10
|
-
res:
|
|
11
|
-
id:
|
|
12
|
-
name:
|
|
13
|
-
}, "strip",
|
|
10
|
+
res: z.ZodObject<{
|
|
11
|
+
id: z.ZodNumber;
|
|
12
|
+
name: z.ZodString;
|
|
13
|
+
}, "strip", z.ZodTypeAny, {
|
|
14
14
|
id: number;
|
|
15
15
|
name: string;
|
|
16
16
|
}, {
|
|
@@ -18,9 +18,9 @@ export declare const fetch: {
|
|
|
18
18
|
name: string;
|
|
19
19
|
}>;
|
|
20
20
|
};
|
|
21
|
-
export declare const userContext:
|
|
22
|
-
id:
|
|
23
|
-
}, "strip",
|
|
21
|
+
export declare const userContext: z.ZodObject<{
|
|
22
|
+
id: z.ZodNumber;
|
|
23
|
+
}, "strip", z.ZodTypeAny, {
|
|
24
24
|
id: number;
|
|
25
25
|
}, {
|
|
26
26
|
id: number;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.userContext = exports.fetch = void 0;
|
|
4
|
-
const
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
5
|
exports.fetch = {
|
|
6
|
-
args:
|
|
7
|
-
id:
|
|
6
|
+
args: zod_1.z.object({
|
|
7
|
+
id: zod_1.z.number().min(1)
|
|
8
8
|
}),
|
|
9
|
-
res:
|
|
10
|
-
id:
|
|
11
|
-
name:
|
|
9
|
+
res: zod_1.z.object({
|
|
10
|
+
id: zod_1.z.number(),
|
|
11
|
+
name: zod_1.z.string()
|
|
12
12
|
})
|
|
13
13
|
};
|
|
14
|
-
exports.userContext =
|
|
15
|
-
id:
|
|
14
|
+
exports.userContext = zod_1.z.object({
|
|
15
|
+
id: zod_1.z.number()
|
|
16
16
|
});
|
package/dist/helpers/index.d.ts
CHANGED
package/dist/helpers/index.js
CHANGED
|
@@ -1,46 +1,13 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
4
|
};
|
|
38
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
6
|
exports.matchesPattern = exports.printStopLogs = exports.printStartLogs = void 0;
|
|
40
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
const interfaces =
|
|
8
|
+
const os_1 = __importDefault(require("os"));
|
|
9
|
+
const getLocalIPList = () => {
|
|
10
|
+
const interfaces = os_1.default.networkInterfaces();
|
|
44
11
|
const ipList = [];
|
|
45
12
|
Object.values(interfaces).forEach(ifaceArr => {
|
|
46
13
|
ifaceArr?.forEach(iface => {
|
|
@@ -57,8 +24,8 @@ const getLocalIPList = async () => {
|
|
|
57
24
|
return ipList;
|
|
58
25
|
};
|
|
59
26
|
const seperator = '\n-x-x-x-x-x-\n';
|
|
60
|
-
const printStartLogs =
|
|
61
|
-
const ipList =
|
|
27
|
+
const printStartLogs = (port) => {
|
|
28
|
+
const ipList = getLocalIPList();
|
|
62
29
|
console.log(ipList);
|
|
63
30
|
console.log(seperator);
|
|
64
31
|
console.log(chalk_1.default.bgWhite.black(' Typed Bridge '));
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import 'dotenv/config';
|
|
2
2
|
export { Application, default as express, Express, NextFunction, Request, Response, Router } from 'express';
|
|
3
|
-
export { z as $z } from 'zod';
|
|
4
3
|
export { createBridge, createMiddleware, onShutdown } from './bridge';
|
|
5
4
|
export { config as tbConfig } from './config';
|
package/dist/index.js
CHANGED
|
@@ -3,13 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.tbConfig = exports.onShutdown = exports.createMiddleware = exports.createBridge = exports
|
|
6
|
+
exports.tbConfig = exports.onShutdown = exports.createMiddleware = exports.createBridge = exports.Router = exports.express = void 0;
|
|
7
7
|
require("dotenv/config");
|
|
8
8
|
var express_1 = require("express");
|
|
9
9
|
Object.defineProperty(exports, "express", { enumerable: true, get: function () { return __importDefault(express_1).default; } });
|
|
10
10
|
Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return express_1.Router; } });
|
|
11
|
-
var zod_1 = require("zod");
|
|
12
|
-
Object.defineProperty(exports, "$z", { enumerable: true, get: function () { return zod_1.z; } });
|
|
13
11
|
var bridge_1 = require("./bridge");
|
|
14
12
|
Object.defineProperty(exports, "createBridge", { enumerable: true, get: function () { return bridge_1.createBridge; } });
|
|
15
13
|
Object.defineProperty(exports, "createMiddleware", { enumerable: true, get: function () { return bridge_1.createMiddleware; } });
|
|
@@ -1,42 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
35
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
6
|
exports.default = cleanTsFile;
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
const
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
40
10
|
// Snippet to inject at the end
|
|
41
11
|
const proxySnippet = () => `
|
|
42
12
|
type typedBridgeConfig = {
|
|
@@ -90,12 +60,12 @@ export default typedBridge
|
|
|
90
60
|
const removeSecondParamTransformer = context => {
|
|
91
61
|
return sourceFile => {
|
|
92
62
|
function visitor(node) {
|
|
93
|
-
if (
|
|
94
|
-
return
|
|
63
|
+
if (typescript_1.default.isFunctionTypeNode(node) && node.parameters.length > 1) {
|
|
64
|
+
return typescript_1.default.factory.updateFunctionTypeNode(node, node.typeParameters, typescript_1.default.factory.createNodeArray([node.parameters[0]]), node.type);
|
|
95
65
|
}
|
|
96
|
-
return
|
|
66
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
97
67
|
}
|
|
98
|
-
return
|
|
68
|
+
return typescript_1.default.visitEachChild(sourceFile, visitor, context);
|
|
99
69
|
};
|
|
100
70
|
};
|
|
101
71
|
/**
|
|
@@ -106,7 +76,7 @@ const removeDefaultExportTransformer = context => {
|
|
|
106
76
|
return sourceFile => {
|
|
107
77
|
function visitor(node) {
|
|
108
78
|
// Look for `export { _default as default }` and drop it
|
|
109
|
-
if (
|
|
79
|
+
if (typescript_1.default.isExportDeclaration(node) && node.exportClause && typescript_1.default.isNamedExports(node.exportClause)) {
|
|
110
80
|
const [el] = node.exportClause.elements;
|
|
111
81
|
if (node.exportClause.elements.length === 1 &&
|
|
112
82
|
el.propertyName?.text === '_default' &&
|
|
@@ -114,15 +84,15 @@ const removeDefaultExportTransformer = context => {
|
|
|
114
84
|
return undefined;
|
|
115
85
|
}
|
|
116
86
|
}
|
|
117
|
-
return
|
|
87
|
+
return typescript_1.default.visitEachChild(node, visitor, context);
|
|
118
88
|
}
|
|
119
89
|
const updatedStatements = [];
|
|
120
90
|
for (const stmt of sourceFile.statements) {
|
|
121
|
-
const newStmt =
|
|
91
|
+
const newStmt = typescript_1.default.visitNode(stmt, visitor);
|
|
122
92
|
if (newStmt)
|
|
123
93
|
updatedStatements.push(newStmt);
|
|
124
94
|
}
|
|
125
|
-
return
|
|
95
|
+
return typescript_1.default.factory.updateSourceFile(sourceFile, typescript_1.default.factory.createNodeArray(updatedStatements));
|
|
126
96
|
};
|
|
127
97
|
};
|
|
128
98
|
/**
|
|
@@ -132,20 +102,20 @@ const removeDefaultExportTransformer = context => {
|
|
|
132
102
|
* 3. Writes the final file output.
|
|
133
103
|
*/
|
|
134
104
|
function cleanTsFile(src) {
|
|
135
|
-
let sourceCode =
|
|
105
|
+
let sourceCode = fs_1.default.readFileSync(src, 'utf-8');
|
|
136
106
|
const eslintDisable = `/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any */`;
|
|
137
107
|
// Ensure the top comment is present if missing
|
|
138
108
|
if (!sourceCode.startsWith(eslintDisable)) {
|
|
139
109
|
sourceCode = eslintDisable + '\n' + sourceCode;
|
|
140
110
|
}
|
|
141
111
|
// Parse the source
|
|
142
|
-
const sourceFile =
|
|
112
|
+
const sourceFile = typescript_1.default.createSourceFile(path_1.default.basename(src), sourceCode, typescript_1.default.ScriptTarget.Latest, true, typescript_1.default.ScriptKind.TS);
|
|
143
113
|
// Run the transformers
|
|
144
|
-
const result =
|
|
114
|
+
const result = typescript_1.default.transform(sourceFile, [removeSecondParamTransformer, removeDefaultExportTransformer]);
|
|
145
115
|
// Print final code
|
|
146
|
-
const printer =
|
|
116
|
+
const printer = typescript_1.default.createPrinter();
|
|
147
117
|
const transformedCode = printer.printFile(result.transformed[0]).concat(proxySnippet());
|
|
148
118
|
// Write back to the same file
|
|
149
|
-
|
|
119
|
+
fs_1.default.writeFileSync(src, transformedCode, 'utf-8');
|
|
150
120
|
console.log(`Cleaned file: ${src}`);
|
|
151
121
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typed-bridge",
|
|
3
3
|
"description": "Strictly typed server functions for typescript apps",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.9",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"author": "neilveil",
|
|
@@ -28,8 +28,7 @@
|
|
|
28
28
|
"express": "^5.1.0",
|
|
29
29
|
"rollup": "^4.40.0",
|
|
30
30
|
"rollup-plugin-dts": "^6.2.1",
|
|
31
|
-
"typescript": "^5.8.3"
|
|
32
|
-
"zod": "^3.24.3"
|
|
31
|
+
"typescript": "^5.8.3"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@eslint/js": "^9.25.0",
|
|
@@ -47,7 +46,8 @@
|
|
|
47
46
|
"rimraf": "^6.0.1",
|
|
48
47
|
"ts-node": "^10.9.2",
|
|
49
48
|
"tsconfig-paths": "^4.2.0",
|
|
50
|
-
"typescript-eslint": "^8.30.1"
|
|
49
|
+
"typescript-eslint": "^8.30.1",
|
|
50
|
+
"zod": "^3.24.3"
|
|
51
51
|
},
|
|
52
52
|
"keywords": [
|
|
53
53
|
"api",
|
package/readme.md
CHANGED
|
@@ -230,33 +230,33 @@ createMiddleware('user.*', async (req, res) => {
|
|
|
230
230
|
`types.ts`
|
|
231
231
|
|
|
232
232
|
```ts
|
|
233
|
-
import {
|
|
233
|
+
import { z } from 'zod'
|
|
234
234
|
|
|
235
235
|
export const fetch = {
|
|
236
|
-
args:
|
|
237
|
-
id:
|
|
236
|
+
args: z.object({
|
|
237
|
+
id: z.number().min(1)
|
|
238
238
|
}),
|
|
239
|
-
res:
|
|
240
|
-
id:
|
|
241
|
-
name:
|
|
239
|
+
res: z.object({
|
|
240
|
+
id: z.number(),
|
|
241
|
+
name: z.string()
|
|
242
242
|
})
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
-
export const userContext =
|
|
246
|
-
id:
|
|
245
|
+
export const userContext = z.object({
|
|
246
|
+
id: z.number()
|
|
247
247
|
})
|
|
248
248
|
```
|
|
249
249
|
|
|
250
250
|
#### Use in bridge handler
|
|
251
251
|
|
|
252
252
|
```ts
|
|
253
|
-
import {
|
|
253
|
+
import { z } from 'zod'
|
|
254
254
|
import * as types from './types'
|
|
255
255
|
|
|
256
256
|
export const fetch = async (
|
|
257
|
-
args:
|
|
257
|
+
args: z.infer<typeof types.fetch.args>,
|
|
258
258
|
context: { id: number }
|
|
259
|
-
): Promise
|
|
259
|
+
): Promise<z.infer<typeof types.fetch.res>> => {
|
|
260
260
|
args = types.fetch.args.parse(args)
|
|
261
261
|
|
|
262
262
|
console.log(context)
|