From b33db08286e75309bb3f98016384905c45865dab Mon Sep 17 00:00:00 2001 From: Dillon Raphael Date: Tue, 22 Nov 2022 16:04:42 -0500 Subject: [PATCH] Ensure superjson registers error classes once (#3982) * bump superjson version & ensure superjson register * update changeset & use error name as property for check if class has been registered * refactor --- .changeset/tender-cooks-tie.md | 7 ++++ packages/blitz-next/package.json | 2 +- packages/blitz-rpc/package.json | 2 +- packages/blitz/package.json | 2 +- packages/blitz/src/errors.ts | 64 ++++++++++++++++---------------- pnpm-lock.yaml | 24 +++++++----- 6 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 .changeset/tender-cooks-tie.md diff --git a/.changeset/tender-cooks-tie.md b/.changeset/tender-cooks-tie.md new file mode 100644 index 0000000000..7e547efdd9 --- /dev/null +++ b/.changeset/tender-cooks-tie.md @@ -0,0 +1,7 @@ +--- +"blitz": patch +"@blitzjs/next": patch +"@blitzjs/rpc": patch +--- + +Fix ambigious class warning log & upgrade superjson from 1.9.1 to 1.11.0 diff --git a/packages/blitz-next/package.json b/packages/blitz-next/package.json index 3b4302f3f0..2d877c0fa4 100644 --- a/packages/blitz-next/package.json +++ b/packages/blitz-next/package.json @@ -30,7 +30,7 @@ "debug": "4.3.3", "fs-extra": "10.0.1", "hoist-non-react-statics": "3.3.2", - "superjson": "1.9.1", + "superjson": "1.11.0", "supports-color": "8.1.1" }, "devDependencies": { diff --git a/packages/blitz-rpc/package.json b/packages/blitz-rpc/package.json index d96aacd9c6..e6c894034c 100644 --- a/packages/blitz-rpc/package.json +++ b/packages/blitz-rpc/package.json @@ -27,7 +27,7 @@ "bad-behavior": "1.0.1", "chalk": "^4.1.0", "debug": "4.3.3", - "superjson": "1.9.1", + "superjson": "1.11.0", "supports-color": "8.1.1" }, "devDependencies": { diff --git a/packages/blitz/package.json b/packages/blitz/package.json index 59fb8fea54..2ac0b37ce8 100644 --- a/packages/blitz/package.json +++ b/packages/blitz/package.json @@ -64,7 +64,7 @@ "resolve-cwd": "3.0.0", "resolve-from": "5.0.0", "rimraf": "3.0.2", - "superjson": "1.9.1", + "superjson": "1.11.0", "supports-color": "8.1.1", "tar": "6.1.11", "ts-node": "10.9.1", diff --git a/packages/blitz/src/errors.ts b/packages/blitz/src/errors.ts index 5c5aded8c6..0663f9b3e4 100644 --- a/packages/blitz/src/errors.ts +++ b/packages/blitz/src/errors.ts @@ -1,6 +1,10 @@ import _SuperJson from "superjson" import type {UrlObject} from "url" +declare module globalThis { + let _BLITZ_ERROR_CLASS_REGISTERED: boolean +} + const SuperJson: typeof _SuperJson = "default" in _SuperJson ? (_SuperJson as any).default : _SuperJson @@ -19,12 +23,6 @@ export class AuthenticationError extends Error { return true } } -if (process.env.JEST_WORKER_ID === undefined) { - SuperJson.registerClass(AuthenticationError, { - identifier: "BlitzAuthenticationError", - allowProps: errorProps, - }) -} export class CSRFTokenMismatchError extends Error { name = "CSRFTokenMismatchError" @@ -33,12 +31,6 @@ export class CSRFTokenMismatchError extends Error { return true } } -if (process.env.JEST_WORKER_ID === undefined) { - SuperJson.registerClass(CSRFTokenMismatchError, { - identifier: "BlitzCSRFTokenMismatchError", - allowProps: errorProps, - }) -} export class AuthorizationError extends Error { name = "AuthorizationError" @@ -50,12 +42,6 @@ export class AuthorizationError extends Error { return true } } -if (process.env.JEST_WORKER_ID === undefined) { - SuperJson.registerClass(AuthorizationError, { - identifier: "BlitzAuthorizationError", - allowProps: errorProps, - }) -} export class NotFoundError extends Error { name = "NotFoundError" @@ -67,12 +53,6 @@ export class NotFoundError extends Error { return true } } -if (process.env.JEST_WORKER_ID === undefined) { - SuperJson.registerClass(NotFoundError, { - identifier: "BlitzNotFoundError", - allowProps: errorProps, - }) -} export class RedirectError extends Error { name = "RedirectError" @@ -86,12 +66,6 @@ export class RedirectError extends Error { return true } } -if (process.env.JEST_WORKER_ID === undefined) { - SuperJson.registerClass(RedirectError, { - identifier: "BlitzRedirectError", - allowProps: errorProps, - }) -} export class PaginationArgumentError extends Error { name = "PaginationArgumentError" @@ -100,9 +74,37 @@ export class PaginationArgumentError extends Error { super(message) } } -if (process.env.JEST_WORKER_ID === undefined) { + +if (process.env.JEST_WORKER_ID === undefined && !globalThis._BLITZ_ERROR_CLASS_REGISTERED) { + SuperJson.registerClass(AuthenticationError, { + identifier: "BlitzAuthenticationError", + allowProps: errorProps, + }) + + SuperJson.registerClass(CSRFTokenMismatchError, { + identifier: "BlitzCSRFTokenMismatchError", + allowProps: errorProps, + }) + + SuperJson.registerClass(AuthorizationError, { + identifier: "BlitzAuthorizationError", + allowProps: errorProps, + }) + + SuperJson.registerClass(NotFoundError, { + identifier: "BlitzNotFoundError", + allowProps: errorProps, + }) + + SuperJson.registerClass(RedirectError, { + identifier: "BlitzRedirectError", + allowProps: errorProps, + }) + SuperJson.registerClass(PaginationArgumentError, { identifier: "BlitzPaginationArgumentError", allowProps: errorProps, }) + + globalThis._BLITZ_ERROR_CLASS_REGISTERED = true } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 29a21decff..327ac0797f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -756,7 +756,7 @@ importers: resolve-cwd: 3.0.0 resolve-from: 5.0.0 rimraf: 3.0.2 - superjson: 1.9.1 + superjson: 1.11.0 supports-color: 8.1.1 tar: 6.1.11 test-listen: 1.1.0 @@ -808,7 +808,7 @@ importers: resolve-cwd: 3.0.0 resolve-from: 5.0.0 rimraf: 3.0.2 - superjson: 1.9.1 + superjson: 1.11.0 supports-color: 8.1.1 tar: 6.1.11 ts-node: 10.9.1_typescript@4.8.4 @@ -934,7 +934,7 @@ importers: react: 18.2.0 react-dom: 18.2.0 resolve-from: 5.0.0 - superjson: 1.9.1 + superjson: 1.11.0 supports-color: 8.1.1 ts-jest: 27.1.4 typescript: ^4.8.4 @@ -947,7 +947,7 @@ importers: debug: 4.3.3_supports-color@8.1.1 fs-extra: 10.0.1 hoist-non-react-statics: 3.3.2 - superjson: 1.9.1 + superjson: 1.11.0 supports-color: 8.1.1 devDependencies: "@blitzjs/config": link:../config @@ -989,7 +989,7 @@ importers: next: 12.2.5 react: 18.2.0 react-dom: 18.2.0 - superjson: 1.9.1 + superjson: 1.11.0 supports-color: 8.1.1 typescript: ^4.8.4 unbuild: 0.7.6 @@ -1003,7 +1003,7 @@ importers: bad-behavior: 1.0.1 chalk: 4.1.2 debug: 4.3.3_supports-color@8.1.1 - superjson: 1.9.1 + superjson: 1.11.0 supports-color: 8.1.1 devDependencies: "@blitzjs/config": link:../config @@ -3524,6 +3524,7 @@ packages: semver: 5.7.1 transitivePeerDependencies: - supports-color + dev: false /@babel/preset-flow/7.17.12_@babel+core@7.18.2: resolution: @@ -6329,6 +6330,7 @@ packages: typescript: 4.8.4 transitivePeerDependencies: - supports-color + dev: false /@typescript-eslint/experimental-utils/5.28.0_rmayb2veg2btbq6mbmnyivgasy: resolution: @@ -10151,6 +10153,7 @@ packages: transitivePeerDependencies: - eslint-import-resolver-webpack - supports-color + dev: false /eslint-config-next/13.0.4_rmayb2veg2btbq6mbmnyivgasy: resolution: @@ -10188,6 +10191,7 @@ packages: hasBin: true peerDependencies: eslint: ">=7.0.0" + dev: false /eslint-config-prettier/8.5.0_eslint@8.27.0: resolution: @@ -13152,7 +13156,7 @@ packages: pretty-format: 29.2.1 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.1_cbe7ovvae6zqfnmtgctpgpys54 + ts-node: 10.9.1_typescript@4.8.4 transitivePeerDependencies: - supports-color @@ -18386,10 +18390,10 @@ packages: minimist: 1.2.6 dev: true - /superjson/1.9.1: + /superjson/1.11.0: resolution: { - integrity: sha512-oT3HA2nPKlU1+5taFgz/HDy+GEaY+CWEbLzaRJVD4gZ7zMVVC4GDNFdgvAZt6/VuIk6D2R7RtPAiCHwmdzlMmg==, + integrity: sha512-6PfAg1FKhqkwWvPb2uXhH4MkMttdc17eJ91+Aoz4s1XUEDZFmLfFx/xVA3wgkPxAGy5dpozgGdK6V/n20Wj9yg==, } engines: {node: ">=10"} dependencies: @@ -18802,6 +18806,7 @@ packages: typescript: 4.8.4 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 + dev: false /ts-node/10.9.1_ieummqxttktzud32hpyrer46t4: resolution: @@ -18868,7 +18873,6 @@ packages: typescript: 4.8.4 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 - dev: false /tsconfig-paths/3.14.1: resolution: