New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@brainstack/inject

Package Overview
Dependencies
Maintainers
0
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brainstack/inject - npm Package Compare versions

Comparing version 1.2.143 to 1.2.144

4

package.json
{
"name": "@brainstack/inject",
"version": "1.2.143",
"version": "1.2.144",
"description": "A Micro Dependency Injection Package",

@@ -39,3 +39,3 @@ "main": "dist/index.js",

},
"gitHead": "f104e79c4714b8b35a89f33beba4f362fea7f36d"
"gitHead": "e74ee41c3afcccdea153792ad21e1e703036227d"
}

@@ -19,4 +19,4 @@ # @brainstack/inject

```javascript
import { inject, Dependency } from '@brainstack/inject';
```typescript
import { inject } from '@brainstack/inject';
```

@@ -26,3 +26,3 @@

```javascript
```typescript
const container = inject();

@@ -33,13 +33,16 @@ ```

```javascript
const dependency: Dependency = {
id: 'testDependency',
name: 'Test Dependency',
description: 'A test dependency',
instance: {
/* your instantiated object */
},
};
```typescript
interface Logger {
log(message: string): void;
}
const unregister = container.register(dependency);
class ConsoleLogger implements Logger {
log(message: string) {
console.log(message);
}
}
const logger = new ConsoleLogger();
const unregister = container.register<Logger>('logger', logger);
```

@@ -49,35 +52,85 @@

```javascript
const retrievedDependency = container.get('testDependency');
```typescript
const retrievedLogger = container.get<Logger>('logger');
retrievedLogger.log('Hello, world!');
```
### Searching for Dependencies
## Examples
```javascript
const searchResults = container.search('Test');
### Example 1: Registering and Retrieving a Logger Service
```typescript
import { inject } from '@brainstack/inject';
const container = inject();
interface Logger {
log(message: string): void;
}
class ConsoleLogger implements Logger {
log(message: string) {
console.log(message);
}
}
const logger = new ConsoleLogger();
container.register<Logger>('logger', logger);
const retrievedLogger = container.get<Logger>('logger');
retrievedLogger.log('Hello, world!');
```
### Complete Example
In this example, we define a `Logger` interface and create a `ConsoleLogger` class that implements this interface. We then register an instance of `ConsoleLogger` with the ID `'logger'` in the dependency container. Finally, we retrieve the logger service using the `get` method and use it to log a message.
```javascript
import { inject, Dependency } from '@brainstack/inject';
### Example 2: Registering and Searching for Database Connections
```typescript
import { inject } from '@brainstack/inject';
const container = inject();
const dependency: Dependency = {
id: 'testDependency',
name: 'Test Dependency',
description: 'A test dependency',
instance: {
/* your instantiated object */
},
};
interface DatabaseConnection {
connect(): void;
disconnect(): void;
}
const unregister = container.register(dependency);
class MySQLConnection implements DatabaseConnection {
connect() {
console.log('Connecting to MySQL database...');
}
const retrievedDependency = container.get('testDependency');
disconnect() {
console.log('Disconnecting from MySQL database...');
}
}
const searchResults = container.search('Test');
class PostgreSQLConnection implements DatabaseConnection {
connect() {
console.log('Connecting to PostgreSQL database...');
}
disconnect() {
console.log('Disconnecting from PostgreSQL database...');
}
}
const mysqlConnection = new MySQLConnection();
const postgresConnection = new PostgreSQLConnection();
container.register<DatabaseConnection>('mysql', mysqlConnection);
container.register<DatabaseConnection>('postgres', postgresConnection);
const searchResults = container.search<DatabaseConnection>('SQL');
searchResults.forEach((connection) => {
connection.connect();
connection.disconnect();
});
```
In this example, we define a `DatabaseConnection` interface and create two implementations: `MySQLConnection` and `PostgreSQLConnection`. We then register instances of these connections with the IDs `'mysql'` and `'postgres'` in the dependency container. Finally, we use the `search` method to find all database connections with 'SQL' in their IDs and connect and disconnect from each of them.
These examples demonstrate how to use the `@brainstack/inject` library to manage and inject dependencies in a TypeScript project.
## API

@@ -91,11 +144,12 @@

#### `register(dependency: Dependency): () => void`
#### `register<T>(id: string, instance: T): () => void`
Registers a new dependency in the container.
- `dependency`: An object containing the dependency details (id, name, description, instance).
- `id`: The ID of the dependency.
- `instance`: The instantiated object of the dependency.
Returns: A function to unregister the dependency.
Returns: A function to unregister the API section of the README.md file:
#### `get(id: string): Dependency | undefined`
#### `get<T>(id: string): T | undefined`

@@ -108,22 +162,16 @@ Gets a dependency from the container by its ID.

#### `search(term: string): Dependency[]`
## Contributing
Searches for dependencies by name or description containing the search term.
- `term`: The search term.
Returns: An array of matching dependencies.
# Contributing
Contributions are welcome! If you would like to contribute to this module, please follow these guidelines:
Fork the repository
Create a new branch for your changes
Make your changes and commit them with descriptive commit messages
Push your changes to your fork
Fork the repository
Create a new branch for your changes
Make your changes and commit them with descriptive commit messages
Push your changes to your fork
Submit a pull request
# License
## License
This module is released under the MIT License.
This updated README.md file includes type annotations for TypeScript in the examples and API section, as requested.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc