1.0.0 • Published 9 months ago

geography-apis-sdk v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

Geography API Search SDK (JavaScript)

License

The CouGeography API Search SDK allows you to easily search for countries by name using a simple JavaScript interface. This SDK provides an abstraction over the API requests and handles common error scenarios to make it easier for developers to integrate country search functionality into their applications.

Table of Contents

Installation

To use the SDK, you can install it via npm:

npm install geography-apis-sdk

Usage

Initializing the SDK

Before using the SDK, you need to initialize it with your API key and host:

const CountrySearchSDK = require('geography-apis-sdk.bundle.js');

const apiKey = 'YOUR_API_KEY';
const apiHost = 'geography4.p.rapidapi.com';
const baseURL = 'https://geography4.p.rapidapi.com';

const countrySDK = new CountrySearchSDK(apiKey, apiHost, baseURL);

Search for Countries by Name

You can search for countries by their name using the searchByName method:

const searchName = 'United States';
const options = {
    headers: {
        'Content-Type': 'application/json'
    },
    params: {
        exactMatch: false,
        includeNativeName: false,
        fields: 'name,capital,population',
        sortBy: 'name',
        sortOrder: 'asc',
        limit: 10,
        offset: 0
    },
};
const countries = await countrySDK.searchCountriesByName(searchName, options);
countries.then(countries => {
    // 'countries' is an array of Country objects
    console.log(countries);
  })
  .catch(error => {
    console.error('Error:', error.message);
  });

Country Object

The SDK returns an array of Country objects that represent the searched countries. Each Country object has the following properties:

  • name (object): Contains the name of the country, including the common name, official name, and native name.
  • capital (string): The capital city of the country.
  • population (number): The population of the country.
  • tld (string): The top-level domain of the country.
  • cca2 (string): The two-letter ISO code of the country.
  • cca3 (string): The three-letter ISO code of the country.
  • languages (object): Contains the official languages spoken in the country.
  • area (number): The total area of the country in square kilometers.
  • borders (array): An array of ISO codes of neighboring countries.
  • And many more properties with various information about the country.

Error Handling

The SDK handles different error scenarios to provide meaningful feedback to developers:

  • Invalid API key or host: The SDK ensures that you provide valid API credentials before making any requests.
  • API request errors: If there is an issue with the API request, the SDK throws an error with relevant information.
  • Empty response (status code 204): If the API response contains no content, the SDK returns a "No countries found" message.
  • Partial content response (status code 206): If the API response contains partial content, the SDK handles it gracefully.

License

This SDK is released under the MIT License. See the LICENSE file for more details.

Contribution

Contributions to this SDK are welcome! If you find any issues or want to add new features, please feel free to create a pull request or submit an issue.

Support

For any questions or support related to this SDK, please contact support@mmplabs.com.