superfaceai / cli

Let AI connect the APIs for you
https://superface.ai
MIT License
22 stars 4 forks source link

[BUG] incorrect handling of non-JSON media type in Comlink map #358

Open zdne opened 1 year ago

zdne commented 1 year ago

Mixpanel' API returns "JSON Lines" in their GET https://data.mixpanel.com/api/2.0/export endpoint.

This is incorrectly handled in map as body.results:

...

const body = response.bodyAuto() ?? {};

if (response.status !== 200) {
  const error = {
    error: body.error || 'An error occurred while fetching data.',
    status: response.status,
    request_id: response.headers['x-request-id']?.[0] || '',
  };
  throw new std.unstable.MapError(error);
}

const results = body.results.map((result) => ({
  event: result.event,
  properties: {
    time: result.properties.time,
    distinct_id: result.properties
...

Note there is no results the body is not JSON at all!

Expected Behavior

Either the CLI (1) fails building the map and reports that it currently does not support JSON lines and user needs to asses it manually. Or (2), preferably, it processes the plain text body, splits the JSON by new lines and parses them before mapping.

Steps to Reproduce

$ sf prepare https://developer.mixpanel.com/reference/query-api "mixpanel"
$ sf new mixpanel "list user activities"
$ sf map ...

Your Environment