1.0.4 • Published 5 years ago

handy-common-utils v1.0.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 years ago

handy-common-utils

Handy utilities with type definitions

How to use

First add it as a dependency:

npm install handy-common-utils

Then you can use it in the code:

import { AwsUtils } from '../handy-common-utils/src';

const apig = new APIGateway();
const domainNameObjects = await AwsUtils.repeatFetchingItemsByPosition(
    pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
);

API

handy-common-utils

Index

Classes

Interfaces

Type aliases

Variables

Type aliases

InParrellelResult

Ƭ InParrellelResult\: T extends void ? void : Array\

Defined in src/utils.ts:1

Type parameters:
Name
T

Variables

delayedReject

Const delayedReject: delayedReject = Utils.delayedReject

Defined in src/utils.ts:152


delayedResolve

Const delayedResolve: delayedResolve = Utils.delayedResolve

Defined in src/utils.ts:151


inParallel

Const inParallel: inParallel = Utils.inParallel

Defined in src/utils.ts:150


parseArn

Const parseArn: parseArn = AwsUtils.parseArn

Defined in src/aws-utils.ts:103


repeat

Const repeat: repeat = Utils.repeat

Defined in src/utils.ts:149


repeatFetchingItemsByMarker

Const repeatFetchingItemsByMarker: repeatFetchingItemsByMarker = AwsUtils.repeatFetchingItemsByMarker

Defined in src/aws-utils.ts:102


repeatFetchingItemsByNextToken

Const repeatFetchingItemsByNextToken: repeatFetchingItemsByNextToken = AwsUtils.repeatFetchingItemsByNextToken

Defined in src/aws-utils.ts:101


repeatFetchingItemsByPosition

Const repeatFetchingItemsByPosition: repeatFetchingItemsByPosition = AwsUtils.repeatFetchingItemsByPosition

Defined in src/aws-utils.ts:100


timeoutReject

Const timeoutReject: timeoutReject = Utils.timeoutReject

Defined in src/utils.ts:154


timeoutResolve

Const timeoutResolve: timeoutResolve = Utils.timeoutResolve

Defined in src/utils.ts:153

Classes

Class: AwsUtils

Hierarchy

  • AwsUtils

Index

Methods

Methods

parseArn

StaticparseArn(arn: string | null | undefined): ReturnType\<typeof simpleParseArn> & { arn: string } | null | undefined

Defined in src/aws-utils.ts:89

Parse ARN

Parameters:
NameTypeDescription
arnstring | null | undefinedthe ARN string that could be null or undefined

Returns: ReturnType\<typeof simpleParseArn> & { arn: string } | null | undefined

null or undeinfed if the input is null or undefined, or parsed ARN including the original ARN string


repeatFetchingItemsByMarker

StaticrepeatFetchingItemsByMarker\(itemsFieldName: string, fetchItemsByMarker: (parameter: { Marker?: undefined | string }) => Promise\<{ NextMarker?: undefined | string }>): Promise\<T[]>

Defined in src/aws-utils.ts:72

Fetch items by Marker repeatedly. This function is useful for client side pagination when the response from AWS API contains NextMarker fields.

example const topics = await AwsUtils.repeatFetchingItemsByNextToken<SNS.Topic>('Topics', pagingParam => sns.listTopics({...pagingParam}).promise(), );

Type parameters:
NameDescription
Ttype of the items returned by AWS API
Parameters:
NameTypeDescription
itemsFieldNamestringname of the field containing returned items in AWS API response
fetchItemsByMarker(parameter: { Marker?: undefined | string }) => Promise\<{ NextMarker?: undefined | string }>the function for fetching items by Marker

Returns: Promise\<T[]>

all items fetched


repeatFetchingItemsByNextToken

StaticrepeatFetchingItemsByNextToken\(itemsFieldName: string, fetchItemsByNextToken: (parameter: { NextToken?: undefined | string }) => Promise\<{ NextToken?: undefined | string }>): Promise\<T[]>

Defined in src/aws-utils.ts:45

Fetch items by NextToken repeatedly. This function is useful for client side pagination when the response from AWS API contains NextToken fields.

example const topics = await AwsUtils.repeatFetchingItemsByNextToken<SNS.Topic>('Topics', pagingParam => sns.listTopics({...pagingParam}).promise(), );

Type parameters:
NameDescription
Ttype of the items returned by AWS API
Parameters:
NameTypeDescription
itemsFieldNamestringname of the field containing returned items in AWS API response
fetchItemsByNextToken(parameter: { NextToken?: undefined | string }) => Promise\<{ NextToken?: undefined | string }>the function for fetching items by NextToken

Returns: Promise\<T[]>

all items fetched


repeatFetchingItemsByPosition

StaticrepeatFetchingItemsByPosition\(fetchItemsByPosition: (parameter: { position?: undefined | string }) => Promise\<{ items?: Array\ ; position?: undefined | string }>): Promise\<T[]>

Defined in src/aws-utils.ts:19

Fetch items by position repeatedly. This function is useful for client side pagination when the response from AWS API contains position and items fields.

example const domainNameObjects = await AwsUtils.repeatFetchingItemsByPosition( pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(), );

Type parameters:
NameDescription
Ttype of the items returned by AWS API
Parameters:
NameTypeDescription
fetchItemsByPosition(parameter: { position?: undefined | string }) => Promise\<{ items?: Array\ ; position?: undefined | string }>the function for fetching items by position

Returns: Promise\<T[]>

all items fetched

Class: OclifUtils

Hierarchy

  • OclifUtils

Index

Methods

Methods

generateHelpText

StaticgenerateHelpText(commandInstance: Command, opts?: Partial\): string

Defined in src/oclif-utils.ts:41

Generate formatted text content of help to a command

Parameters:
NameTypeDescription
commandInstanceCommandinstance of the Command
opts?Partial\format options

Returns: string


getCommandConfig

StaticgetCommandConfig(commandInstance: Command): Command

Defined in src/oclif-utils.ts:16

Parameters:
NameType
commandInstanceCommand

Returns: Command


injectHelpTextIntoReadmeMd

StaticinjectHelpTextIntoReadmeMd(commandInstance: Command, opts?: Partial\): Promise\

Defined in src/oclif-utils.ts:50

Parameters:
NameType
commandInstanceCommand
opts?Partial\

Returns: Promise\


prependCliToExamples

StaticprependCliToExamples(commandInstance: Command): void

Defined in src/oclif-utils.ts:29

Use this function to prepend command line to examples. This function needs to be called from init() function of the Command.

Parameters:
NameTypeDescription
commandInstanceCommandinstance of the Command

Returns: void

Class: SingleCommandHelp

Hierarchy

  • Help

    SingleCommandHelp

Index

Constructors
Properties
Accessors
Methods

Constructors

constructor

+ new SingleCommandHelp(commandInstance: Command, opts?: Partial\): SingleCommandHelp

Overrides void

Defined in src/oclif-utils.ts:67

Parameters:
NameType
commandInstanceCommand
opts?Partial\

Returns: SingleCommandHelp

Properties

commandInstance

Protected commandInstance: Command

Defined in src/oclif-utils.ts:68


config

Protected config: IConfig

Inherited from SingleCommandHelp.config

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:10


opts

Protected opts: HelpOptions

Inherited from SingleCommandHelp.opts

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:11


render

render: (input: string) => string

Inherited from SingleCommandHelp.render

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:25

Accessors

sortedCommands

Protectedget sortedCommands(): Plugin[]

Inherited from SingleCommandHelp.sortedCommands

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:27

Returns: Plugin[]


sortedTopics

Protectedget sortedTopics(): Topic[]

Inherited from SingleCommandHelp.sortedTopics

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:28

Returns: Topic[]

Methods

command

Protectedcommand(command: Command): string

Inherited from SingleCommandHelp.command

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:44

deprecated used for readme generation

Parameters:
NameTypeDescription
commandCommandThe command to generate readme help for

Returns: string

the readme help string for the given command


formatCommand

ProtectedformatCommand(command: Command): string

Inherited from SingleCommandHelp.formatCommand

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:35

Parameters:
NameType
commandCommand

Returns: string


formatCommands

ProtectedformatCommands(commands: Command[]): string

Inherited from SingleCommandHelp.formatCommands

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:36

Parameters:
NameType
commandsCommand[]

Returns: string


formatRoot

ProtectedformatRoot(): string

Inherited from SingleCommandHelp.formatRoot

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:34

Returns: string


formatTopic

ProtectedformatTopic(topic: Topic): string

Inherited from SingleCommandHelp.formatTopic

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:37

Parameters:
NameType
topicTopic

Returns: string


formatTopics

ProtectedformatTopics(topics: Topic[]): string

Inherited from SingleCommandHelp.formatTopics

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:38

Parameters:
NameType
topicsTopic[]

Returns: string


generateHelpText

generateHelpText(): string

Defined in src/oclif-utils.ts:72

Returns: string


showCommandHelp

showCommandHelp(command: Command): void

Inherited from SingleCommandHelp.showCommandHelp

Overrides void

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:31

Parameters:
NameType
commandCommand

Returns: void


showHelp

showHelp(argv: string[]): void

Inherited from SingleCommandHelp.showHelp

Overrides void

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:30

Parameters:
NameType
argvstring[]

Returns: void


showRootHelp

ProtectedshowRootHelp(): void

Inherited from SingleCommandHelp.showRootHelp

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:32

Returns: void


showTopicHelp

ProtectedshowTopicHelp(topic: Topic): void

Inherited from SingleCommandHelp.showTopicHelp

Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:33

Parameters:
NameType
topicTopic

Returns: void

Class: Utils

Hierarchy

  • Utils

Index

Methods

Methods

delayedReject

StaticdelayedReject\(ms: number, reason: any): Promise\

Defined in src/utils.ts:119

Create a Promise that rejects after number of milliseconds specified

Type parameters:
NameDefault
Tnever
Parameters:
NameTypeDescription
msnumbernumber of milliseconds after which the created Promise would reject
reasonanythe reason of the rejection for the Promise

Returns: Promise\

the new Promise created


delayedResolve

StaticdelayedResolve\(ms: number, result?: T | PromiseLike\ | undefined): Promise\

Defined in src/utils.ts:109

Create a Promise that resolves after number of milliseconds specified

Type parameters:
Name
T
Parameters:
NameTypeDescription
msnumbernumber of milliseconds after which the created Promise would resolve
result?T | PromiseLike\ | undefinedthe result to be resolved for the Promise

Returns: Promise\

the new Promise created


inParallel

StaticinParallel\<Data, Result>(parallelism: number, jobs: Iterable\, operation: (job: Data, index: number) => Promise\): Promise\<InParrellelResult\>

Defined in src/utils.ts:72

Run multiple jobs/operations in parallel.

example const topicArns = topics.map(topic => topic.TopicArn!); await Utils.inParallel(5, topicArns, async topicArn => { const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!; const topicDetails = { ...topicAttributes, subscriptions: [] } as any; if (this.shouldInclude(topicArn)) { inventory.snsTopicsByArn.set(topicArn, topicDetails); } });

Type parameters:
NameDescription
DataType of the job data, usually it would be an Array
ResultType of the return value of the operation function
Parameters:
NameTypeDescription
parallelismnumberhow many jobs/operations can be running at the same time
jobsIterable\job data which will be the input to operation function. This function is safe when there are infinite unknown number of elements in the job data.
operation(job: Data, index: number) => Promise\the function that turns job data into result asynchronously

Returns: Promise\<InParrellelResult\>

Promise of void if the operation function does not return a value, or promise of an arry containing results returned from the operation function.


repeat

Staticrepeat\<Result, Param, Collection>(operation: (parameter: Partial\) => Promise\, nextParameter: (response: Result) => Partial\ | null, collect: (collection: Collection, result: Result) => Collection, initialCollection: Collection, initialParameter?: Partial\): Promise\

Defined in src/utils.ts:31

Do an operation repeatedly and collect all the results. This function is useful for client side pagination.

example const domainNameObjects = await Utils.repeat( pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(), esponse => response.position? {position: response.position} : null, (collection, response) => collection.concat(response.items!), [] as APIGateway.DomainName[], );

Type parameters:
NameDescription
Resulttype of the operation result
Paramtype of the input to the operation, normally the input is a paging parameter
Collectiontype of the returned value of this function
Parameters:
NameTypeDefault valueDescription
operation(parameter: Partial\) => Promise\-a function that takes paging parameter as input and outputs a result, normally the operation supports paging
nextParameter(response: Result) => Partial\ | null-The function for calculating next parameter from the operation result. Normally the parameter controls paging, This function should return null when next invocation of the operation function is not desired.
collect(collection: Collection, result: Result) => Collection-the function for merging operation result into the collection
initialCollectionCollection-initial collection which would be the first argument passed into the first invocation of the collect function
initialParameterPartial\{}the parameter for the first operation

Returns: Promise\

Promise of collection of all the results returned by the operation function


timeoutReject

StatictimeoutReject\(operation: Promise\, ms: number, rejectReason: any): Promise\

Defined in src/utils.ts:144

Apply timeout to an operation, in case timeout happens, reject with the reason specified. If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.

Type parameters:
Name
T
Parameters:
NameTypeDescription
operationPromise\the original operation that timeout would be applied
msnumbernumber of milliseconds for the timeout
rejectReasonanythe reason of the rejection in case timeout happens

Returns: Promise\


timeoutResolve

StatictimeoutResolve\(operation: Promise\, ms: number, result?: T | PromiseLike\ | undefined): Promise\

Defined in src/utils.ts:132

Apply timeout to an operation, in case timeout happens, resolve to the result specified. If timeout does not happen, the resolved result or rejection reason of the original operation would be returned.

Type parameters:
Name
T
Parameters:
NameTypeDescription
operationPromise\the original operation that timeout would be applied
msnumbernumber of milliseconds for the timeout
result?T | PromiseLike\ | undefinedthe result to be resolved in case timeout happens

Returns: Promise\

Interfaces

Interface: OclifHelpContent

Hierarchy

  • OclifHelpContent

Index

Properties

Properties

aliases

Optional aliases: undefined | string

Defined in src/oclif-utils.ts:11


args

Optional args: undefined | string

Defined in src/oclif-utils.ts:8


description

Optional description: undefined | string

Defined in src/oclif-utils.ts:10


examples

Optional examples: undefined | string

Defined in src/oclif-utils.ts:12


flags

Optional flags: undefined | string

Defined in src/oclif-utils.ts:9


usage

Optional usage: undefined | string

Defined in src/oclif-utils.ts:7

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago