Options
All
  • Public
  • Public/Protected
  • All
Menu

Developer Chat Apache 2.0 License Typescript Web


Reading data

Before data can be read, we should already have a user access token for this user.

If not, you'll need to authorize them first, and ask them to onboard any extra services to provide the data you're requesting.

Getting a Read Session

To start reading user data, we first need to obtain a session:

// initialize the SDK
import { init } from "@digime/digime-sdk-nodejs";
const sdk = init({ applicationId });

// contractDetails - The same one used in getAuthorizeUrl().
// userAccessToken - The user access token from the authorization step.
// scope - (Optional) For filtering the amount of data retrieved.

const { session, updatedAccessToken }  = await sdk.readSession({
    contractDetails,
    userAccessToken,
});

The session received can now be used to query data.

Reading All Files

Using the session received above, we can trigger readAllFiles() to read all available files from this user.

// ... initialize the SDK

// session - The session we received from readSession().
// privateKey - The private key for this contract.
// onFileData - A function that will be called when a file is successfully downloaded.
// onFileError - A function that will be called when an error occurs when downloading a file.

const { stopPolling, filePromise } = await sdk.readAllFiles({
    sessionKey: session.key,
    privateKey: <private-key-of-contract>,
    onFileData: ({fileData, fileName, fileMetadata}) => {
        // This is where you deal with any data you receive from digi.me,
        const data = JSON.parse(fileData.toString("utf8"));
        console.log("Retrieved: ", fileName);
        console.log("Metadata:\n", JSON.stringify(fileMetadata, null, 2));
        console.log("Content:\n", JSON.stringify(data, null, 2));
    },
    onFileError: ({fileName, error}) => {
        console.log(`Error retrieving file ${fileName}: ${error.toString()}`);
    },
});

// filePromise is a promise that will resolve when data fetching is complete.
// stopPolling is a function that you can call if you would like to stop the process when it's still running.

The type of FileMetadata that is returned depends on the type of data it is. Please see here for more information.

Selecting Files

If you'd like more control over the downloading of the files, we can call readFileList() to see all available files from this user:

// ... initialize the SDK
// session - The session we received from readSession().

const response = await readFileList({ sessionKey: session.key });

Response contains a fileList and status of each user service onboarded.

You can then download the files manually using readFile().

// ... initialize the SDK
// session - The session we received from readSession().
// file - The file object from getFileList() that you want to download.
// privateKey - private key of your contract.

const data = await readFile({
    sessionKey: session.key,
    fileName: file.name,
    privateKey,
});

Generated using TypeDoc