Skip to content

Commit

Permalink
Merge pull request #2948 from dovrosenberg/create-compendium-metadata
Browse files Browse the repository at this point in the history
cleaned up optional parameters for collections
  • Loading branch information
LukeAbby authored Dec 10, 2024
2 parents 6bf0037 + 95cdae9 commit 630b72d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
35 changes: 27 additions & 8 deletions src/foundry/client/data/collections/compendium-collection.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ declare global {
T extends CompendiumCollection.Metadata,
> extends DirectoryCollectionMixin_DocumentCollection<Document.ConfiguredClassForName<T["type"]>, T["name"]> {
/** @param metadata - The compendium metadata, an object provided by game.data */
constructor(metadata: T);
constructor(metadata: CompendiumCollection.ConstructorMetadata<T>);

/** The compendium metadata which defines the compendium content and location */
metadata: T;
Expand Down Expand Up @@ -131,7 +131,7 @@ declare global {
/**
* The visibility configuration of this compendium pack.
* */
get ownership(): foundry.packages.BasePackage.OwnershipRecord;
get ownership(): InexactPartial<foundry.packages.BasePackage.OwnershipRecord>;

/** Is this Compendium pack visible to the current game User? */
get visible(): boolean;
Expand Down Expand Up @@ -315,7 +315,7 @@ declare global {
* Prompt the gamemaster with a dialog to configure ownership of this Compendium pack.
* @returns The configured ownership for the pack
*/
configureOwnershipDialog(): Promise<foundry.packages.BasePackage.OwnershipRecord>;
configureOwnershipDialog(): Promise<InexactPartial<foundry.packages.BasePackage.OwnershipRecord>>;

/**
* Activate the Socket event listeners used to receive responses to compendium management events.
Expand All @@ -331,7 +331,8 @@ declare global {
* default `{}`
*/
static createCompendium<T extends CompendiumCollection.Metadata>(
metadata: T,
this: abstract new (arg0: never, ...args: never[]) => CompendiumCollection<T>,
metadata: CompendiumCollection.CreateCompendiumMetadata<NoInfer<T>>,
options?: Document.OnCreateOptions<T["type"]>,
): Promise<CompendiumCollection<T>>;

Expand Down Expand Up @@ -416,17 +417,35 @@ declare global {
type Any = CompendiumCollection<any>;

interface Configuration {
ownership: foundry.packages.BasePackage.OwnershipRecord;
ownership: InexactPartial<foundry.packages.BasePackage.OwnershipRecord>;
locked: boolean;
}

// The type that's passed to `createCompendium`.
interface CreateCompendiumMetadata<T extends CompendiumCollection.Metadata> {
type: T["type"];
label: string;
name?: string | null | undefined;
}

// The type that's passed to `new CompendiumCollection(...)`
type ConstructorMetadata<T extends CompendiumCollection.Metadata> = T & {
index: IndexTypeForMetadata<T>;
folders: Folder[];
}

// The type that appears in `compendium.metadata` after initialization.
interface Metadata {
type: foundry.CONST.COMPENDIUM_DOCUMENT_TYPES;
name: string;
label: string;
name: string;

flags: Record<string, never>; // created by the server, but always empty and no way to change it in a way that is s
ownership: InexactPartial<foundry.packages.BasePackage.OwnershipRecord>;
path: string;
private: boolean;
package: string;
system?: string;
system: string;

/** Added by PackageCompendiumPacks#initialize */
id: string;
packageType: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,42 @@ import { expectTypeOf } from "vitest";
import type { DeepPartial } from "../../../../../src/types/utils.d.mts";
import type Document from "../../../../../src/foundry/common/abstract/document.d.mts";

const metadata = {
const compendiumCollection = await CompendiumCollection.createCompendium({
type: "JournalEntry" as const,
label: "Important Plotholes",
id: "world.plotholes",
name: "plotholes",
package: "some-package",
path: "path/to/file",
private: false,
});

expectTypeOf(compendiumCollection).toEqualTypeOf<CompendiumCollection<CompendiumCollection.Metadata>>();
expectTypeOf(compendiumCollection.metadata).toEqualTypeOf<CompendiumCollection.Metadata>();

const metadata: CompendiumCollection.Metadata = {
name: "plotholes",
type: "JournalEntry" as const,
label: "Important Plotholes",
flags: {},
id: "plotholes",
system: "core",
package: "plotholes",
packageName: "plotholes",
packageType: "module",
path: "path",
ownership: {
PLAYER: "OWNER",
}
};

const constructorMetadata: CompendiumCollection.ConstructorMetadata<typeof metadata> = {
...(metadata as CompendiumCollection.Metadata),
index: new foundry.utils.Collection(),
folders: [],
};

const compendiumCollection = new CompendiumCollection(metadata);
const compendium2 = new CompendiumCollection(constructorMetadata);

expectTypeOf(compendium2).toEqualTypeOf<CompendiumCollection<CompendiumCollection.Metadata>>();
expectTypeOf(compendium2.metadata).toEqualTypeOf<CompendiumCollection.Metadata>();

expectTypeOf(compendiumCollection.get("", { strict: true })).toEqualTypeOf<Document.Stored<JournalEntry>>();
// expectTypeOf(compendiumCollection.toJSON()).toEqualTypeOf<
// Array<Document.Stored<foundry.documents.BaseJournalEntry>["_source"]>
Expand All @@ -36,13 +61,10 @@ expectTypeOf((await compendiumCollection.getIndex()).get("some id", { strict: tr
expectTypeOf(compendiumCollection.documentClass).toEqualTypeOf<typeof JournalEntry>();

const itemCollection = new CompendiumCollection({
type: "Item",
label: "Important items",
id: "world.items",
name: "items",
package: "other-package",
path: "path/to/items",
private: false,
...metadata,
type: "Item" as const,
index: new foundry.utils.Collection(),
folders: [],
});
expectTypeOf((await itemCollection.getIndex()).get("some id", { strict: true })).toEqualTypeOf<
{ _id: string; uuid: string } & DeepPartial<foundry.documents.BaseItem["_source"]>
Expand Down

0 comments on commit 630b72d

Please sign in to comment.