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

View File

@@ -1,5 +1,5 @@
import { HydrateClientUser, RenderCustomComponent } from '@payloadcms/ui' import { HydrateClientUser, RenderCustomComponent } from '@payloadcms/ui'
import LinkDefault from 'next/link.js' import LinkImport from 'next/link.js'
import { isEntityHidden } from 'payload/utilities' import { isEntityHidden } from 'payload/utilities'
import React, { Fragment } from 'react' import React, { Fragment } from 'react'
@@ -10,7 +10,7 @@ import { DefaultDashboard } from './Default/index.js'
export { generateDashboardMetadata } from './meta.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> = ({ export const Dashboard: React.FC<AdminViewProps> = ({
initPageResult, 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' 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 }) => const generateID: FieldHook = ({ operation, value }) =>
(operation !== 'create' ? value : false) || new ObjectId.default().toHexString() (operation !== 'create' ? value : false) || new ObjectId().toHexString()
export const baseIDField: Field = { export const baseIDField: Field = {
name: 'id', 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 = ( export const isValidID = (
value: number | string, value: number | string,
@@ -6,7 +9,7 @@ export const isValidID = (
): boolean => { ): boolean => {
if (type === 'text' && value) { if (type === 'text' && value) {
if (['object', 'string'].includes(typeof 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 typeof value === 'string' || isObjectID
} }
return false return false
@@ -15,6 +18,6 @@ export const isValidID = (
if (typeof value === 'number' && !Number.isNaN(value)) return true if (typeof value === 'number' && !Number.isNaN(value)) return true
if (type === 'ObjectID') { 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 { useNav } from '../Nav/context.js'
import './index.scss' import './index.scss'
const AnimateHeight = AnimateHeightImport.default || AnimateHeightImport const AnimateHeight = (AnimateHeightImport.default ||
AnimateHeightImport) as typeof AnimateHeightImport.default
const baseClass = 'nav-group' const baseClass = 'nav-group'

View File

@@ -1,6 +1,6 @@
import type { FormField, FormState, Row } from 'payload/types' import type { FormField, FormState, Row } from 'payload/types'
import ObjectIdDefault from 'bson-objectid' import ObjectIdImport from 'bson-objectid'
import equal from 'deep-equal' import equal from 'deep-equal'
import { deepCopyObject } from 'payload/utilities' import { deepCopyObject } from 'payload/utilities'
@@ -8,7 +8,8 @@ import type { FieldAction } from './types.d.ts'
import { flattenRows, separateRows } from './rows.js' 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. * 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, PayloadRequest,
} from 'payload/types' } from 'payload/types'
import ObjectIdDefault from 'bson-objectid' import ObjectIdImport from 'bson-objectid'
import { fieldAffectsData, fieldHasSubFields, tabHasName } from 'payload/types' import { fieldAffectsData, fieldHasSubFields, tabHasName } from 'payload/types'
import { getDefaultValue } from 'payload/utilities' import { getDefaultValue } from 'payload/utilities'
import { iterateFields } from './iterateFields.js' import { iterateFields } from './iterateFields.js'
const ObjectId = ObjectIdDefault.default const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
export type AddFieldStatePromiseArgs = { export type AddFieldStatePromiseArgs = {
/** /**

View File

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