Skip to content

Pagination #19

@jimmymcpeter

Description

Similar to phrase/phrase-cli#161, a response's Link/Pagination HTTP headers are only available if I use the library's Raw suffixed functions and parse the headers myself like the example below. Is there anything you can add into the library to better help with pagination?

package.json

{
  "name": "phrase-js-key-lister",
  "version": "0.0.0",
  "description": "Phrase JS Key Lister",
  "private": true,
  "type": "module",
  "engines": {
    "node": ">=20"
  },
  "scripts": {
    "local": "node -r dotenv/config src/main.js"
  },
  "author": "jimmymcpeter",
  "dependencies": {
    "phrase-js": "^3.0.3",
    "form-data": "^4.0.1"
  },
  "devDependencies": {
    "dotenv": "^16.4.7"
  }
}

main.js

import { Configuration, KeysApi } from 'phrase-js';
import FormData from 'form-data';

main();

/**
 * @typedef {object} PageInfoObj
 * @property {number} total_count
 * @property {number} total_pages_count
 * @property {number} current_page
 * @property {number} current_per_page
 * @property {number} previous_page
 * @property {number} next_page
 */

async function main() {
    try {
        const token = process.env.PHRASE_TOKEN;
        const projectId = process.env.PHRASE_PROJECT_ID;
        const uploadId = process.env.PHRASE_UPLOAD_ID;
        
        // FormData is needed for phrase-js to work in Node.js
        const globalAny = global;
        globalAny.FormData = FormData;

        const config = new Configuration({
            apiKey: `token ${token}`,
            fetchApi: fetch, // Native Node.js fetch
        });

        const keysApi = new KeysApi(config);

        /** @type {import('phrase-js').KeysListRequest} */
        const keysListRequest = {
            projectId: projectId,
            q: `uploads:${uploadId}`,
            perPage: 100,
        };

        //const keysList = await keysApi.keysList(keysListRequest);

        const keysListRaw = await keysApi.keysListRaw(keysListRequest);

        /** @type {PageInfoObj | undefined} */
        const keysPageInfo = JSON.parse(keysListRaw.raw.headers.get('Pagination'));
        
        console.log(`Found ${keysPageInfo.total_count} keys`);

    } catch (error) {
        console.error(error);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions