-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c2c2a73
Showing
27 changed files
with
8,441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es6: true, | ||
}, | ||
root: true, | ||
parser: "@typescript-eslint/parser", | ||
plugins: ["@typescript-eslint", "eslint-plugin-import", "eslint-plugin-prefer-arrow"], | ||
rules: { | ||
"@typescript-eslint/member-ordering": [ | ||
"error", | ||
{"classes": [ | ||
"static-field", | ||
"static-method", | ||
"public-instance-field", | ||
"protected-instance-field", | ||
"#private-instance-field", | ||
"constructor", | ||
"public-instance-method", | ||
"protected-instance-method", | ||
"#private-instance-method", | ||
] }, | ||
], | ||
"@typescript-eslint/consistent-type-imports": "error", | ||
"@typescript-eslint/explicit-module-boundary-types": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"import/order": "error", | ||
"max-classes-per-file": ["error", 1], | ||
"max-len": [ | ||
"error", | ||
{ | ||
code: 120, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
docs | ||
node_modules | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v20.15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
docs | ||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"printWidth": 120, | ||
"trailingComma": "es5", | ||
"arrowParens": "always" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Contributing guide | ||
|
||
Thanks for taking the time to contribute! We appreciate contributions whether it's: | ||
|
||
- Reporting a bug | ||
- Submitting a fix or new feature | ||
- Proposing new features | ||
|
||
## Report bugs using Github [issues](../../issues) | ||
|
||
We use GitHub [issues](../../issues) to track public bugs. Report a bug by opening a new issue; it's easy! | ||
|
||
When reporting bugs, please include enough details so that it can be investigated. Bug reports should have: | ||
|
||
- A summary or background | ||
- Steps to reproduce | ||
- Give code sample if you can | ||
- What is the expected result | ||
- What actually happens | ||
|
||
## Contributing fixes and features | ||
|
||
Pull requests are the best way to propose code changes: | ||
|
||
1. Fork the repo and create your branch from `main`. | ||
2. Run `nvm use && npm ci` and check in any changes to generated code. | ||
3. Add tests, if appropriate. | ||
4. Run `npm test` to ensure the test suite passes with your change. | ||
5. Run `npm run styleguide:fix` to ensure coding guidelines are followed. | ||
6. Issue the pull request. | ||
|
||
## License | ||
|
||
Any contributions you make will be under the same MIT license that covers the project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Copyright (c) 2024 Snap Inc. | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# ts-inject | ||
|
||
`ts-inject` is a 100% typesafe dependency injection framework for TypeScript projects, designed to enhance code sharing and modularity by ensuring compile-time dependency resolution. This framework leverages the dependency injection design pattern to decouple dependency usage from creation, allowing components to rely on interfaces rather than implementations. | ||
|
||
## Features and Alternatives | ||
|
||
`ts-inject` brings typesafety to dependency injection, setting it apart from a vast majority of frameworks, like [InversifyJS](https://github.com/inversify/InversifyJS), which operate at runtime and therefore lack this level of typesafety. | ||
|
||
While [typed-inject](https://github.com/nicojs/typed-inject) also prioritizes typesafety, it lacks several key features that `ts-inject` offers: | ||
|
||
- **Overcomes TypeScript Nested Type Limitations**: Unlike some frameworks, `ts-inject` navigates around [TypeScript's limits on nested types](https://github.com/nicojs/typed-inject/issues/22), making it more robust for complex applications. | ||
- **Composable Containers**: `ts-inject` enables merging multiple containers, facilitating greater modularity and code reuse. | ||
- **PartialContainer**: It allows service registration without pre-defined dependencies, offering more flexibility compared to regular containers. | ||
|
||
## Getting Started | ||
|
||
### Installation | ||
|
||
```bash | ||
npm install @snap/ts-inject | ||
``` | ||
|
||
### Sample Usage | ||
|
||
This quick start guide demonstrates how to define services, register them in a container, and then retrieve them for use. | ||
|
||
#### Defining Services | ||
|
||
Define a couple of services. For simplicity, we'll use a `Logger` service and a `Database` service, where `Database` depends on `Logger` for logging purposes. | ||
|
||
```ts | ||
// Logger service definition | ||
class Logger { | ||
log(message: string) { | ||
console.log(`Log: ${message}`); | ||
} | ||
} | ||
|
||
// Database service depends on Logger | ||
class Database { | ||
constructor(private logger: Logger) {} | ||
|
||
save(record: string) { | ||
this.logger.log(`Saving record: ${record}`); | ||
// Assume record saving logic here | ||
} | ||
} | ||
``` | ||
|
||
#### Setting Up the Container | ||
|
||
With `ts-inject`, you can easily set up a container to manage these services: | ||
|
||
```ts | ||
import { Container, Injectable } from "@snap/ts-inject"; | ||
|
||
// Define Injectable factory functions for services | ||
const loggerFactory = Injectable("Logger", () => new Logger()); | ||
const databaseFactory = Injectable("Database", ["Logger"] as const, (logger: Logger) => new Database(logger)); | ||
|
||
// Create a container and register services | ||
const container = Container.provides(loggerFactory).provides(databaseFactory); | ||
|
||
// Now, retrieve the Database service from the container | ||
const db = container.get("Database"); | ||
db.save("user1"); // Log: Saving record: user1 | ||
``` | ||
|
||
#### Composable Containers | ||
|
||
`ts-inject` supports composable containers, allowing you to modularize service registration: | ||
|
||
```ts | ||
const baseContainer = Container.provides(loggerFactory); | ||
const appContainer = Container.provides(baseContainer).provide(databaseFactory); | ||
|
||
const db: Database = appContainer.get("Database"); | ||
db.save("user2"); // Log: Saving record: user2 | ||
``` | ||
|
||
### Key Concepts | ||
|
||
- **Container**: A registry for all services, handling their creation and retrieval. | ||
- **PartialContainer**: Similar to a Container but allows services to be registered without defining all dependencies upfront. Unlike a regular Container, it does not support retrieving services directly. | ||
- **Service**: Any value or instance provided by the Container. | ||
- **Token**: A unique identifier for each service, used for registration and retrieval within the Container. | ||
- **InjectableFunction**: Functions that return service instances. They can include dependencies which are injected when the service is requested. | ||
- **InjectableClass**: Classes that can be instantiated by the Container. Dependencies should be specified in a static "dependencies" field to enable proper injection. | ||
|
||
### API Reference | ||
|
||
For comprehensive documentation of all ts-inject features and APIs, please refer to the [API Reference](https://github.sc-corp.net/pages/Snapchat/ts-inject/modules.html). | ||
|
||
## Contributing | ||
|
||
[Contributing guide](CONTRIBUTING.md). | ||
|
||
## License | ||
|
||
`ts-inject` is published under [MIT license](LICENSE.md). | ||
|
||
## Project Origins | ||
|
||
`ts-inject` originated as an internal project at [Snap Inc.](https://snap.com/), developed by [Weston Fribley](https://github.com/wfribley). Inspired by the principles of [typed-inject](https://github.com/nicojs/typed-inject), it was designed to address the limitations of existing dependency injection frameworks and improve typesafe dependency resolution in TypeScript. Initially aimed at enhancing [CameraKit](https://www.npmjs.com/package/@snap/camera-kit)'s codebase, its success led to its adoption across various teams at [Snap Inc.](https://snap.com/), and now it has evolved into an open-source project to benefit the wider TypeScript community. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { JestConfigWithTsJest } from "ts-jest"; | ||
|
||
const config: JestConfigWithTsJest = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+\\.tsx?$": [ | ||
"ts-jest", | ||
{ | ||
tsconfig: "tsconfig.jest.json", | ||
}, | ||
], | ||
}, | ||
collectCoverage: true, | ||
coverageReporters: ["text"], | ||
coveragePathIgnorePatterns: ["node_modules"], | ||
}; | ||
|
||
export default config; |
Oops, something went wrong.