Skip to content

Instantly share code, notes, and snippets.

@entipe
Created June 2, 2022 21:30
Show Gist options
  • Save entipe/2d4415c7912a9ac93be0ed968c2d8318 to your computer and use it in GitHub Desktop.
Save entipe/2d4415c7912a9ac93be0ed968c2d8318 to your computer and use it in GitHub Desktop.
List all Stripe products with RxJS/Typescript
const reachEnd = new ReplaySubject<boolean>(1);
let startingAfter: string = undefined;
return
// loop interval
interval(parseInt(process.env.STRIPE_API_DELAY))
.pipe(
// stop loop when api list reaches en
takeUntil(reachEnd),
// actual call to stripeApi
mergeMap(_ => from(this.stripeApi.products.list({
limit: 100,
starting_after: startingAfter
}))),
// set startingAfter for next loop or trigger loop end
tap((response: ProductResponse) => {
if (response.has_more) {
startingAfter = response.data[response.data.length - 1].id
} else {
startingAfter = undefined;
reachEnd.next(true);
reachEnd.complete();
}
}),
// get product[]
map((response: ProductResponse) => response.data),
// populate products[] into an array and return this array
map(products => this.populateList(products)),
// emit the last value (the fullfilled array)
takeLast(1),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment