@faable/sdk-base 1.0.13 → 1.0.15
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/dist/FaableApi.d.ts +2 -1
- package/dist/FaableApi.d.ts.map +1 -1
- package/dist/FaableApi.js +2 -2
- package/dist/auth/client_credentials/client_credentials.d.ts +0 -3
- package/dist/auth/client_credentials/client_credentials.d.ts.map +1 -1
- package/dist/auth/client_credentials/client_credentials.js +8 -14
- package/dist/auth/client_credentials/store.d.ts +9 -5
- package/dist/auth/client_credentials/store.d.ts.map +1 -1
- package/dist/auth/client_credentials/store.js +8 -5
- package/dist/auth/client_credentials/types.d.ts +6 -0
- package/dist/auth/client_credentials/types.d.ts.map +1 -0
- package/dist/auth/client_credentials/types.js +1 -0
- package/dist/fetcher/Fetcher.d.ts +4 -4
- package/dist/fetcher/Fetcher.d.ts.map +1 -1
- package/dist/fetcher/fetcher_axios.d.ts.map +1 -1
- package/dist/fetcher/fetcher_axios.js +4 -1
- 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 +4 -3
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;
|
|
@@ -6,9 +6,6 @@ type Params = {
|
|
|
6
6
|
debug?: boolean;
|
|
7
7
|
};
|
|
8
8
|
export type ClientCredentialsAuthParams = Partial<[Partial<Params>]>;
|
|
9
|
-
export interface ClientCredentialsResponse {
|
|
10
|
-
access_token: string;
|
|
11
|
-
}
|
|
12
9
|
export declare const createClientCredentials: StrategyInterface<ClientCredentialsAuthParams>;
|
|
13
10
|
export {};
|
|
14
11
|
//# sourceMappingURL=client_credentials.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client_credentials.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/client_credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAKrE,KAAK,MAAM,GAAG;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAMrE,
|
|
1
|
+
{"version":3,"file":"client_credentials.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/client_credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAKrE,KAAK,MAAM,GAAG;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAMrE,eAAO,MAAM,uBAAuB,EAAE,iBAAiB,CACrD,2BAA2B,CAwF5B,CAAC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { fetcher_axios } from "../../fetcher/fetcher_axios.js";
|
|
2
|
-
import { jwtDecode } from "jwt-decode";
|
|
3
2
|
import { createTokenStore } from "./store.js";
|
|
4
3
|
import PQueue from "p-queue";
|
|
5
4
|
const CLIENT_ID_ENV = "FAABLEAUTH_CLIENT_ID";
|
|
@@ -21,14 +20,9 @@ export const createClientCredentials = (params) => {
|
|
|
21
20
|
}
|
|
22
21
|
const store = createTokenStore({ debug });
|
|
23
22
|
const queue = new PQueue({ concurrency: 1, timeout: 20 * 1000 });
|
|
24
|
-
const isTokenExpired = (token) => {
|
|
25
|
-
const {
|
|
26
|
-
|
|
27
|
-
if (!exp) {
|
|
28
|
-
throw new Error("Missing expire date");
|
|
29
|
-
}
|
|
30
|
-
const currentSeconds = new Date().getTime() / 1000;
|
|
31
|
-
return currentSeconds > exp;
|
|
23
|
+
const isTokenExpired = (token, issued_at) => {
|
|
24
|
+
const { expires_in } = token;
|
|
25
|
+
return issued_at + expires_in < Date.now();
|
|
32
26
|
};
|
|
33
27
|
const fetcher = fetcher_axios({
|
|
34
28
|
baseURL: auth_domain,
|
|
@@ -40,16 +34,16 @@ export const createClientCredentials = (params) => {
|
|
|
40
34
|
params.append("client_secret", client_secret);
|
|
41
35
|
const token_response = await fetcher.post("/oauth/token", params);
|
|
42
36
|
// Save token in store
|
|
43
|
-
store.
|
|
37
|
+
store.saveTokenResponse(token_response);
|
|
44
38
|
return token_response;
|
|
45
39
|
};
|
|
46
40
|
const getToken = async () => {
|
|
47
|
-
let
|
|
41
|
+
let res = store.getTokenResponse();
|
|
48
42
|
// We don't have a token or it's expired
|
|
49
|
-
if (!
|
|
50
|
-
|
|
43
|
+
if (!res || isTokenExpired(res.response, res.iat)) {
|
|
44
|
+
return requestToken();
|
|
51
45
|
}
|
|
52
|
-
return
|
|
46
|
+
return res.response;
|
|
53
47
|
};
|
|
54
48
|
const getTokenWithLimits = async () => {
|
|
55
49
|
const token = await queue.add(() => getToken());
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
access_token: string;
|
|
3
|
-
}
|
|
1
|
+
import { ClientCredentialsResponse } from "./types.js";
|
|
4
2
|
type TokenStoreOptions = {
|
|
5
3
|
debug?: boolean;
|
|
6
4
|
};
|
|
7
5
|
export declare const createTokenStore: ({ debug }?: TokenStoreOptions) => {
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
saveTokenResponse: (response: ClientCredentialsResponse) => {
|
|
7
|
+
response: ClientCredentialsResponse;
|
|
8
|
+
iat: number;
|
|
9
|
+
};
|
|
10
|
+
getTokenResponse: () => {
|
|
11
|
+
response: ClientCredentialsResponse;
|
|
12
|
+
iat: number;
|
|
13
|
+
} | null;
|
|
10
14
|
};
|
|
11
15
|
export {};
|
|
12
16
|
//# sourceMappingURL=store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/store.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEvD,KAAK,iBAAiB,GAAG;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,gBAAgB,eAAe,iBAAiB;kCAStB,yBAAyB;kBAPlD,yBAAyB;aAC9B,MAAM;;;kBADD,yBAAyB;aAC9B,MAAM;;CAkBd,CAAC"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
export const createTokenStore = ({ debug } = {}) => {
|
|
2
2
|
let _stored_token;
|
|
3
|
-
const
|
|
3
|
+
const getTokenResponse = () => {
|
|
4
4
|
return _stored_token;
|
|
5
5
|
};
|
|
6
|
-
const
|
|
7
|
-
_stored_token =
|
|
6
|
+
const saveTokenResponse = (response) => {
|
|
7
|
+
_stored_token = {
|
|
8
|
+
response,
|
|
9
|
+
iat: Date.now(),
|
|
10
|
+
};
|
|
8
11
|
debug && console.log(`[SDK] Token stored successfully`);
|
|
9
12
|
return _stored_token;
|
|
10
13
|
};
|
|
11
14
|
return {
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
saveTokenResponse,
|
|
16
|
+
getTokenResponse,
|
|
14
17
|
};
|
|
15
18
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/auth/client_credentials/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,yBAAyB;IACxC,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -20,10 +20,10 @@ export type FetcherConfig = {
|
|
|
20
20
|
params?: Partial<Params>;
|
|
21
21
|
};
|
|
22
22
|
export type Fetcher = {
|
|
23
|
-
get: <T>(url: string, config?: FetcherConfig) => Promise<T>;
|
|
24
|
-
post: <T>(url: string, data: any, config?: FetcherConfig) => Promise<T>;
|
|
25
|
-
request: <T>(params: FetcherRequestParams) => Promise<T>;
|
|
26
|
-
check:
|
|
23
|
+
get: <T = any>(url: string, config?: FetcherConfig) => Promise<T>;
|
|
24
|
+
post: <T = any>(url: string, data: any, config?: FetcherConfig) => Promise<T>;
|
|
25
|
+
request: <T = any>(params: FetcherRequestParams) => Promise<T>;
|
|
26
|
+
check: (url: string, status?: number, config?: FetcherConfig) => Promise<boolean>;
|
|
27
27
|
};
|
|
28
28
|
export type FetcherResponse = {
|
|
29
29
|
status?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Fetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/Fetcher.ts"],"names":[],"mappings":"AAAA,KAAK,MAAM,GACP,KAAK,GACL,KAAK,GACL,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,SAAS,GACT,SAAS,GACT,MAAM,GACN,MAAM,GACN,KAAK,GACL,KAAK,GACL,OAAO,GACP,OAAO,GACP,OAAO,GACP,OAAO,GACP,MAAM,GACN,MAAM,GACN,QAAQ,GACR,QAAQ,CAAC;AACb,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,GAAG,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3B,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3B,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"Fetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/Fetcher.ts"],"names":[],"mappings":"AAAA,KAAK,MAAM,GACP,KAAK,GACL,KAAK,GACL,QAAQ,GACR,QAAQ,GACR,MAAM,GACN,MAAM,GACN,SAAS,GACT,SAAS,GACT,MAAM,GACN,MAAM,GACN,KAAK,GACL,KAAK,GACL,OAAO,GACP,OAAO,GACP,OAAO,GACP,OAAO,GACP,MAAM,GACN,MAAM,GACN,QAAQ,GACR,QAAQ,CAAC;AACb,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,GAAG,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3B,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3B,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,GAAG,EAAE,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAClE,IAAI,EAAE,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAC9E,OAAO,EAAE,CAAC,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,oBAAoB,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAC/D,KAAK,EAAE,CACL,GAAG,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,aAAa,KACnB,OAAO,CAAC,OAAO,CAAC,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,MAAM,CAAC,EAAE;QACP,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;CACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher_axios.d.ts","sourceRoot":"","sources":["../../src/fetcher/fetcher_axios.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAiB,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAY5C,eAAO,MAAM,aAAa,YAAY,SAAS,KAAQ,
|
|
1
|
+
{"version":3,"file":"fetcher_axios.d.ts","sourceRoot":"","sources":["../../src/fetcher/fetcher_axios.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAiB,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAY5C,eAAO,MAAM,aAAa,YAAY,SAAS,KAAQ,OAgEtD,CAAC"}
|
|
@@ -20,7 +20,7 @@ export const fetcher_axios = (params = {}) => {
|
|
|
20
20
|
});
|
|
21
21
|
// Add base interceptor
|
|
22
22
|
instance.interceptors.response.use((res) => {
|
|
23
|
-
if (params.debug) {
|
|
23
|
+
if (params.debug && res.config.params) {
|
|
24
24
|
const queryparams = new URLSearchParams(res.config.params);
|
|
25
25
|
console.log(`[${res.status}] ${res.config.url}${queryparams.size > 0 ? `?${queryparams.toString()}` : ""}`);
|
|
26
26
|
}
|
|
@@ -36,6 +36,9 @@ export const fetcher_axios = (params = {}) => {
|
|
|
36
36
|
return res.data;
|
|
37
37
|
},
|
|
38
38
|
post: async (url, data, config) => {
|
|
39
|
+
if (!data) {
|
|
40
|
+
throw new FaableApiError("empty body");
|
|
41
|
+
}
|
|
39
42
|
const res = await instance.request({
|
|
40
43
|
method: "POST",
|
|
41
44
|
url,
|
|
@@ -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
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faable/sdk-base",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"author": "Marc Pomar <marc@faable.com>",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"axios": "^1.7.7",
|
|
14
|
-
"jwt-decode": "^4.0.0",
|
|
15
14
|
"p-queue": "^8.0.1",
|
|
16
15
|
"ramda": "^0.30.1"
|
|
17
16
|
},
|
|
@@ -21,13 +20,15 @@
|
|
|
21
20
|
"@types/ramda": "^0.30.2",
|
|
22
21
|
"ava": "^6.1.3",
|
|
23
22
|
"dotenv": "^16.4.5",
|
|
23
|
+
"json-server": "^1.0.0-beta.3",
|
|
24
24
|
"nock": "^13.5.5",
|
|
25
25
|
"openapi-typescript": "^7.4.0",
|
|
26
26
|
"rimraf": "^6.0.1",
|
|
27
27
|
"semantic-release": "^24.1.1",
|
|
28
28
|
"tsimp": "^2.0.11",
|
|
29
29
|
"tsx": "^4.19.1",
|
|
30
|
-
"typescript": "^5.6.2"
|
|
30
|
+
"typescript": "^5.6.2",
|
|
31
|
+
"wait-on": "^8.0.1"
|
|
31
32
|
},
|
|
32
33
|
"scripts": {
|
|
33
34
|
"generate-types": "openapi-typescript https://api.faable.com/docs/json -o src/api/types.ts",
|