geocodeearth / core-js

JavaScript client for the Geocode Earth API
1 stars 0 forks source link

fleshing out result.ts #5

Closed missinglink closed 3 years ago

missinglink commented 3 years ago

@mxlje would it be helpful for me to better define the JSON response format schema(s)?

Geocoding results

I found someone else has already defined the envelope format for GeoJSON: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/geojson/index.d.ts

Within that we can then define our application-specific fields:

Errors

The errors JSON format is actually different, it looks like this:

{
  "meta": {
    "version": 1,
    "status_code": 403
  },
  "results": {
    "error": {
      "type": "KeyError",
      "message": "Invalid api_key specified."
    }
  }
}

Is this something we want to tackle now or is it fine to leave it fairly generic?

mxlje commented 3 years ago

Response: This is exactly what we needed, great!

errors: I adjusted the error we return a bit to make the format simpler, but we could also return it as is from the API and add the rate limiting Info.

I think having a simpler error format might make sense for this library though. We don’t need to return a results section etc, we can extract the error info and return it in a bit of a more compact Form.

mxlje commented 3 years ago

After my initial excitement I looked a the types more closely trying to implement them, but unfortunately the main section of a feature, its properties, are not typed at all and are just a map of string: any, which we can just as well leave out in that case. You already mentioned that in your comment, I’m sorry.

I’ll see that I use the other types as a base to define our result type. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/geojson/index.d.ts#L145

mxlje commented 3 years ago

This is what I have so far for an individual Feature.

Are there other values for accuracy possible? I tried a bunch of different API requests and could only get the two.

I’m not sure about addendum, I think we could leave that as any for now as I think there can be a lot of different properties coming from openstreetmap.

type Layers = 'address' |
  'borough' |
  'coarse' |
  'country' |
  'county' |
  'localadmin' |
  'locality' |
  'macrocounty' |
  'macroregion' |
  'neighbourhood' |
  'postalcode' |
  'region' |
  'street' |
  'venue'

type Sources = 'openstreetmap' | 'openaddresses' | 'geonames' | 'whosonfirst'

type Feature = {
  type: 'Feature'
  geometry: {
    type: 'Point'
    coordinates: [number, number]
  }
  properties: {
    id: string
    gid: string
    layer: Layers
    source: Sources
    source_id: string

    label: string
    name: string

    accuracy: 'centroid' | 'point'

    neighbourhood?: string
    neighbourhood_gid?: string

    borough?: string
    borough_gid?: string

    county?: string
    county_a?: string
    county_gid?: string

    locality_gid?: string
    locality?: string

    localadmin?: string
    localadmin_gid?: string

    region?: string
    region_a?: string
    region_gid?: string
    macroregion?: string
    macroregion_gid?: string

    country?: string
    country_a?: string
    country_gid?: string

    continent?: string
    continent_gid?: string

    street?: string
    housenumber?: string
    postalcode?: string

    addendum?: any
  }
  bbox?: [number, number, number, number] | [number, number, number, number, number, number]
}
missinglink commented 3 years ago

Hmmm this is actually quite tricky, @orangejulius there's a lot of things to consider:

sources & layers

geometry

addendum

accuracy

the list of 'admin' fields

other

orangejulius commented 3 years ago

The response format documentation should be helpful here. There's definitely a lot of nuance in the details and some of the lesser-known fields like accuracy and match_type.

mxlje commented 3 years ago

This is all super helpful, I’ll open a PR with an updated format soon so we can review it directly in there.