chore: safely accesses bson-objectid within both ts and webpack

This commit is contained in:
James
2024-03-07 10:54:27 -05:00
parent 030ddbe12f
commit bd66cda0ee
8 changed files with 28 additions and 16 deletions

View File

@@ -3,7 +3,7 @@ import type { PathToQuery } from 'payload/database'
import type { Field } from 'payload/types'
import type { Operator } from 'payload/types'
import ObjectId from 'bson-objectid'
import ObjectIdImport from 'bson-objectid'
import mongoose from 'mongoose'
import { getLocalizedPaths } from 'payload/database'
import { fieldAffectsData } from 'payload/types'
@@ -14,6 +14,9 @@ import type { MongooseAdapter } from '../index.d.ts'
import { operatorMap } from './operatorMap.js'
import { sanitizeQueryValue } from './sanitizeQueryValue.js'
const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
type SearchParam = {
path?: string
rawQuery?: unknown
@@ -205,7 +208,7 @@ export async function buildSearchParam({
if (typeof formattedValue === 'string') {
if (mongoose.Types.ObjectId.isValid(formattedValue)) {
result.value.$or.push({
[path]: { [operatorKey]: new ObjectId.default(formattedValue) },
[path]: { [operatorKey]: new ObjectId(formattedValue) },
})
} else {
;(Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo]).forEach(

View File

@@ -1,5 +1,5 @@
import { HydrateClientUser, RenderCustomComponent } from '@payloadcms/ui'
import LinkDefault from 'next/link.js'
import LinkImport from 'next/link.js'
import { isEntityHidden } from 'payload/utilities'
import React, { Fragment } from 'react'
@@ -10,7 +10,7 @@ import { DefaultDashboard } from './Default/index.js'
export { generateDashboardMetadata } from './meta.js'
const Link = LinkDefault
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
export const Dashboard: React.FC<AdminViewProps> = ({
initPageResult,

View File

@@ -1,9 +1,12 @@
import ObjectId from 'bson-objectid'
import ObjectIdImport from 'bson-objectid'
import type { Field, FieldHook } from '../config/types.d.ts'
const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
const generateID: FieldHook = ({ operation, value }) =>
(operation !== 'create' ? value : false) || new ObjectId.default().toHexString()
(operation !== 'create' ? value : false) || new ObjectId().toHexString()
export const baseIDField: Field = {
name: 'id',

View File

@@ -1,4 +1,7 @@
import ObjectId from 'bson-objectid'
import ObjectIdImport from 'bson-objectid'
const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
export const isValidID = (
value: number | string,
@@ -6,7 +9,7 @@ export const isValidID = (
): boolean => {
if (type === 'text' && value) {
if (['object', 'string'].includes(typeof value)) {
const isObjectID = ObjectId.default.isValid(value as string)
const isObjectID = ObjectId.isValid(value as string)
return typeof value === 'string' || isObjectID
}
return false
@@ -15,6 +18,6 @@ export const isValidID = (
if (typeof value === 'number' && !Number.isNaN(value)) return true
if (type === 'ObjectID') {
return ObjectId.default.isValid(String(value))
return ObjectId.isValid(String(value))
}
}

View File

@@ -7,7 +7,8 @@ import { usePreferences } from '../../providers/Preferences/index.js'
import { useNav } from '../Nav/context.js'
import './index.scss'
const AnimateHeight = AnimateHeightImport.default || AnimateHeightImport
const AnimateHeight = (AnimateHeightImport.default ||
AnimateHeightImport) as typeof AnimateHeightImport.default
const baseClass = 'nav-group'

View File

@@ -1,6 +1,6 @@
import type { FormField, FormState, Row } from 'payload/types'
import ObjectIdDefault from 'bson-objectid'
import ObjectIdImport from 'bson-objectid'
import equal from 'deep-equal'
import { deepCopyObject } from 'payload/utilities'
@@ -8,7 +8,8 @@ import type { FieldAction } from './types.d.ts'
import { flattenRows, separateRows } from './rows.js'
const ObjectId = ObjectIdDefault.default
const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
/**
* Reducer which modifies the form field state (all the current data of the fields in the form). When called using dispatch, it will return a new state object.

View File

@@ -7,13 +7,14 @@ import type {
PayloadRequest,
} from 'payload/types'
import ObjectIdDefault from 'bson-objectid'
import ObjectIdImport from 'bson-objectid'
import { fieldAffectsData, fieldHasSubFields, tabHasName } from 'payload/types'
import { getDefaultValue } from 'payload/utilities'
import { iterateFields } from './iterateFields.js'
const ObjectId = ObjectIdDefault.default
const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
export type AddFieldStatePromiseArgs = {
/**

View File

@@ -2,9 +2,9 @@
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"module": "NodeNext",
"target": "esnext",
"moduleResolution": "Node16",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowJs": true,
"checkJs": false,
"esModuleInterop": true,