@prisma-next/utils 0.3.0-dev.34 → 0.3.0-dev.36
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/dist/abortable.d.mts +28 -0
- package/dist/abortable.d.mts.map +1 -0
- package/dist/abortable.mjs +39 -0
- package/dist/abortable.mjs.map +1 -0
- package/dist/{defined-BuK2JPgV.mjs → defined-BSROFTyI.mjs} +1 -1
- package/dist/{defined-BuK2JPgV.mjs.map → defined-BSROFTyI.mjs.map} +1 -1
- package/dist/defined.mjs +1 -1
- package/dist/redact-db-url.mjs +1 -1
- package/package.json +8 -2
- package/src/abortable.ts +40 -0
- package/src/exports/abortable.ts +1 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/abortable.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Allows aborting an async operation by returning a Promise which rejects if
|
|
4
|
+
* the provided AbortSignal aborts and otherwise resolves with the result of
|
|
5
|
+
* the async operation.
|
|
6
|
+
*
|
|
7
|
+
* Throws immediately if the signal is already aborted.
|
|
8
|
+
* When the signal is aborted later, any wrapped promise will reject with
|
|
9
|
+
* the signal's reason (or a default DOMException).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const { signal = new AbortController().signal } = options;
|
|
14
|
+
* const unlessAborted = abortable(signal);
|
|
15
|
+
*
|
|
16
|
+
* // Any of these will reject if signal is aborted
|
|
17
|
+
* await unlessAborted(asyncOperation());
|
|
18
|
+
* await unlessAborted(fetch(url));
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param signal - The AbortSignal to race against
|
|
22
|
+
* @returns A function that wraps promises to be cancelable
|
|
23
|
+
* @throws If signal is already aborted
|
|
24
|
+
*/
|
|
25
|
+
declare function abortable(signal: AbortSignal): <T>(promise: Promise<T>) => Promise<T>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { abortable };
|
|
28
|
+
//# sourceMappingURL=abortable.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abortable.d.mts","names":[],"sources":["../src/abortable.ts"],"sourcesContent":[],"mappings":";;AAuBA;;;;;;;;;;;;;;;;;;;;;;iBAAgB,SAAA,SAAkB,2BAA2B,QAAQ,OAAO,QAAQ"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region src/abortable.ts
|
|
2
|
+
/**
|
|
3
|
+
* Allows aborting an async operation by returning a Promise which rejects if
|
|
4
|
+
* the provided AbortSignal aborts and otherwise resolves with the result of
|
|
5
|
+
* the async operation.
|
|
6
|
+
*
|
|
7
|
+
* Throws immediately if the signal is already aborted.
|
|
8
|
+
* When the signal is aborted later, any wrapped promise will reject with
|
|
9
|
+
* the signal's reason (or a default DOMException).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const { signal = new AbortController().signal } = options;
|
|
14
|
+
* const unlessAborted = abortable(signal);
|
|
15
|
+
*
|
|
16
|
+
* // Any of these will reject if signal is aborted
|
|
17
|
+
* await unlessAborted(asyncOperation());
|
|
18
|
+
* await unlessAborted(fetch(url));
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param signal - The AbortSignal to race against
|
|
22
|
+
* @returns A function that wraps promises to be cancelable
|
|
23
|
+
* @throws If signal is already aborted
|
|
24
|
+
*/
|
|
25
|
+
function abortable(signal) {
|
|
26
|
+
signal.throwIfAborted();
|
|
27
|
+
const abortPromise = new Promise((_resolve, reject) => {
|
|
28
|
+
signal.addEventListener("abort", () => {
|
|
29
|
+
reject(signal.reason ?? new DOMException("Operation cancelled"));
|
|
30
|
+
}, { once: true });
|
|
31
|
+
});
|
|
32
|
+
return (operation) => {
|
|
33
|
+
return Promise.race([operation, abortPromise]);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { abortable };
|
|
39
|
+
//# sourceMappingURL=abortable.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abortable.mjs","names":[],"sources":["../src/abortable.ts"],"sourcesContent":["/**\n * Allows aborting an async operation by returning a Promise which rejects if\n * the provided AbortSignal aborts and otherwise resolves with the result of\n * the async operation.\n *\n * Throws immediately if the signal is already aborted.\n * When the signal is aborted later, any wrapped promise will reject with\n * the signal's reason (or a default DOMException).\n *\n * @example\n * ```typescript\n * const { signal = new AbortController().signal } = options;\n * const unlessAborted = abortable(signal);\n *\n * // Any of these will reject if signal is aborted\n * await unlessAborted(asyncOperation());\n * await unlessAborted(fetch(url));\n * ```\n *\n * @param signal - The AbortSignal to race against\n * @returns A function that wraps promises to be cancelable\n * @throws If signal is already aborted\n */\nexport function abortable(signal: AbortSignal): <T>(promise: Promise<T>) => Promise<T> {\n signal.throwIfAborted();\n\n const abortPromise = new Promise<never>((_resolve, reject) => {\n signal.addEventListener(\n 'abort',\n () => {\n reject(signal.reason ?? new DOMException('Operation cancelled'));\n },\n { once: true },\n );\n });\n\n return <T>(operation: Promise<T>): Promise<T> => {\n return Promise.race([operation, abortPromise]);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,UAAU,QAA6D;AACrF,QAAO,gBAAgB;CAEvB,MAAM,eAAe,IAAI,SAAgB,UAAU,WAAW;AAC5D,SAAO,iBACL,eACM;AACJ,UAAO,OAAO,UAAU,IAAI,aAAa,sBAAsB,CAAC;KAElE,EAAE,MAAM,MAAM,CACf;GACD;AAEF,SAAW,cAAsC;AAC/C,SAAO,QAAQ,KAAK,CAAC,WAAW,aAAa,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defined-
|
|
1
|
+
{"version":3,"file":"defined-BSROFTyI.mjs","names":[],"sources":["../src/defined.ts"],"sourcesContent":["/**\n * Returns an object with the key/value if value is defined, otherwise an empty object.\n *\n * Use with spread to conditionally include optional properties while satisfying\n * exactOptionalPropertyTypes. This is explicit about which properties are optional\n * and won't inadvertently strip other undefined values.\n *\n * @example\n * ```typescript\n * // Instead of:\n * const obj = {\n * required: 'value',\n * ...(optional ? { optional } : {}),\n * };\n *\n * // Use:\n * const obj = {\n * required: 'value',\n * ...ifDefined('optional', optional),\n * };\n * ```\n */\nexport function ifDefined<K extends string, V>(\n key: K,\n value: V | undefined,\n): Record<never, never> | { [P in K]: V } {\n return value !== undefined ? ({ [key]: value } as { [P in K]: V }) : {};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,UACd,KACA,OACwC;AACxC,QAAO,UAAU,SAAa,GAAG,MAAM,OAAO,GAAuB,EAAE"}
|
package/dist/defined.mjs
CHANGED
package/dist/redact-db-url.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/utils",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.36",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "Shared utility functions for Prisma Next",
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"tsdown": "0.18.4",
|
|
9
9
|
"typescript": "5.9.3",
|
|
10
|
-
"vitest": "4.0.
|
|
10
|
+
"vitest": "4.0.17",
|
|
11
11
|
"@prisma-next/tsconfig": "0.0.0",
|
|
12
12
|
"@prisma-next/tsdown": "0.0.0"
|
|
13
13
|
},
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"node": ">=20"
|
|
20
20
|
},
|
|
21
21
|
"exports": {
|
|
22
|
+
"./abortable": "./dist/abortable.mjs",
|
|
22
23
|
"./array-equal": "./dist/array-equal.mjs",
|
|
23
24
|
"./assertions": "./dist/assertions.mjs",
|
|
24
25
|
"./defined": "./dist/defined.mjs",
|
|
@@ -26,6 +27,11 @@
|
|
|
26
27
|
"./result": "./dist/result.mjs",
|
|
27
28
|
"./package.json": "./package.json"
|
|
28
29
|
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
33
|
+
"directory": "packages/1-framework/1-core/shared/utils"
|
|
34
|
+
},
|
|
29
35
|
"scripts": {
|
|
30
36
|
"build": "tsdown",
|
|
31
37
|
"test": "vitest run",
|
package/src/abortable.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Allows aborting an async operation by returning a Promise which rejects if
|
|
3
|
+
* the provided AbortSignal aborts and otherwise resolves with the result of
|
|
4
|
+
* the async operation.
|
|
5
|
+
*
|
|
6
|
+
* Throws immediately if the signal is already aborted.
|
|
7
|
+
* When the signal is aborted later, any wrapped promise will reject with
|
|
8
|
+
* the signal's reason (or a default DOMException).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const { signal = new AbortController().signal } = options;
|
|
13
|
+
* const unlessAborted = abortable(signal);
|
|
14
|
+
*
|
|
15
|
+
* // Any of these will reject if signal is aborted
|
|
16
|
+
* await unlessAborted(asyncOperation());
|
|
17
|
+
* await unlessAborted(fetch(url));
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @param signal - The AbortSignal to race against
|
|
21
|
+
* @returns A function that wraps promises to be cancelable
|
|
22
|
+
* @throws If signal is already aborted
|
|
23
|
+
*/
|
|
24
|
+
export function abortable(signal: AbortSignal): <T>(promise: Promise<T>) => Promise<T> {
|
|
25
|
+
signal.throwIfAborted();
|
|
26
|
+
|
|
27
|
+
const abortPromise = new Promise<never>((_resolve, reject) => {
|
|
28
|
+
signal.addEventListener(
|
|
29
|
+
'abort',
|
|
30
|
+
() => {
|
|
31
|
+
reject(signal.reason ?? new DOMException('Operation cancelled'));
|
|
32
|
+
},
|
|
33
|
+
{ once: true },
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return <T>(operation: Promise<T>): Promise<T> => {
|
|
38
|
+
return Promise.race([operation, abortPromise]);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { abortable } from '../abortable';
|