0.46.0 • Published 7 months ago

@pretto/places v0.46.0

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

@pretto/places

npm version

Allows you to search for an address, a postcode or a French department. You can also search a country.

The library is typed but you can retrieve the types in the dist/types folder if needed

FAQ

How to implement the library?

yarn add @pretto/places

For municipality and zipcode

import { searchPlaces } from '@pretto/places';
import { MunicipalitySearchResult } from '@pretto/places/dist/types';

const result = await searchPlaces("paris", { limit: 21 })

// result object format :
[
    {
        label: "Paris 1er Arrondissement 75001",
        value: {
            center: [2.3359, 48.862],
            city: "Paris 1er Arrondissement",
            country: "fr",
            outline: [[{"latitude": 48.870688, "longitude": 2.347918},...]],
            zipcode: "75001",
        },
    },
    {
        label: "Paris 2e Arrondissement 75002",
        value: {
            center: [2.3411, 48.8677],
            city: "Paris 2e Arrondissement",
            country: "fr",
            outline: [Array],
            zipcode: "75002",
        },
    },
    ...
]

For department only

import { searchDepartment } from '@pretto/places'
import { DepartmentSearchResult } from '@pretto/places/dist/types';

const result = await searchDepartment("pa", { limit: 21 })

// result object format :
[
    {
        label: "Paris (75)",
        value: {
            code: "75",
            codeRegion: "11",
            country: "fr",
            region: {
                code: "11",
                nom: "Île-de-France"
            }
        },
    },
    {
        label: "Pas-de-Calais (62)",
        value: {
            code: "62",
            codeRegion: "32",
            country: "fr",
            region: {
                code: "32",
                nom: "Hauts-de-France"
            }
        },
    },
    ...
]

For Address (France only)

import { addressSearch } from '@pretto/places'
import { AddressSearchResult } from '@pretto/places/dist/types';

const result = await addressSearch.get("55 rue de paradis", { limit: 10 })

// result object format :
[
  {
    label: "42 Rue de Paradis 75010, Paris",
    value: {
      city: "Paris",
      country: "fr",
      street: "42 Rue de Paradis",
      zipcode: "75010",
    },
  },
  {
    label: "42 Rue de Paradis 95100, Argenteuil",
    value: {
      city: "Argenteuil",
      country: "fr",
      street: "42 Rue de Paradis",
      zipcode: "95100",
    },
  },
    ...
]

For country

import { searchCountry } from '@pretto/places'

const countriesApi = searchCountry.init(ALGOLIA_COUNTRIES_APP_ID, ALGOLIA_COUNTRIES_API_KEY)
const results = await countriesApi.get('al', { limit: 10 })[
  // result object format :
  ({
    label: 'Allemagne (99109)',
    value: 'de',
  },
  {
    label: 'Albanie (99125)',
    value: 'al',
  },
  {
    label: 'Algerie (99352)',
    value: 'dz',
  })
]

For reverse geolocalisation

import { searchByCoordinates } from '@pretto/places'
import { MunicipalitySearchResult } from '@pretto/places/dist/types';

const results = await searchByCoordinates({ latitude: 48.87571729159758, longitude: 2.3503000982950883 })

// result object format :
[
  {
    label: "Paris 10e Arrondissement 75010",
    value: {
      center: [Object],
      city: "Paris 10e Arrondissement",
      country: "fr",
      outline: [Array],
      zipcode: "75010",
    },
  },
];

Options

You can pass options to the search function.

OptionTypeDefault Value
debouncetimenumber300
limitnumber10
signalAbortSignalAbortController
const debounceValue = 1000 // --- 1000 = 1s
const results = await searchPlaces('paris', { debouncetime: debounceValue, limit: 100 })

The signal allows you to abort a request if you no longer want to wait for the response. If it's not defined in the options, the library will use a locally managed AbortController(). If you need to manage your own abort, you can do it like this:

let controller = null
...
if(!search) {
   controller.abort()
   setResults([])
   return
}

if(controller) {
  controller.abort()
}

controller = new AbortController()
const places = await searchPlaces(search, {signal: controller.signal})

How to publish a new version?

When a branch is merged into master, it will automatically deploy a new version to npm.

How it works?

This library is based on the API of data.gouv. These data are quite precise and are regularly updated: title

0.42.0

8 months ago

0.43.0

8 months ago

0.40.0

10 months ago

0.41.0

10 months ago

0.39.0

10 months ago

0.38.0

10 months ago

0.37.0

11 months ago

0.35.0

11 months ago

0.33.0

11 months ago

0.31.0

11 months ago

0.30.0

11 months ago

0.28.0

2 years ago

0.46.0

7 months ago

0.44.0

8 months ago

0.45.0

8 months ago

0.26.0

2 years ago

0.21.0

2 years ago

0.25.0

2 years ago

0.24.0

2 years ago

0.23.0

2 years ago

0.22.0

2 years ago

0.20.0

3 years ago

0.19.0

3 years ago

0.16.0

3 years ago

0.17.0

3 years ago

0.18.0

3 years ago

0.12.0

3 years ago

0.13.0

3 years ago

0.14.0

3 years ago

0.15.0

3 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago