@sapphire/pieces
Namespaces
Enumerations
Classes
- AliasPiece
- AliasStore
- LoaderError
- LoaderStrategy
- MissingExportsError
- Piece
- PieceLocation
- Store
- StoreRegistry
Interfaces
- AliasPieceJSON
- AliasPieceOptions
- Container
- HydratedModuleData
- ILoaderStrategy
- LoaderPieceContext
- ModuleData
- PieceContext
- PieceJSON
- PieceLocationJSON
- PieceOptions
- RootData
- StoreLogger
- StoreManagerManuallyRegisteredPiece
- StoreManuallyRegisteredPiece
- StoreOptions
- StoreRegistryEntries
Type Aliases
AsyncPreloadResult
Ƭ AsyncPreloadResult<T
>: Promise
<Constructor
<T
> & Record
<PropertyKey
, unknown
>>
Represents the return data from ILoaderStrategy.preload
Type parameters
Name | Type |
---|---|
T | extends 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
Name | Type |
---|---|
T | extends 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
Name | Type |
---|---|
T | extends 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
Name | Type |
---|---|
StoreName | extends 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
Name | Type |
---|---|
T | extends 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
Name | Type |
---|---|
StoreName | extends 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
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 thepackage.json
, it uses the correspondingmain
ormodule
file path as the root.- If there is no
main
ormodule
then it uses the current working directory as the root, while retaining the matchingCommonJS
orESM
based on thetype
- If there is no
-
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
fields | resolved as |
---|---|
type=commonjs && main | CommonJS |
type=commonjs && module | CommonJS |
type=module && main | ESM |
type=module && module | ESM |
type=undefined && main | CommonJS |
type=undefined && module | ESM |
no package.json on cwd | CommonJS |
Returns
The root data object containing the root path and the type of the project.