@faable/sdk-base 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/FaableApi.d.ts +2 -1
- package/dist/FaableApi.d.ts.map +1 -1
- package/dist/FaableApi.js +2 -2
- package/dist/helpers/paginator.d.ts +4 -1
- package/dist/helpers/paginator.d.ts.map +1 -1
- package/dist/helpers/paginator.js +44 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
</p>
|
|
7
7
|
|
|
8
8
|
<p align="center">
|
|
9
|
-
<a aria-label="NPM version" href="https://www.npmjs.com/package/@faable/sdk-
|
|
10
|
-
<img alt="" src="https://img.shields.io/npm/v/@faable/sdk-
|
|
9
|
+
<a aria-label="NPM version" href="https://www.npmjs.com/package/@faable/sdk-base">
|
|
10
|
+
<img alt="" src="https://img.shields.io/npm/v/@faable/sdk-base.svg?style=for-the-badge&labelColor=000000">
|
|
11
11
|
</a>
|
|
12
12
|
</p>
|
|
13
13
|
|
|
14
14
|
## Install
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
|
-
npm install @faable/sdk-
|
|
17
|
+
npm install @faable/sdk-base
|
|
18
18
|
```
|
package/dist/FaableApi.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { AuthInterface } from "./types/AuthInterface.js";
|
|
2
2
|
import { Fetcher, FetcherCreateParams } from "./fetcher/Fetcher.js";
|
|
3
|
-
import { Paginator } from "./helpers/paginator.js";
|
|
3
|
+
import { Paginator, PaginatorOptions } from "./helpers/paginator.js";
|
|
4
4
|
export type ApiParams = {
|
|
5
5
|
baseURL?: string;
|
|
6
6
|
fetcher?: FetcherCreateParams;
|
|
7
7
|
auth?: AuthInterface<any, any>;
|
|
8
8
|
debug?: boolean;
|
|
9
|
+
paginator?: Partial<PaginatorOptions>;
|
|
9
10
|
};
|
|
10
11
|
export declare abstract class FaableApi<Params extends ApiParams = ApiParams> {
|
|
11
12
|
protected fetcher: Fetcher;
|
package/dist/FaableApi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FaableApi.d.ts","sourceRoot":"","sources":["../src/FaableApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAEpE,OAAO,
|
|
1
|
+
{"version":3,"file":"FaableApi.d.ts","sourceRoot":"","sources":["../src/FaableApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAEpE,OAAO,EAEL,SAAS,EACT,gBAAgB,EACjB,MAAM,wBAAwB,CAAC;AAEhC,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,IAAI,CAAC,EAAE,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC/B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;CACvC,CAAC;AAEF,8BAAsB,SAAS,CAAC,MAAM,SAAS,SAAS,GAAG,SAAS;IAClE,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC;IAC3B,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC;IAC/B,SAAS,aAAa,MAAM,CAAC,EAAE,MAAM;IAMrC,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,QAAQ,MAAM,IAAI,EAAE,MAAM,KAAK,GAAG,EAAE,MAAM,GAAG,GAAG,EACtE,IAAI,EAAE,CAAC,EACP,MAAM,CAAC,EAAE,MAAM,GACd,YAAY,CAAC,CAAC,CAAC;CAInB"}
|
package/dist/FaableApi.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { fetcher_axios } from "./fetcher/fetcher_axios.js";
|
|
2
|
-
import { buildPaginator } from "./helpers/paginator.js";
|
|
2
|
+
import { buildPaginator, } from "./helpers/paginator.js";
|
|
3
3
|
export class FaableApi {
|
|
4
4
|
fetcher;
|
|
5
5
|
paginator;
|
|
6
6
|
constructor(params) {
|
|
7
7
|
const options = params || {};
|
|
8
8
|
this.fetcher = fetcher_axios(options);
|
|
9
|
-
this.paginator = buildPaginator(this.fetcher);
|
|
9
|
+
this.paginator = buildPaginator(this.fetcher, options.paginator);
|
|
10
10
|
}
|
|
11
11
|
static create(params) {
|
|
12
12
|
let Cns = this;
|
|
@@ -4,7 +4,10 @@ export interface Page<T> {
|
|
|
4
4
|
results: T[];
|
|
5
5
|
}
|
|
6
6
|
export type Paginator = ReturnType<typeof buildPaginator>;
|
|
7
|
-
export
|
|
7
|
+
export type PaginatorOptions = {
|
|
8
|
+
limit_pages: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const buildPaginator: (fetcher: Fetcher, options?: Partial<PaginatorOptions>) => <T>(req: FetcherRequestParams) => {
|
|
8
11
|
all: () => Promise<T[]>;
|
|
9
12
|
first: () => Promise<Page<T>>;
|
|
10
13
|
pass: (params?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paginator.d.ts","sourceRoot":"","sources":["../../src/helpers/paginator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAGtE,MAAM,WAAW,IAAI,CAAC,CAAC;IACrB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,CAAC,EAAE,CAAC;CACd;AACD,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"paginator.d.ts","sourceRoot":"","sources":["../../src/helpers/paginator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAGtE,MAAM,WAAW,IAAI,CAAC,CAAC;IACrB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,CAAC,EAAE,CAAC;CACd;AACD,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AAE1D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,eAAO,MAAM,cAAc,YAChB,OAAO,YACN,OAAO,CAAC,gBAAgB,CAAC,MAO3B,CAAC,OAAO,oBAAoB;;;oBAoCX;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE;CAWhE,CAAC"}
|
|
@@ -1,38 +1,49 @@
|
|
|
1
1
|
import { FaableApiError } from "./FaableApiError.js";
|
|
2
|
-
export const buildPaginator = (fetcher
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
export const buildPaginator = (fetcher, options) => {
|
|
3
|
+
const LIMIT_PAGES = options?.limit_pages || 100;
|
|
4
|
+
if (LIMIT_PAGES < 0) {
|
|
5
|
+
throw new Error("limit_pages cannot be negative");
|
|
6
|
+
}
|
|
7
|
+
return (req) => {
|
|
8
|
+
return {
|
|
9
|
+
all: async () => {
|
|
10
|
+
const pages = [];
|
|
11
|
+
let data;
|
|
12
|
+
let nextCurrent;
|
|
13
|
+
do {
|
|
14
|
+
// Upper bound to kill if limit pages reached
|
|
15
|
+
if (LIMIT_PAGES > 0 && pages.length >= LIMIT_PAGES) {
|
|
16
|
+
throw new FaableApiError(`Cannot fetch more than ${LIMIT_PAGES} pages, update paginator configuration to continue`);
|
|
17
|
+
}
|
|
18
|
+
// Do request
|
|
19
|
+
data = await fetcher.request({
|
|
20
|
+
...req,
|
|
21
|
+
params: data?.next
|
|
22
|
+
? { ...req.params, next: data.next }
|
|
23
|
+
: req.params,
|
|
24
|
+
});
|
|
25
|
+
// Next cursor cannot be the same as previous received one. Sanity check the api.
|
|
26
|
+
if (data?.next != null && nextCurrent == data?.next) {
|
|
27
|
+
throw new FaableApiError("Bad next cursor");
|
|
28
|
+
}
|
|
29
|
+
nextCurrent = data.next;
|
|
30
|
+
// Add to data array
|
|
31
|
+
pages.push(data);
|
|
32
|
+
} while (data.next);
|
|
33
|
+
return pages.map((page) => page.results).flat();
|
|
34
|
+
},
|
|
35
|
+
first: async () => {
|
|
36
|
+
return fetcher.request(req);
|
|
37
|
+
},
|
|
38
|
+
pass: async (params = {}) => {
|
|
39
|
+
return fetcher.request({
|
|
10
40
|
...req,
|
|
11
|
-
params:
|
|
12
|
-
|
|
13
|
-
|
|
41
|
+
params: {
|
|
42
|
+
...req.params,
|
|
43
|
+
...params,
|
|
44
|
+
},
|
|
14
45
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
throw new FaableApiError("Bad next cursor");
|
|
18
|
-
}
|
|
19
|
-
nextCurrent = data.next;
|
|
20
|
-
// Add to data array
|
|
21
|
-
pages.push(data);
|
|
22
|
-
} while (data.next);
|
|
23
|
-
return pages.map((page) => page.results).flat();
|
|
24
|
-
},
|
|
25
|
-
first: async () => {
|
|
26
|
-
return fetcher.request(req);
|
|
27
|
-
},
|
|
28
|
-
pass: async (params = {}) => {
|
|
29
|
-
return fetcher.request({
|
|
30
|
-
...req,
|
|
31
|
-
params: {
|
|
32
|
-
...req.params,
|
|
33
|
-
...params,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
37
48
|
};
|
|
38
49
|
};
|