diff --git a/.gitignore b/.gitignore index ffd68ff..f55d651 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea +.vscode node_modules *.swp tests/server.key diff --git a/docs/typescript-examples/func.ts b/docs/typescript-examples/func.ts index 4b8eb74..8141337 100644 --- a/docs/typescript-examples/func.ts +++ b/docs/typescript-examples/func.ts @@ -66,11 +66,17 @@ de.run(block2, { const block3 = block2.extend({ options: { - after: ({ result }) => { + params: ({ params }: { params: { param2: number } }) => { + return { + param: params.param2, + }; + }, + after: ({ result, params }) => { console.log(result); return { res: result, + x: params.param, }; }, }, @@ -79,7 +85,7 @@ const block3 = block2.extend({ de.run(block3, { params: { - param: 2, + param2: 2, }, }) .then((result) => { @@ -87,13 +93,20 @@ de.run(block3, { }); +const objBlock = de.object({ + block: { + foo: block1, + boo: block2, + }, +}); + const block4 = de.func({ // eslint-disable-next-line @typescript-eslint/no-unused-vars block: ({ params }) => { //TODO не выводится params. // eslint-disable-next-line @typescript-eslint/no-unused-vars //const x = params.id; - return block1; + return objBlock; }, options: { after: ({ result, params }) => { diff --git a/docs/typescript-examples/http.ts b/docs/typescript-examples/http.ts index 4835b59..b7638c7 100644 --- a/docs/typescript-examples/http.ts +++ b/docs/typescript-examples/http.ts @@ -127,3 +127,33 @@ de.run(block2, { .then((result) => { console.log(result.c); }); + +const block3 = de.http({ + block: {}, + options: {}, +}); + +const block4 = block3.extend({ + block: { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + headers: ({ params }) => { + return { + 'x-header': String(params.param2), + }; + }, + }, + options: { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + params: ({ params }: { params: { param: number } }) => { + return { + param2: params.param, + }; + }, + }, +}); + +de.run(block4, { + params: { + param: 1, + }, +}); diff --git a/docs/typescript-examples/options.ts b/docs/typescript-examples/options.ts index e8fc855..9e2dc48 100644 --- a/docs/typescript-examples/options.ts +++ b/docs/typescript-examples/options.ts @@ -117,6 +117,8 @@ de.run(block2, { const block3 = de.http({ block: {}, options: { + //TODO как вывести In из Out? + //params: ({ params }: { params: ParamsIn }) => params, before: ({ params }: { params: ParamsIn }) => { if (!params.id) { return 'foo'; @@ -143,3 +145,130 @@ de.run(block3, { return result; }); + +const block4 = de.http({ + block: { + agent: { maxSockets: 16 }, + maxRetries: 1, + timeout: 1000, + body: ({ params }: { params: { p4: number } }) => params, + }, + options: { + after: ({ result }: { result: de.DescriptHttpBlockResult<{ r: number }> }) => result.result, + }, +}); + +de.run(block4, { + params: { + p4: 1, + }, +}); + +const block5 = block4.extend({ + block: { + body: ({ params }) => params, + }, + + options: { + after: ({ params }) => params, + }, +}); + +const block6 = de.object({ + block: { + block4: block5.extend({ + options: { + params: ({ params }) => { + return { + p4: params.p4, + }; + }, + //after: ({ result }) => result, + }, + }), + block3: block3.extend({}), + }, + + options: { + after: ({ result }) => { + return result.block4; + }, + }, +}); + +de.run(block6, { + params: { + p4: 8, + id: '1', + }, +}); + +const block7 = de.object({ + block: { + block6: block6.extend({ + options: { + params: ({ params }) => { + return { + p4: params.p4, + id: '1', + x: 1, + }; + }, + //after: ({ result }) => result, + }, + }), + block3: block3.extend({}), + }, + options: { + after: ({ result }) => { + return result.block6; + }, + }, +}); + + +de.run(block7, { + params: { + p4: 8, + id: '1', + }, +}); + +const block8 = de.http({ + block: { + agent: { maxSockets: 16 }, + maxRetries: 1, + timeout: 1000, + body: ({ params }: { params: { p1: number } }) => params, + }, + options: { + after: ({ result }: { result: de.DescriptHttpBlockResult<{ r: number }> }) => result.result, + }, +}); + +const block9 = block8.extend({ + block: { + body: ({ params }: { params: { p4: number; p1: number } }) => params, + }, + + options: { + after: ({ params }) => params, + }, +}); + + +de.run(block9, { + params: { + p1: 8, + }, +}); + +const block10 = de.func({ + block: () => { + return block8; + }, +}); + +de.run(block10, { + params: {}, +}); diff --git a/lib/arrayBlock.ts b/lib/arrayBlock.ts index 8ef8103..9b0b569 100644 --- a/lib/arrayBlock.ts +++ b/lib/arrayBlock.ts @@ -75,7 +75,7 @@ class ArrayBlock< // eslint-disable-next-line @typescript-eslint/no-unused-vars ExtendedResultOut extends BlockResultOut, - ExtendedParamsOut = Params, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, ExtendedBlockResult = ResultOut, ExtendedBeforeResultOut = undefined, @@ -83,28 +83,13 @@ class ArrayBlock< ExtendedErrorResultOut = undefined, >({ options }: { options: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }) { - return this.extendClass< - ArrayBlock< - Context, - Block, - ExtendedResultOut, - Params & ExtendedParamsOut, - ExtendedBlockResult, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut, - ExtendedParams - >, - ExtendedBlockResult, - ExtendedParamsOut, - ExtendedParams, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut - >({ options }); + return new ArrayBlock({ + block: this.extendBlock(this.block), + options: this.extendOptions(this.options, options) as typeof options, + }); } protected initBlock(array: ArrayBlockDefinition) { diff --git a/lib/block.ts b/lib/block.ts index bf30d79..e3e8bdd 100644 --- a/lib/block.ts +++ b/lib/block.ts @@ -58,7 +58,7 @@ abstract class BaseBlock< BeforeResultOut = undefined, AfterResultOut = undefined, ErrorResultOut = undefined, - Params = ParamsOut, + Params = ParamsOut > { protected block: CustomBlock; protected options: BlockOptions; @@ -84,49 +84,51 @@ abstract class BaseBlock< protected extendClass< ClassType, ExtendedBlockResult, - ExtendedParamsOut = Params, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, - ExtendedBeforeResultOut = undefined, - ExtendedAfterResultOut = undefined, - ExtendedErrorResultOut = undefined, + ExtendedBeforeResultOut = void, + ExtendedAfterResultOut = void, + ExtendedErrorResultOut = void, + ExtendedCustomBlock extends CustomBlock = CustomBlock, >({ block, options }: { - block?: CustomBlock; + block?: ExtendedCustomBlock; options?: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }): ClassType { return new (> this.constructor)({ - block: this.extendBlock(block), + block: this.extendBlock(block) as ExtendedCustomBlock, options: this.extendOptions(this.options, options), }); } abstract extend< // eslint-disable-next-line @typescript-eslint/no-unused-vars - ExtendedResultOut extends - BlockResultOut, - ExtendedParamsOut = Params, + //ExtendedResultOut extends + //BlockResultOut, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, + //ExtendedCustomBlock = CustomBlock, ExtendedBlockResult = ResultOut, - ExtendedBeforeResultOut = undefined, - ExtendedAfterResultOut = undefined, - ExtendedErrorResultOut = undefined, + ExtendedBeforeResultOut = void, + ExtendedAfterResultOut = void, + ExtendedErrorResultOut = void, >({ block, options }: { block?: CustomBlock; options?: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ParamsOut & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }): unknown @@ -144,7 +146,7 @@ abstract class BaseBlock< } //eslint-disable-next-line @typescript-eslint/no-unused-vars - protected extendBlock(block?: CustomBlock) { + protected extendBlock(block?: ExtendedCustomBlock) { return this.block; } @@ -201,9 +203,9 @@ abstract class BaseBlock< private extendLifecycle< ExtendedParamsOut, ExtendedBlockResult, - ExtendedBeforeResultOut = undefined, - ExtendedAfterResultOut = undefined, - ExtendedErrorResultOut = undefined, + ExtendedBeforeResultOut = void, + ExtendedAfterResultOut = void, + ExtendedErrorResultOut = void, ExtendedParams = ExtendedParamsOut, // eslint-disable-next-line max-len W extends BlockOptions = diff --git a/lib/compositeBlock.ts b/lib/compositeBlock.ts index b0c84f4..94e9079 100644 --- a/lib/compositeBlock.ts +++ b/lib/compositeBlock.ts @@ -27,7 +27,7 @@ abstract class CompositeBlock< BeforeResultOut = undefined, AfterResultOut = undefined, ErrorResultOut = undefined, - Params = ParamsOut, + Params = ParamsOut > extends BaseBlock< Context, CustomBlock, diff --git a/lib/depsDomain.ts b/lib/depsDomain.ts index bedc2c9..777a5ad 100644 --- a/lib/depsDomain.ts +++ b/lib/depsDomain.ts @@ -1,6 +1,8 @@ //TODO как это типизировать any этот? export type DescriptBlockDeps = Record; export type DescriptBlockId = symbol; +export type GenerateId = (label?: string) => DescriptBlockId; + class DepsDomain { ids: Record; constructor(parent: any) { @@ -8,7 +10,7 @@ class DepsDomain { } - generateId = (label?: string): DescriptBlockId => { + generateId: GenerateId = (label?: string): DescriptBlockId => { const id = Symbol(label); this.ids[ id ] = true; return id; diff --git a/lib/error.ts b/lib/error.ts index 0a0e3e5..42f705d 100644 --- a/lib/error.ts +++ b/lib/error.ts @@ -43,6 +43,8 @@ export interface ErrorData { headers?: OutgoingHttpHeaders; statusCode?: number; + location?: string; + errno?: number | undefined; code?: string | undefined; path?: string | undefined; @@ -105,6 +107,7 @@ export class DescriptError { stack: error.stack, errno: error.errno, syscall: error.syscall, + location: error.location, }; this.error = _error; diff --git a/lib/firstBlock.ts b/lib/firstBlock.ts index ad472bd..0361a3b 100644 --- a/lib/firstBlock.ts +++ b/lib/firstBlock.ts @@ -87,7 +87,8 @@ class FirstBlock< // eslint-disable-next-line @typescript-eslint/no-unused-vars ExtendedResultOut extends BlockResultOut, - ExtendedParamsOut = Params, + //ExtendedCustomBlock extends FirstBlockDefinition, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, ExtendedBlockResult = ResultOut, ExtendedBeforeResultOut = undefined, @@ -95,28 +96,13 @@ class FirstBlock< ExtendedErrorResultOut = undefined, >({ options }: { options: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }) { - return this.extendClass< - FirstBlock< - Context, - Block, - ExtendedResultOut, - Params & ExtendedParamsOut, - ExtendedBlockResult, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut, - ExtendedParams - >, - ExtendedBlockResult, - ExtendedParamsOut, - ExtendedParams, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut - >({ options }); + return new FirstBlock({ + block: this.extendBlock(this.block), + options: this.extendOptions(this.options, options) as typeof options, + }); } protected initBlock(block: FirstBlockDefinition) { diff --git a/lib/functionBlock.ts b/lib/functionBlock.ts index a326916..b6a34a3 100644 --- a/lib/functionBlock.ts +++ b/lib/functionBlock.ts @@ -2,7 +2,7 @@ import BaseBlock from './block'; import type { DescriptBlockDeps } from './depsDomain'; import DepsDomain from './depsDomain'; import { createError, ERROR_ID } from './error'; -import type { BlockResultOut, DescriptBlockOptions } from './types'; +import type { BlockResultOut, DescriptBlockOptions, InferParamsOutFromBlock } from './types'; import type ContextClass from './context'; import type Cancel from './cancel'; @@ -27,7 +27,7 @@ class FunctionBlock< BeforeResultOut = undefined, AfterResultOut = undefined, ErrorResultOut = undefined, - Params = ParamsOut + Params = never extends InferParamsOutFromBlock ? ParamsOut : InferParamsOutFromBlock > extends BaseBlock< Context, FunctionBlockDefinition, @@ -93,40 +93,26 @@ class FunctionBlock< } extend< - // eslint-disable-next-line @typescript-eslint/no-unused-vars - ExtendedResultOut extends - BlockResultOut, - ExtendedParamsOut = Params, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + //ExtendedCustomBlock = DescriptHttpBlockDescription, + ExtendedBlockResult = ResultOut, ExtendedBeforeResultOut = undefined, ExtendedAfterResultOut = undefined, ExtendedErrorResultOut = undefined, >({ options }: { options: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }) { - return this.extendClass< - FunctionBlock< - Context, - //FNBlock, - //FunctionBlockDefinition, - Params & ExtendedParamsOut, - ExtendedBlockResult, - ExtendedResultOut, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut, - ExtendedParams - >, - ExtendedBlockResult, - Params & ExtendedParamsOut, - ExtendedParams, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut - >({ options }); + return new FunctionBlock({ + block: this.extendBlock(this.block) as typeof this.block, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + //@ts-expect-error + options: this.extendOptions(this.options, options) as typeof options, + }); } } diff --git a/lib/httpBlock.ts b/lib/httpBlock.ts index f394192..310b3d4 100644 --- a/lib/httpBlock.ts +++ b/lib/httpBlock.ts @@ -24,8 +24,8 @@ type DescriptHttpBlockDescriptionCallback< T, Params, Context > = T | ((args: { deps: DescriptBlockDeps; }) => T); -type DescriptHttpBlockQueryValue = string | number | boolean | undefined | null | Array; -type DescriptHttpBlockQuery = Record< string, DescriptHttpBlockQueryValue >; +export type DescriptHttpBlockQueryValue = string | number | boolean | undefined | null | Array; +export type DescriptHttpBlockQuery = Record< string, DescriptHttpBlockQueryValue >; type HttpQuery = Record< string, @@ -46,7 +46,7 @@ DescriptHttpBlockQueryValue | }) => DescriptHttpBlockQuery ); -type HttpHeaders = Record< string, +export type HttpHeaders = Record< string, string | ((args: { params: Params; @@ -179,41 +179,41 @@ class HttpBlock< Params > { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + extend< - ExtendedResultOut extends - BlockResultOut, - ExtendedParamsOut = Params, + ExtendedResultOut extends BlockResultOut, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + //ExtendedCustomBlock = DescriptHttpBlockDescription, ExtendedBlockResult = ResultOut, ExtendedBeforeResultOut = undefined, ExtendedAfterResultOut = undefined, ExtendedErrorResultOut = undefined, >({ options, block }: { - block?: DescriptHttpBlockDescription; + block?: DescriptHttpBlockDescription; options?: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }) { - return this.extendClass< - HttpBlock< + const x = new HttpBlock< Context, ExtendedParamsOut, HttpResult, - Params & ExtendedResultOut, + ExtendedResultOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams - >, - ExtendedBlockResult, - Params & ExtendedParamsOut, - ExtendedParams, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut - >({ options, block }); + >({ + block: this.extendBlock(block), + options: this.extendOptions(this.options, options), + }); + + return x; } protected logger: Logger; @@ -237,7 +237,7 @@ class HttpBlock< // this._compiled_props = compile_props( this.block ); } - protected extendBlock(by: DescriptHttpBlockDescription = {}) { + protected extendBlock(by: DescriptHttpBlockDescription = {}) { const what = this.block; const headers = extendOption(what.headers, by.headers); const query = extendOption(what.query, by.query); diff --git a/lib/index.d2.jtxt b/lib/index.d2.jtxt deleted file mode 100644 index fdacaa9..0000000 --- a/lib/index.d2.jtxt +++ /dev/null @@ -1,825 +0,0 @@ -import type { ZlibOptions } from 'node:zlib'; - -type First< T > = - T extends readonly [ infer First, ...infer Rest ] | [ infer First, ...infer Rest ] ? First : never; - -type Tail< T > = - T extends readonly [ infer First, ...infer Rest ] | [ infer First, ...infer Rest ] ? Rest : never; - -type Equal< A, B > = A extends B ? (B extends A ? A : never) : never; - - -type InferContext = Type extends DescriptBlock< infer Context, infer ParamsIn, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? Context : never; -type InferParamsIn = Type extends DescriptBlock< infer Context, infer ParamsIn, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ParamsIn : never; -type InferParamsOut = Type extends DescriptBlock< infer Context, infer ParamsIn, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ParamsOut : never; -type InferResultIn = Type extends DescriptBlock< infer Context, infer ParamsIn, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ResultIn : never; -type InferResultOut = Type extends DescriptBlock< infer Context, infer ParamsIn, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ResultOut : never; - - -type DescriptBlockParams = { - b: BlockParams; - i: ParamsFnIn; - o: ParamsFnOut; -}; -type GetDescriptBlockParamsBlock = T extends DescriptBlockParams ? BlockParams : T; -type GetDescriptBlockParamsFnIn = T extends DescriptBlockParams ? ParamsFnIn : T; -type GetDescriptBlockParamsFnOut = T extends DescriptBlockParams ? ParamsFnOut : T; - -type DescriptBlockResult = { - i: ResultIn; - o: ResultOut; -}; -type GetDescriptBlockResultIn = T extends DescriptBlockResult ? ResultIn : T; -type GetDescriptBlockResultOut = T extends DescriptBlockResult ? ResultOut : T; - -type InferResultInFromBlockOrReturnResultIn = Type extends DescriptBlock< infer Context, infer ParamsIn, infer Result, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? Result : Type; -type InferResultOutFromBlockOrReturnResultOut = Type extends DescriptBlock< infer Context, infer ParamsIn, infer Result, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ResultOut : Type; -type InferParamsFromBlockOrReturnParams = Block extends DescriptBlock< infer Context, infer ParamsIn, infer Result, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ParamsIn : Params; - -type InferResultInFromBlocks = Block extends DescriptBlock< infer Context, infer ParamsIn, infer Result, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? - { [ K in keyof Result ]: InferResultInFromBlocks } : - Block; - -type NotUnknown = NonNullable; - -type InferResultOutFromBlocks = Block extends DescriptBlock< infer Context, infer ParamsIn, infer Result, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? - { [ K in keyof ResultOut ]: InferResultOutFromBlocks } : - Block; - -// --------------------------------------------------------------------------------------------------------------- // - -import type { OutgoingHttpHeaders } from 'http'; -import type { - RequestOptions as HttpsRequestOptions, - Agent as HttpsAgent, - AgentOptions as HttpsAgentOptions, -} from 'https'; - -// --------------------------------------------------------------------------------------------------------------- // - -type DescriptJSON = - boolean | - number | - string | - undefined | - null | - { [ property: string ]: DescriptJSON } | - object | - Array< DescriptJSON >; - -// --------------------------------------------------------------------------------------------------------------- // - -interface DescriptError { - error: { - id: string; - message?: string; - // для http-ошибок - body?: Error; - headers?: OutgoingHttpHeaders; - status_code?: number; - }; -} - -// --------------------------------------------------------------------------------------------------------------- // - -declare class Cancel { - cancel(reason: DescriptError): void; -} - -// --------------------------------------------------------------------------------------------------------------- // - -declare class Cache< Result, Context > { - get(args: { key: string; context: Context }): Result | Promise< Result >; - set(args: { key: string; value: Result; maxage: number; context: Context }): void; -} - -// --------------------------------------------------------------------------------------------------------------- // - -interface DescriptRequestOptions { - http_options: HttpsRequestOptions; - body: string | Buffer | null; - url: string; - - retries: number; - - extra?: { - name: string; - }; -} - -// --------------------------------------------------------------------------------------------------------------- // - -interface DescriptBlockResultJSON { - result: Result; -} - -interface DescriptHttpResult { - status_code: number; - headers: Record< string, string >; - request_options: HttpsRequestOptions; - body: Buffer; -} - -interface LoggerEvent { - type: EVENT; - - request_options: DescriptRequestOptions; - - timestamps: { - start: number; - socket: number; - tcp_connection: number; - body: number; - request_end: number; - end: number; - }; - - result?: DescriptHttpResult; - - error?: DescriptError; -} - -interface DescriptLogger< Context > { - log(event: LoggerEvent, context: Context): void; -} - -declare namespace Logger { - export enum EVENT { - REQUEST_START = 'REQUEST_START', - REQUEST_SUCCESS = 'REQUEST_SUCCESS', - REQUEST_ERROR = 'REQUEST_ERROR', - } -} - -// --------------------------------------------------------------------------------------------------------------- // - -type DescriptBlockId = symbol; -type DescriptBlockGenerateId = () => DescriptBlockId; - -type DescriptBlockDeps = Record< DescriptBlockId, any >; - -type ComplexResult = Result | GetDescriptBlockResultOut | Promise< Result | GetDescriptBlockResultOut > | void | null; - -interface DescriptBlockOptions< - Context, - Params, - ResultIn, - ParamsOut = Params, - BeforeResultOut = ResultIn, - ErrorResultOut = ResultIn, - AfterResultIn = ResultIn, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> { - name?: string; - - id?: DescriptBlockId; - deps?: DescriptBlockId | Array< DescriptBlockId >; - - params?: (args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - }) => ParamsOut; - - before?: (args: { - params: ParamsOut; - context: Context; - deps: DescriptBlockDeps; - cancel: Cancel; - }) => ComplexResult; - - after?: (args: { - params: ParamsOut; - context: Context; - deps: DescriptBlockDeps; - cancel: Cancel; - result: AfterResultIn; - }) => ComplexResult; - - error?: (args: { - params: ParamsOut; - context: Context; - deps: DescriptBlockDeps; - cancel: Cancel; - error: DescriptError; - }) => ComplexResult; - - timeout?: number; - - key?: string | ((args: { - params: ParamsOut; - context: Context; - deps: DescriptBlockDeps; - }) => string); - maxage?: number; - cache?: Cache< ResultOut | GetDescriptBlockResultOut, Context >; - - required?: boolean; - - logger?: DescriptLogger< Context >; -} - -// --------------------------------------------------------------------------------------------------------------- // -// HttpBlock - -type DescriptHttpBlockDescriptionCallback< T, Params, Context > = T | ((args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; -}) => T); - -type DescriptHttpBlockQueryValue = string | number | boolean | undefined | Array; -type DescriptHttpBlockQuery = Record< string, DescriptHttpBlockQueryValue >; - -type DescriptHttpBlockHeaders = Record< string, string >; - -interface DescriptHttpBlockDescription< Params, Context > { - protocol?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - hostname?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - port?: DescriptHttpBlockDescriptionCallback< number, Params, Context >; - method?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - pathname?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - - query?: - Record< string, - DescriptHttpBlockQueryValue | - null | - ((args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - query: DescriptHttpBlockQuery; - }) => DescriptHttpBlockQueryValue) - > | - ( - (args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - query: DescriptHttpBlockQuery; - }) => DescriptHttpBlockQuery - ); - - headers?: - Record< string, - string | - ((args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - headers: DescriptHttpBlockHeaders; - }) => string) - > | - ( - (args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - headers: DescriptHttpBlockHeaders; - }) => DescriptHttpBlockHeaders - ); - - body?: - string | - Buffer | - ((args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - }) => string | Buffer | DescriptJSON); - - body_compress?: boolean | ZlibOptions; - - is_json?: boolean; - - timeout?: number; - - is_error?: (error: DescriptError, request_options: DescriptRequestOptions) => boolean; - - is_retry_allowed?: (error: DescriptError, request_options: DescriptRequestOptions) => boolean; - max_retries?: number; - retry_timeout?: number; - - prepare_request_options?: (options: HttpsRequestOptions) => HttpsRequestOptions; - parse_body?: (result: { headers: Record< string, string >; body?: Buffer }, context: Context) => {}; - - family?: DescriptHttpBlockDescriptionCallback< number, Params, Context >; - - agent?: HttpsAgent | HttpsAgentOptions | false; - - auth?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - - ca?: DescriptHttpBlockDescriptionCallback< string | Buffer, Params, Context >; - cert?: DescriptHttpBlockDescriptionCallback< string | Buffer, Params, Context >; - ciphers?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - key?: DescriptHttpBlockDescriptionCallback< string | Buffer, Params, Context >; - passphrase?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - pfx?: DescriptHttpBlockDescriptionCallback< string | Buffer, Params, Context >; - rejectUnauthorized?: DescriptHttpBlockDescriptionCallback< boolean, Params, Context >; - secureProtocol?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; - servername?: DescriptHttpBlockDescriptionCallback< string, Params, Context >; -} - -interface DescriptHttpBlock< - Context, - Params, - Result, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = Result, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> { - < - ExtendedParams = Params, - ExtendedResult = Result, - ExtendedParamsOut = ExtendedParams, - ExtendedBeforeResultOut = undefined, - ExtendedErrorResultOut = undefined, - ExtendedAfterResultIn = ExtendedResult, - ExtendedAfterResultOut = ExtendedAfterResultIn, - ExtendedResultOut = Exclude, - >(args: { - block?: DescriptHttpBlockDescription< GetDescriptBlockParamsFnOut, Context >; - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn, - GetDescriptBlockParamsFnOut, - ExtendedBeforeResultOut, - ExtendedErrorResultOut, - GetDescriptBlockResultIn, - ExtendedAfterResultOut, - ExtendedResultOut - >; - }): DescriptHttpBlock< - Context, - DescriptBlockParams, GetDescriptBlockParamsFnOut>, - GetDescriptBlockResultOut> - >; -} - -declare function http< - Context, - Params, - Result, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = Result, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> ( - args: { - block: DescriptHttpBlockDescription< GetDescriptBlockParamsFnOut, Context >; - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn, - GetDescriptBlockParamsFnOut, - BeforeResultOut, - ErrorResultOut, - GetDescriptBlockResultIn, - AfterResultOut, - ResultOut - >; - }, -): DescriptHttpBlock< -Context, -DescriptBlockParams, GetDescriptBlockParamsFnOut>, -GetDescriptBlockResultOut ->; - -// --------------------------------------------------------------------------------------------------------------- // -// FuncBlock - -type DescriptFuncBlockDescription< - Context, - Params, - Result, -> = (args: { - params: Params; - context: Context; - deps: DescriptBlockDeps; - generate_id: DescriptBlockGenerateId; - cancel: Cancel; -}) => -Result | -Promise; - -interface DescriptFuncBlock< - Context, - Params, - Result, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = Result, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> { - < - ExtendedParams = Params, - ExtendedResult = Result, - ExtendedParamsOut = ExtendedParams, - ExtendedBeforeResultOut = undefined, - ExtendedErrorResultOut = undefined, - ExtendedAfterResultIn = ExtendedResult, - ExtendedAfterResultOut = ExtendedAfterResultIn, - ExtendedResultOut = ExtendedBeforeResultOut | ExtendedErrorResultOut | ExtendedAfterResultOut, - >(args: { - block?: DescriptFuncBlockDescription< Context, GetDescriptBlockParamsFnOut, GetDescriptBlockResultIn>; - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn>, - GetDescriptBlockParamsFnOut, - ExtendedBeforeResultOut, - ExtendedErrorResultOut, - GetDescriptBlockResultIn>, - ExtendedAfterResultOut, - ExtendedResultOut - >; - }): DescriptFuncBlock< Context, - DescriptBlockParams>, GetDescriptBlockParamsFnOut>>, - GetDescriptBlockResultOut> - >; -} - -declare function func< - Context, - Params, - ResultIn, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = ResultIn, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> ( - args: { - block: DescriptFuncBlockDescription< Context, GetDescriptBlockParamsFnOut, GetDescriptBlockResultIn >; - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn>, - GetDescriptBlockParamsFnOut, - BeforeResultOut, - ErrorResultOut, - GetDescriptBlockResultIn>, - AfterResultOut, - ResultOut - >; - }, -): DescriptFuncBlock< Context, -DescriptBlockParams>, GetDescriptBlockParamsFnOut>>, -GetDescriptBlockResultOut> ->; - -// --------------------------------------------------------------------------------------------------------------- // -// ArrayBlock - -type GetArrayBlockResult< T > = { - 0: never; - 1: [ GetDescriptBlockResult< First< T > > ]; - 2: [ GetDescriptBlockResult< First< T > >, ...GetArrayBlockResult< Tail< T > > ]; -}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ]; - -type GetArrayBlockParams< T > = { - 0: never; - 1: GetDescriptBlockParams< First< T > >; - 2: GetDescriptBlockParams< First< T > > & GetArrayBlockParams< Tail< T > >; -}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ]; - -type GetArrayBlockContext< T > = { - 0: never; - 1: GetDescriptBlockContext< First< T > >; - 2: Equal< GetDescriptBlockContext< First< T > >, GetArrayBlockContext< Tail< T > > >; -}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ]; - -type DescriptArrayBlockDescription< T > = { - [ P in keyof T ]: T[ P ] extends DescriptBlock< infer Context, infer Params, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer Result > ? T[ P ] : never -} - -interface DescriptArrayBlock< - Context, - Params, - ResultIn, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = ResultIn, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> { - < - ExtendedParams = Params, - ExtendedResultIn = ResultIn, - ExtendedParamsOut = ExtendedParams, - ExtendedBeforeResultOut = undefined, - ExtendedErrorResultOut = undefined, - ExtendedAfterResultIn = ExtendedResultIn, - ExtendedAfterResultOut = ExtendedAfterResultIn, - ExtendedResultOut = Exclude, - >(args: { - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - ExtendedResultIn, - GetDescriptBlockParamsFnOut, - ExtendedBeforeResultOut, - ExtendedErrorResultOut, - ExtendedAfterResultIn, - ExtendedAfterResultOut, - ExtendedResultOut - >; - }): DescriptArrayBlock< - Context, - DescriptBlockParams, GetDescriptBlockParamsFnOut>, - GetDescriptBlockResultOut> - >; -} - -declare function array< - Block extends ReadonlyArray< unknown >, - Context = GetArrayBlockContext< Block >, - Params = GetArrayBlockParams< Block >, - ResultIn = GetArrayBlockResult< Block >, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = GetArrayBlockResult< Block >, - AfterResultOut = GetArrayBlockResult< Block >, - ResultOut = Exclude, -> ( - args: { - block: DescriptArrayBlockDescription< Block >; - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - ResultIn, - GetDescriptBlockParamsFnOut, - BeforeResultOut, - ErrorResultOut, - AfterResultIn, - AfterResultOut, - ResultOut - >; - }, -): DescriptArrayBlock< -Context, -DescriptBlockParams, GetDescriptBlockParamsFnOut>, -GetDescriptBlockResultOut> ->; - -// --------------------------------------------------------------------------------------------------------------- // -// ObjectBlock - -type GetObjectBlockResultIn< T > = { - [ P in keyof T ]: InferResultIn -} -type GetObjectBlockResultOut< T > = { - [ P in keyof T ]: InferResultOut -} -type GetObjectBlockParamsMap< T extends {} > = { - [ P in keyof T ]: InferParamsIn; -} - -type GetObjectBlockParamsBlockMap< T extends {} > = { - [ P in keyof T ]: GetDescriptBlockParamsBlock>; -} - -type GetObjectBlockParamsFnInMap< T extends {} > = { - [ P in keyof T ]: GetDescriptBlockParamsFnIn>; -} - -type GetObjectBlockParamsFnOutMap< T extends {} > = { - [ P in keyof T ]: GetDescriptBlockParamsFnOut>; -} - -type GetObjectBlockParams< - T extends {}, - PB = GetObjectBlockParamsBlockMap< T >, - PI = GetObjectBlockParamsFnInMap< T >, - PO = GetObjectBlockParamsFnOutMap< T >, -> = DescriptBlockParams< -UnionToIntersection, -UnionToIntersection, -UnionToIntersection ->; - -type GetObjectBlockContextMap< T extends {} > = { - [ P in keyof T ]: InferContext; -} -// Тут не совсем правда, но лучше пока непонятно как сделать. -type GetObjectBlockContext< T extends {}, M = GetObjectBlockContextMap< T > > = UnionToIntersection< M[ keyof M ] >; - -type DescriptObjectBlockDescription< T extends {} > = { - [ P in keyof T ]: T[ P ] extends DescriptBlock< infer Context, infer Params, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? T[ P ] : never -} - -type DescriptObjectBlockDescriptionResults< T extends {} > = { - [ P in keyof T ]: T[ P ] extends DescriptBlock< infer Context, infer Params, infer ResultIn, infer ParamsOut, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer ResultOut > ? ResultOut : never -} - -interface DescriptObjectBlock< - Context, - Params, - ResultIn, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = ResultIn, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> { - < - ExtendedParams = Params, - ExtendedResultIn = ResultIn, - ExtendedParamsOut = ExtendedParams, - ExtendedBeforeResultOut = undefined, - ExtendedErrorResultOut = undefined, - ExtendedAfterResultIn = ExtendedResultIn, - ExtendedAfterResultOut = ExtendedAfterResultIn, - ExtendedResultOut = Exclude, - >(args: { - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn>, - GetDescriptBlockParamsFnOut, - ExtendedBeforeResultOut, - ExtendedErrorResultOut, - ExtendedAfterResultIn, - ExtendedAfterResultOut, - ExtendedResultOut - >; - }): DescriptObjectBlock< - Context, - DescriptBlockParams, GetDescriptBlockParamsFnOut>, - GetDescriptBlockResultOut> - >; -} - -declare function object< - Block extends {}, - Context = GetObjectBlockContext< Block >, - Params = GetObjectBlockParams< Block >, - ResultIn = GetObjectBlockResultIn< Block >, - ParamsOut = Params, - //ResultOut = GetObjectBlockResultOut, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = GetObjectBlockResultOut, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> ( - args: { - block: DescriptObjectBlockDescription< Block >; - options?: DescriptBlockOptions< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn>, - GetDescriptBlockParamsFnOut, - BeforeResultOut, - ErrorResultOut, - GetDescriptBlockResultIn>, - AfterResultOut, - ResultOut - - /*GetDescriptBlockResultIn>, - GetDescriptBlockResultIn>, - GetDescriptBlockResultIn>, - InferResultOutFromBlocks, - InferResultOutFromBlocks*/ - >; - }, -): DescriptObjectBlock< -Context, -DescriptBlockParams, GetDescriptBlockParamsFnOut>, -GetDescriptBlockResultOut> ->; - -// --------------------------------------------------------------------------------------------------------------- // -/* -// --------------------------------------------------------------------------------------------------------------- // - -type DescriptPipeBlockDescription = Array< DescriptBlock >; - -interface DescriptPipeBlock { - ( args: { block?: DescriptPipeBlockDescription, options?: DescriptBlockOptions } ): DescriptPipeBlock; -} - -// --------------------------------------------------------------------------------------------------------------- // - -type DescriptFirstBlockDescription = Array< DescriptBlock >; - -interface DescriptFirstBlock { - ( args: { block?: DescriptFirstBlockDescription, options?: DescriptBlockOptions } ): DescriptFirstBlock; -} - -// --------------------------------------------------------------------------------------------------------------- // - -*/ - -type DescriptBlock< Context, Params, Result, ParamsOut = Params, BeforeResultOut = Result, ErrorResultOut = Result, AfterResultIn = Result, AfterResultOut = Result, ResultOut = BeforeResultOut | ErrorResultOut | AfterResultOut > = - DescriptHttpBlock< Context, Params, Result, ParamsOut, BeforeResultOut, ErrorResultOut, AfterResultIn, AfterResultOut, ResultOut > | - DescriptFuncBlock< Context, Params, Result, ParamsOut, BeforeResultOut, ErrorResultOut, AfterResultIn, AfterResultOut, ResultOut > | - DescriptArrayBlock< Context, Params, Result, ParamsOut, BeforeResultOut, ErrorResultOut, AfterResultIn, AfterResultOut, ResultOut > | - DescriptObjectBlock< Context, Params, Result, ParamsOut, BeforeResultOut, ErrorResultOut, AfterResultIn, AfterResultOut, ResultOut >; - -// --------------------------------------------------------------------------------------------------------------- // - -export type GetDescriptBlockResult< T > = - T extends DescriptBlock< infer Context, infer Params, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer Result > ? Result : never; - -export type GetDescriptBlockParams< T > = - T extends DescriptBlock< infer Context, infer Params, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer Result > ? Params : never; - -export type GetDescriptBlockContext< T > = - T extends DescriptBlock< infer Context, infer Params, infer BeforeResultOut, infer ErrorResultOut, infer AfterResultIn, infer AfterResultOut, infer Result > ? Context : never; - -// --------------------------------------------------------------------------------------------------------------- // - -declare function run< - Context, - Params, - Result, - ParamsOut = Params, - BeforeResultOut = undefined, - ErrorResultOut = undefined, - AfterResultIn = Result, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, -> ( - block: DescriptBlock< - Context, - GetDescriptBlockParamsFnIn, - GetDescriptBlockResultIn>, - GetDescriptBlockParamsFnOut, - BeforeResultOut, - ErrorResultOut, - AfterResultIn, - AfterResultOut, - ResultOut - >, - args: { - params?: GetDescriptBlockParamsBlock; - context: Context; - }, -): Promise< GetDescriptBlockResultOut>> ; - -// --------------------------------------------------------------------------------------------------------------- // - -declare function is_block(block: any): boolean; - -// --------------------------------------------------------------------------------------------------------------- // - -declare function error(error: { id: string; [ key: string ]: any }): DescriptError; -declare function is_error(error: any): error is DescriptError; - -declare enum ERROR_ID { - ALL_BLOCKS_FAILED = 'ALL_BLOCKS_FAILED', - BLOCK_TIMED_OUT = 'BLOCK_TIMED_OUT', - DEPS_ERROR = 'DEPS_ERROR', - DEPS_NOT_RESOLVED = 'DEPS_NOT_RESOLVED', - HTTP_REQUEST_ABORTED = 'HTTP_REQUEST_ABORTED', - HTTP_UNKNOWN_ERROR = 'HTTP_UNKNOWN_ERROR', - INCOMPLETE_RESPONSE = 'INCOMPLETE_RESPONSE', - INVALID_BLOCK = 'INVALID_BLOCK', - INVALID_DEPS_ID = 'INVALID_DEPS_ID', - INVALID_JSON = 'INVALID_JSON', - INVALID_OPTIONS_PARAMS = 'INVALID_OPTIONS_PARAMS', - PARSE_BODY_ERROR = 'PARSE_BODY_ERROR', - REQUEST_TIMEOUT = 'REQUEST_TIMEOUT', - REQUIRED_BLOCK_FAILED = 'REQUIRED_BLOCK_FAILED', - TCP_CONNECTION_TIMEOUT = 'TCP_CONNECTION_TIMEOUT', - TOO_MANY_AFTERS_OR_ERRORS = 'TOO_MANY_AFTERS_OR_ERRORS', - UNKNOWN_ERROR = 'UNKNOWN_ERROR', -} - -// --------------------------------------------------------------------------------------------------------------- // - -declare namespace request { - interface DefaultOptions { - is_error: (error: DescriptError, request_options: DescriptRequestOptions) => boolean; - is_retry_allowed: (error: DescriptError, request_options: DescriptRequestOptions) => boolean; - } - - export const DEFAULT_OPTIONS: DefaultOptions; -} - - -declare function inferBlockTypes< - Context, - Params, - ResultIn, - ParamsOut = Params, - BeforeResultOut = ResultIn, - ErrorResultOut = ResultIn, - AfterResultIn = ResultIn, - AfterResultOut = AfterResultIn, - ResultOut = Exclude, ->(block: DescriptBlock): typeof block; diff --git a/lib/index.ts b/lib/index.ts index 403c4b6..9d52dae 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,10 +3,10 @@ import { ERROR_ID, createError, isError, DescriptError } from './error'; import Cancel from './cancel'; import Logger from './logger'; -import Cache from './cache'; +import Cache, { CacheItem } from './cache'; import request from './request'; - +import type { GenerateId, DescriptBlockDeps, DescriptBlockId } from './depsDomain'; import Block from './block'; import ArrayBlock from './arrayBlock'; import ObjectBlock from './objectBlock'; @@ -15,9 +15,17 @@ import FunctionBlock from './functionBlock'; import HttpBlock from './httpBlock'; import FirstBlock from './firstBlock'; -import type { DescriptHttpBlockResult, BlockResultOut, DescriptBlockOptions } from './types'; +import type { + DescriptHttpBlockResult, + BlockResultOut, + DescriptBlockOptions, + DescriptHttpBlockHeaders, + InferResultFromBlock, + InferParamsInFromBlock, + InferBlock, +} from './types'; import type BaseBlock from './block'; -import type { DescriptHttpBlockDescription } from './httpBlock'; +import type { DescriptHttpBlockDescription, DescriptHttpBlockQuery, DescriptHttpBlockQueryValue } from './httpBlock'; import type { GetObjectBlockParams, GetObjectBlockResult, ObjectBlockDefinition } from './objectBlock'; import type { GetArrayBlockParams, GetArrayBlockResult, ArrayBlockDefinition } from './arrayBlock'; import type { GetFirstBlockParams, GetFirstBlockResult, FirstBlockDefinition } from './firstBlock'; @@ -169,6 +177,7 @@ const run = function< export { Logger, Cache, + CacheItem, request, ERROR_ID, createError as error, @@ -184,4 +193,18 @@ export { run, DescriptError, DescriptHttpBlockResult, + DescriptHttpBlockHeaders, + DescriptHttpBlockDescription, + DescriptHttpBlockQuery, + DescriptHttpBlockQueryValue, + GenerateId, + DescriptBlockId, + InferResultFromBlock, + InferParamsInFromBlock, + InferBlock, + DescriptBlockDeps, + BaseBlock, + BlockResultOut, + + HttpBlock, }; diff --git a/lib/objectBlock.ts b/lib/objectBlock.ts index fecb11a..2df7443 100644 --- a/lib/objectBlock.ts +++ b/lib/objectBlock.ts @@ -2,7 +2,7 @@ import CompositeBlock from './compositeBlock'; import type { DescriptError } from './error'; import { createError, ERROR_ID } from './error'; import type BaseBlock from './block'; -import type { InferParamsInFromBlock, InferResultFromBlock, DescriptBlockOptions, BlockResultOut, UnionToIntersection } from './types'; +import type { InferParamsInFromBlock, DescriptBlockOptions, BlockResultOut, UnionToIntersection, InferResultFromBlock } from './types'; import type ContextClass from './context'; import type Cancel from './cancel'; import type { DescriptBlockDeps } from './depsDomain'; @@ -112,7 +112,8 @@ class ObjectBlock< // eslint-disable-next-line @typescript-eslint/no-unused-vars ExtendedResultOut extends BlockResultOut, - ExtendedParamsOut = Params, + //ExtendedCustomBlock extends ObjectBlockDefinition, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, ExtendedBlockResult = ResultOut, ExtendedBeforeResultOut = undefined, @@ -120,28 +121,13 @@ class ObjectBlock< ExtendedErrorResultOut = undefined, >({ options }: { options: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }) { - return this.extendClass< - ObjectBlock< - Context, - Blocks, - ExtendedResultOut, - Params & ExtendedParamsOut, - ExtendedBlockResult, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut, - ExtendedParams - >, - ExtendedBlockResult, - Params & ExtendedParamsOut, - ExtendedParams, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut - >({ options }); + return new ObjectBlock({ + block: this.extendBlock(this.block), + options: this.extendOptions(this.options, options) as typeof options, + }); } } diff --git a/lib/pipeBlock.ts b/lib/pipeBlock.ts index 4ad0749..6c5a07c 100644 --- a/lib/pipeBlock.ts +++ b/lib/pipeBlock.ts @@ -85,9 +85,10 @@ class PipeBlock< extend< // eslint-disable-next-line @typescript-eslint/no-unused-vars - ExtendedResultOut extends - BlockResultOut, - ExtendedParamsOut = Params, + //ExtendedResultOut extends + //BlockResultOut, + //ExtendedCustomBlock extends PipeBlockDefinition, + ExtendedParamsOut extends Params = Params, ExtendedParams = Params, ExtendedBlockResult = ResultOut, ExtendedBeforeResultOut = undefined, @@ -95,28 +96,13 @@ class PipeBlock< ExtendedErrorResultOut = undefined, >({ options }: { options: DescriptBlockOptions< - Context, Params & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams + Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams >; }) { - return this.extendClass< - PipeBlock< - Context, - Block, - ExtendedResultOut, - Params & ExtendedParamsOut, - ExtendedBlockResult, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut, - ExtendedParams - >, - ExtendedBlockResult, - ExtendedParamsOut, - ExtendedParams, - ExtendedBeforeResultOut, - ExtendedAfterResultOut, - ExtendedErrorResultOut - >({ options }); + return new PipeBlock({ + block: this.extendBlock(this.block), + options: this.extendOptions(this.options, options) as typeof options, + }); } diff --git a/lib/types.ts b/lib/types.ts index 0847b6f..aeead71 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -59,12 +59,12 @@ export type BlockResultOut< ErrorResultOut > = (never | undefined)extends ErrorResultOut ? - AfterResultOut extends undefined ? + undefined extends AfterResultOut ? never | undefined extends BeforeResultOut ? BlockResult : BeforeResultOut | BlockResult : AfterResultOut : - AfterResultOut extends undefined ? + undefined extends AfterResultOut ? never | undefined extends BeforeResultOut ? BlockResult | ErrorResultOut : BeforeResultOut | BlockResult | ErrorResultOut : @@ -77,6 +77,13 @@ infer Context, infer CustomBlock, infer ParamsOut, infer ResultOut, infer Interm infer BlockResult, infer BeforeResultOut, infer AfterResultOut, infer ErrorResultOut, infer Params > ? InferResultOrResult : Result; +export type InferResultOrResultOnce = Result extends BaseBlock< +// eslint-disable-next-line @typescript-eslint/no-unused-vars +infer Context, infer CustomBlock, infer ParamsOut, infer ResultOut, infer IntermediateResult, +// eslint-disable-next-line @typescript-eslint/no-unused-vars +infer BlockResult, infer BeforeResultOut, infer AfterResultOut, infer ErrorResultOut, infer Params +> ? ResultOut : Result; + export type InferResultFromBlock = Type extends BaseBlock< // eslint-disable-next-line @typescript-eslint/no-unused-vars infer Context, infer CustomBlock, infer ParamsOut, infer ResultOut, infer IntermediateResult, @@ -89,7 +96,6 @@ export type InferParamsInFromBlock = Type extends BaseBlock< infer Context, infer CustomBlock, infer ParamsOut, infer ResultOut, infer IntermediateResult, // eslint-disable-next-line @typescript-eslint/no-unused-vars infer BlockResult, infer BeforeResultOut, infer AfterResultOut, infer ErrorResultOut, infer Params - > ? Params : never; @@ -109,6 +115,13 @@ infer BlockResult, infer BeforeResultOut, infer AfterResultOut, infer ErrorResul > ? ParamsOut : never; +export type InferBlock = Type extends BaseBlock< +// eslint-disable-next-line @typescript-eslint/no-unused-vars +infer Context, infer CustomBlock, infer ParamsOut, infer ResultOut, infer IntermediateResult, +// eslint-disable-next-line @typescript-eslint/no-unused-vars +infer BlockResult, infer BeforeResultOut, infer AfterResultOut, infer ErrorResultOut, infer Params +> ? Type : never; + export type InferContextFromBlock< T > = T extends BaseBlock< // eslint-disable-next-line @typescript-eslint/no-unused-vars infer Context, infer CustomBlock, infer ParamsOut, infer ResultOut, infer IntermediateResult, @@ -131,9 +144,9 @@ export interface DescriptBlockOptions< Context, ParamsOut, BlockResult, - BeforeResultOut, - AfterResultOut, - ErrorResultOut, + BeforeResultOut = undefined, + AfterResultOut = undefined, + ErrorResultOut = undefined, Params = ParamsOut, > { name?: string; @@ -159,7 +172,7 @@ export interface DescriptBlockOptions< context?: Context; deps: DescriptBlockDeps; cancel: Cancel; - result: BeforeResultOut extends (undefined | void) ? + result: (undefined) extends BeforeResultOut ? InferResultOrResult : InferResultOrResult | InferResultOrResult; }) => AfterResultOut; diff --git a/package.json b/package.json index 4113e76..4e684c7 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "typescript": "^5.6.2" }, "files": [ - "lib" + "build" ], "main": "./build/index.js", "engines": {