Giter Site home page Giter Site logo

neuroforgede / restful-stream.ts Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 0.0 138 KB

Turn paginated REST APIs into iterators. npm @ https://www.npmjs.com/package/restful-stream

Home Page: https://neuroforge.de/typescript-wie-man-eine-seitenbasierte-rest-api-in-einen-daten-stream-umwandelt/

License: MIT License

TypeScript 100.00%
etl stream typescript big data rest restful-api

restful-stream.ts's Introduction

RESTful-stream.ts

This repository contains a small library function that allows to convert paginated RESTful data into a AsyncIterableIterator which chunks the pages into memory as soon as they are requested (and caches them) and serves the elements on the page one by one in a stream.

This is particularly useful if you want to consume paginated REST apis in e.g. a nodejs program. Given two methods:

function queryInitial(): Promise<PageType> {
    // use your asynchronous rest library of choice here
}

function querySubsequent(link: string): Promise<PageType> {
    // use your asynchronous rest library of choice here
}

function parseFn(obj: PageType): Promise<DataType[]> {
    // convert a JSON page into 
}

instead of writing

let page = await queryInitial();
while(page) {
    // do stuff with page
    const parsed = await parseFn(page);

    for(const elem of parsed) {
        // do stuff with element
    }

    if(page.nextLink && page.nextLink != null) {
        page = await querySubsequent(page.nextLink);
    } else {
        page = null;
    }
}

we can now write:

type PageType = ...;
type DataType = ...;

const initialLink: string = // url to the initial link
const ctrl: Control<PageType, DataType> = {
    hasNext(page: PageType) {
        return page.nextLink && page.nextLink != null;
    },
    next(page: PageType) {
        return querySubsequent(page.nextLink);
    },
    parse(page: PageType) {
        return parseFn(initialLink);
    }
};

for await(const elem of iterate(parse(ctrl, queryInitial()))) {
    // do stuff with element
}

We can even go further and define a utility method ctrlGen specific to our REST API:

type PageType = // generic PageType for our API
function ctrlGen<DataType>(initialLink: string, parseFn: (page: PageType) => Promise<DataType>) {
    const ctrl: Control<PageType, DataType> = {
        hasNext(page: PageType) {
            return page.nextLink && page.nextLink != null;
        },
        next(page: PageType) {
            return querySubsequent(page.initialLink);
        },
        parse(page: PageType) {
            return parseFn(initialLink);
        }
    };
};

This allows us to define the parse function for each Entity type, and can then use it:

function parse1(page: PageType): Promise<Type1> {
    const ret = ...;
    return ret;
}

function parse2(page: PageType): Promise<Type1> {
    const ret = ...;
    return ret;
}

for await(const elem1 of iterate(parse(ctrlGen('https://url.to.rest.api/type1', parse1), queryInitial1()))) {
    for await(const elem2 of iterate(parse(ctrlGen(`https://url.to.rest.api/${type1.id}/type2`, parse2), queryInitial2()))) {
        // do stuff
    }
}

If you like this project, consider leaving a star on the repository at GitHub.

Proudly made by NeuroForge in Bayreuth, Germany.

restful-stream.ts's People

Contributors

dependabot[bot] avatar s4ke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.