fix: type augmentation of RequestContext (#9035)

### What?

Makes this to actually work
```ts
import type { RequestContext as OriginalRequestContext } from 'payload'

declare module 'payload' {
  // Create a new interface that merges your additional fields with the original one
  export interface RequestContext extends OriginalRequestContext {
    myObject?: string
    // ...
  }
}
```
<img width="502" alt="image"
src="https://github.com/user-attachments/assets/38570d3c-e8a8-48aa-a57d-6d11e79394f5">


### Why?
This is described in our docs
https://payloadcms.com/docs/beta/hooks/context#typescript therefore it
should work.

### How?
In order to get the declaration work, we need to reuse the type from the
root file `payload/src/index.js`. Additionally, removes `RequestContext`
type duplication in both `payload/src/types/index.js` and
`payload/src/index.js`.

Fixes https://github.com/payloadcms/payload/issues/8851
This commit is contained in:
Sasha
2024-11-05 23:14:04 +02:00
committed by GitHub
parent 2eeed4a8ae
commit f52b7c45c0
32 changed files with 76 additions and 69 deletions

View File

@@ -12,7 +12,8 @@ import type {
Validate,
} from '../fields/config/types.js'
import type { SanitizedGlobalConfig } from '../globals/config/types.js'
import type { JsonObject, Payload, PayloadRequest, RequestContext } from '../types/index.js'
import type { RequestContext } from '../index.js'
import type { JsonObject, Payload, PayloadRequest } from '../types/index.js'
import type { RichTextFieldClientProps } from './fields/RichText.js'
import type { CreateMappedComponent } from './types.js'

View File

@@ -35,13 +35,13 @@ import type { Field, JoinField, RelationshipField, UploadField } from '../../fie
import type {
CollectionSlug,
JsonObject,
RequestContext,
TypedAuthOperations,
TypedCollection,
TypedCollectionSelect,
} from '../../index.js'
import type {
PayloadRequest,
RequestContext,
SelectType,
Sort,
TransformCollectionWithSelect,

View File

@@ -1,5 +1,5 @@
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, RequestContext, Where } from '../../../types/index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, Where } from '../../../types/index.js'
import { APIError } from '../../../errors/index.js'
import { createLocalReq } from '../../../utilities/createLocalReq.js'

View File

@@ -1,8 +1,7 @@
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type {
Document,
PayloadRequest,
RequestContext,
SelectType,
TransformCollectionWithSelect,
} from '../../../types/index.js'

View File

@@ -1,8 +1,7 @@
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type {
Document,
PayloadRequest,
RequestContext,
SelectType,
TransformCollectionWithSelect,
Where,

View File

@@ -1,13 +1,12 @@
import type { CollectionSlug, TypedLocale } from '../../..//index.js'
import type { Payload } from '../../../index.js'
import type { Payload, RequestContext } from '../../../index.js'
import type {
Document,
PayloadRequest,
RequestContext,
SelectType,
TransformCollectionWithSelect,
} from '../../../types/index.js'
import type { DataFromCollectionSlug, SelectFromCollectionSlug } from '../../config/types.js'
import type { SelectFromCollectionSlug } from '../../config/types.js'
import { APIError } from '../../../errors/index.js'
import { createLocalReq } from '../../../utilities/createLocalReq.js'

View File

@@ -1,9 +1,14 @@
import type { PaginatedDocs } from '../../../database/types.js'
import type { CollectionSlug, JoinQuery, Payload, TypedLocale } from '../../../index.js'
import type {
CollectionSlug,
JoinQuery,
Payload,
RequestContext,
TypedLocale,
} from '../../../index.js'
import type {
Document,
PayloadRequest,
RequestContext,
SelectType,
Sort,
TransformCollectionWithSelect,

View File

@@ -1,10 +1,16 @@
/* eslint-disable no-restricted-exports */
import type { CollectionSlug, JoinQuery, Payload, SelectType, TypedLocale } from '../../../index.js'
import type {
CollectionSlug,
JoinQuery,
Payload,
RequestContext,
SelectType,
TypedLocale,
} from '../../../index.js'
import type {
ApplyDisableErrors,
Document,
PayloadRequest,
RequestContext,
TransformCollectionWithSelect,
} from '../../../types/index.js'
import type { SelectFromCollectionSlug } from '../../config/types.js'

View File

@@ -1,5 +1,5 @@
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, RequestContext, SelectType } from '../../../types/index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, SelectType } from '../../../types/index.js'
import type { TypeWithVersion } from '../../../versions/types.js'
import type { DataFromCollectionSlug } from '../../config/types.js'

View File

@@ -1,13 +1,6 @@
import type { PaginatedDocs } from '../../../database/types.js'
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type {
Document,
PayloadRequest,
RequestContext,
SelectType,
Sort,
Where,
} from '../../../types/index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, SelectType, Sort, Where } from '../../../types/index.js'
import type { TypeWithVersion } from '../../../versions/types.js'
import type { DataFromCollectionSlug } from '../../config/types.js'

View File

@@ -1,5 +1,5 @@
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, RequestContext, SelectType } from '../../../types/index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type { Document, PayloadRequest, SelectType } from '../../../types/index.js'
import type { DataFromCollectionSlug } from '../../config/types.js'
import { APIError } from '../../../errors/index.js'

View File

@@ -1,10 +1,9 @@
import type { DeepPartial } from 'ts-essentials'
import type { CollectionSlug, Payload, TypedLocale } from '../../../index.js'
import type { CollectionSlug, Payload, RequestContext, TypedLocale } from '../../../index.js'
import type {
Document,
PayloadRequest,
RequestContext,
SelectType,
TransformCollectionWithSelect,
Where,

View File

@@ -121,11 +121,12 @@ import type {
JSONFieldValidation,
PointFieldValidation,
RadioFieldValidation,
RequestContext,
Sort,
TextareaFieldValidation,
} from '../../index.js'
import type { DocumentPreferences } from '../../preferences/types.js'
import type { Operation, PayloadRequest, RequestContext, Where } from '../../types/index.js'
import type { Operation, PayloadRequest, Where } from '../../types/index.js'
import type {
NumberFieldManyValidation,
NumberFieldSingleValidation,

View File

@@ -1,6 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import { deepCopyObjectSimple } from '../../../utilities/deepCopyObject.js'
import { traverseFields } from './traverseFields.js'

View File

@@ -1,7 +1,8 @@
import type { RichTextAdapter } from '../../../admin/RichText.js'
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { MissingEditorProp } from '../../../errors/index.js'

View File

@@ -1,6 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { promise } from './promise.js'

View File

@@ -1,11 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type {
JsonObject,
PayloadRequest,
RequestContext,
SelectType,
} from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest, SelectType } from '../../../types/index.js'
import { deepCopyObjectSimple } from '../../../utilities/deepCopyObject.js'
import { getSelectMode } from '../../../utilities/getSelectMode.js'

View File

@@ -1,13 +1,8 @@
import type { RichTextAdapter } from '../../../admin/RichText.js'
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type {
JsonObject,
PayloadRequest,
RequestContext,
SelectMode,
SelectType,
} from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest, SelectMode, SelectType } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { MissingEditorProp } from '../../../errors/index.js'

View File

@@ -1,12 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type {
JsonObject,
PayloadRequest,
RequestContext,
SelectMode,
SelectType,
} from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest, SelectMode, SelectType } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { promise } from './promise.js'

View File

@@ -1,6 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, Operation, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, Operation, PayloadRequest } from '../../../types/index.js'
import { ValidationError } from '../../../errors/index.js'
import { deepCopyObjectSimple } from '../../../utilities/deepCopyObject.js'

View File

@@ -1,7 +1,8 @@
import type { RichTextAdapter } from '../../../admin/RichText.js'
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, Operation, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, Operation, PayloadRequest } from '../../../types/index.js'
import type { Field, FieldHookArgs, TabAsField, ValidateOptions } from '../../config/types.js'
import { MissingEditorProp } from '../../../errors/index.js'

View File

@@ -1,6 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, Operation, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, Operation, PayloadRequest } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { promise } from './promise.js'

View File

@@ -1,5 +1,6 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import { deepCopyObjectSimple } from '../../../utilities/deepCopyObject.js'
import { traverseFields } from './traverseFields.js'

View File

@@ -1,5 +1,6 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import type { Field, FieldHookArgs, TabAsField } from '../../config/types.js'
import { fieldAffectsData, tabHasName } from '../../config/types.js'

View File

@@ -1,5 +1,6 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { promise } from './promise.js'

View File

@@ -1,6 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import { deepCopyObjectSimple } from '../../../utilities/deepCopyObject.js'
import { traverseFields } from './traverseFields.js'

View File

@@ -1,7 +1,8 @@
import type { RichTextAdapter } from '../../../admin/RichText.js'
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, JsonValue, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, JsonValue, PayloadRequest } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { MissingEditorProp } from '../../../errors/index.js'

View File

@@ -1,6 +1,7 @@
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
import type { JsonObject, PayloadRequest, RequestContext } from '../../../types/index.js'
import type { RequestContext } from '../../../index.js'
import type { JsonObject, PayloadRequest } from '../../../types/index.js'
import type { Field, TabAsField } from '../../config/types.js'
import { promise } from './promise.js'

View File

@@ -19,8 +19,8 @@ import type {
} from '../../config/types.js'
import type { DBIdentifierName } from '../../database/types.js'
import type { Field } from '../../fields/config/types.js'
import type { GlobalSlug, TypedGlobal, TypedGlobalSelect } from '../../index.js'
import type { PayloadRequest, RequestContext, Where } from '../../types/index.js'
import type { GlobalSlug, RequestContext, TypedGlobal, TypedGlobalSelect } from '../../index.js'
import type { PayloadRequest, Where } from '../../types/index.js'
import type { IncomingGlobalVersions, SanitizedGlobalVersions } from '../../versions/types.js'
export type DataFromGlobalSlug<TSlug extends GlobalSlug> = TypedGlobal[TSlug]

View File

@@ -12,6 +12,7 @@ import type {
CollectionSlug,
DataFromGlobalSlug,
GlobalSlug,
RequestContext,
TypedLocale,
TypedUser,
} from '../index.js'
@@ -94,10 +95,6 @@ export type PayloadRequest = CustomPayloadRequestProperties &
PayloadRequestData &
Required<Pick<Request, 'headers'>>
export interface RequestContext {
[key: string]: unknown
}
export type Operator = (typeof validOperators)[number]
// Makes it so things like passing new Date() will error

View File

@@ -54,6 +54,7 @@ describe('_Community Tests', () => {
data: {
text: 'LOCAL API EXAMPLE',
},
context: {},
})
expect(newPost.text).toEqual('LOCAL API EXAMPLE')

9
test/_community/types.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
import type { RequestContext as OriginalRequestContext } from 'payload'
declare module 'payload' {
// Create a new interface that merges your additional fields with the original one
export interface RequestContext extends OriginalRequestContext {
myObject?: string
// ...
}
}