eugene-manuilov / novaposhta

A small library that provides a helper class to work with Nova Poshta API.
MIT License
14 stars 4 forks source link

Novaposhta

Version Downloads/week License

A small library that provides a helper class to work with Nova Poshta API.

Usage

Install it using the following command:

npm i novaposhta

The library exports the NovaPoshta class that you need to use to make requests to API. You need to instantiate an instance of the class providing an object with configuration options. The instance of the class has getters that allows you to get access to a specific model of the APIs.

Also, please, pay attention that some API methods require API key for authorization. You can obtain it by going to this page.

Here is a basic example that shows how to use the library:

import NovaPoshta from 'novaposhta';

const api = new NovaPoshta({ apiKey: '...' });

api.address
    .getCities({ Ref: "ebc0eda9-93ec-11e3-b441-0050568002cf" })
    .then((json) => {
        // do something
    })
    .catch((errors) => {
        if (Array.isArray(errors)) {
            errors.forEach((error) => console.log(`[${ error.code || '-' }] ${ error.en || error.uk || error.ru || error.message }`));
        }
    });

A bit more advanced example that demonstrates how you can configure a Winston Logger and use it with the library:

import NovaPoshta from 'novaposhta';
import Winston from 'winston';
import WinstonFormatter from 'winston-console-formatter';

const winstonLogger = new Winston.Logger({ level: "debug" });
winstonLogger.add(Winston.transports.Console, WinstonFormatter.config());

const api = new NovaPoshta({
    apiKey: '...',
    logger: winstonLogger,
});

api.address.getCities({ Ref: "ebc0eda9-93ec-11e3-b441-0050568002cf" }).then((json) => {
    // do something
});

Supported API Methods

Address

const api = new NovaPoshta({ apiKey: '...' });

api.address.getCities({ Ref: "ebc0eda9-93ec-11e3-b441-0050568002cf" }).then((json) => {
    // do something
});

Common

const api = new NovaPoshta({ apiKey: '...' });

api.common.getTimeIntervals({ "RecipientCityRef": "8d5a980d-391c-11dd-90d9-001a92567626" }).then((json) => {
    // do something
});

Counterparty

const api = new NovaPoshta({ apiKey: '...' });

api.counterparty.getCounterpartyContactPersons({ ... }).then((json) => {
    // do something
});

ContactPerson

const api = new NovaPoshta({ apiKey: '...' });

api.contactPerson.save({ ... }).then((json) => {
    // do something
});

Internet Document

const api = new NovaPoshta({ apiKey: '...' });

api.internetDocument.getDocumentList({ ... }).then((json) => {
    // do something
});

ScanSheet

const api = new NovaPoshta({ apiKey: '...' });

api.scanSheet.getScanSheetList({ ... }).then((json) => {
    // do something
});

AdditionalService

const api = new NovaPoshta({ apiKey: '...' });

api.additionalService.getReturnOrdersList({ ... }).then((json) => {
    // do something
});

AdditionalServiceGeneral

const api = new NovaPoshta({ apiKey: '...' });

api.additionalServiceGeneral.checkPossibilityForRedirecting({ ... }).then((json) => {
    // do something
});

Contribute

Want to help or have a suggestion? Open a new ticket and we can discuss it or submit a pull request. Please, make sure you run npm test before submitting a pull request.

License

MIT