Skip to content

Commit

Permalink
refactor(api): remove handlers types
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed May 26, 2022
1 parent dbaaeaa commit e4ea448
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 328 deletions.
61 changes: 0 additions & 61 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@
<dt><a href="#initializer">initializer(properties, initializer)</a> ⇒ <code>function</code></dt>
<dd><p>Decorator to set an initializer properties.</p>
</dd>
<dt><a href="#handler">handler(handlerFunction, [name], [dependencies], [options])</a> ⇒ <code>function</code></dt>
<dd><p>Shortcut to create an initializer with a simple handler</p>
</dd>
<dt><a href="#autoHandler">autoHandler(handlerFunction)</a> ⇒ <code>function</code></dt>
<dd><p>Allows to create an initializer with a simple handler automagically</p>
</dd>
<dt><a href="#parseDependencyDeclaration">parseDependencyDeclaration(dependencyDeclaration)</a> ⇒ <code>Object</code></dt>
<dd><p>Explode a dependency declaration an returns its parts.</p>
</dd>
Expand Down Expand Up @@ -717,61 +711,6 @@ new Knifecycle()
singleton: true,
}, myServiceInitializer));
```
<a name="handler"></a>

## handler(handlerFunction, [name], [dependencies], [options]) ⇒ <code>function</code>
Shortcut to create an initializer with a simple handler

**Kind**: global function
**Returns**: <code>function</code> - Returns a new initializer

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| handlerFunction | <code>function</code> | | The handler function |
| [name] | <code>String</code> | | The name of the handler. Default to the DI prop if exists |
| [dependencies] | <code>Array.&lt;String&gt;</code> | <code>[]</code> | The dependencies to inject in it |
| [options] | <code>Object</code> | | Options attached to the built initializer |

**Example**
```js
import Knifecycle, { handler } from 'knifecycle';

new Knifecycle()
.register(handler(getUser, 'getUser', ['db', '?log']));

const QUERY = `SELECT * FROM users WHERE id=$1`
async function getUser({ db }, userId) {
const [row] = await db.query(QUERY, userId);

return row;
}
```
<a name="autoHandler"></a>

## autoHandler(handlerFunction) ⇒ <code>function</code>
Allows to create an initializer with a simple handler automagically

**Kind**: global function
**Returns**: <code>function</code> - Returns a new initializer

| Param | Type | Description |
| --- | --- | --- |
| handlerFunction | <code>function</code> | The handler function |

**Example**
```js
import Knifecycle, { autoHandler } from 'knifecycle';

new Knifecycle()
.register(autoHandler(getUser));

const QUERY = `SELECT * FROM users WHERE id=$1`
async function getUser({ db }, userId) {
const [row] = await db.query(QUERY, userId);

return row;
}
```
<a name="parseDependencyDeclaration"></a>

## parseDependencyDeclaration(dependencyDeclaration) ⇒ <code>Object</code>
Expand Down
12 changes: 6 additions & 6 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ It is designed to have a low footprint on services code.
at all since they are just simple functions with annotations
set as a property.

[See in context](./src/index.ts#L155-L171)
[See in context](./src/index.ts#L151-L167)



Expand All @@ -49,7 +49,7 @@ A service provider is full of state since its concern is
[encapsulate](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))
your application global states.

[See in context](./src/index.ts#L173-L182)
[See in context](./src/index.ts#L169-L178)



Expand Down Expand Up @@ -89,7 +89,7 @@ The `?` flag indicates an optional dependency.
It allows to write generic services with fixed
dependencies and remap their name at injection time.

[See in context](./src/util.ts#L1249-L1258)
[See in context](./src/util.ts#L1141-L1150)



Expand Down Expand Up @@ -117,7 +117,7 @@ Initializers can be of three types:
executions silos using them (we will cover this
topic later on).

[See in context](./src/index.ts#L249-L272)
[See in context](./src/index.ts#L245-L268)



Expand All @@ -133,7 +133,7 @@ Depending on your application design, you could run it
in only one execution silo or into several ones
according to the isolation level your wish to reach.

[See in context](./src/index.ts#L536-L546)
[See in context](./src/index.ts#L532-L542)



Expand Down Expand Up @@ -169,5 +169,5 @@ Sadly TypeScript does not allow to add generic types
For more details, see:
https://stackoverflow.com/questions/64948037/generics-type-loss-while-infering/64950184#64950184

[See in context](./src/util.ts#L1319-L1330)
[See in context](./src/util.ts#L1211-L1222)

61 changes: 0 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,6 @@ is maybe the best feature of this library ;).
<dt><a href="#initializer">initializer(properties, initializer)</a> ⇒ <code>function</code></dt>
<dd><p>Decorator to set an initializer properties.</p>
</dd>
<dt><a href="#handler">handler(handlerFunction, [name], [dependencies], [options])</a> ⇒ <code>function</code></dt>
<dd><p>Shortcut to create an initializer with a simple handler</p>
</dd>
<dt><a href="#autoHandler">autoHandler(handlerFunction)</a> ⇒ <code>function</code></dt>
<dd><p>Allows to create an initializer with a simple handler automagically</p>
</dd>
<dt><a href="#parseDependencyDeclaration">parseDependencyDeclaration(dependencyDeclaration)</a> ⇒ <code>Object</code></dt>
<dd><p>Explode a dependency declaration an returns its parts.</p>
</dd>
Expand Down Expand Up @@ -1115,61 +1109,6 @@ new Knifecycle()
singleton: true,
}, myServiceInitializer));
```
<a name="handler"></a>

## handler(handlerFunction, [name], [dependencies], [options]) ⇒ <code>function</code>
Shortcut to create an initializer with a simple handler

**Kind**: global function
**Returns**: <code>function</code> - Returns a new initializer

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| handlerFunction | <code>function</code> | | The handler function |
| [name] | <code>String</code> | | The name of the handler. Default to the DI prop if exists |
| [dependencies] | <code>Array.&lt;String&gt;</code> | <code>[]</code> | The dependencies to inject in it |
| [options] | <code>Object</code> | | Options attached to the built initializer |

**Example**
```js
import Knifecycle, { handler } from 'knifecycle';

new Knifecycle()
.register(handler(getUser, 'getUser', ['db', '?log']));

const QUERY = `SELECT * FROM users WHERE id=$1`
async function getUser({ db }, userId) {
const [row] = await db.query(QUERY, userId);

return row;
}
```
<a name="autoHandler"></a>

## autoHandler(handlerFunction) ⇒ <code>function</code>
Allows to create an initializer with a simple handler automagically

**Kind**: global function
**Returns**: <code>function</code> - Returns a new initializer

| Param | Type | Description |
| --- | --- | --- |
| handlerFunction | <code>function</code> | The handler function |

**Example**
```js
import Knifecycle, { autoHandler } from 'knifecycle';

new Knifecycle()
.register(autoHandler(getUser));

const QUERY = `SELECT * FROM users WHERE id=$1`
async function getUser({ db }, userId) {
const [row] = await db.query(QUERY, userId);

return row;
}
```
<a name="parseDependencyDeclaration"></a>

## parseDependencyDeclaration(dependencyDeclaration) ⇒ <code>Object</code>
Expand Down
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import {
provider,
autoProvider,
wrapInitializer,
handler,
autoHandler,
parseDependencyDeclaration,
stringifyDependencyDeclaration,
unwrapInitializerProperties,
Expand Down Expand Up @@ -62,7 +60,6 @@ import type {
Initializer,
ServiceInitializerWrapper,
ProviderInitializerWrapper,
HandlerFunction,
Parameters,
} from './util';
import type { BuildInitializer } from './build';
Expand Down Expand Up @@ -92,7 +89,6 @@ export type {
Initializer,
ServiceInitializerWrapper,
ProviderInitializerWrapper,
HandlerFunction,
Parameters,
BuildInitializer,
};
Expand Down Expand Up @@ -1179,8 +1175,6 @@ export {
autoService,
provider,
autoProvider,
handler,
autoHandler,
parseDependencyDeclaration,
stringifyDependencyDeclaration,
unwrapInitializerProperties,
Expand Down
86 changes: 0 additions & 86 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
autoService,
provider,
autoProvider,
handler,
autoHandler,
SPECIAL_PROPS,
} from './util';
import type { PromiseValue } from 'type-fest';
Expand Down Expand Up @@ -888,90 +886,6 @@ describe('autoProvider', () => {
});
});

describe('handler', () => {
it('should work', async () => {
const baseName = 'sampleHandler';
const injectedServices = ['kikooo', 'lol'];
const services = {
kikooo: 'kikooo',
lol: 'lol',
};
const theInitializer = handler(sampleHandler, baseName, injectedServices);

assert.deepEqual((theInitializer as any).$name, baseName);
assert.deepEqual((theInitializer as any).$inject, injectedServices);

const theHandler = await theInitializer(services);
const result = await theHandler('test');
assert.deepEqual(result, {
deps: services,
args: ['test'],
});

async function sampleHandler(deps, ...args) {
return { deps, args };
}
});

it('should fail with no name', () => {
assert.throws(() => {
handler(async () => undefined);
}, /E_NO_HANDLER_NAME/);
});
});

describe('autoHandler', () => {
it('should work', async () => {
const services = {
kikooo: 'kikooo',
lol: 'lol',
};
const theInitializer = autoHandler(sampleHandler);

assert.deepEqual((theInitializer as any).$name, sampleHandler.name);
assert.deepEqual((theInitializer as any).$inject, ['kikooo', 'lol']);

const theHandler = await theInitializer(services);
const result = await theHandler('test');
assert.deepEqual(result, {
deps: services,
args: ['test'],
});

async function sampleHandler({ kikooo, lol }, ...args) {
return { deps: { kikooo, lol }, args };
}
});

it('should work with spread services', async () => {
const services = {
kikooo: 'kikooo',
lol: 'lol',
};
const theInitializer = autoHandler(sampleHandler);

assert.deepEqual((theInitializer as any).$name, sampleHandler.name);
assert.deepEqual((theInitializer as any).$inject, ['kikooo', 'lol']);

const theHandler = await theInitializer(services);
const result = await theHandler('test');
assert.deepEqual(result, {
deps: services,
args: ['test'],
});

async function sampleHandler({ kikooo, lol, ...services }, ...args) {
return { deps: { kikooo, lol, ...services }, args };
}
});

it('should fail for anonymous functions', () => {
assert.throws(() => {
autoHandler(async () => undefined);
}, /E_AUTO_NAMING_FAILURE/);
});
});

describe('parseDependencyDeclaration', () => {
it('should work', () => {
assert.deepEqual(parseDependencyDeclaration('db>pgsql'), {
Expand Down
Loading

0 comments on commit e4ea448

Please sign in to comment.