Skip to content

Commit e99d9b9

Browse files
authored
Merge pull request #3 from block65/feat/v14
feat!: publish raw TypeScript, target node 24
2 parents de867db + 9994a36 commit e99d9b9

File tree

14 files changed

+663
-563
lines changed

14 files changed

+663
-563
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v4
1313

14-
- uses: pnpm/action-setup@v2
14+
- uses: pnpm/action-setup@v4
1515

1616
- uses: actions/setup-node@v4
1717
with:
18-
node-version: 20
18+
node-version: 24
1919
registry-url: https://registry.npmjs.org/
2020
cache: 'pnpm'
2121

.github/workflows/pr.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ jobs:
88
test:
99
strategy:
1010
matrix:
11-
version: [20, 22]
11+
version: [24]
1212
os: [ubuntu-latest]
1313

1414
runs-on: ${{ matrix.os }}
1515

1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1818

19-
- uses: pnpm/action-setup@v2
19+
- uses: pnpm/action-setup@v4
2020

2121
- uses: actions/setup-node@v4
2222
with:
2323
node-version: ${{ matrix.version }}
24-
registry-url: https://registry.npmjs.org/
2524
cache: 'pnpm'
2625

2726
- run: make test publint

Makefile

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11

22
SRCS = $(wildcard lib/**)
33

4-
all: dist
4+
all: typecheck
55

6-
.PHONY: clean distclean
6+
.PHONY: distclean
77
distclean:
88
rm -rf node_modules
99

10-
.PHONY: clean
11-
clean: node_modules
12-
pnpm exec tsc -b --clean
13-
1410
.PHONY: test
1511
test: node_modules
1612
pnpm exec vitest
1713

1814
node_modules: package.json
1915
pnpm install
2016

21-
dist: node_modules tsconfig.json $(SRCS)
17+
.PHONY: typecheck
18+
typecheck: node_modules tsconfig.json $(SRCS)
2219
pnpm exec tsc
2320

24-
.PHONY: dev
25-
dev: node_modules
26-
pnpm exec tsc -w
27-
2821
.PHONY: pretty
2922
pretty: node_modules
3023
pnpm exec prettier --write .
3124

3225
.PHONY: publint
33-
publint: dist
26+
publint: typecheck
3427
npx publint --strict

lib/custom-error.ts

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
import { isErrorLike } from "serialize-error";
2-
import type { ErrorDetail, LocalisedMessage } from "./types.js";
3-
import { withNullProto } from "./utils.js";
2+
import type { ErrorDetail, LocalisedMessage } from "./types.ts";
3+
import { withNullProto } from "./utils.ts";
44

55
export type DebugData = Record<string, unknown>;
66

7-
enum StatusCode {
8-
OK = 0,
9-
CANCELLED = 1,
10-
UNKNOWN = 2,
11-
INVALID_ARGUMENT = 3,
12-
DEADLINE_EXCEEDED = 4,
13-
NOT_FOUND = 5,
14-
ALREADY_EXISTS = 6,
15-
PERMISSION_DENIED = 7,
16-
RESOURCE_EXHAUSTED = 8,
17-
FAILED_PRECONDITION = 9,
18-
ABORTED = 10,
19-
OUT_OF_RANGE = 11,
20-
UNIMPLEMENTED = 12,
21-
INTERNAL = 13,
22-
UNAVAILABLE = 14,
23-
DATA_LOSS = 15,
24-
UNAUTHENTICATED = 16,
7+
const StatusCode = {
8+
OK: 0 as const,
9+
CANCELLED: 1 as const,
10+
UNKNOWN: 2 as const,
11+
INVALID_ARGUMENT: 3 as const,
12+
DEADLINE_EXCEEDED: 4 as const,
13+
NOT_FOUND: 5 as const,
14+
ALREADY_EXISTS: 6 as const,
15+
PERMISSION_DENIED: 7 as const,
16+
RESOURCE_EXHAUSTED: 8 as const,
17+
FAILED_PRECONDITION: 9 as const,
18+
ABORTED: 10 as const,
19+
OUT_OF_RANGE: 11 as const,
20+
UNIMPLEMENTED: 12 as const,
21+
INTERNAL: 13 as const,
22+
UNAVAILABLE: 14 as const,
23+
DATA_LOSS: 15 as const,
24+
UNAUTHENTICATED: 16 as const,
2525
}
2626

2727
// just export the type, we CustomError.XX should be used for the actual code
28-
export type { StatusCode };
28+
export type StatusCode = typeof StatusCode[keyof typeof StatusCode];
2929

30-
type SerializedError<T extends StatusCode | number> = {
30+
const statusCodes: ReadonlySet<number> = new Set(Object.values(StatusCode));
31+
32+
export function isStatusCode(value: unknown): value is StatusCode {
33+
return typeof value === 'number' && statusCodes.has(value);
34+
}
35+
36+
export type SerializedError<T extends StatusCode> = {
3137
readonly debug?: DebugData;
3238
readonly stack?: string;
3339
readonly cause?: SerializedError<StatusCode>[];
@@ -60,17 +66,17 @@ export class CustomError extends Error {
6066

6167
static http = Object.freeze({
6268
[CustomError.OK]: 200,
63-
[CustomError.CANCELLED]: 299,
69+
[CustomError.CANCELLED]: 499,
6470
[CustomError.UNKNOWN]: 500,
6571
[CustomError.INVALID_ARGUMENT]: 400,
6672
[CustomError.DEADLINE_EXCEEDED]: 504,
6773
[CustomError.NOT_FOUND]: 404,
6874
[CustomError.ALREADY_EXISTS]: 409,
6975
[CustomError.PERMISSION_DENIED]: 403,
70-
[CustomError.RESOURCE_EXHAUSTED]: 403,
76+
[CustomError.RESOURCE_EXHAUSTED]: 429,
7177
[CustomError.FAILED_PRECONDITION]: 400,
72-
[CustomError.ABORTED]: 299,
73-
[CustomError.OUT_OF_RANGE]: 400,
78+
[CustomError.ABORTED]: 409,
79+
[CustomError.OUT_OF_RANGE]: 422,
7480
[CustomError.UNIMPLEMENTED]: 501,
7581
[CustomError.INTERNAL]: 500,
7682
[CustomError.UNAVAILABLE]: 503,
@@ -114,7 +120,7 @@ export class CustomError extends Error {
114120
this.cause = cause;
115121

116122
// FF doesnt have captureStackTrace
117-
if (Error.captureStackTrace) {
123+
if ('captureStackTrace' in Error) {
118124
Error.captureStackTrace(this, this.constructor);
119125
}
120126

@@ -189,13 +195,13 @@ export class CustomError extends Error {
189195
/**
190196
* "Hydrates" a previously serialised error object
191197
*/
192-
public static fromJSON<const T extends StatusCode | number>(
198+
public static fromJSON<const T extends StatusCode>(
193199
params: SerializedError<T>,
194200
) {
195201
const { message, details, code } = params;
196202

197203
class ImportedError extends CustomError {
198-
override readonly code = code;
204+
override readonly code = code as StatusCode;
199205
}
200206

201207
const err = new ImportedError(message).debug({
@@ -215,9 +221,11 @@ export class CustomError extends Error {
215221
public static suggestHttpResponseCode(err: Error | CustomError | unknown) {
216222
return (
217223
(CustomError.isCustomError(err) && CustomError.http[err.code]) ||
218-
CustomError.http[CustomError.UNKNOWN]
224+
CustomError.http[CustomError.UNKNOWN] ||
225+
500
219226
);
220227
}
228+
221229
}
222230

223231
// Mark all instances of 'CustomError'

lib/index.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { addKnownErrorConstructor } from "serialize-error";
2+
import { CustomError } from "./custom-error.ts";
3+
4+
try {
5+
addKnownErrorConstructor(CustomError);
6+
} catch{}
7+
8+
export * from "./custom-error.ts";
9+
export * from "./serialize.ts";
10+
11+
export type * from "./types.ts";

lib/serialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isErrorLike,
55
serializeError,
66
} from "serialize-error";
7-
import { CustomError } from "./custom-error.js";
7+
import { CustomError } from "./custom-error.ts";
88

99
function flatten(
1010
err: unknown | ErrorLike | Error | CustomError,

package.json

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
{
22
"name": "@block65/custom-error",
3-
"version": "13.0.0-rc.0",
3+
"version": "14.0.0",
44
"private": false,
55
"license": "MIT",
66
"type": "module",
7-
"exports": {
8-
".": {
9-
"types": "./dist/lib/index.d.ts",
10-
"default": "./dist/lib/index.js"
11-
}
12-
},
7+
"exports": "./lib/main.ts",
138
"files": [
14-
"dist/lib/*.js",
15-
"dist/lib/*.d.ts"
9+
"lib"
1610
],
1711
"scripts": {
18-
"preversion": "make clean && make test && make"
12+
"preversion": "make test && make"
1913
},
2014
"dependencies": {
21-
"serialize-error": "^12.0.0"
15+
"serialize-error": "^13.0.1"
2216
},
2317
"devDependencies": {
24-
"@tsconfig/node18": "^18.2.4",
25-
"@tsconfig/strictest": "^2.0.5",
26-
"vitest": "^2.1.8",
27-
"@biomejs/biome": "^1.9.4",
28-
"@types/node": "^20.17.12",
29-
"typescript": "^5.7.3"
18+
"@tsconfig/node24": "^24.0.4",
19+
"@tsconfig/strictest": "^2.0.8",
20+
"@types/node": "^24.0.0",
21+
"typescript": "^5.8.0",
22+
"vitest": "^3.0.0"
3023
},
3124
"engines": {
32-
"node": ">=20.0.0"
25+
"node": ">=24.0.0"
3326
},
34-
"packageManager": "pnpm@9.15.3+sha512.1f79bc245a66eb0b07c5d4d83131240774642caaa86ef7d0434ab47c0d16f66b04e21e0c086eb61e62c77efc4d7f7ec071afad3796af64892fae66509173893a"
27+
"packageManager": "pnpm@10.30.3"
3528
}

0 commit comments

Comments
 (0)