Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type augmentation of RequestContext #9035

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/payload/src/admin/RichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/collections/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/payload/src/collections/operations/local/count.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 1 addition & 2 deletions packages/payload/src/collections/operations/local/create.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 1 addition & 2 deletions packages/payload/src/collections/operations/local/delete.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
9 changes: 7 additions & 2 deletions packages/payload/src/collections/operations/local/find.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 8 additions & 2 deletions packages/payload/src/collections/operations/local/findByID.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 1 addition & 2 deletions packages/payload/src/collections/operations/local/update.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/afterChange/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/afterChange/promise.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
8 changes: 2 additions & 6 deletions packages/payload/src/fields/hooks/afterRead/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
9 changes: 2 additions & 7 deletions packages/payload/src/fields/hooks/afterRead/promise.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
9 changes: 2 additions & 7 deletions packages/payload/src/fields/hooks/afterRead/traverseFields.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/beforeChange/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/beforeChange/promise.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/beforeDuplicate/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/beforeDuplicate/promise.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/beforeValidate/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/payload/src/fields/hooks/beforeValidate/promise.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions packages/payload/src/globals/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 1 addition & 4 deletions packages/payload/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
CollectionSlug,
DataFromGlobalSlug,
GlobalSlug,
RequestContext,
TypedLocale,
TypedUser,
} from '../index.js'
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/_community/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('_Community Tests', () => {
data: {
text: 'LOCAL API EXAMPLE',
},
context: {},
})

expect(newPost.text).toEqual('LOCAL API EXAMPLE')
Expand Down
9 changes: 9 additions & 0 deletions test/_community/types.d.ts
Original file line number Diff line number Diff line change
@@ -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
// ...
}
}
Loading