typed-bridge 2.1.3 → 2.1.5

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.
@@ -91,6 +91,7 @@ const createBridge = (bridge, port, path = '/bridge') => {
91
91
  if (shuttingDown)
92
92
  return;
93
93
  shuttingDown = true;
94
+ // Server.close waits for all active connections to be completed & stops accepting new connections
94
95
  server.close(() => {
95
96
  (0, helpers_1.printStopLogs)();
96
97
  shutdownCallback();
@@ -78,9 +78,9 @@ const resolveZodTypesTransformer = context => {
78
78
  if (typescript_1.default.isTypeReferenceNode(node) &&
79
79
  typescript_1.default.isQualifiedName(node.typeName) &&
80
80
  typescript_1.default.isIdentifier(node.typeName.left) &&
81
- (node.typeName.left.text === 'z' ||
82
- node.typeName.left.text === 'zod' ||
83
- node.typeName.left.text.startsWith('zod_'))) {
81
+ (/^z(\$\d+)?$/.test(node.typeName.left.text) ||
82
+ /^zod(\$\d+)?$/.test(node.typeName.left.text) ||
83
+ /^zod_/.test(node.typeName.left.text))) {
84
84
  return { name: node.typeName.right.text, typeArgs: node.typeArguments };
85
85
  }
86
86
  return null;
@@ -124,6 +124,9 @@ const resolveZodTypesTransformer = context => {
124
124
  return resolveZodType(typeArgs[0]);
125
125
  if (name === 'ZodLiteral' && typeArgs && typeArgs.length >= 1)
126
126
  return typeArgs[0];
127
+ // v4: ZodRecord<ZodString, ZodNumber> -> Record<string, number>
128
+ if (name === 'ZodRecord' && typeArgs && typeArgs.length >= 2)
129
+ return typescript_1.default.factory.createTypeReferenceNode('Record', [resolveZodType(typeArgs[0]), resolveZodType(typeArgs[1])]);
127
130
  // v4: ZodEnum<{pending: "pending", confirmed: "confirmed", ...}>
128
131
  if (name === 'ZodEnum' && typeArgs && typeArgs.length >= 1 && typescript_1.default.isTypeLiteralNode(typeArgs[0])) {
129
132
  const types = [];
@@ -133,6 +136,9 @@ const resolveZodTypesTransformer = context => {
133
136
  }
134
137
  if (types.length > 0)
135
138
  return typescript_1.default.factory.createUnionTypeNode(types);
139
+ // ZodEnum<{[x: string]: string}> -> string (dynamic enum with index signature)
140
+ if (typeArgs[0].members.some(m => typescript_1.default.isIndexSignatureDeclaration(m)))
141
+ return typescript_1.default.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.StringKeyword);
136
142
  }
137
143
  // v4: ZodDiscriminatedUnion<[ZodObject<...>, ...], "disc">
138
144
  if (name === 'ZodDiscriminatedUnion' && typeArgs && typeArgs.length >= 1) {
@@ -173,7 +179,7 @@ const resolveZodTypesTransformer = context => {
173
179
  for (const stmt of sourceFile.statements) {
174
180
  if (typescript_1.default.isImportDeclaration(stmt) &&
175
181
  typescript_1.default.isStringLiteral(stmt.moduleSpecifier) &&
176
- (stmt.moduleSpecifier.text === 'zod' || stmt.moduleSpecifier.text.startsWith('zod/'))) {
182
+ (stmt.moduleSpecifier.text === 'zod' || stmt.moduleSpecifier.text.startsWith('zod/') || stmt.moduleSpecifier.text === 'typed-bridge')) {
177
183
  continue;
178
184
  }
179
185
  const transformed = typescript_1.default.visitEachChild(stmt, typeVisitor, context);
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.1.3",
4
+ "version": "2.1.5",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "neilveil",
@@ -14,7 +14,7 @@
14
14
  "test:cli": "tsx src/scripts/cli.ts gen-typed-bridge-client --src ./src/demo/bridge/index.ts --dest ./test/bridge.ts",
15
15
  "test:bridge": "tsx test/index.ts",
16
16
  "lint": "eslint",
17
- "docs": "cat readme.md docs/* > docs.md"
17
+ "prepublishOnly": "npm run dist"
18
18
  },
19
19
  "bin": {
20
20
  "typed-bridge": "./dist/scripts/cli.js"
@@ -1,56 +0,0 @@
1
- ## Automatically sync bridge file from back-end to front-end
2
-
3
- ### Step 1: Host the bridge file
4
-
5
- ```ts
6
- // Back-end
7
-
8
- const app = createBridge(bridge, 8080, '/')
9
-
10
- app.use(express.static(path.join(__dirname, 'public')))
11
- ```
12
-
13
- ### Step 2: Configure generate command to build in public directory
14
-
15
- ```json
16
- {
17
- "scripts": {
18
- "gen-typed-bridge": "typed-bridge gen-typed-bridge --src ./src/bridge/index.ts --dest ./public/bridge.ts"
19
- }
20
- }
21
- ```
22
-
23
- ### Step 3: Setup typed-bridge types file clone script
24
-
25
- [Clone Kit](https://www.npmjs.com/package/clone-kit)
26
-
27
- ```bash
28
- npm install clone-kit
29
- ```
30
-
31
- Add clone-kit configuration file in front-end project `clone-kit.json`
32
-
33
- ```json
34
- {
35
- "files": [
36
- {
37
- "name": "Typed Bridge",
38
- "src": "https://www.my-server.com/bridge.ts",
39
- "dst": "src/bridge.ts"
40
- }
41
- ]
42
- }
43
- ```
44
-
45
- ### Step 4: Setup clone script to run before project start/build
46
-
47
- ```json
48
- {
49
- "scripts": {
50
- "start": "next start && clone-kit ./clone-kit.json",
51
- "build": "next build && clone-kit ./clone-kit.json"
52
- }
53
- }
54
- ```
55
-
56
- > It's completely safe to share Typed Bridge types file publicly as it only contains request & response types.