@webext-core/proxy-service 1.1.1 → 1.2.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/lib/index.cjs +35 -2
- package/lib/index.d.ts +34 -7
- package/lib/index.global.js +33 -1
- package/lib/index.js +33 -1
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -46,7 +46,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
46
46
|
// src/index.ts
|
|
47
47
|
var src_exports = {};
|
|
48
48
|
__export(src_exports, {
|
|
49
|
-
defineProxyService: () => defineProxyService
|
|
49
|
+
defineProxyService: () => defineProxyService,
|
|
50
|
+
flattenPromise: () => flattenPromise
|
|
50
51
|
});
|
|
51
52
|
module.exports = __toCommonJS(src_exports);
|
|
52
53
|
|
|
@@ -126,7 +127,39 @@ function defineProxyService(name, init, config) {
|
|
|
126
127
|
}
|
|
127
128
|
];
|
|
128
129
|
}
|
|
130
|
+
|
|
131
|
+
// src/flattenPromise.ts
|
|
132
|
+
var import_get_value2 = __toESM(require("get-value"), 1);
|
|
133
|
+
function flattenPromise(promise) {
|
|
134
|
+
function createProxy(location2) {
|
|
135
|
+
const wrapped = () => {
|
|
136
|
+
};
|
|
137
|
+
const proxy = new Proxy(wrapped, {
|
|
138
|
+
apply(_target, _thisArg, args) {
|
|
139
|
+
return __async(this, null, function* () {
|
|
140
|
+
const t = yield promise;
|
|
141
|
+
const thisArg = (location2 == null ? void 0 : location2.parentPath) ? (0, import_get_value2.default)(t, location2.parentPath) : t;
|
|
142
|
+
const fn = location2 ? (0, import_get_value2.default)(t, location2.propertyPath) : t;
|
|
143
|
+
return fn.apply(thisArg, args);
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
get(target, propertyName, receiver) {
|
|
147
|
+
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
148
|
+
return Reflect.get(target, propertyName, receiver);
|
|
149
|
+
}
|
|
150
|
+
return createProxy({
|
|
151
|
+
propertyPath: location2 == null ? propertyName : `${location2.propertyPath}.${propertyName}`,
|
|
152
|
+
parentPath: location2 == null ? void 0 : location2.propertyPath
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
proxy.__proxy = true;
|
|
157
|
+
return proxy;
|
|
158
|
+
}
|
|
159
|
+
return createProxy();
|
|
160
|
+
}
|
|
129
161
|
// Annotate the CommonJS export names for ESM import in node:
|
|
130
162
|
0 && (module.exports = {
|
|
131
|
-
defineProxyService
|
|
163
|
+
defineProxyService,
|
|
164
|
+
flattenPromise
|
|
132
165
|
});
|
package/lib/index.d.ts
CHANGED
|
@@ -4,18 +4,45 @@ declare type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
|
4
4
|
declare type Service = ((...args: any[]) => Promise<any>) | {
|
|
5
5
|
[key: string]: any | Service;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* If a service is already fully async, return the original service type. If the service isn't
|
|
9
|
+
* completely async, it should return `DeepAsync<TService>`.
|
|
10
|
+
*/
|
|
11
|
+
declare type ProxyService<TService> = TService extends DeepAsync<TService> ? TService : DeepAsync<TService>;
|
|
7
12
|
/**
|
|
8
13
|
* Make all functions at all nested levels of an object async
|
|
9
14
|
*/
|
|
10
|
-
declare type DeepAsync<TService> = TService extends (...args: any) => any ? ToAsyncFunction<TService> : {
|
|
11
|
-
[
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
+
declare type DeepAsync<TService> = TService extends (...args: any) => any ? ToAsyncFunction<TService> : TService extends {
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
} ? {
|
|
18
|
+
[fn in keyof TService]: DeepAsync<TService[fn]>;
|
|
19
|
+
} : never;
|
|
15
20
|
declare type ToAsyncFunction<T extends (...args: any) => any> = (...args: Parameters<T>) => Proimsify<ReturnType<T>>;
|
|
16
21
|
interface ProxyServiceConfig extends ExtensionMessagingConfig {
|
|
17
22
|
}
|
|
18
23
|
|
|
19
|
-
declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [registerService: (...args: TArgs) => TService, getService: () =>
|
|
24
|
+
declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [registerService: (...args: TArgs) => TService, getService: () => ProxyService<TService>];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Given a promise of a variable, return a proxy to that awaits the promise internally so you don't
|
|
28
|
+
* have to call `await` twice.
|
|
29
|
+
*
|
|
30
|
+
* You can unwrap promises of functions, objects, or classes.
|
|
31
|
+
*
|
|
32
|
+
* This is meant to be used to simplify service implementations, like so:
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* function createService(dependencyPromise: Promise<SomeDependency>) {
|
|
36
|
+
* const dependency = flattenPromise(dependencyPromise);
|
|
37
|
+
*
|
|
38
|
+
* return {
|
|
39
|
+
* doSomething() {
|
|
40
|
+
* await dependency.someAsyncWork();
|
|
41
|
+
* // Instead of `await (await dependencyPromise).someAsyncWork();`
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
*/
|
|
46
|
+
declare function flattenPromise<T>(promise: Promise<T>): DeepAsync<T>;
|
|
20
47
|
|
|
21
|
-
export { ProxyServiceConfig, defineProxyService };
|
|
48
|
+
export { DeepAsync, ProxyService, ProxyServiceConfig, defineProxyService, flattenPromise };
|
package/lib/index.global.js
CHANGED
|
@@ -1143,7 +1143,8 @@ var webExtCoreProxyService = (() => {
|
|
|
1143
1143
|
// src/index.ts
|
|
1144
1144
|
var src_exports = {};
|
|
1145
1145
|
__export(src_exports, {
|
|
1146
|
-
defineProxyService: () => defineProxyService
|
|
1146
|
+
defineProxyService: () => defineProxyService,
|
|
1147
|
+
flattenPromise: () => flattenPromise
|
|
1147
1148
|
});
|
|
1148
1149
|
|
|
1149
1150
|
// src/isBackground.ts
|
|
@@ -1342,6 +1343,37 @@ var webExtCoreProxyService = (() => {
|
|
|
1342
1343
|
}
|
|
1343
1344
|
];
|
|
1344
1345
|
}
|
|
1346
|
+
|
|
1347
|
+
// src/flattenPromise.ts
|
|
1348
|
+
var import_get_value2 = __toESM(require_get_value(), 1);
|
|
1349
|
+
function flattenPromise(promise) {
|
|
1350
|
+
function createProxy(location2) {
|
|
1351
|
+
const wrapped = () => {
|
|
1352
|
+
};
|
|
1353
|
+
const proxy = new Proxy(wrapped, {
|
|
1354
|
+
apply(_target, _thisArg, args) {
|
|
1355
|
+
return __async(this, null, function* () {
|
|
1356
|
+
const t = yield promise;
|
|
1357
|
+
const thisArg = (location2 == null ? void 0 : location2.parentPath) ? (0, import_get_value2.default)(t, location2.parentPath) : t;
|
|
1358
|
+
const fn = location2 ? (0, import_get_value2.default)(t, location2.propertyPath) : t;
|
|
1359
|
+
return fn.apply(thisArg, args);
|
|
1360
|
+
});
|
|
1361
|
+
},
|
|
1362
|
+
get(target, propertyName, receiver) {
|
|
1363
|
+
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
1364
|
+
return Reflect.get(target, propertyName, receiver);
|
|
1365
|
+
}
|
|
1366
|
+
return createProxy({
|
|
1367
|
+
propertyPath: location2 == null ? propertyName : `${location2.propertyPath}.${propertyName}`,
|
|
1368
|
+
parentPath: location2 == null ? void 0 : location2.propertyPath
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
proxy.__proxy = true;
|
|
1373
|
+
return proxy;
|
|
1374
|
+
}
|
|
1375
|
+
return createProxy();
|
|
1376
|
+
}
|
|
1345
1377
|
return __toCommonJS(src_exports);
|
|
1346
1378
|
})();
|
|
1347
1379
|
/*!
|
package/lib/index.js
CHANGED
|
@@ -95,6 +95,38 @@ function defineProxyService(name, init, config) {
|
|
|
95
95
|
}
|
|
96
96
|
];
|
|
97
97
|
}
|
|
98
|
+
|
|
99
|
+
// src/flattenPromise.ts
|
|
100
|
+
import get2 from "get-value";
|
|
101
|
+
function flattenPromise(promise) {
|
|
102
|
+
function createProxy(location2) {
|
|
103
|
+
const wrapped = () => {
|
|
104
|
+
};
|
|
105
|
+
const proxy = new Proxy(wrapped, {
|
|
106
|
+
apply(_target, _thisArg, args) {
|
|
107
|
+
return __async(this, null, function* () {
|
|
108
|
+
const t = yield promise;
|
|
109
|
+
const thisArg = (location2 == null ? void 0 : location2.parentPath) ? get2(t, location2.parentPath) : t;
|
|
110
|
+
const fn = location2 ? get2(t, location2.propertyPath) : t;
|
|
111
|
+
return fn.apply(thisArg, args);
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
get(target, propertyName, receiver) {
|
|
115
|
+
if (propertyName === "__proxy" || typeof propertyName === "symbol") {
|
|
116
|
+
return Reflect.get(target, propertyName, receiver);
|
|
117
|
+
}
|
|
118
|
+
return createProxy({
|
|
119
|
+
propertyPath: location2 == null ? propertyName : `${location2.propertyPath}.${propertyName}`,
|
|
120
|
+
parentPath: location2 == null ? void 0 : location2.propertyPath
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
proxy.__proxy = true;
|
|
125
|
+
return proxy;
|
|
126
|
+
}
|
|
127
|
+
return createProxy();
|
|
128
|
+
}
|
|
98
129
|
export {
|
|
99
|
-
defineProxyService
|
|
130
|
+
defineProxyService,
|
|
131
|
+
flattenPromise
|
|
100
132
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webext-core/proxy-service",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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. Supports all browsers (Chrome, Firefox, Safari, etc)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|