Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neo9/n9-node-utils
Node Utils Module
https://github.com/neo9/n9-node-utils
Last synced: 1 day ago
JSON representation
Node Utils Module
- Host: GitHub
- URL: https://github.com/neo9/n9-node-utils
- Owner: neo9
- License: gpl-3.0
- Created: 2017-08-30T10:04:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-14T17:16:30.000Z (about 1 year ago)
- Last Synced: 2024-12-13T08:28:06.851Z (14 days ago)
- Language: TypeScript
- Size: 1.11 MB
- Stars: 5
- Watchers: 13
- Forks: 3
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# n9-node-utils
Neo9 Node Utils Module.
[![npm version](https://img.shields.io/npm/v/@neo9/n9-node-utils.svg)](https://www.npmjs.com/package/@neo9/n9-node-utils)
[![Travis](https://img.shields.io/travis/neo9/n9-node-utils/master.svg)](https://travis-ci.org/neo9/n9-node-utils)
[![Coverage](https://img.shields.io/codecov/c/github/neo9/n9-node-utils/master.svg)](https://codecov.io/gh/neo9/n9-node-utils)
[![license](https://img.shields.io/github/license/neo9/n9-node-utils.svg)](https://github.com/neo9/n9-node-utils/blob/master/LICENSE)## Installation
```bash
npm install --save @neo9/n9-node-utils
```or
```bash
yarn add --dev @neo9/n9-node-utils
```### V2 Breaking changes
- Drop Node 8 support
- Rename `N9JsonStreamResponse` to `N9JSONStreamResponse`## Contents
- [N9Error](#n9error)
- [ok](#ok)
- [cb](#cb)
- [waitFor](#waitfor)
- [waitForEvent](#waitforevent)
- [asyncObject](#asyncobject)## N9Error
Custom error class (extends `Error`), used by [n9-node-micro](https://github.com/neo9/n9-node-micro) for API errors with status code and context.
```ts
new N9Error(message [, statusCode] [, context])
```Arguments:
- message: `String`, **required**
- status: `Number`, default: `500`
- context: `Object`, default: `{}`Example:
```ts
import { N9Error } from '@neo9/n9-node-utils';throw new N9Error('file-not-found', 404, { path: '/tmp/my-file.txt' });
```## ok
Waits for the value of promise. If promise throws an Error, returns `undefined`.
```ts
ok(promise: Object): Promise
```Arguments:
- promise: `Promise`
Example:
```ts
import { readFile } from 'fs-extra';
import { ok } from '@neo9/n9-node-utils';// readFile sends back a Promise since we use fs-extra
const file = await ok(readFile('./my-file.txt', 'utf-8'));if (file) console.log('File found:', file);
```## cb
Calls a function `fn` that takes arguments `args` and an `(err, result)` callback. Waits for the `callback` result, throwing an `Error` if `err` is truthy.
```ts
cb(fn: Function, ...args: any[]): Promise
```Arguments:
- fn: `Function`, a function that takes a callback
- args: (...`any`) arguments to pass to `fn`Example:
```ts
import { cb } from '@neo9/n9-node-utils';const file = await cb(readFile('./my-file.txt', 'utf-8'));
console.log('File content:', file);
```## waitFor
Waits for ms milliseconds to pass, use `setTimeout` under the hood.
```ts
waitFor(ms: number): Promise
```Arguments:
- ms: `Number`, default: `0`
Example:
```ts
import { waitFor } from '@neo9/n9-node-utils';await waitFor(1000); // wait for 1s
```## waitForEvent
Waits for emitter to emit an eventName event.
```ts
waitForEvent(emitter: EventEmitter, eventName: string): Promise
```Arguments:
- emitter: `EventEmitter`
- eventName: `String`Example:
```ts
import { waitForEvent } from '@neo9/n9-node-utils';await waitForEvent(sever, 'listen');
```## asyncObject
Waits for all Promises in the keys of `obj` to resolve.
```ts
asyncObject(obj: Object): Promise
```Arguments:
- obj: `Object`, default: `{}`
Example:
```ts
import { asyncObject } from '@neo9/n9-node-utils';const results = await asyncObject({
pictures: getPictures(),
comments: getComments(),
tweets: getTweets(),
});console.log(results.pictures, results.comments, results.tweets);
```