@prisma-next/utils 0.1.0-pr.59.5 → 0.1.0-pr.60.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.
@@ -15,6 +15,8 @@
15
15
  interface Ok<T> {
16
16
  readonly ok: true;
17
17
  readonly value: T;
18
+ assertOk(): T;
19
+ assertNotOk(): never;
18
20
  }
19
21
  /**
20
22
  * Represents an unsuccessful result containing failure details.
@@ -22,6 +24,8 @@ interface Ok<T> {
22
24
  interface NotOk<F> {
23
25
  readonly ok: false;
24
26
  readonly failure: F;
27
+ assertOk(): never;
28
+ assertNotOk(): F;
25
29
  }
26
30
  /**
27
31
  * A discriminated union representing either success (Ok) or failure (NotOk).
@@ -1,11 +1,69 @@
1
1
  // src/result.ts
2
+ var ResultImpl = class _ResultImpl {
3
+ ok;
4
+ _value;
5
+ _failure;
6
+ constructor(ok2, valueOrFailure) {
7
+ this.ok = ok2;
8
+ if (ok2) {
9
+ this._value = valueOrFailure;
10
+ } else {
11
+ this._failure = valueOrFailure;
12
+ }
13
+ Object.freeze(this);
14
+ }
15
+ get value() {
16
+ if (!this.ok) {
17
+ throw new Error("Cannot access value on NotOk result");
18
+ }
19
+ return this._value;
20
+ }
21
+ get failure() {
22
+ if (this.ok) {
23
+ throw new Error("Cannot access failure on Ok result");
24
+ }
25
+ return this._failure;
26
+ }
27
+ /**
28
+ * Creates a successful result.
29
+ */
30
+ static ok(value) {
31
+ return new _ResultImpl(true, value);
32
+ }
33
+ /**
34
+ * Creates an unsuccessful result.
35
+ */
36
+ static notOk(failure) {
37
+ return new _ResultImpl(false, failure);
38
+ }
39
+ /**
40
+ * Asserts that this result is Ok and returns the value.
41
+ * Throws if the result is NotOk.
42
+ */
43
+ assertOk() {
44
+ if (!this.ok) {
45
+ throw new Error("Expected Ok result but got NotOk");
46
+ }
47
+ return this.value;
48
+ }
49
+ /**
50
+ * Asserts that this result is NotOk and returns the failure.
51
+ * Throws if the result is Ok.
52
+ */
53
+ assertNotOk() {
54
+ if (this.ok) {
55
+ throw new Error("Expected NotOk result but got Ok");
56
+ }
57
+ return this.failure;
58
+ }
59
+ };
2
60
  function ok(value) {
3
- return Object.freeze({ ok: true, value });
61
+ return ResultImpl.ok(value);
4
62
  }
5
63
  function notOk(failure) {
6
- return Object.freeze({ ok: false, failure });
64
+ return ResultImpl.notOk(failure);
7
65
  }
8
- var OK_VOID = Object.freeze({ ok: true, value: void 0 });
66
+ var OK_VOID = ResultImpl.ok(void 0);
9
67
  function okVoid() {
10
68
  return OK_VOID;
11
69
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/result.ts"],"sourcesContent":["/**\n * Generic Result type for representing success or failure outcomes.\n *\n * This is the standard way to return \"expected failures\" as values rather than\n * throwing exceptions. See docs/Error Handling.md for the full taxonomy.\n *\n * Naming rationale:\n * - `Ok<T>` / `NotOk<F>` mirror the `ok: true/false` discriminator\n * - `NotOk` avoids collision with domain types like \"Failure\" or \"Error\"\n * - `failure` property distinguishes from JS Error semantics\n */\n\n/**\n * Represents a successful result containing a value.\n */\nexport interface Ok<T> {\n readonly ok: true;\n readonly value: T;\n}\n\n/**\n * Represents an unsuccessful result containing failure details.\n */\nexport interface NotOk<F> {\n readonly ok: false;\n readonly failure: F;\n}\n\n/**\n * A discriminated union representing either success (Ok) or failure (NotOk).\n *\n * @typeParam T - The success value type\n * @typeParam F - The failure details type\n */\nexport type Result<T, F> = Ok<T> | NotOk<F>;\n\n/**\n * Creates a successful result.\n */\nexport function ok<T>(value: T): Ok<T> {\n return Object.freeze({ ok: true, value });\n}\n\n/**\n * Creates an unsuccessful result.\n */\nexport function notOk<F>(failure: F): NotOk<F> {\n return Object.freeze({ ok: false, failure });\n}\n\n/**\n * Singleton for void success results.\n * Use this for validation checks that don't produce a value.\n */\nconst OK_VOID: Ok<void> = Object.freeze({ ok: true, value: undefined });\n\n/**\n * Returns a successful void result.\n * Use this for validation checks that don't produce a value.\n */\nexport function okVoid(): Ok<void> {\n return OK_VOID;\n}\n"],"mappings":";AAuCO,SAAS,GAAM,OAAiB;AACrC,SAAO,OAAO,OAAO,EAAE,IAAI,MAAM,MAAM,CAAC;AAC1C;AAKO,SAAS,MAAS,SAAsB;AAC7C,SAAO,OAAO,OAAO,EAAE,IAAI,OAAO,QAAQ,CAAC;AAC7C;AAMA,IAAM,UAAoB,OAAO,OAAO,EAAE,IAAI,MAAM,OAAO,OAAU,CAAC;AAM/D,SAAS,SAAmB;AACjC,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/result.ts"],"sourcesContent":["/**\n * Generic Result type for representing success or failure outcomes.\n *\n * This is the standard way to return \"expected failures\" as values rather than\n * throwing exceptions. See docs/Error Handling.md for the full taxonomy.\n *\n * Naming rationale:\n * - `Ok<T>` / `NotOk<F>` mirror the `ok: true/false` discriminator\n * - `NotOk` avoids collision with domain types like \"Failure\" or \"Error\"\n * - `failure` property distinguishes from JS Error semantics\n */\n\n/**\n * Represents a successful result containing a value.\n */\nexport interface Ok<T> {\n readonly ok: true;\n readonly value: T;\n assertOk(): T;\n assertNotOk(): never;\n}\n\n/**\n * Represents an unsuccessful result containing failure details.\n */\nexport interface NotOk<F> {\n readonly ok: false;\n readonly failure: F;\n assertOk(): never;\n assertNotOk(): F;\n}\n\n/**\n * A discriminated union representing either success (Ok) or failure (NotOk).\n *\n * @typeParam T - The success value type\n * @typeParam F - The failure details type\n */\nexport type Result<T, F> = Ok<T> | NotOk<F>;\n\n/**\n * Result class that implements both Ok and NotOk variants.\n */\nclass ResultImpl<T, F> {\n readonly ok: boolean;\n private readonly _value?: T;\n private readonly _failure?: F;\n\n private constructor(ok: boolean, valueOrFailure: T | F) {\n this.ok = ok;\n if (ok) {\n this._value = valueOrFailure as T;\n } else {\n this._failure = valueOrFailure as F;\n }\n Object.freeze(this);\n }\n\n get value(): T {\n if (!this.ok) {\n throw new Error('Cannot access value on NotOk result');\n }\n // biome-ignore lint/style/noNonNullAssertion: must be present if ok is true\n return this._value!;\n }\n\n get failure(): F {\n if (this.ok) {\n throw new Error('Cannot access failure on Ok result');\n }\n // biome-ignore lint/style/noNonNullAssertion: must be present if ok is false\n return this._failure!;\n }\n\n /**\n * Creates a successful result.\n */\n static ok<T, F = never>(value: T): Ok<T> {\n // TypeScript cannot express discriminated return types for a single implementation.\n // Cast is safe: ok=true guarantees this is an Ok<T> at runtime.\n return new ResultImpl<T, F>(true, value) as unknown as Ok<T>;\n }\n\n /**\n * Creates an unsuccessful result.\n */\n static notOk<T = never, F = unknown>(failure: F): NotOk<F> {\n // TypeScript cannot express discriminated return types for a single implementation.\n // Cast is safe: ok=false guarantees this is a NotOk<F> at runtime.\n return new ResultImpl<T, F>(false, failure) as unknown as NotOk<F>;\n }\n\n /**\n * Asserts that this result is Ok and returns the value.\n * Throws if the result is NotOk.\n */\n assertOk(this: Result<T, F>): T {\n if (!this.ok) {\n throw new Error('Expected Ok result but got NotOk');\n }\n return this.value;\n }\n\n /**\n * Asserts that this result is NotOk and returns the failure.\n * Throws if the result is Ok.\n */\n assertNotOk(this: Result<T, F>): F {\n if (this.ok) {\n throw new Error('Expected NotOk result but got Ok');\n }\n return this.failure;\n }\n}\n\n/**\n * Creates a successful result.\n */\nexport function ok<T>(value: T): Ok<T> {\n return ResultImpl.ok(value);\n}\n\n/**\n * Creates an unsuccessful result.\n */\nexport function notOk<F>(failure: F): NotOk<F> {\n return ResultImpl.notOk(failure);\n}\n\n/**\n * Singleton for void success results.\n * Use this for validation checks that don't produce a value.\n */\nconst OK_VOID: Ok<void> = ResultImpl.ok<void>(undefined);\n\n/**\n * Returns a successful void result.\n * Use this for validation checks that don't produce a value.\n */\nexport function okVoid(): Ok<void> {\n return OK_VOID;\n}\n"],"mappings":";AA2CA,IAAM,aAAN,MAAM,YAAiB;AAAA,EACZ;AAAA,EACQ;AAAA,EACA;AAAA,EAET,YAAYA,KAAa,gBAAuB;AACtD,SAAK,KAAKA;AACV,QAAIA,KAAI;AACN,WAAK,SAAS;AAAA,IAChB,OAAO;AACL,WAAK,WAAW;AAAA,IAClB;AACA,WAAO,OAAO,IAAI;AAAA,EACpB;AAAA,EAEA,IAAI,QAAW;AACb,QAAI,CAAC,KAAK,IAAI;AACZ,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,UAAa;AACf,QAAI,KAAK,IAAI;AACX,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACtD;AAEA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,GAAiB,OAAiB;AAGvC,WAAO,IAAI,YAAiB,MAAM,KAAK;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,MAA8B,SAAsB;AAGzD,WAAO,IAAI,YAAiB,OAAO,OAAO;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,WAAgC;AAC9B,QAAI,CAAC,KAAK,IAAI;AACZ,YAAM,IAAI,MAAM,kCAAkC;AAAA,IACpD;AACA,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,cAAmC;AACjC,QAAI,KAAK,IAAI;AACX,YAAM,IAAI,MAAM,kCAAkC;AAAA,IACpD;AACA,WAAO,KAAK;AAAA,EACd;AACF;AAKO,SAAS,GAAM,OAAiB;AACrC,SAAO,WAAW,GAAG,KAAK;AAC5B;AAKO,SAAS,MAAS,SAAsB;AAC7C,SAAO,WAAW,MAAM,OAAO;AACjC;AAMA,IAAM,UAAoB,WAAW,GAAS,MAAS;AAMhD,SAAS,SAAmB;AACjC,SAAO;AACT;","names":["ok"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma-next/utils",
3
- "version": "0.1.0-pr.59.5",
3
+ "version": "0.1.0-pr.60.2",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "Shared utility functions for Prisma Next",