From b7d59e3f7ca817c905e7fb9300a886963c8c14c7 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Thu, 17 Jun 2021 14:55:18 -0400 Subject: [PATCH] fix: support URL as a field value type Closes #5762 --- src/util/OrmUtils.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/util/OrmUtils.ts b/src/util/OrmUtils.ts index 4687b2e963..843e8baf75 100644 --- a/src/util/OrmUtils.ts +++ b/src/util/OrmUtils.ts @@ -1,5 +1,14 @@ import {ObjectLiteral} from "../common/ObjectLiteral"; +/** + * Access `URL` in environments that support it. + * For example, React Native does not include `URL` (see https://github.com/typeorm/typeorm/issues/6142), + * but Node.js (>= v10) and browser implementations do. + * + * Note: `globalThis` is being extended with a `URL` property as @types/node does not declare it (see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34960) + */ +const GlobalURL = (typeof globalThis !== "undefined" ? globalThis as (typeof globalThis & {URL?: (typeof import('url').URL)}) : {URL: undefined}).URL; + export class OrmUtils { // ------------------------------------------------------------------------- @@ -82,7 +91,8 @@ export class OrmUtils { && !(value instanceof Set) && !(value instanceof Date) && !(value instanceof Buffer) - && !(value instanceof RegExp)) { + && !(value instanceof RegExp) + && !(GlobalURL && value instanceof GlobalURL)) { if (!target[key]) Object.assign(target, { [key]: Object.create(Object.getPrototypeOf(value)) }); this.mergeDeep(target[key], value); @@ -212,7 +222,8 @@ export class OrmUtils { (x instanceof Date && y instanceof Date) || (x instanceof RegExp && y instanceof RegExp) || (x instanceof String && y instanceof String) || - (x instanceof Number && y instanceof Number)) + (x instanceof Number && y instanceof Number) || + (GlobalURL && x instanceof GlobalURL && y instanceof GlobalURL)) return x.toString() === y.toString(); // At last checking prototypes as good as we can