@faable/sdk-base 1.0.14 → 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/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/package.json +4 -3
|
@@ -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;CA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O,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,
|
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",
|