@gongt/trpc-lib 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.
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/lib/bin/index.d.ts +2 -0
- package/lib/bin/index.d.ts.map +1 -0
- package/lib/bin/index.js +2 -0
- package/lib/bin/index.js.map +1 -0
- package/lib/client/index.d.ts +2 -0
- package/lib/client/index.d.ts.map +1 -0
- package/lib/client/index.js +2 -0
- package/lib/client/index.js.map +1 -0
- package/lib/server/index.d.ts +2 -0
- package/lib/server/index.d.ts.map +1 -0
- package/lib/server/index.js +2 -0
- package/lib/server/index.js.map +1 -0
- package/loader/bin.js +7 -0
- package/package.json +44 -0
- package/src/bin/index.ts +0 -0
- package/src/client/index.ts +0 -0
- package/src/server/index.ts +0 -0
- package/src/tsconfig.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 GongT<admin@gongt.me>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# library for [trpc](https://trpc.io/)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
## socket-io link
|
|
5
|
+
|
|
6
|
+
复用socket-io的连接,执行trpc调用
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
// Client side
|
|
10
|
+
import { createTRPCClient } from '@trpc/client';
|
|
11
|
+
import { SocketIoLink } from '@gongt/trpc-lib/client';
|
|
12
|
+
|
|
13
|
+
const client = createTRPCClient({
|
|
14
|
+
links: [
|
|
15
|
+
SocketIoLink({
|
|
16
|
+
url: 'ws://localhost:3000/socket.io/',
|
|
17
|
+
namespace: '/trpc',
|
|
18
|
+
}),
|
|
19
|
+
],
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// Server side
|
|
25
|
+
import { createSocketIoMiddleware } from '@gongt/trpc-lib/server';
|
|
26
|
+
import { initTRPC } from '@trpc/server';
|
|
27
|
+
|
|
28
|
+
const io = new SocketIO.Server({
|
|
29
|
+
path: '/socket.io/',
|
|
30
|
+
addTrailingSlash: true,
|
|
31
|
+
transports: ['websocket', 'polling'],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const trpc = initTRPC.context<XXX>().create({});
|
|
35
|
+
export const appRouter = trpc.router(...);
|
|
36
|
+
|
|
37
|
+
io.of('/trpc').use(
|
|
38
|
+
createSocketIoMiddleware({
|
|
39
|
+
router: appRouter,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## api-extractor
|
|
45
|
+
|
|
46
|
+
调用 [api-extractor](https://api-extractor.com/) 提取trpc的类型定义,生成客户端可用的类型文件,避免客户端依赖服务端。同时生成sourcemap保持调试友好
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
npx @gongt/trpc-lib extract ./lib/router.d.ts -o ../client/src/trpc-server-define.d.ts
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
说明:
|
|
53
|
+
1. 必须编译,不能直接使用ts文件(例如./src/router.ts)
|
|
54
|
+
1. 服务端`router.d.ts`中必须导出有且仅有一个 `BuiltRouter` 类型 (此类型来自 `@trpc/server`)
|
|
55
|
+
1. 输出文件将会`import type xxx from '@trpc/server'`,因此客户端需要安装 `@trpc/server` 依赖,不过这个项目并不依赖 `@types/node` 之类的定义,所以不会污染客户端类型
|
|
56
|
+
1. 如果源文件中存在 `import type xxx from '...'`,那么目标文件中也会存在相同的import语句,因此需要保证客户端也能找到这些类型定义,这些定义最好来自一个独立的包,否则提取操作没有意义
|
|
57
|
+
1. *输出文件实际也可以放入独立协议包中*
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bin/index.ts"],"names":[],"mappings":""}
|
package/lib/bin/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bin/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":""}
|
package/loader/bin.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gongt/trpc-lib",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A tRPC library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"trpc",
|
|
7
|
+
"socket.io",
|
|
8
|
+
"api-extractor"
|
|
9
|
+
],
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"exports": {
|
|
12
|
+
"./server": {
|
|
13
|
+
"default": "./lib/server/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./client": {
|
|
16
|
+
"default": "./lib/client/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"trpc-api-extractor": "./lib/extract.js"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@trpc/server": "^11.5.1",
|
|
25
|
+
"@idlebox/common": "^1.5.10",
|
|
26
|
+
"@idlebox/node": "^1.4.19",
|
|
27
|
+
"@idlebox/source-map-support": "^0.0.13"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^24.0.14",
|
|
31
|
+
"@build-script/codegen": "^1.0.15",
|
|
32
|
+
"@build-script/single-dog-asset": "latest",
|
|
33
|
+
"@mpis/run": "^0.0.16"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"author": "GongT <admin@gongt.me>",
|
|
37
|
+
"repository": "https://github.com/GongT/baobao",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "mpis-run build",
|
|
40
|
+
"watch": "mpis-run watch",
|
|
41
|
+
"clean": "mpis-run clean",
|
|
42
|
+
"lint": "internal-lint"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/bin/index.ts
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|