New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

connectedpapers-js

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connectedpapers-js

The official client for the connected papers API

0.1.3
Source
npm
Version published
Weekly downloads
5
66.67%
Maintainers
1
Weekly downloads
 
Created
Source

connectedpapers-js

The JS client for the connected papers API.

Installation

For npm:

npm install connectedpapers-js

For yarn:

yarn add connectedpapers-js

Usage

import { ConnectedPapersClient } from 'connectedpapers-js';

const DEEPFRUITS_PAPER_ID = "9397e7acd062245d37350f5c05faf56e9cfae0d6"

const client = new ConnectedPapersClient();
client.getGraph({paper_id: DEEPFRUITS_PAPER_ID, fresh_only: true}).then((paper) => {
  console.log(paper);
});
client.getRemainingUsages().then((remainingUses) => {
  console.log(`Remaining uses: ${remainingUses}`);
});
client.getFreeAccessPapers().then((freeAccessPapers) => {
  console.log(`Free access papers: ${freeAccessPapers}`);
});

API

The following async functions are part of the connected papers API:

  • getPaper({paper_id: string, fresh_only: boolean}): Returns the paper with the given ID. If fresh_only is true, then if the graph is over 30 days old, the call will wait for a rebuild.
  • getRemainingUsages(): Returns the number of remaining usages for the current API key.
  • getFreeAccessPapers(): Returns the number of free access papers for the current API key.

Free access papers

If you've already accessed a paper's graph within the past 30 days, any additional access to the same graph during this period won't be counted against your usage limit. Such repeated access is considered "free," and the paper in question is referred to as a "free access paper."

Supplying an API key

There are two ways to supply an API key to the client:

  • Set the CONNECTED_PAPERS_API_KEY environment variable.
export CONNECTED_PAPERS_API_KEY="<your api key>"

Then create the client without any arguments:

const client = new ConnectedPapersClient();
  • Pass the API key as an argument to the constructor:
const client = new ConnectedPapersClient({access_token: "<your api key>"});

Getting an API key

To get an early-access API key, contact us at hello@connectedpapers.com.

Async iterator access

The client also supports async iterator access to the API. This is useful for tracking the progress of graph builds and getting existing as well as rebuilt papers.

Calling the getGraphAsyncIterator returns an async iterator that yields the GraphResponse type:

export enum GraphResponseStatuses {
  BAD_ID = 'BAD_ID',
  ERROR = 'ERROR',
  NOT_IN_DB = 'NOT_IN_DB',
  OLD_GRAPH = 'OLD_GRAPH',
  FRESH_GRAPH = 'FRESH_GRAPH',
  IN_PROGRESS = 'IN_PROGRESS',
  QUEUED = 'QUEUED',
  BAD_TOKEN = 'BAD_TOKEN',
  BAD_REQUEST = 'BAD_REQUEST',
  OUT_OF_REQUESTS = 'OUT_OF_REQUESTS',
}

export type GraphResponse = {
  status: GraphResponseStatuses;
  graph_json?: Graph;
  progress?: number;
};

Once the status is one of BAD_ID, ERROR, NOT_IN_DB, BAD_TOKEN, BAD_REQUEST, OUT_OF_REQUESTS, the iterator will stop yielding values.

Signature:

class ConnectedPapersClient {
  // ...
  public async* getGraphAsyncIterator(args: {
    paper_id: PaperId;
    fresh_only?: boolean;
    loop_until_fresh?: boolean
  }): AsyncGenerator<GraphResponse>;
}

Call with fresh_only = false, loop_until_fresh = true to get the currently existing graph and keep waiting for a rebuild.

The response will have status GraphResponseStatuses.OLD_GRAPH in the first response, then it will go throught the GraphResponseStatuses.QUEUED, GraphResponseStatuses.IN_PROGRESS states with the progress field set to the percentage of the graph build that is done. When the rebuild is done, the status will be GraphResponseStatuses.FRESH_GRAPH and the loop will stop. The graph_json field will have the graph at each of these responses.

FAQs

Package last updated on 14 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts