Skip to main content

@sapphire/pieces

Namespaces

Enumerations

Classes

Interfaces

Type Aliases

AsyncPreloadResult

Ƭ AsyncPreloadResult<T>: Promise<Constructor<T> & Record<PropertyKey, unknown>>

Represents the return data from ILoaderStrategy.preload

Type parameters

NameType
Textends Piece

Defined in

projects/pieces/src/lib/strategies/ILoaderStrategy.ts:48


FilterResult

Ƭ FilterResult: ModuleData | null

The result from the filter.

Defined in

projects/pieces/src/lib/strategies/ILoaderStrategy.ts:38


ILoaderResult

Ƭ ILoaderResult<T>: AsyncIterableIterator<ILoaderResultEntry<T>>

Represents the return data from ILoaderStrategy.load.

Type parameters

NameType
Textends Piece

Defined in

projects/pieces/src/lib/strategies/ILoaderStrategy.ts:58


ILoaderResultEntry

Ƭ ILoaderResultEntry<T>: Ctor<ConstructorParameters<typeof Piece>, T>

Represents an entry from ILoaderResult.

Type parameters

NameType
Textends Piece

Defined in

projects/pieces/src/lib/strategies/ILoaderStrategy.ts:53


PieceOf

Ƭ PieceOf<StoreName>: StoreRegistryKey extends never ? Piece<Options, StoreName> : StoreRegistryEntries[StoreName] extends Store<infer PieceType> ? PieceType : Piece<Options, StoreName>

Type utility to get the Piece given its Store's name.

Since

3.10.0

Type parameters

NameType
StoreNameextends StoreRegistryKey

Defined in

projects/pieces/src/lib/structures/StoreRegistry.ts:229


PreloadResult

Ƭ PreloadResult<T>: Awaitable<Constructor<T> & Record<PropertyKey, unknown>>

Represents the return data from ILoaderStrategy.preload

Type parameters

NameType
Textends Piece

Defined in

projects/pieces/src/lib/strategies/ILoaderStrategy.ts:43


StoreOf

Ƭ StoreOf<StoreName>: StoreRegistryKey extends never ? Store<Piece<Options, StoreName>> : StoreRegistryEntries[StoreName]

Type utility to get the Store given its name.

Since

3.10.0

Type parameters

NameType
StoreNameextends StoreRegistryKey

Defined in

projects/pieces/src/lib/structures/StoreRegistry.ts:221


StoreRegistryKey

Ƭ StoreRegistryKey: keyof StoreRegistryEntries

A type utility to get the keys of StoreRegistryEntries.

Since

3.10.0

Defined in

projects/pieces/src/lib/structures/StoreRegistry.ts:194


StoreRegistryValue

Ƭ StoreRegistryValue: StoreRegistryEntries[StoreRegistryKey]

A type utility to get the values of StoreRegistryEntries.

Since

3.10.0

Defined in

projects/pieces/src/lib/structures/StoreRegistry.ts:200

Variables

VirtualPath

Const VirtualPath: "::virtual::"

Defined in

projects/pieces/src/lib/internal/constants.ts:1


container

Const container: Container

The injected variables that will be accessible to any place. To add an extra property, simply add a property with a regular assignment, and it will be available in all places simultaneously.

Example

// Add a reference for the version:
import { container } from '@sapphire/pieces';

container.version = '1.0.0';

// Can be placed anywhere in a TypeScript file, for JavaScript projects,
// you can create an `augments.d.ts` and place the code there.
declare module '@sapphire/pieces' {
interface Container {
version: string;
}
}

// In any piece, core, plugin, or custom:
export class UserCommand extends Command {
public messageRun(message, args) {
// The injected version is available here:
const { version } = this.container;

// ...
}
}

Example

// In a plugin's context, e.g. API:
class Api extends Plugin {
static [postInitialization]() {
const server = new Server(this);
container.server = server;

// ...
}
}

declare module '@sapphire/pieces' {
interface Container {
server: Server;
}
}

// In any piece, even those that aren't routes nor middlewares:
export class UserRoute extends Route {
public [methods.POST](message, args) {
// The injected server is available here:
const { server } = this.container;

// ...
}
}

Defined in

projects/pieces/src/lib/shared/Container.ts:78

Functions

getRootData

getRootData(): RootData

Returns

RootData

Defined in

projects/pieces/src/lib/internal/RootScan.ts:42


parseRootData

parseRootData(): RootData

Retrieves the root data of the project.

This function reads the package.json file in the current working directory and determines the root path and type of the project.

  • If the package.json file is not found or cannot be parsed, it assumes the project is using CommonJS and the current working directory is used as the root

  • If the project type is specified as "commonjs" or "module" in the package.json, it uses the corresponding main or module file path as the root.

    • If there is no main or module then it uses the current working directory as the root, while retaining the matching CommonJS or ESM based on the type
  • If the main or module file path is not specified, it uses the current working directory as the root.

The following table shows how different situations resolve to different root data

fieldsresolved as
type=commonjs && mainCommonJS
type=commonjs && moduleCommonJS
type=module && mainESM
type=module && moduleESM
type=undefined && mainCommonJS
type=undefined && moduleESM
no package.json on cwdCommonJS

Returns

RootData

The root data object containing the root path and the type of the project.

Defined in

projects/pieces/src/lib/internal/RootScan.ts:77