diff --git a/types/seneca/index.d.ts b/types/seneca/index.d.ts index ecf3c29096..4dfd82cb1c 100644 --- a/types/seneca/index.d.ts +++ b/types/seneca/index.d.ts @@ -1,7 +1,9 @@ // Type definitions for seneca v2.1.0 // Project: https://www.npmjs.com/package/seneca // Definitions by: Peter Snider +// Kevyn Bruyere // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped/seneca +// TypeScript Version: 2.3 declare module "seneca" { @@ -168,11 +170,21 @@ declare module "seneca" { sort$?: any; } + interface PartialMessagePayload { + transport$: object | {}; + plugin$: any; + fatal$: boolean; + tx$: string; + } + type MessagePayload = PartialMessagePayload & T; type Pattern = string | MinimalPattern; type GlobalErrorHandler = (error: Error) => void; - type AddCallback = (msg: any, respond: (error: Error | null, msg?: any) => void) => void; + type AddCallback = ( + msg: MessagePayload, + respond: (error: Error | null, msg?: any) => void, + ) => void; type ActCallback = (error: Error | null, result?: any) => void; type CloseCallback = (optional: any, done: (error: Error) => void) => void; type DatabaseID = string; @@ -199,10 +211,18 @@ declare module "seneca" { ready(callback: (error: Error) => void): void; - add(pattern: Pattern, action: AddCallback): this; - add(pattern: Pattern, paramspec: any, action: AddCallback): this; - act(pattern: Pattern, respond: ActCallback): void; - act(pattern: Pattern, msg: any, respond: ActCallback): void; + add( + pattern: PatternType, + action: AddCallback, + ): this; + add( + pattern: PatternType, + paramspec: any, + action: AddCallback, + ): this; + + act(pattern: PatternWithArgs, respond: ActCallback): void; + act(pattern: PatternWithArgs, msg: any, respond: ActCallback): void; make(entity_canon: string, properties?: any): Entity; make(base: string, entity_canon: string, properties?: any): Entity; make(zone: string, base: string, entity_canon: string, properties?: any): Entity; diff --git a/types/seneca/seneca-tests.ts b/types/seneca/seneca-tests.ts index 6ff0331ff6..376cede3f1 100644 --- a/types/seneca/seneca-tests.ts +++ b/types/seneca/seneca-tests.ts @@ -126,3 +126,53 @@ seneca.act('cmd:salestax,net:100,country:US,state:NY', function (err, result) { seneca.act('cmd:salestax,net:100,country:IE,category:reduced', function (err, result) { console.log('IE: ' + result.total) }) + +/** With Generic Type usage **/ +seneca.add({cmd: 'salestax'}, function(args, callback) { + var rate = 0.23; + var total = args.net * (1 + rate); + callback(null, {total: total}); +}); + +seneca.act({cmd: 'salestax', net: 100}, function(err, result) { + console.log(result.total); +}); + +seneca.add({cmd: 'config'}, function(args, callback) { + var config: any = { + // any added here due to following config[] access + rate: 0.23, + }; + var value = config[args.prop]; + callback(null, {value: value}); +}); + +seneca.add({cmd: 'salestax'}, function(args, callback) { + seneca.act({cmd: 'config', prop: 'rate'}, function(err, result) { + var rate = parseFloat(result.value); + var total = args.net * (1 + rate); + callback(null, {total: total}); + }); +}); + + +interface SalestaxPattern { + cmd: 'salestax'; +} + +interface SalestaxParams { + net: number; +} + +export interface SalesTaxRequest extends SalestaxPattern, SalestaxParams {} + +interface ConfigPattern { + cmd: 'config'; +} + +interface ConfigParams { + prop: string; +} + +export interface ConfigRequest extends ConfigPattern, ConfigParams {} +