Skip to content

Commit

Permalink
Merge pull request #2 from TiMESPLiNTER/feat/mercylessly-typed
Browse files Browse the repository at this point in the history
Add mercylessly typing to container
  • Loading branch information
TiMESPLiNTER authored Jan 31, 2024
2 parents 030d494 + f97d300 commit abcd7d4
Show file tree
Hide file tree
Showing 21 changed files with 4,446 additions and 4,640 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Publish Package to npmjs
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Run Jest
uses: stefanoeb/[email protected]
- uses: actions/checkout@v2
- name: Install modules
run: npm install
- name: Run coverage
run: npm run test:cov
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ and the original PHP Pimple container by Fabien Potencier.

## Usage

```js
```ts
import Pimple from '@timesplinter/pimple';

const container: Container = new Pimple({env: 'dev'});
type ServiceMap = {
'foo': string,
'bar': string,
};

container.set('foo', (container: Pimple) => {
return `bar (${container.get('env')})`;
const container: Container = new Pimple<ServiceMap>({env: 'dev'});

container.set('foo', () => {
return `baz (${container.get('env')})`;
});

container.set('bar', (container: Pimple<ServiceMap>) => {
return `bar: ${container.get('foo')}`;
});

console.log(container.get('foo')); // 'bar (dev)';
Expand Down
11 changes: 0 additions & 11 deletions jest.config.json

This file was deleted.

19 changes: 19 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {Config} from 'jest';

const config: Config = {
coverageDirectory: "./build/coverage",
coverageReporters: ['text', 'html', 'lcov'],
coverageThreshold: {
global: {
branches: 80,
functions: 88,
lines: 93,
statements: 93
}
},
collectCoverageFrom: [
"src/**/*"
],
};

export default config;
7 changes: 4 additions & 3 deletions lib/cjs/container.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface Container {
get(service: string): any;
has(service: string): boolean;
export type ServiceKey<T> = keyof T;
export default interface Container<T> {
get<K extends ServiceKey<T>>(key: K): T[K];
has<K extends ServiceKey<T>>(key: K): boolean;
}
35 changes: 20 additions & 15 deletions lib/cjs/pimple.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Container from "./container";
import Container, { ServiceKey } from "./container";
import ServiceProvider from "./serviceProvider";
/** Declaration types */
declare type ServiceDeclaration = Function | Object;
declare type ProviderDeclaration = Function | ServiceProvider;
type ServiceProviderFunction<T> = (container: Pimple<T>) => void;
type ProviderDeclaration<T> = ServiceProviderFunction<T> | ServiceProvider<T>;
type LazyServiceDefinition<T, S> = (container: Pimple<T>) => S;
type ProtectedServiceDefinition<T, S> = () => LazyServiceDefinition<T, S>;
type PlainServiceDefinition<S> = S extends Function ? () => S : S;
type ServiceDefinition<T, S> = PlainServiceDefinition<S> | LazyServiceDefinition<T, S> | ProtectedServiceDefinition<T, S> | (S extends Function ? () => S : S);
type ServiceMap<T> = {
[key in ServiceKey<T>]: ServiceDefinition<T, T[ServiceKey<T>]>;
};
/**
* Pimple dependency injection container
*
Expand All @@ -12,7 +19,7 @@ declare type ProviderDeclaration = Function | ServiceProvider;
* @license LGPL
* @version 3.0.0
*/
export default class Pimple implements Container {
export default class Pimple<T> implements Container<T> {
/**
* @type {string}
*/
Expand All @@ -27,41 +34,39 @@ export default class Pimple implements Container {
* @private
*/
private _raw;
constructor(services?: {
[key: string]: any;
});
constructor(services?: Partial<ServiceMap<T>>);
/**
* Define a service
*/
set(name: string, service: ServiceDeclaration): Pimple;
set<K extends ServiceKey<T>>(name: K, service: ServiceDefinition<T, T[K]>): Pimple<T>;
/**
* Register a factory
*/
factory(name: string, callback: Function): Pimple;
factory<K extends ServiceKey<T>>(name: K, callback: ServiceDefinition<T, T[K]>): Pimple<T>;
/**
* Get a service instance
*/
get(name: string): any;
get<K extends ServiceKey<T>>(name: K): T[K];
/**
* Checks whether a service is registered or not
*/
has(service: string): boolean;
has<K extends ServiceKey<T>>(name: K): boolean;
/**
* Register a protected function
*/
protect(service: Function): Function;
protect<T extends Function>(func: T): () => T;
/**
* Extend a service
*/
extend(serviceName: string, service: Function): Function;
extend<K extends ServiceKey<T>>(serviceName: K, service: Function): Function;
/**
* Get a service raw definition
*/
raw(name: string): Function;
raw<K extends ServiceKey<T>>(name: K): ServiceDefinition<T, T[K]>;
/**
* Register a service provider
*/
register(provider: ProviderDeclaration): Pimple;
register(provider: ProviderDeclaration<T>): Pimple<T>;
private instanceOfServiceProvider;
}
export {};
33 changes: 15 additions & 18 deletions lib/cjs/pimple.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/cjs/pimple.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/cjs/serviceProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Pimple from "./pimple";
/**
* Service provider class for service injecting in Pimple container
*/
export default interface ServiceProvider {
register(container: Pimple): void;
export default interface ServiceProvider<T> {
register(container: Pimple<T>): void;
}
7 changes: 4 additions & 3 deletions lib/esm/container.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface Container {
get(service: string): any;
has(service: string): boolean;
export type ServiceKey<T> = keyof T;
export default interface Container<T> {
get<K extends ServiceKey<T>>(key: K): T[K];
has<K extends ServiceKey<T>>(key: K): boolean;
}
35 changes: 20 additions & 15 deletions lib/esm/pimple.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Container from "./container";
import Container, { ServiceKey } from "./container";
import ServiceProvider from "./serviceProvider";
/** Declaration types */
declare type ServiceDeclaration = Function | Object;
declare type ProviderDeclaration = Function | ServiceProvider;
type ServiceProviderFunction<T> = (container: Pimple<T>) => void;
type ProviderDeclaration<T> = ServiceProviderFunction<T> | ServiceProvider<T>;
type LazyServiceDefinition<T, S> = (container: Pimple<T>) => S;
type ProtectedServiceDefinition<T, S> = () => LazyServiceDefinition<T, S>;
type PlainServiceDefinition<S> = S extends Function ? () => S : S;
type ServiceDefinition<T, S> = PlainServiceDefinition<S> | LazyServiceDefinition<T, S> | ProtectedServiceDefinition<T, S> | (S extends Function ? () => S : S);
type ServiceMap<T> = {
[key in ServiceKey<T>]: ServiceDefinition<T, T[ServiceKey<T>]>;
};
/**
* Pimple dependency injection container
*
Expand All @@ -12,7 +19,7 @@ declare type ProviderDeclaration = Function | ServiceProvider;
* @license LGPL
* @version 3.0.0
*/
export default class Pimple implements Container {
export default class Pimple<T> implements Container<T> {
/**
* @type {string}
*/
Expand All @@ -27,41 +34,39 @@ export default class Pimple implements Container {
* @private
*/
private _raw;
constructor(services?: {
[key: string]: any;
});
constructor(services?: Partial<ServiceMap<T>>);
/**
* Define a service
*/
set(name: string, service: ServiceDeclaration): Pimple;
set<K extends ServiceKey<T>>(name: K, service: ServiceDefinition<T, T[K]>): Pimple<T>;
/**
* Register a factory
*/
factory(name: string, callback: Function): Pimple;
factory<K extends ServiceKey<T>>(name: K, callback: ServiceDefinition<T, T[K]>): Pimple<T>;
/**
* Get a service instance
*/
get(name: string): any;
get<K extends ServiceKey<T>>(name: K): T[K];
/**
* Checks whether a service is registered or not
*/
has(service: string): boolean;
has<K extends ServiceKey<T>>(name: K): boolean;
/**
* Register a protected function
*/
protect(service: Function): Function;
protect<T extends Function>(func: T): () => T;
/**
* Extend a service
*/
extend(serviceName: string, service: Function): Function;
extend<K extends ServiceKey<T>>(serviceName: K, service: Function): Function;
/**
* Get a service raw definition
*/
raw(name: string): Function;
raw<K extends ServiceKey<T>>(name: K): ServiceDefinition<T, T[K]>;
/**
* Register a service provider
*/
register(provider: ProviderDeclaration): Pimple;
register(provider: ProviderDeclaration<T>): Pimple<T>;
private instanceOfServiceProvider;
}
export {};
Loading

0 comments on commit abcd7d4

Please sign in to comment.