Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## `4.0.20`

* Добавлена возможность передавать функцию в options.cache.
* Добавлена передача params в getRetryStrategy.

## `4.0.19`

* Добавлена возможность использования пользовательских стратегий ретраев.
Expand Down
2 changes: 2 additions & 0 deletions docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const cache = new de.Cache();
Он хранит все просто в памяти, нет ограничений на количество записей.
Так что для каких-то серьезных ситуаций лучше его не использовать, а использовать [descript-redis-cache](https://www.npmjs.com/package/descript-redis-cache).

`cache` также может быть функцией `({ params }) => Promise<CacheInterface<Result>>`. Если функция выбросит ошибку, то блок продолжит выполнение без кэширования.

## `options.key`

Обязательный параметр `key` задает ключ хранения.
Expand Down
2 changes: 1 addition & 1 deletion docs/http_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ block: {

```js
block: {
getRetryStragety: ( { requestOptions, request } ) => {
getRetryStragety: ( { requestOptions, request, params } ) => {
return new MyAwesomeRetryStrategy({ request, ... });
},
},
Expand Down
10 changes: 9 additions & 1 deletion lib/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
blockCancel.throwIfCancelled();

if (typeof step.after === 'function') {
resultAfter = await step.after({ cancel, params, context, deps, result: (resultBefore || resultBlock) as any });

Check warning on line 441 in lib/block.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
blockCancel.throwIfCancelled();

if (resultAfter instanceof BaseBlock) {
Expand Down Expand Up @@ -523,8 +523,16 @@
depsDomain?: DepsDomain;
}): Promise<BlockResult> {
let result: BlockResult | undefined = undefined;
let cache;

try {
cache = typeof this.options.cache === 'function' ?
await this.options.cache({ params }) :
this.options.cache;
} catch {
// Do nothing
}

const cache = this.options.cache;
let key;
const optionsKey = this.options.key;

Expand Down
15 changes: 12 additions & 3 deletions lib/httpBlock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Block from './block';
import { ERROR_ID, createError } from './error';

import type { DescriptRequestOptions, BlockRequestOptions } from './request';
import type { DescriptRequestOptions, BlockRequestOptions, GetRetryStrategyParams } from './request';
import request from './request';

import extend from './extend';
Expand All @@ -13,6 +13,7 @@ import type ContextClass from './context';
import type Cancel from './cancel';
import type DepsDomain from './depsDomain';
import type { LoggerInterface } from './logger';
import { RetryStrategyInterface } from './retryStrategy';

// --------------------------------------------------------------------------------------------------------------- //

Expand Down Expand Up @@ -79,7 +80,7 @@ export interface DescriptHttpBlockDescription<
HTTPResult,
> extends Pick<
DescriptRequestOptions,
'isError' | 'isRetryAllowed' | 'retryTimeout' | 'getRetryStrategy'
'isError' | 'isRetryAllowed' | 'retryTimeout'
> {
// sync with EVALUABLE_PROPS
agent?: DescriptHttpBlockDescriptionCallback<DescriptRequestOptions['agent'], Params, Context>;
Expand Down Expand Up @@ -120,6 +121,9 @@ export interface DescriptHttpBlockDescription<
parseBody?: (result: { body: DescriptHttpResult['body']; headers: DescriptHttpResult['headers'] }, context: Context) =>
HTTPResult;

getRetryStrategy?: (args: {
params: Params;
} & GetRetryStrategyParams) => RetryStrategyInterface;
}

const EVALUABLE_PROPS: Array<keyof Pick<DescriptRequestOptions, 'agent' |
Expand Down Expand Up @@ -285,11 +289,16 @@ class HttpBlock<

const callbackArgs: CallbackArgs<ParamsOut, Context> = { params, context, deps };

const blockGetRetryStrategy = block.getRetryStrategy;
const getRetryStrategy = typeof blockGetRetryStrategy === 'function' ?
(args: GetRetryStrategyParams) => blockGetRetryStrategy({ ...args, params }) :
undefined;

let options: DescriptRequestOptions = {
isError: block.isError,
isRetryAllowed: block.isRetryAllowed,
retryTimeout: block.retryTimeout,
getRetryStrategy: block.getRetryStrategy,
getRetryStrategy,
body: null,
...(
EVALUABLE_PROPS.reduce((ret, prop) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export interface DescriptBlockOptions<
deps: DescriptBlockDeps;
}) => string);
maxage?: number;
cache?: CacheInterface<BlockResult>;
cache?: CacheInterface<BlockResult> | ((args: { params: ParamsOut }) => Promise<CacheInterface<BlockResult>>);

required?: boolean;

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"name": "descript",
"description": "descript",
"version": "4.0.19",
"version": "4.0.20",
"homepage": "https://github.com/descript-org/descript",
"repository": {
"type": "git",
Expand Down
43 changes: 43 additions & 0 deletions tests/options.cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,47 @@ describe('options.cache, options.key, options.maxage', () => {
}).not.toThrow();
});

it('cache can be a function', async() => {
const cache = new Cache();
const cachedValue = 'cached';
const params = { foo: 'bar' };

await cache.set({ key: 'KEY', value: cachedValue, maxage: 10000 });

const spy = vi.fn(() => 'result');
const cacheFn = vi.fn(() => Promise.resolve(cache));
const block = getResultBlock(spy, 50).extend({
options: {
cache: cacheFn,
key: 'KEY',
maxage: 10000,
},
});

const result = await de.run(block, { params });

expect(result).toBe(cachedValue);
expect(spy.mock.calls).toHaveLength(0);
expect(cacheFn).toHaveBeenCalledWith({ params });
});

it('cache function throws, block executes without cache', async() => {
const blockValue = 'result';

const spy = vi.fn(() => blockValue);
const cacheFn = vi.fn(() => Promise.reject(de.error('cache error')));
const block = getResultBlock(spy, 50).extend({
options: {
cache: cacheFn,
key: 'KEY',
maxage: 10000,
},
});

const result = await de.run(block);

expect(result).toBe(blockValue);
expect(spy.mock.calls).toHaveLength(1);
});

});
Loading