0

I'm trying out Lichess API. I'm trying to export games of a user. According to documentation I can receive either PGN or ndjson as response.

Right now this don't work:

const api_url = "https://lichess.org/api/games/user/Terinieks?max=5&since=1578607200000&perfType=bullet"

async function getData() {
   let response = await fetch(api_url, {
      headers: {
         "Accept": "application/x-ndjson"
      }
   });
   let data = await response.json();
}
getData()

I'm struggling to find how to make await response.json() work.

Since as far as I understand I now need somehow convert my response (which is in ndjson) into json, but how?

Update 1

I'm trying to use NPM package 'can-ndjson-stream', which was suggested from article: Streaming Data with Fetch() and NDJSON

In app.js I have import ndjsonStream from "can-ndjson-stream"; but I get error:

module "c:/Users/Juris/Desktop/Chess Stats/node_modules/can-ndjson-stream/can-ndjson-stream"

Could not find a declaration file for module 'can-ndjson-stream'.
'c:/Users/Juris/Desktop/Chess Stats/node_modules/can-ndjson-stream/
can-ndjson-stream.js' implicitly has an 'any' type.

  Try npm install @types/can-ndjson-stream if it exists 
or add a new declaration (.d.ts) file containing
declare module 'can-ndjson-stream';ts(7016)

And in Chrome this error:

Uncaught TypeError: Failed to resolve module specifier "can-ndjson-stream". Relative references must start with either "/", "./", or "../".

index.html has <script type="module" src="JS/app.js"></script>

Not sure how to get import statement work correctly.

1 Answers1

1

As ndjson is in fact a collection of JSON lines, so, separated by \n characters, you should be able to get the results by changing this line:

let data = await response.json();

to:

let data = (await response.text()).match(/.+/g).map(JSON.parse);

NB: /.+/g matches non-empty lines.

trincot
  • 317,000
  • 35
  • 244
  • 286