Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): remove handlers types #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 0 additions & 61 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
builder by automatically detecting its name
and dependencies</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 @@ -352,61 +346,6 @@ Decorator that creates an initializer from a provider
| --- | --- | --- |
| providerBuilder | <code>function</code> | An async function to build the service provider |

<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 @@ -37,7 +37,7 @@ It is designed to have a low footprint on services code.
In fact, the Knifecycle API is aimed to allow to statically
build its services load/unload code once in production.

[See in context](./src/index.ts#L201-L220)
[See in context](./src/index.ts#L197-L216)



Expand All @@ -52,7 +52,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#L222-L231)
[See in context](./src/index.ts#L218-L227)



Expand Down Expand Up @@ -92,7 +92,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#L1304-L1313)
[See in context](./src/util.ts#L1196-L1205)



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

[See in context](./src/index.ts#L311-L335)
[See in context](./src/index.ts#L307-L331)



Expand All @@ -137,7 +137,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#L644-L654)
[See in context](./src/index.ts#L640-L650)



Expand Down Expand Up @@ -173,5 +173,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#L1374-L1385)
[See in context](./src/util.ts#L1266-L1277)

65 changes: 0 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,6 @@ is maybe the best feature of this library 😉.
builder by automatically detecting its name
and dependencies</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 @@ -813,65 +807,6 @@ detecting its name and dependencies
| --------------- | --------------------- | ----------------------------------------------- |
| providerBuilder | <code>function</code> | An async function to build the service provider |

<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 @@ -29,8 +29,6 @@
provider,
autoProvider,
wrapInitializer,
handler,
autoHandler,
parseDependencyDeclaration,
stringifyDependencyDeclaration,
unwrapInitializerProperties,
Expand Down Expand Up @@ -66,7 +64,6 @@
Initializer,
ServiceInitializerWrapper,
ProviderInitializerWrapper,
HandlerFunction,
Parameters,
} from './util.js';
import type { BuildInitializer } from './build.js';
Expand Down Expand Up @@ -98,7 +95,6 @@
Initializer,
ServiceInitializerWrapper,
ProviderInitializerWrapper,
HandlerFunction,
Parameters,
BuildInitializer,
FatalErrorService,
Expand Down Expand Up @@ -948,7 +944,7 @@
await fatalErrorInitializerState.singletonProviderLoadPromise;

const fatalError = (
fatalErrorInitializerState.singletonProvider as Provider<FatalErrorService>

Check warning on line 947 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

This line has a length of 83. Maximum allowed is 80

Check warning on line 947 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 83. Maximum allowed is 80
).service;

debug('Registering service descriptor error promise:', serviceName);
Expand Down Expand Up @@ -1302,8 +1298,6 @@
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 @@ -24,8 +24,6 @@ import {
autoService,
provider,
autoProvider,
handler,
autoHandler,
SPECIAL_PROPS,
} from './util.js';
import type { Provider } from './util.js';
Expand Down Expand Up @@ -890,90 +888,6 @@ describe('autoProvider', () => {
});
});

describe('handler', () => {
test('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 };
}
});

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

describe('autoHandler', () => {
test('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 };
}
});

test('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 };
}
});

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

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