@northsea4/proxy-service 1.0.1 → 1.0.2
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/README.md +4 -16
- package/lib/index.d.ts +2 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
# @northsea4/proxy-service
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
基于 [@webext-core/proxy-service](https://github.com/aklinker1/webext-core/tree/main/packages/proxy-service) 的二次开发版本,支持 ServiceCallContext 上下文对象,提供更丰富的调用上下文信息。
|
|
5
4
|
|
|
6
5
|
类型安全的浏览器扩展消息传递 API 封装,允许你在任何地方调用函数,但在后台脚本中执行。支持所有主流浏览器(Chrome、Firefox、Safari 等)。
|
|
7
6
|
|
|
8
|
-
|
|
9
7
|
## 主要特性
|
|
10
8
|
|
|
11
9
|
- 支持 ServiceCallContext:服务方法可获取完整的调用上下文(sender、requestId、timestamp、metadata 等)
|
|
@@ -20,7 +18,6 @@ pnpm add @northsea4/proxy-service
|
|
|
20
18
|
|
|
21
19
|
## 快速开始
|
|
22
20
|
|
|
23
|
-
|
|
24
21
|
### 1. 定义服务
|
|
25
22
|
|
|
26
23
|
```ts
|
|
@@ -34,7 +31,7 @@ class TabService {
|
|
|
34
31
|
url: context?.sender?.tab?.url,
|
|
35
32
|
title: context?.sender?.tab?.title,
|
|
36
33
|
requestId: context?.requestId,
|
|
37
|
-
timestamp: context?.timestamp
|
|
34
|
+
timestamp: context?.timestamp
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
37
|
|
|
@@ -50,7 +47,6 @@ export const [registerTabService, getTabService] = defineProxyService(
|
|
|
50
47
|
)
|
|
51
48
|
```
|
|
52
49
|
|
|
53
|
-
|
|
54
50
|
### 2. 注册服务(Background)
|
|
55
51
|
|
|
56
52
|
```ts
|
|
@@ -75,7 +71,6 @@ console.log('Current tab:', tabInfo)
|
|
|
75
71
|
await tabService.closeTab(tabInfo.tabId)
|
|
76
72
|
```
|
|
77
73
|
|
|
78
|
-
|
|
79
74
|
## ServiceCallContext 类型
|
|
80
75
|
|
|
81
76
|
```ts
|
|
@@ -91,7 +86,6 @@ interface ServiceCallContext {
|
|
|
91
86
|
|
|
92
87
|
服务方法的最后一个参数应为 `context?: ServiceCallContext`,并通过 `context?.sender` 获取调用方信息。
|
|
93
88
|
|
|
94
|
-
|
|
95
89
|
## 常见场景示例
|
|
96
90
|
|
|
97
91
|
### 1. 获取调用者信息
|
|
@@ -104,7 +98,7 @@ class ContextService {
|
|
|
104
98
|
fromUrl: context?.sender?.tab?.url,
|
|
105
99
|
fromFrame: context?.sender?.frameId,
|
|
106
100
|
requestId: context?.requestId,
|
|
107
|
-
timestamp: context?.timestamp
|
|
101
|
+
timestamp: context?.timestamp
|
|
108
102
|
}
|
|
109
103
|
}
|
|
110
104
|
}
|
|
@@ -140,7 +134,6 @@ class LoggingService {
|
|
|
140
134
|
}
|
|
141
135
|
```
|
|
142
136
|
|
|
143
|
-
|
|
144
137
|
## TypeScript 类型支持
|
|
145
138
|
|
|
146
139
|
服务方法签名应为:
|
|
@@ -156,7 +149,6 @@ interface MyService {
|
|
|
156
149
|
|
|
157
150
|
## 高级功能
|
|
158
151
|
|
|
159
|
-
|
|
160
152
|
### flattenPromise
|
|
161
153
|
|
|
162
154
|
简化异步依赖的处理:
|
|
@@ -170,12 +162,11 @@ function createService(dbPromise: Promise<IDBDatabase>) {
|
|
|
170
162
|
return {
|
|
171
163
|
async getData(context?: ServiceCallContext) {
|
|
172
164
|
return await db.get('store', 'key')
|
|
173
|
-
}
|
|
165
|
+
}
|
|
174
166
|
}
|
|
175
167
|
}
|
|
176
168
|
```
|
|
177
169
|
|
|
178
|
-
|
|
179
170
|
### defineProxyService
|
|
180
171
|
|
|
181
172
|
简化服务定义和类型推导:
|
|
@@ -203,7 +194,6 @@ const myService = getMyService()
|
|
|
203
194
|
await myService.add(1, 2)
|
|
204
195
|
```
|
|
205
196
|
|
|
206
|
-
|
|
207
197
|
## 配置选项
|
|
208
198
|
|
|
209
199
|
支持 `@webext-core/messaging` 的所有配置项:
|
|
@@ -212,14 +202,13 @@ await myService.add(1, 2)
|
|
|
212
202
|
import { registerService, createProxyService } from '@northsea4/proxy-service'
|
|
213
203
|
|
|
214
204
|
const config = {
|
|
215
|
-
logger: console
|
|
205
|
+
logger: console // 自定义日志记录器
|
|
216
206
|
}
|
|
217
207
|
|
|
218
208
|
registerService('my-service', myService, config)
|
|
219
209
|
const proxy = createProxyService('my-service', config)
|
|
220
210
|
```
|
|
221
211
|
|
|
222
|
-
|
|
223
212
|
## 开发
|
|
224
213
|
|
|
225
214
|
```bash
|
|
@@ -234,7 +223,6 @@ pnpm dev
|
|
|
234
223
|
|
|
235
224
|
MIT
|
|
236
225
|
|
|
237
|
-
|
|
238
226
|
## 致谢
|
|
239
227
|
|
|
240
228
|
本项目基于 [@webext-core/proxy-service](https://github.com/aklinker1/webext-core/tree/main/packages/proxy-service) 开发,感谢原作者的优秀工作。
|
package/lib/index.d.ts
CHANGED
|
@@ -31,8 +31,7 @@ declare function createProxyService<T extends Service>(key: ProxyServiceKey<T> |
|
|
|
31
31
|
*/
|
|
32
32
|
declare function registerService<T extends Service, K extends string = ProxyServiceKey<T> | string>(key: K, realService: T, config?: ExtensionMessagingConfig): RemoveListenerCallback;
|
|
33
33
|
declare function isProxyService<T>(obj: unknown): obj is ProxyService<T>;
|
|
34
|
-
|
|
35
|
-
}
|
|
34
|
+
type ProxyServiceConstraint<_> = Record<string, unknown>;
|
|
36
35
|
/**
|
|
37
36
|
* Used to constrain a service's type between calls to `createProxyService` and
|
|
38
37
|
* `registerService`.
|
|
@@ -109,7 +108,6 @@ declare function defineProxyService<TService extends Service, TArgs extends any[
|
|
|
109
108
|
* Configure a proxy service's behavior. It uses `@webext-core/messaging` internally, so any
|
|
110
109
|
* config from `ExtensionMessagingConfig` can be passed as well.
|
|
111
110
|
*/
|
|
112
|
-
|
|
113
|
-
}
|
|
111
|
+
type ProxyServiceConfig = ExtensionMessagingConfig;
|
|
114
112
|
|
|
115
113
|
export { DeepAsync, type ProxyService, type ProxyServiceConfig, type ProxyServiceKey, Service, createProxyService, defineProxyService, flattenPromise, isProxyService, registerService };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@northsea4/proxy-service",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A type-safe wrapper around the web extension messaging APIs that lets you call a function from anywhere, but execute it in the background. Forked from @webext-core/proxy-service with sender parameter support.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|