Skip to content

Commit

Permalink
Merge pull request #927 from League-of-Foundry-Developers/0.8.x/bette…
Browse files Browse the repository at this point in the history
…r-way-to-extract-constructor-data

Ad a more readable way to extract constructor data from DocumentData.
  • Loading branch information
kmoschcau authored Jul 8, 2021
2 parents 48e5ce3 + 24700a0 commit cb30941
Show file tree
Hide file tree
Showing 27 changed files with 153 additions and 98 deletions.
46 changes: 28 additions & 18 deletions src/foundry/common/abstract/document.mjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ConfiguredDocumentClass,
DocumentConstructor,
ToObjectFalseType,
DocumentType
DocumentType,
ConstructorDataType
} from '../../../types/helperTypes';
import EmbeddedCollection from './embedded-collection.mjs';

Expand All @@ -25,7 +26,7 @@ declare abstract class Document<
* @param data - Initial data provided to construct the Document
* @param context - Additional parameters which define Document context
*/
constructor(data?: Parameters<ConcreteDocumentData['_initializeSource']>[0], context?: Context<Parent>);
constructor(data?: ConstructorDataType<ConcreteDocumentData>, context?: Context<Parent>);

/**
* An immutable reverse-reference to the parent Document to which this embedded Document belongs.
Expand Down Expand Up @@ -142,9 +143,11 @@ declare abstract class Document<
* @returns The cloned Document instance
*/
clone(
data: DeepPartial<Parameters<ConcreteDocumentData['_initializeSource']>[0]>,
{ save, keepId }: { save?: boolean; keepId?: boolean }
): this | Promise<this>;
data?: DeepPartial<
ConstructorDataType<ConcreteDocumentData> | (ConstructorDataType<ConcreteDocumentData> & Record<string, unknown>)
>,
{ save, keepId }?: { save?: boolean; keepId?: boolean }
): this | Promise<this | undefined>;

/**
* Get the permission level that a specific User has over this Document, a value in CONST.ENTITY_PERMISSIONS.
Expand Down Expand Up @@ -175,7 +178,7 @@ declare abstract class Document<
* (default: `{}`)
* @returns Does the User have permission?
*/
canUserModify(user: BaseUser, action: string, data?: object): boolean;
canUserModify(user: BaseUser, action: 'create' | 'update' | 'delete', data?: object): boolean;

/**
* Create multiple Documents using provided input data.
Expand Down Expand Up @@ -214,7 +217,10 @@ declare abstract class Document<
*/
static createDocuments<T extends DocumentConstructor>(
this: T,
data?: Array<Parameters<InstanceType<T>['data']['_initializeSource']>[0] & Record<string, unknown>>,
data?: Array<
| ConstructorDataType<InstanceType<T>['data']>
| (ConstructorDataType<InstanceType<T>['data']> & Record<string, unknown>)
>,
context?: DocumentModificationContext
): Promise<InstanceType<ConfiguredDocumentClass<T>>[]>;

Expand Down Expand Up @@ -256,10 +262,10 @@ declare abstract class Document<
static updateDocuments<T extends DocumentConstructor>(
this: T,
updates?: Array<
DeepPartial<Parameters<InstanceType<T>['data']['_initializeSource']>[0]> & { _id: string } & Record<
string,
unknown
>
DeepPartial<
| ConstructorDataType<InstanceType<T>['data']>
| (ConstructorDataType<InstanceType<T>['data']> & Record<string, unknown>)
>
>,
context?: DocumentModificationContext
): Promise<InstanceType<ConfiguredDocumentClass<T>>[]>;
Expand Down Expand Up @@ -338,7 +344,9 @@ declare abstract class Document<
*/
static create<T extends DocumentConstructor>(
this: T,
data: Parameters<InstanceType<T>['data']['_initializeSource']>[0] & Record<string, unknown>,
data:
| ConstructorDataType<InstanceType<T>['data']>
| (ConstructorDataType<InstanceType<T>['data']> & Record<string, unknown>),
context?: DocumentModificationContext
): Promise<InstanceType<ConfiguredDocumentClass<T>> | undefined>;

Expand All @@ -354,7 +362,9 @@ declare abstract class Document<
* @remarks If no document has actually been updated, the returned {@link Promise} resolves to `undefined`.
*/
update(
data?: DeepPartial<Parameters<ConcreteDocumentData['_initializeSource']>[0]> & Record<string, unknown>,
data?: DeepPartial<
ConstructorDataType<ConcreteDocumentData> | (ConstructorDataType<ConcreteDocumentData> & Record<string, unknown>)
>,
context?: DocumentModificationContext
): Promise<this | undefined>;

Expand Down Expand Up @@ -484,7 +494,7 @@ declare abstract class Document<
* @param user - The User requesting the document creation
*/
protected _preCreate(
data: Parameters<ConcreteDocumentData['_initializeSource']>[0],
data: ConstructorDataType<ConcreteDocumentData>,
options: DocumentModificationOptions,
user: BaseUser
): Promise<void>;
Expand All @@ -497,7 +507,7 @@ declare abstract class Document<
* @param user - The User requesting the document update
*/
protected _preUpdate(
changed: DeepPartial<Parameters<ConcreteDocumentData['_initializeSource']>[0]> & Record<string, unknown>,
changed: DeepPartial<ConstructorDataType<ConcreteDocumentData>>,
options: DocumentModificationOptions,
user: BaseUser
): Promise<void>;
Expand Down Expand Up @@ -707,9 +717,9 @@ export interface Metadata<ConcreteDocument extends Document<any, any>> {
embedded: Record<string, ConstructorOf<Document<any, any>>>;
hasSystemData: boolean;
permissions: {
create: string | ((user: BaseUser, doc: ConcreteDocument, data?: any) => boolean);
update: string | ((user: BaseUser, doc: ConcreteDocument, data?: any) => boolean);
delete: string | ((user: BaseUser, doc: ConcreteDocument, data?: any) => boolean);
create: string | ((user: BaseUser, doc: ConcreteDocument, data?: object) => boolean); // data isn't actually ever passed in on the client side
update: string | ((user: BaseUser, doc: ConcreteDocument, data?: object) => boolean); // data isn't actually ever passed in on the client side
delete: string | ((user: BaseUser, doc: ConcreteDocument, data?: object) => boolean); // data isn't actually ever passed in on the client side
};
pack: any;
}
Expand Down
10 changes: 8 additions & 2 deletions src/foundry/common/data/data.mjs/combatData.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { ConfiguredDocumentClass, FieldReturnType, PropertiesToSource } from '../../../../types/helperTypes';
import {
ConfiguredDocumentClass,
ConstructorDataType,
FieldReturnType,
PropertiesToSource
} from '../../../../types/helperTypes';
import EmbeddedCollection from '../../abstract/embedded-collection.mjs';
import { DocumentData } from '../../abstract/module.mjs';
import * as documents from '../../documents.mjs';
import * as fields from '../fields.mjs';
import { CombatantData } from './combatantData';

interface CombatDataSchema extends DocumentSchema {
_id: typeof fields.DOCUMENT_ID;
Expand Down Expand Up @@ -64,7 +70,7 @@ interface CombatDataConstructorData {
scene?: string | null;

/** A Collection of Combatant embedded Documents */
combatants?: Parameters<foundry.data.CombatantData['_initializeSource']>[0][] | null;
combatants?: ConstructorDataType<CombatantData>[] | null;

/**
* Is the Combat encounter currently active?
Expand Down
4 changes: 2 additions & 2 deletions src/foundry/common/documents.mjs/baseActiveEffect.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocumentMetadata, DocumentModificationOptions } from '../abstract/document.mjs';
import { ConfiguredDocumentClass } from '../../../types/helperTypes';
import { ConfiguredDocumentClass, ConstructorDataType } from '../../../types/helperTypes';
import { Document } from '../abstract/module.mjs';
import * as data from '../data/data.mjs';
import { BaseActor } from './baseActor';
Expand All @@ -26,7 +26,7 @@ export declare class BaseActiveEffect extends Document<
>;

protected _preCreate(
data: Parameters<data.ActiveEffectData['_initializeSource']>[0],
data: ConstructorDataType<data.ActiveEffectData>,
options: DocumentModificationOptions,
user: BaseUser
): Promise<void>;
Expand Down
5 changes: 3 additions & 2 deletions src/foundry/common/documents.mjs/baseActor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseActiveEffect } from './baseActiveEffect';
import { BaseItem } from './baseItem';
import * as data from '../data/data.mjs';
import { BaseUser } from './baseUser';
import { ConstructorDataType } from '../../../types/helperTypes';

//TODO Add Token as parent class once it is available
/**
Expand Down Expand Up @@ -56,7 +57,7 @@ export declare class BaseActor extends Document<data.ActorData, Document<any, an
* @param user - The User requesting the document creation
*/
protected _preCreate(
data: Parameters<data.ActorData['_initializeSource']>[0],
data: ConstructorDataType<data.ActorData>,
options: DocumentModificationOptions,
user: BaseUser
): Promise<void>;
Expand All @@ -69,7 +70,7 @@ export declare class BaseActor extends Document<data.ActorData, Document<any, an
* @param user - The User requesting the document update
*/
protected _preUpdate(
changed: DeepPartial<Parameters<data.ActorData['_initializeSource']>[0]> & Record<string, unknown>,
changed: DeepPartial<ConstructorDataType<data.ActorData>>,
options: DocumentModificationOptions,
user: BaseUser
): Promise<void>;
Expand Down
5 changes: 2 additions & 3 deletions src/foundry/common/documents.mjs/baseChatMessage.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DocumentMetadata } from '../abstract/document.mjs';
import { Document } from '../abstract/module.mjs';
import { ChatMessageData } from '../data/data.mjs';
import { BaseUser } from './baseUser';
import * as data from '../data/data.mjs';

Expand All @@ -19,7 +18,7 @@ export declare class BaseChatMessage extends Document<data.ChatMessageData, null
isPrimary: true;
permissions: {
create: (user: BaseUser, doc: BaseChatMessage) => boolean;
update: (user: BaseUser, doc: BaseChatMessage, data: ChatMessageData['_source']) => boolean;
update: (user: BaseUser, doc: BaseChatMessage, data?: object) => boolean;
delete: (user: BaseUser, doc: BaseChatMessage) => boolean;
};
}
Expand All @@ -33,7 +32,7 @@ export declare class BaseChatMessage extends Document<data.ChatMessageData, null
/**
* Is a user able to update an existing chat message?
*/
protected static _canUpdate(user: BaseUser, doc: BaseChatMessage, data: ChatMessageData['_source']): boolean;
protected static _canUpdate(user: BaseUser, doc: BaseChatMessage, data?: object): boolean;

/**
* Is a user able to delete an existing chat message?
Expand Down
7 changes: 2 additions & 5 deletions src/foundry/common/documents.mjs/baseCombat.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConstructorDataType } from '../../../types/helperTypes';
import { DocumentMetadata } from '../abstract/document.mjs';
import { Document } from '../abstract/module.mjs';
import { data } from '../module.mjs';
Expand Down Expand Up @@ -33,9 +34,5 @@ export declare class BaseCombat extends Document<data.CombatData> {
* Is a user able to update an existing Combat?
* @param doc - (unused)
*/
protected static _canUpdate(
user: BaseUser,
doc: unknown,
data: Parameters<data.CombatantData['_initializeSource']>[0]
): boolean;
protected static _canUpdate(user: BaseUser, doc: unknown, data?: ConstructorDataType<data.CombatData>): boolean;
}
2 changes: 1 addition & 1 deletion src/foundry/common/documents.mjs/baseCombatant.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export declare class BaseCombatant extends Document<
* Is a user able to update an existing Combatant?
* @remarks doc seems unused
*/
protected static _canUpdate(user: BaseUser, doc: unknown, data: data.CombatantData): boolean;
protected static _canUpdate(user: BaseUser, doc: unknown, data?: object): boolean;
}
4 changes: 2 additions & 2 deletions src/foundry/common/documents.mjs/baseDrawing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export declare class BaseDrawing extends Document<any, any> {
isEmbedded: true;
permissions: {
create: 'DRAWING_CREATE';
update: (user: BaseUser, doc: any, data: any) => boolean;
delete: (user: BaseUser, doc: any, data: any) => boolean;
update: (user: BaseUser, doc: any, data?: object) => boolean;
delete: (user: BaseUser, doc: any, data?: object) => boolean;
};
}
>;
Expand Down
4 changes: 2 additions & 2 deletions src/foundry/common/documents.mjs/baseFogExploration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export declare class BaseFogExploration extends Document<any, any> {
isPrimary: true;
permissions: {
create: 'PLAYER';
update: (user: BaseUser, doc: any, data: any) => boolean;
delete: (user: BaseUser, doc: any, data: any) => boolean;
update: (user: BaseUser, doc: any, data?: object) => boolean;
delete: (user: BaseUser, doc: any, data?: object) => boolean;
};
}
>;
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/common/documents.mjs/baseItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export declare class BaseItem extends Document<data.ItemData, InstanceType<Confi
*/
get effects(): this['data']['effects'];

canUserModify(user: BaseUser, action: string, data?: object): boolean;
canUserModify(user: BaseUser, action: 'create' | 'update' | 'delete', data?: object): boolean;

testUserPermission(
user: BaseUser,
Expand Down
7 changes: 4 additions & 3 deletions src/foundry/common/documents.mjs/baseMacro.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as data from '../data/data.mjs';
import { Document } from '../abstract/module.mjs';
import { DocumentMetadata, DocumentModificationOptions } from '../abstract/document.mjs';
import { BaseUser } from './baseUser';
import { ConstructorDataType } from '../../../types/helperTypes';

/**
* The base Macro model definition which defines common behavior of an Macro document between both client and server.
Expand All @@ -19,22 +20,22 @@ export declare class BaseMacro extends Document<data.MacroData> {
types: ['script', 'chat']; // TODO: Automatically infer from CONST.MACRO_TYPES
permissions: {
create: 'PLAYER';
update: (user: BaseUser, doc: BaseMacro, data: any) => boolean;
update: (user: BaseUser, doc: BaseMacro, data?: object) => boolean;
delete: (user: BaseUser, doc: BaseMacro) => boolean;
};
}
>;

protected _preCreate(
data: Parameters<data.MacroData['_initializeSource']>[0],
data: ConstructorDataType<data.MacroData>,
options: DocumentModificationOptions,
user: BaseUser
): Promise<void>;

/**
* Is a user able to update an existing Macro document?
*/
protected static _canUpdate(user: BaseUser, doc: BaseMacro, data: unknown): boolean;
protected static _canUpdate(user: BaseUser, doc: BaseMacro, data?: object): boolean;

/**
* Is a user able to delete an existing Macro document?
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/common/documents.mjs/baseTableResult.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class BaseTableResult extends Document<any, any> {
label: 'DOCUMENT.TableResult';
types: typeof CONST.TABLE_RESULT_TYPES;
permissions: {
update: (user: BaseUser, doc: any, data: any) => boolean;
update: (user: BaseUser, doc: any, data?: object) => boolean;
};
}
>;
Expand Down
2 changes: 1 addition & 1 deletion src/foundry/common/documents.mjs/baseWall.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export declare class BaseWall extends Document<any, InstanceType<ConfiguredDocum
label: 'DOCUMENT.Wall';
isEmbedded: true;
permissions: {
update: (user: BaseUser, doc: any, data: any) => boolean;
update: (user: BaseUser, doc: any, data?: object) => boolean;
};
}
>;
Expand Down
10 changes: 5 additions & 5 deletions src/foundry/common/packages.mjs/packageData.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldReturnType, PropertiesToSource } from '../../../types/helperTypes';
import { ConstructorDataType, FieldReturnType, PropertiesToSource } from '../../../types/helperTypes';
import { DocumentData } from '../abstract/module.mjs';
import * as fields from '../data/fields.mjs';
import { PackageAuthorData } from './packageAuthorData';
Expand Down Expand Up @@ -187,7 +187,7 @@ export interface PackageDataConstructorData {
* An array of author objects who are co-authors of this package. Preferred to the singular author field.
* @defaultValue `[]`
*/
authors?: Parameters<PackageAuthorData['_initializeSource']>[0][] | null;
authors?: ConstructorDataType<PackageAuthorData>[] | null;

/** A web url where more details about the package may be found */
url?: string | null;
Expand Down Expand Up @@ -241,13 +241,13 @@ export interface PackageDataConstructorData {
* An array of language data objects which are included by this package
* @defaultValue `[]`
*/
languages?: Parameters<PackageLanguageData['_initializeSource']>[0][] | null;
languages?: ConstructorDataType<PackageLanguageData>[] | null;

/**
* An array of compendium packs which are included by this package
* @defaultValue `[]`
*/
packs?: Parameters<PackageCompendiumData['_initializeSource']>[0][] | null;
packs?: ConstructorDataType<PackageCompendiumData>[] | null;

/**
* An array of game system names which this module supports
Expand All @@ -259,7 +259,7 @@ export interface PackageDataConstructorData {
* An array of dependency objects which define required dependencies for using this package
* @defaultValue `[]`
*/
dependencies?: Parameters<PackageDependencyData['_initializeSource']>[0][] | null;
dependencies?: ConstructorDataType<PackageDependencyData>[] | null;

/**
* Whether to require a package-specific socket namespace for this package
Expand Down
9 changes: 5 additions & 4 deletions src/foundry/foundry.js/canvasDocumentMixin.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConstructorDataType } from '../../types/helperTypes';
import { ContextType, DocumentModificationOptions } from '../common/abstract/document.mjs';
import { ClientDocumentMixin } from './clientDocumentMixin';

Expand All @@ -18,23 +19,23 @@ type CanvasDocumentConstructor<T extends ConstructorOf<foundry.abstract.Document
};

declare class CanvasDocumentMixin<T extends foundry.abstract.Document<any, any>> extends ClientDocumentMixin<T> {
constructor(data?: Parameters<T['data']['_initializeSource']>[0], context?: ContextType<T>);
constructor(data?: ConstructorDataType<T['data']>, context?: ContextType<T>);

/**
* A reference to the PlaceableObject instance which represents this Embedded Document.
* @defaultValue `null`
*/
protected _object: PlaceableObject | null; // TODO: Replace mit InstanceType<ObjectClass<T>> | null once the circular reference problem has been solved
protected _object: PlaceableObject | null; // TODO: Replace with InstanceType<ObjectClass<T>> | null once the circular reference problem has been solved

/**
* A lazily constructed PlaceableObject instance which can represent this Document on the game canvas.
*/
get object(): PlaceableObject | null; // TODO: Replace mit InstanceType<ObjectClass<T>> | null once the circular reference problem has been solved
get object(): PlaceableObject | null; // TODO: Replace with InstanceType<ObjectClass<T>> | null once the circular reference problem has been solved

/**
* A reference to the CanvasLayer which contains Document objects of this type.
*/
get layer(): PlaceablesLayer | null; // TODO: Replace mit InstanceType<LayerClass<T>> | null once the circular reference problem has been solved
get layer(): PlaceablesLayer | null; // TODO: Replace with InstanceType<LayerClass<T>> | null once the circular reference problem has been solved

/**
* An indicator for whether this document is currently rendered on the game canvas.
Expand Down
Loading

0 comments on commit cb30941

Please sign in to comment.