handy-common-utils v1.0.4
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
- delayedReject
- delayedResolve
- inParallel
- parseArn
- repeat
- repeatFetchingItemsByMarker
- repeatFetchingItemsByNextToken
- repeatFetchingItemsByPosition
- timeoutReject
- timeoutResolve
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
▸ Static
parseArn(arn
: string | null | undefined): ReturnType\<typeof simpleParseArn> & { arn: string } | null | undefined
Defined in src/aws-utils.ts:89
Parse ARN
Parameters:
Name | Type | Description |
---|---|---|
arn | string | null | undefined | the 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
▸ Static
repeatFetchingItemsByMarker\(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:
Name | Description |
---|---|
T | type of the items returned by AWS API |
Parameters:
Name | Type | Description |
---|---|---|
itemsFieldName | string | name 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
▸ Static
repeatFetchingItemsByNextToken\(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:
Name | Description |
---|---|
T | type of the items returned by AWS API |
Parameters:
Name | Type | Description |
---|---|---|
itemsFieldName | string | name 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
▸ Static
repeatFetchingItemsByPosition\(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:
Name | Description |
---|---|
T | type of the items returned by AWS API |
Parameters:
Name | Type | Description |
---|---|---|
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
▸ Static
generateHelpText(commandInstance
: Command, opts?
: Partial\): string
Defined in src/oclif-utils.ts:41
Generate formatted text content of help to a command
Parameters:
Name | Type | Description |
---|---|---|
commandInstance | Command | instance of the Command |
opts? | Partial\ | format options |
Returns: string
getCommandConfig
▸ Static
getCommandConfig(commandInstance
: Command): Command
Defined in src/oclif-utils.ts:16
Parameters:
Name | Type |
---|---|
commandInstance | Command |
Returns: Command
injectHelpTextIntoReadmeMd
▸ Static
injectHelpTextIntoReadmeMd(commandInstance
: Command, opts?
: Partial\): Promise\
Defined in src/oclif-utils.ts:50
Parameters:
Name | Type |
---|---|
commandInstance | Command |
opts? | Partial\ |
Returns: Promise\
prependCliToExamples
▸ Static
prependCliToExamples(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:
Name | Type | Description |
---|---|---|
commandInstance | Command | instance of the Command |
Returns: void
Class: SingleCommandHelp
Hierarchy
Help
↳ SingleCommandHelp
Index
Constructors
Properties
Accessors
Methods
- command
- formatCommand
- formatCommands
- formatRoot
- formatTopic
- formatTopics
- generateHelpText
- showCommandHelp
- showHelp
- showRootHelp
- showTopicHelp
Constructors
constructor
+ new SingleCommandHelp(commandInstance
: Command, opts?
: Partial\): SingleCommandHelp
Overrides void
Defined in src/oclif-utils.ts:67
Parameters:
Name | Type |
---|---|
commandInstance | Command |
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
• Protected
get sortedCommands(): Plugin[]
Inherited from SingleCommandHelp.sortedCommands
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:27
Returns: Plugin[]
sortedTopics
• Protected
get sortedTopics(): Topic[]
Inherited from SingleCommandHelp.sortedTopics
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:28
Returns: Topic[]
Methods
command
▸ Protected
command(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:
Name | Type | Description |
---|---|---|
command | Command | The command to generate readme help for |
Returns: string
the readme help string for the given command
formatCommand
▸ Protected
formatCommand(command
: Command): string
Inherited from SingleCommandHelp.formatCommand
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:35
Parameters:
Name | Type |
---|---|
command | Command |
Returns: string
formatCommands
▸ Protected
formatCommands(commands
: Command[]): string
Inherited from SingleCommandHelp.formatCommands
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:36
Parameters:
Name | Type |
---|---|
commands | Command[] |
Returns: string
formatRoot
▸ Protected
formatRoot(): string
Inherited from SingleCommandHelp.formatRoot
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:34
Returns: string
formatTopic
▸ Protected
formatTopic(topic
: Topic): string
Inherited from SingleCommandHelp.formatTopic
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:37
Parameters:
Name | Type |
---|---|
topic | Topic |
Returns: string
formatTopics
▸ Protected
formatTopics(topics
: Topic[]): string
Inherited from SingleCommandHelp.formatTopics
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:38
Parameters:
Name | Type |
---|---|
topics | Topic[] |
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:
Name | Type |
---|---|
command | Command |
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:
Name | Type |
---|---|
argv | string[] |
Returns: void
showRootHelp
▸ Protected
showRootHelp(): void
Inherited from SingleCommandHelp.showRootHelp
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:32
Returns: void
showTopicHelp
▸ Protected
showTopicHelp(topic
: Topic): void
Inherited from SingleCommandHelp.showTopicHelp
Defined in node_modules/@oclif/plugin-help/lib/index.d.ts:33
Parameters:
Name | Type |
---|---|
topic | Topic |
Returns: void
Class: Utils
Hierarchy
- Utils
Index
Methods
Methods
delayedReject
▸ Static
delayedReject\(ms
: number, reason
: any): Promise\
Defined in src/utils.ts:119
Create a Promise that rejects after number of milliseconds specified
Type parameters:
Name | Default |
---|---|
T | never |
Parameters:
Name | Type | Description |
---|---|---|
ms | number | number of milliseconds after which the created Promise would reject |
reason | any | the reason of the rejection for the Promise |
Returns: Promise\
the new Promise created
delayedResolve
▸ Static
delayedResolve\(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:
Name | Type | Description |
---|---|---|
ms | number | number of milliseconds after which the created Promise would resolve |
result? | T | PromiseLike\ | undefined | the result to be resolved for the Promise |
Returns: Promise\
the new Promise created
inParallel
▸ Static
inParallel\<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:
Name | Description |
---|---|
Data | Type of the job data, usually it would be an Array |
Result | Type of the return value of the operation function |
Parameters:
Name | Type | Description |
---|---|---|
parallelism | number | how many jobs/operations can be running at the same time |
jobs | Iterable\ | 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
▸ Static
repeat\<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:
Name | Description |
---|---|
Result | type of the operation result |
Param | type of the input to the operation, normally the input is a paging parameter |
Collection | type of the returned value of this function |
Parameters:
Name | Type | Default value | Description |
---|---|---|---|
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 |
initialCollection | Collection | - | initial collection which would be the first argument passed into the first invocation of the collect function |
initialParameter | Partial\ | {} | the parameter for the first operation |
Returns: Promise\
Promise of collection of all the results returned by the operation function
timeoutReject
▸ Static
timeoutReject\(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:
Name | Type | Description |
---|---|---|
operation | Promise\ | the original operation that timeout would be applied |
ms | number | number of milliseconds for the timeout |
rejectReason | any | the reason of the rejection in case timeout happens |
Returns: Promise\
timeoutResolve
▸ Static
timeoutResolve\(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:
Name | Type | Description |
---|---|---|
operation | Promise\ | the original operation that timeout would be applied |
ms | number | number of milliseconds for the timeout |
result? | T | PromiseLike\ | undefined | the 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