Skip to content

Commit

Permalink
✨ added getAllLocations method
Browse files Browse the repository at this point in the history
  • Loading branch information
loloToster committed Feb 12, 2022
1 parent 1cbee93 commit e64016d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ weather.getCurrent().then(data => {
* [setLocationByCoordinates][slocbycoor]
* [setLocationByZipCode][slocbyzip]
* [getLocation][gloc]
* [getAllLocations][gallloc]
* [Getting & Manipulating Weather Data][gcur]
* [getCurrent][gcur]
* [getMinutelyForecast][gminutely]
Expand Down Expand Up @@ -321,6 +322,26 @@ location = await weather.getLocation({locationName: "Tokio"})
```
*See also:* [options][opt], [setLocationByName][slocbyname], [setLocationByCoordinates][slocbycoor]

## `async` getAllLocations(query, options = {})

**Description:**

Getter for all locations from query

**Arguments:**
* **query** - query used to search the locations
* **options** - options used only for this call (defaults to empty object)

**Returns:**

Array of [Location Objects][lobj] - `Array`

**Example:**
```js
let locations = await weather.getAllLocations("London")
```
*See also:* [options][opt]

## `async` getCurrent(options = {})

**Description:**
Expand Down Expand Up @@ -782,6 +803,7 @@ When using raw API the problem might be getting your head around how unorganised
[slocbycoor]: #setlocationbycoordinateslat-lon
[slocbyzip]:#setlocationbyzipcodezipcode
[gloc]: #async-getlocationoptions
[gallloc]: #async-getAllLocationsquery-options--
[gcur]: #async-getcurrentoptions
[gminutely]: #async-getminutelyforecastlimit--numberpositive_infinity-options--
[ghourly]: #async-gethourlyforecastlimit--numberpositive_infinity-options--
Expand Down
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,21 @@ class OpenWeatherAPI {
return data.length ? data[0] : null
}

/**
* Getter for locations from query
*
* @param {String} query - query used to search the locations (`q` parameter [here](https://openweathermap.org/api/geocoding-api#direct_name))
* @param {Options} [options={}] - options used only for this call
* @returns {Promise<Location[]>} all found locations
*/
async getAllLocations(query, options = {}) {
if (!query) throw new Error("No query")
options = await this.#parseOptions(options)
let response = await this.#fetch(`${API_ENDPOINT}${GEO_PATH}direct?q=${query}&limit=5&appid=${options.key}`)
let data = response.data
return data
}

// Weather getters

/**
Expand Down
5 changes: 5 additions & 0 deletions test/getter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe("Getting tests:", function () {
assert(location.name.toLowerCase().includes("new york"))
})

it("gets all locations", async () => {
let locations = await weather.getAllLocations("London")
assert(locations.length == 5)
})

it("gets current", async () => {
weather.setLocationByName("warsaw")
let current = await weather.getCurrent()
Expand Down
8 changes: 8 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ declare class OpenWeatherAPI {
* @returns {Promise<Location|null>} location
*/
getLocation(options?: Options): Promise<Location | null>;
/**
* Getter for locations from query
*
* @param {String} query - query used to search the locations (`q` parameter [here](https://openweathermap.org/api/geocoding-api#direct_name))
* @param {Options} [options={}] - options used only for this call
* @returns {Promise<Location[]>} all found locations
*/
getAllLocations(query: string, options?: Options): Promise<Location[]>;
/**
* Getter for current weather
*
Expand Down

0 comments on commit e64016d

Please sign in to comment.