Created
June 2, 2022 21:30
-
-
Save entipe/2d4415c7912a9ac93be0ed968c2d8318 to your computer and use it in GitHub Desktop.
List all Stripe products with RxJS/Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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