print customers

This commit is contained in:
Kiara Grouwstra 2021-10-05 18:51:47 +02:00
parent ced2be41ad
commit d7e5e03105
7 changed files with 30 additions and 67 deletions

3
.gitignore vendored
View File

@ -17,3 +17,6 @@ docs/
# Build directory
build/
# javascript
*.js

View File

@ -14,9 +14,6 @@ set the API keys for [AirTable](https://airtable.com/account) and [Mollie](https
export NODE_OPTIONS=--experimental-vm-modules npx jest
export AIRTABLE_API_KEY=<MY_KEY>
export MOLLIE_API_KEY_LEDEN=<MY_KEY>
export MOLLIE_API_KEY_DOEMEE=<MY_KEY>
export MOLLIE_API_KEY_SHOP=<MY_KEY>
export MOLLIE_API_KEY_DONEER=<MY_KEY>
```
`yarn start` launches the app

View File

@ -5,7 +5,7 @@
"transform": {},
"main": "src/app.js",
"scripts": {
"start": "node src/app.js",
"start": "tsc; node src/app.js",
"dev": "chokidar src -c 'npm run test'",
"test": "jshint --config .jshintrc ./src && jest"
},
@ -24,7 +24,6 @@
"airtable": "^0.11.1",
"chokidar-cli": "^3.0.0",
"mollie": "^2.0.2",
"rxjs": "^7.3.0",
"typescript": "^4.4.3"
},
"devDependencies": {

View File

@ -1,3 +1,16 @@
"use strict";
// import "./airtable";
// import "./mollie";
import { createMollieClient } from '@mollie/api-client';
import { log } from './utils';
import { mollieKeys, listAll } from './mollie';
async function main() {
const mollie = createMollieClient({ apiKey: mollieKeys.leden });
const page$ = mollie.customers.list();
const list = await listAll(page$, 500);
// const list = Array.from(await page$);
// log(list);
const list_ = list.map(({ id, name, email, createdAt }) => ({ id, name, email, createdAt }));
log(list_);
log(list_.length);
}
main();

View File

@ -1,43 +1,14 @@
"use strict";
import { of, from, concat, Observable, firstValueFrom } from "rxjs";
import { toArray, mergeMap } from "rxjs/operators";
import { List } from "@mollie/api-client";
import { log } from "./utils";
function printId<T>(x: T): T {
log(x);
return x;
}
export const mollieKeys = {
leden: process.env.MOLLIE_API_KEY_LEDEN || '',
doemee: process.env.MOLLIE_API_KEY_DOEMEE || '',
shop: process.env.MOLLIE_API_KEY_SHOP || '',
doneer: process.env.MOLLIE_API_KEY_DONEER || '',
};
export function unrollResult<T>(result: List<T>): Observable<T> {
log('unrollResult');
// log(result);
const { count, nextPage, links } = result;
log(count);
// log(nextPage);
return concat(
of(...result),
of(... result.nextPage ? listAll(result.nextPage()) : []),
);
}
export function listAll<T>(page$: Promise<List<T>>): Observable<T> {
log('listAll');
// log(page$);
// page$.then(log);
return from(page$).pipe(
printId,
mergeMap(unrollResult)
);
}
export function listPromise<T>(page$: Promise<List<T>>): Promise<T[]> {
return firstValueFrom(listAll(page$).pipe(toArray()));
export async function listAll<T>(list$: Promise<List<T>>, ms: number = 1000): Promise<T[]> {
const list = await list$;
const { count, nextPage, links } = list;
await new Promise(resolve => setTimeout(resolve, ms));
const rest = await (nextPage ? listAll(nextPage()) : Promise.resolve([]));
return [...list, ...rest];
}

View File

@ -1,26 +1,18 @@
import { createMollieClient } from '@mollie/api-client';
import { log } from "../src/utils";
import { mollieKeys, listPromise } from '../src/mollie';
import { mollieKeys, listAll } from '../src/mollie';
describe('airtable', () => {
const mollie = createMollieClient({ apiKey: mollieKeys.leden });
it('can paginate customers', async () => {
const list = await listPromise(mollie.customers.list());
// log(list);
expect(list.length).toBeGreaterThan(50);
});
xit('can fetch a page of payments', async () => {
it('can fetch a page of payments', async () => {
const list = await mollie.payments.list();
expect(list.length).toEqual(50);
});
xit('can paginate payments', async () => {
const list = await listPromise(mollie.payments.list());
// log(list);
log(list.length);
xit('can paginate customers', async () => {
const list = await listAll(mollie.customers.list());
expect(list.length).toBeGreaterThan(50);
});
})

View File

@ -2550,13 +2550,6 @@ rimraf@^3.0.0:
dependencies:
glob "^7.1.3"
rxjs@^7.3.0:
version "7.3.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.3.0.tgz#39fe4f3461dc1e50be1475b2b85a0a88c1e938c6"
integrity sha512-p2yuGIg9S1epc3vrjKf6iVb3RCaAYjYskkO+jHIaV0IjOPlJop4UnodOoFb2xeNwlguqLYvGw1b1McillYb5Gw==
dependencies:
tslib "~2.1.0"
safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
@ -2836,11 +2829,6 @@ ts-jest@^27.0.5:
semver "7.x"
yargs-parser "20.x"
tslib@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"
integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==
type-check@~0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"