promisify-ts 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 +116 -0
- package/index.ts +20 -0
- package/package.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Egor Kushnarev
|
|
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,116 @@
|
|
|
1
|
+
# promisify-ts
|
|
2
|
+
Compile-time functional type for convert common interfaces to it's promisified variant
|
|
3
|
+
|
|
4
|
+
## when is this useful?
|
|
5
|
+
It is good tool for converting your sync API interfaces that is shared through async transport like Electron IPC
|
|
6
|
+
|
|
7
|
+
## install
|
|
8
|
+
`npm install promisify-ts`
|
|
9
|
+
|
|
10
|
+
## possibilities
|
|
11
|
+
|
|
12
|
+
### promisify methods
|
|
13
|
+
```ts
|
|
14
|
+
import { Promisify } from "promisify-ts";
|
|
15
|
+
|
|
16
|
+
interface MyInterface {
|
|
17
|
+
transformToString(value: number): string;
|
|
18
|
+
getPromise(): Promise<string>;
|
|
19
|
+
getDoublePromise(): Promise<Promise<string>>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type PromisifiedInterface = Promisify<MyInterface>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`PromisifiedInterface` will be transformed as
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
type PromisifiedInterface = {
|
|
29
|
+
transformToString(value: number): Promise<string>; // (number) => string -> (number) => Promise<string>
|
|
30
|
+
getPromise(): Promise<string>; // () => Promise<string> -> () => Promise<string>
|
|
31
|
+
getDoublePromise(): Promise<string> // () => Promise<Promise<string>> -> () => Promise<string>
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### optional decay properties
|
|
36
|
+
By default, everything except methods is removed, so
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { Promisify } from "promisify-ts";
|
|
40
|
+
|
|
41
|
+
interface MyInterface {
|
|
42
|
+
readonly id: number;
|
|
43
|
+
transformToString(value: number): string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type PromisifiedInterface = Promisify<MyInterface>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
will be transformed as
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
type PromisifiedInterface = {
|
|
53
|
+
readonly id: never; // number -> never
|
|
54
|
+
transformToString(value: number): Promise<string>; // (number) => string -> (number) => Promise<string>
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
but `Promisify` has second optional boolean parameter `RemoveProperties`, `true` by default that tells function remove or not properties
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
type PromisifiedInterface = Promisify<MyInterface, false>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
makes
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
type PromisifiedInterface = {
|
|
68
|
+
readonly id: number; // number -> number
|
|
69
|
+
transformToString(value: number): Promise<string>; // (number) => string -> (number) => Promise<string>
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
This may be useful if further conversions of the interface type are expected.
|
|
74
|
+
|
|
75
|
+
### works good with generic methods
|
|
76
|
+
```ts
|
|
77
|
+
import { Promisify } from "promisify-ts";
|
|
78
|
+
|
|
79
|
+
interface MyInterface<U> {
|
|
80
|
+
transformStringToU(value: string): U;
|
|
81
|
+
transformUToString(value: U): string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type PromisifiedInterface = Promisify<MyInterface<number>>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
will tranform into
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
type PromisifiedInterface = {
|
|
91
|
+
transformStringToU(value: string): Promise<number>; // (string) => U -> (string) => Promise<U>
|
|
92
|
+
transformUToString(value: number): Promise<string>; // (U) => string -> (number) => Promise<string>
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### also promisifies returned functions
|
|
97
|
+
```ts
|
|
98
|
+
import { Promisify } from "promisify-ts";
|
|
99
|
+
|
|
100
|
+
interface MyInterface {
|
|
101
|
+
getCallback(): () => string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type PromisifiedInterface = Promisify<MyInterface>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
makes
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
type PromisifiedInterface = {
|
|
111
|
+
getCallback(): Promise<() => Promise<string>>; // () => () => string -> () => Promise<() => Promise<string>>
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## thanks
|
|
116
|
+
Special thanks for [@Nipheris](https://github.com/Nipheris) who once helped in this brainstorming
|
package/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
type Method<P extends unknown[], R extends unknown = unknown> = (...args: P) => R;
|
|
3
|
+
type PromisifyedMethod<P extends unknown[], R extends unknown = unknown> = Method<P, Promise<R>>;
|
|
4
|
+
|
|
5
|
+
type DecayPromiseAndMethod<R> =
|
|
6
|
+
R extends Promise<infer T> ? DecayPromiseAndMethod<T> :
|
|
7
|
+
R extends Method<infer P> ? PromisifyMethod<R> :
|
|
8
|
+
R;
|
|
9
|
+
|
|
10
|
+
type PromisifyMethod<M> =
|
|
11
|
+
M extends <R>(...args: infer P) => Promise<R> ? <R>(...args: P) => Promise<DecayPromiseAndMethod<R>> :
|
|
12
|
+
M extends <R>(...args: infer P) => R ? <R>(...args: P) => Promise<DecayPromiseAndMethod<R>> :
|
|
13
|
+
M extends Method<infer P, infer R> ? PromisifyedMethod<P, DecayPromiseAndMethod<R>> :
|
|
14
|
+
never;
|
|
15
|
+
|
|
16
|
+
export type Promisify<T, RemoveProperties = true> = {
|
|
17
|
+
[K in keyof T]:
|
|
18
|
+
T[K] extends Method<infer P> ? PromisifyMethod<T[K]> :
|
|
19
|
+
RemoveProperties extends false ? T[K] : never;
|
|
20
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "promisify-ts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Compile-time functional type for convert common interfaces to it's promisified variant",
|
|
5
|
+
"homepage": "https://github.com/origin-yaropolk/promisify-ts",
|
|
6
|
+
"keywords": ["promisify", "promisified", "inteface", "async", "promisify-ts"],
|
|
7
|
+
"author": "Egor Kushnarev",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"packageManager": "pnpm@10.10.0",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"default": "./index.ts"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|