Skip to content

Commit b929098

Browse files
committed
feat: add vendored neverthrow
1 parent 5b01a1c commit b929098

File tree

11 files changed

+59
-18
lines changed

11 files changed

+59
-18
lines changed

src/__tests__/exception.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Err } from 'neverthrow';
21
import type { Simplify } from 'type-fest';
32
import { describe, expect, expectTypeOf, it, test } from 'vitest';
43

54
import { BaseException, ExceptionBuilder, OutOfRangeException, ValueException } from '../exception.js';
5+
import { Err } from '../vendor/neverthrow.js';
66

77
import type { ExceptionConstructor } from '../exception.js';
88

src/__tests__/http.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { Err } from 'neverthrow';
21
import { afterEach, describe, expect, it, vi } from 'vitest';
32

43
import * as datetime from '../datetime.js';
54
import { safeFetch, waitForServer } from '../http.js';
65

6+
import type { Err } from '../vendor/neverthrow.js';
7+
78
const fetch = vi.hoisted(() => vi.fn());
89
const networkError = new Error('Network Error');
910

src/__tests__/result.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Err, err, Ok, ok } from 'neverthrow';
21
import { describe, expect, it } from 'vitest';
32

43
import { asyncResultify } from '../result.js';
4+
import { Err, err, Ok, ok } from '../vendor/neverthrow.js';
55

66
describe('asyncResultify', () => {
77
it('should convert a successful Result to ResultAsync', async () => {

src/datetime.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { err, ok } from 'neverthrow';
2-
import type { Result } from 'neverthrow';
3-
41
import { OutOfRangeException } from './exception.js';
2+
import { err, ok } from './vendor/neverthrow.js';
3+
4+
import type { Result } from './vendor/neverthrow.js';
55

66
export type Duration = {
77
days: number;

src/exception.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
import cleanStack from 'clean-stack';
66
import extractStack from 'extract-stack';
7-
import { err, errAsync, Result, ResultAsync } from 'neverthrow';
87
import stringifyObject from 'stringify-object';
98
import type { IsNever, RequiredKeysOf } from 'type-fest';
109
import type { z } from 'zod';
1110

1211
import { objectify } from './object.js';
1312
import { indentLines } from './string.js';
13+
import { err, errAsync, Result, ResultAsync } from './vendor/neverthrow.js';
1414

1515
import type { SingleKeyMap, ToAbstractConstructor } from './types.js';
1616

src/http.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { ok, ResultAsync } from 'neverthrow';
2-
31
import { sleep } from './datetime.js';
42
import { ExceptionBuilder, RuntimeException } from './exception.js';
53
import { asyncResultify } from './result.js';
4+
import { ok, ResultAsync } from './vendor/neverthrow.js';
65

76
import type { ExceptionLike } from './exception.js';
87

src/random.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ok } from 'neverthrow';
2-
import type { Result } from 'neverthrow';
3-
41
import { ValueException } from './exception.js';
2+
import { ok } from './vendor/neverthrow.js';
3+
4+
import type { Result } from './vendor/neverthrow.js';
55

66
/** Returns a random integer between `min` (inclusive) and `max` (not inclusive) */
77
export function randomInt(min: number, max: number): Result<number, typeof ValueException.Instance> {

src/range.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ok } from 'neverthrow';
2-
import type { Result } from 'neverthrow';
3-
41
import { ValueException } from './exception.js';
2+
import { ok } from './vendor/neverthrow.js';
3+
4+
import type { Result } from './vendor/neverthrow.js';
55

66
/** Return an array of integers between 0 (inclusive) and `end` (not inclusive) */
77
export function range(end: number): Result<readonly number[], typeof ValueException.Instance>;

src/result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Result, ResultAsync } from 'neverthrow';
1+
import { Result, ResultAsync } from './vendor/neverthrow.js';
22

33
export function asyncResultify<T, E>(fn: () => Promise<Result<T, E>>): ResultAsync<T, E> {
44
return new ResultAsync(fn());

src/vendor/neverthrow.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable @typescript-eslint/only-throw-error */
2+
/* eslint-disable @typescript-eslint/no-unused-vars */
3+
/* eslint-disable @typescript-eslint/consistent-type-definitions */
4+
5+
import { Err, Ok, Result } from 'neverthrow';
6+
7+
declare module 'neverthrow' {
8+
interface Err<T, E> {
9+
unwrap(): T;
10+
}
11+
interface Ok<T, E> {
12+
unwrap(): T;
13+
}
14+
}
15+
16+
function unwrap<T, E>(this: Result<T, E>): any {
17+
if (this.isErr()) {
18+
throw this.error;
19+
}
20+
return this.value;
21+
}
22+
23+
Err.prototype.unwrap = unwrap;
24+
Ok.prototype.unwrap = unwrap;
25+
26+
export { Err, Ok };
27+
28+
export {
29+
err,
30+
errAsync,
31+
fromAsyncThrowable,
32+
fromPromise,
33+
fromSafePromise,
34+
fromThrowable,
35+
ok,
36+
okAsync,
37+
Result,
38+
ResultAsync,
39+
safeTry
40+
} from 'neverthrow';

0 commit comments

Comments
 (0)