mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-04 04:01:52 +00:00
add types for 'bagpipes' (#15857)
* add types for 'bagpipes.js' * fix typing feedback
This commit is contained in:
Executable
+100
@@ -0,0 +1,100 @@
|
||||
import * as Bagpipes from 'bagpipes';
|
||||
|
||||
// test `create`
|
||||
Bagpipes.create({});
|
||||
Bagpipes.create({ CustomPipe: [] });
|
||||
Bagpipes.create({ CustomPipe: [] }, undefined);
|
||||
Bagpipes.create({ CustomPipe: [] }, {});
|
||||
|
||||
// test `PipeDefs` Pipe Definitions Object
|
||||
const perDefsMixed: Bagpipes.PipeDefMap = {
|
||||
HelloWorld: [
|
||||
{ emit: 'Hello, World!' },
|
||||
'StringTypeDef'
|
||||
],
|
||||
xxx: 'ddd'
|
||||
};
|
||||
const pipesDefsEmpty: Bagpipes.PipeDefMap = {};
|
||||
|
||||
const pipesDefs: Bagpipes.PipeDefMap = {
|
||||
_router: {
|
||||
name: 'swagger_router',
|
||||
mockMode: false,
|
||||
mockControllersDirs: [ 'api/mocks' ],
|
||||
controllersDirs: [ 'api/controllers' ]
|
||||
},
|
||||
_swagger_validate: {
|
||||
name: 'swagger_validator',
|
||||
validateReponse: true
|
||||
},
|
||||
swagger_controllers: [
|
||||
'cors',
|
||||
'swagger_params_parser',
|
||||
'swagger_security',
|
||||
'_swagger_validate',
|
||||
'express_compatibility',
|
||||
'_router'
|
||||
]
|
||||
};
|
||||
|
||||
// test `Config` object
|
||||
const pipesConfigA: Bagpipes.Config = {
|
||||
connectMiddlewareDirs: ['some_dir', 'ssssss']
|
||||
};
|
||||
|
||||
const pipesConfigempty: Bagpipes.Config = { };
|
||||
|
||||
const pipesConfigFull: Bagpipes.Config = {
|
||||
connectMiddlewareDirs: ['test'],
|
||||
userFittingsDirs: ['test'],
|
||||
userViewsDirs: ['test']
|
||||
};
|
||||
|
||||
const pipesConfigFullEmpty: Bagpipes.Config = {
|
||||
connectMiddlewareDirs: [],
|
||||
userFittingsDirs: [],
|
||||
userViewsDirs: []
|
||||
};
|
||||
|
||||
let pipesA = Bagpipes.create(perDefsMixed, {
|
||||
connectMiddlewareDirs: ['some_dir', 'ssssss'],
|
||||
swaggerNodeRunner: {}
|
||||
});
|
||||
let pipeA = pipesA.getPipe('HelloWorld');
|
||||
|
||||
// log the output to standard out
|
||||
pipeA.fit((context, cb) => {
|
||||
cb(null, context);
|
||||
});
|
||||
|
||||
pipesA.play(pipeA, {});
|
||||
|
||||
// test cb with err
|
||||
const pipesEnty = Bagpipes.create({
|
||||
HelloWorld: []
|
||||
}, {});
|
||||
|
||||
// test fit a registered pipe and return error
|
||||
const pipeErrTest = pipesEnty.pipes['any'].fit((context, cb) => {
|
||||
cb(new Error("test err"));
|
||||
});
|
||||
pipesEnty.play(pipeErrTest, {});
|
||||
|
||||
const fittingsC = ["xxxx", "aaa"].map((name) => {
|
||||
let fittingDef = {} as Bagpipes.PipeDefMap;
|
||||
fittingDef[name] = 'nothing';
|
||||
return fittingDef;
|
||||
});
|
||||
const bagpipesC = Bagpipes.create({ fittings: fittingsC });
|
||||
|
||||
// test create with array of object literals
|
||||
const pipeWithObj = [{ emit: 'something' }];
|
||||
const bagpipesD = Bagpipes.create({ objPipe: pipeWithObj });
|
||||
bagpipesD.play(bagpipesD.getPipe('objPipe'), {});
|
||||
|
||||
// Test full create
|
||||
const userFittingsDirs = ['./fixtures/fittings'];
|
||||
const pipeWithString = ['emit'];
|
||||
let contextPlain = {};
|
||||
const bagpipesWithPipeAndFittings = Bagpipes.create({ myCustomPipe: pipeWithString }, { userFittingsDirs });
|
||||
bagpipesWithPipeAndFittings.play(bagpipesWithPipeAndFittings.getPipe('myCustomPipe'), contextPlain);
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
// Type definitions for bagpipes 0.1
|
||||
// Project: https://github.com/apigee-127/bagpipes
|
||||
// Definitions by: Michael Mrowetz <https://github.com/micmro>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
interface FittingContext {
|
||||
/** The input defined in the fitting definition
|
||||
* (string, number, object, array)
|
||||
*/
|
||||
input: any;
|
||||
/** Output to be delivered to the next fitting or client */
|
||||
output: any;
|
||||
[prop: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fitting types has pre-defined fittings `system` and `user`
|
||||
* but can be any any string for custom types like `swagger` or `node-machine`
|
||||
*/
|
||||
type FittingType = "system" | "user" | string;
|
||||
|
||||
interface FittingDef {
|
||||
/**
|
||||
* If type is omitted (as it must be for in-line usage), Bagpipes will
|
||||
* first check the user fittings then the system fittings for the name and
|
||||
* use the first fitting found.
|
||||
*
|
||||
* Thus be aware that if you define a fitting with the same name as a
|
||||
* system one, your fitting will override it.
|
||||
*/
|
||||
type?: FittingType;
|
||||
/** The name of the fitting of the type specified */
|
||||
name?: string;
|
||||
/** Static values passed to the fitting during construction */
|
||||
config?: any;
|
||||
/** Dynamic values passed to the fitting during execution */
|
||||
input?: any;
|
||||
/** The name of the context key to which the output value is assigned */
|
||||
output?: any;
|
||||
// allow other config settings
|
||||
[prop: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fitting Execution Function
|
||||
*
|
||||
* Will be called called when the `Pipe` it is contained it gets 'played'
|
||||
*/
|
||||
type Fitting = (
|
||||
context: FittingContext,
|
||||
next: {(err: Error | null | undefined, res?: any): void}) => void;
|
||||
|
||||
/**
|
||||
* Fitting creation Function
|
||||
*
|
||||
* Executed during parsing
|
||||
* @see {@link https://github.com/apigee-127/bagpipes#fittings|Docs}
|
||||
*
|
||||
* @param {Object} fittingDef Fitting Definition
|
||||
*/
|
||||
type FittingFactory = (fittingDef: FittingDef, bagpipes: any) => Fitting;
|
||||
|
||||
/**
|
||||
* Hashmap of `fittingType`s (the name of a fitting) and the
|
||||
* `FittingFactory` functions used to create them
|
||||
*/
|
||||
interface FittingTypesMap {
|
||||
[fittingType: string]: FittingFactory;
|
||||
}
|
||||
|
||||
/** The Pipe Definition */
|
||||
type PipeDef = any[] | string | FittingDef;
|
||||
|
||||
/** Hashmap of Pipe Definitons */
|
||||
export interface PipeDefMap {
|
||||
[name: string]: PipeDef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bagpipes Instance (a collection of pipes)
|
||||
* @see {@link https://github.com/apigee-127/bagpipes#pipes|Docs}
|
||||
*/
|
||||
export class Bagpipes {
|
||||
/**
|
||||
* Hashmap of `fittingType`s (the name of a fitting) and the
|
||||
* `FittingFactory` functions used to create them
|
||||
*/
|
||||
fittingTypes: FittingTypesMap;
|
||||
/** The pipes */
|
||||
pipes: {
|
||||
[name: string]: Pipe;
|
||||
};
|
||||
/** The `Bagpipes`' configuration */
|
||||
config: Config;
|
||||
/**
|
||||
* Creates a `Fitting`
|
||||
* @throws {Error} Throws error if an invalid (not defined) fitting typ is used
|
||||
* (can't find argument `fittingType` in `Bagpipes.fittingTypes`)
|
||||
*/
|
||||
createFitting(fittingDef: FittingDef): Fitting;
|
||||
/** returns `pipeworks` Pipe instance */
|
||||
createPipe(pipeDef: PipeDef): Pipe;
|
||||
createPipeFromFitting(fitting: Fitting, fittingDef: FittingDef): Pipe;
|
||||
/**
|
||||
* Finds and returns a `Pipe` by name and lazily creates if it is not defined
|
||||
* @throws {Error} Throws error if `pipeDef` pipe is not yet defined and `pipeDef` is
|
||||
* not supplied
|
||||
*/
|
||||
getPipe(pipeName: string, pipeDef?: PipeDef): Pipe;
|
||||
/** Handler for errors that occure when a `Fitting` gets 'played' */
|
||||
handleError(context: FittingContext, err: Error): void;
|
||||
/** Loads `FittingFactory`s from file-system and adds them to `Bagpipes.fittingTypes` */
|
||||
loadFittingTypes(): FittingTypesMap;
|
||||
/**
|
||||
* Builds a new (wrappend) `Fitting`
|
||||
* @throws {Error} Throws error if an invalid (not defined) fitting typ is used
|
||||
* (can't find argument `fittingType` in `Bagpipes.fittingTypes`)
|
||||
*/
|
||||
newFitting(fittingType: string, fittingDef: FittingDef): Fitting;
|
||||
/** Run the pipeline */
|
||||
play(pipe: Pipe, context: any): void;
|
||||
/**
|
||||
* Wraps `Fitting` with debugging, `preflight`, `postflight`
|
||||
* and error handling functionality and returns as new Fitting
|
||||
*/
|
||||
wrapFitting(fitting: Fitting, fittingDef: FittingDef): Fitting | null;
|
||||
}
|
||||
|
||||
/** Configuration object for `Bagpipes` */
|
||||
export interface Config {
|
||||
connectMiddlewareDirs?: string[];
|
||||
userFittingsDirs?: string[];
|
||||
userViewsDirs?: string[];
|
||||
// allow to store custom values e.g. for swagger-node-runner
|
||||
// see https://github.com/theganyo/swagger-node-runner/blob/v0.7.1/index.js#L304
|
||||
[name: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates `Bagpipes`
|
||||
*/
|
||||
export function create(pipesDefs: PipeDefMap, conf?: Config): Bagpipes;
|
||||
|
||||
// Types for imports from `pipeworks` module
|
||||
|
||||
type Affinity = "hoist" | "sink";
|
||||
|
||||
/** PipeDef used in `pipeworks` module */
|
||||
interface PipeworksOptions {
|
||||
/**
|
||||
* Adds to the pre and post queues, respectively.
|
||||
* Ensures a pipe gets fitted before or after the main execution pipeline.
|
||||
*/
|
||||
affinity: Affinity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance of a `Pipeworks` pipeline (in `pipeworks` module) -
|
||||
* called 'pipe' in `Bagpipes`
|
||||
*
|
||||
* _(simplified version)_
|
||||
*/
|
||||
export class Pipe {
|
||||
/** add a new `Fitting` (piece) to the pipe (aka. pipeline) */
|
||||
fit(pipe: Fitting): Pipe;
|
||||
/** add a new `Fitting` (piece) to the pipe (aka. pipeline) */
|
||||
fit(options: PipeworksOptions, pipe: Fitting): Pipe;
|
||||
/** Redirect the flow to another pipe (aka. pipeline). */
|
||||
siphon(pipe: Fitting): Pipe;
|
||||
/** Redirect the flow to another pipe (aka. pipeline). */
|
||||
siphon(options: PipeworksOptions, pipe: Fitting): Pipe;
|
||||
/**
|
||||
* Send something down the pipe (aka. pipeline)! Any number of arguments
|
||||
* can be sent, but often there's just a single `context` object.
|
||||
*/
|
||||
flow(...args: any[]): Pipe;
|
||||
}
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"bagpipes-tests.ts"
|
||||
]
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user