@data-fair/lib-node 2.11.0 → 2.12.0

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/axios-auth.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { AxiosInstance } from 'axios';
2
- import { CookieJar } from 'tough-cookie';
1
+ import { type AxiosWithCookiesInstance } from './axios-with-cookies.js';
3
2
  export interface AxiosAuthOptions {
4
3
  email: string;
5
4
  password: string;
@@ -11,8 +10,7 @@ export interface AxiosAuthOptions {
11
10
  orgStorage?: boolean;
12
11
  globalCookies?: boolean;
13
12
  }
14
- export type AxiosAuthInstance = AxiosInstance & {
15
- cookieJar: CookieJar;
13
+ export type AxiosAuthInstance = AxiosWithCookiesInstance & {
16
14
  setOrg: (org: string, dep?: string) => void;
17
15
  };
18
16
  export declare function axiosAuth(opts: AxiosAuthOptions): Promise<AxiosAuthInstance>;
package/axios-auth.js CHANGED
@@ -1,13 +1,9 @@
1
1
  // build axios instances with sessions on a simple-directory instance
2
2
  // WARNING: use for integration testing only
3
3
  import { Agent } from 'node:http';
4
- import { axiosBuilder, axiosInstance } from './axios.js';
5
4
  import { CookieJar } from 'tough-cookie';
6
- const getConfigUrl = (config) => {
7
- if (!config.url)
8
- throw new Error('url is required');
9
- return new URL((config.baseURL && config.url?.startsWith('/')) ? (config.baseURL + config.url) : config.url);
10
- };
5
+ import { axiosInstance } from './axios.js';
6
+ import { axiosWithCookies } from './axios-with-cookies.js';
11
7
  export async function axiosAuth(opts) {
12
8
  const body = { email: opts.email, password: opts.password };
13
9
  if (opts.org)
@@ -28,8 +24,8 @@ export async function axiosAuth(opts) {
28
24
  if (callbackUrl.startsWith(directoryUrl + '/simple-directory')) {
29
25
  callbackUrl = callbackUrl.replace(directoryUrl + '/simple-directory', directoryUrl);
30
26
  }
31
- const cookieJar = new CookieJar();
32
27
  const sdOrigin = new URL(directoryUrl).origin;
28
+ const cookieJar = new CookieJar();
33
29
  try {
34
30
  await axiosInstance.get(callbackUrl, { maxRedirects: 0 });
35
31
  }
@@ -43,38 +39,15 @@ export async function axiosAuth(opts) {
43
39
  for (const cookie of err.headers['set-cookie'] ?? [])
44
40
  cookieJar.setCookieSync(cookie, sdOrigin);
45
41
  }
46
- const ax = axiosBuilder(axiosOpts, (ax) => {
47
- ax.interceptors.request.use((config) => {
48
- const url = getConfigUrl(config);
49
- // if opts.globalCookies is true the cookie jar is used to store cookies shared accross all domains
50
- // this behavior is useful for testing purposes if serves are not on the same domain
51
- if (!opts.globalCookies || url.origin === sdOrigin) {
52
- config.headers.Cookie = cookieJar.getCookiesSync(url.href);
53
- }
54
- else {
55
- config.headers.Cookie = cookieJar.getCookiesSync(sdOrigin);
56
- }
57
- return config;
58
- });
59
- ax.interceptors.response.use((res) => {
60
- const origin = opts.globalCookies ? sdOrigin : getConfigUrl(res.config).origin;
61
- for (const cookie of res.headers['set-cookie'] ?? [])
62
- cookieJar.setCookieSync(cookie, origin);
63
- return res;
64
- }, async (error) => {
65
- const res = error.response;
66
- if (res) {
67
- const origin = opts.globalCookies ? sdOrigin : getConfigUrl(res.config).origin;
68
- for (const cookie of res.headers['set-cookie'] ?? [])
69
- cookieJar.setCookieSync(cookie, origin);
70
- }
71
- return Promise.reject(error);
72
- });
42
+ const ax = axiosWithCookies({
43
+ axiosOpts,
44
+ cookiesOrigin: sdOrigin,
45
+ globalCookies: opts.globalCookies,
46
+ cookieJar
73
47
  });
74
- ax.cookieJar = cookieJar;
75
48
  ax.setOrg = (org, dep) => {
76
- cookieJar.setCookieSync(`id_token_org=${org}`, sdOrigin);
77
- cookieJar.setCookieSync(`id_token_dep=${dep ?? ''}`, sdOrigin);
49
+ ax.cookieJar.setCookieSync(`id_token_org=${org}`, sdOrigin);
50
+ ax.cookieJar.setCookieSync(`id_token_dep=${dep ?? ''}`, sdOrigin);
78
51
  };
79
52
  return ax;
80
53
  }
@@ -0,0 +1,13 @@
1
+ import type { AxiosInstance } from 'axios';
2
+ import { CookieJar } from 'tough-cookie';
3
+ export interface AxiosWithCookiesOptions {
4
+ axiosOpts?: any;
5
+ cookiesOrigin?: string;
6
+ globalCookies?: boolean;
7
+ cookieJar?: CookieJar;
8
+ }
9
+ export type AxiosWithCookiesInstance = AxiosInstance & {
10
+ cookieJar: CookieJar;
11
+ };
12
+ export declare function axiosWithCookies(opts?: AxiosWithCookiesOptions): AxiosWithCookiesInstance;
13
+ export default axiosWithCookies;
@@ -0,0 +1,41 @@
1
+ // build axios instances with cookie jar support
2
+ import { axiosBuilder } from './axios.js';
3
+ import { CookieJar } from 'tough-cookie';
4
+ const getConfigUrl = (config) => {
5
+ if (!config.url)
6
+ throw new Error('url is required');
7
+ return new URL((config.baseURL && config.url?.startsWith('/')) ? (config.baseURL + config.url) : config.url);
8
+ };
9
+ export function axiosWithCookies(opts = {}) {
10
+ const cookieJar = opts.cookieJar ?? new CookieJar();
11
+ const cookiesOrigin = opts.cookiesOrigin ?? '';
12
+ const ax = axiosBuilder(opts.axiosOpts, (ax) => {
13
+ ax.interceptors.request.use((config) => {
14
+ const url = getConfigUrl(config);
15
+ if (!opts.globalCookies || url.origin === cookiesOrigin) {
16
+ config.headers.Cookie = cookieJar.getCookiesSync(url.href);
17
+ }
18
+ else {
19
+ config.headers.Cookie = cookieJar.getCookiesSync(cookiesOrigin);
20
+ }
21
+ return config;
22
+ });
23
+ ax.interceptors.response.use((res) => {
24
+ const origin = opts.globalCookies ? cookiesOrigin : getConfigUrl(res.config).origin;
25
+ for (const cookie of res.headers['set-cookie'] ?? [])
26
+ cookieJar.setCookieSync(cookie, origin);
27
+ return res;
28
+ }, async (error) => {
29
+ const res = error.response;
30
+ if (res) {
31
+ const origin = opts.globalCookies ? cookiesOrigin : getConfigUrl(res.config).origin;
32
+ for (const cookie of res.headers['set-cookie'] ?? [])
33
+ cookieJar.setCookieSync(cookie, origin);
34
+ }
35
+ return Promise.reject(error);
36
+ });
37
+ });
38
+ ax.cookieJar = cookieJar;
39
+ return ax;
40
+ }
41
+ export default axiosWithCookies;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.11.0",
3
+ "version": "2.12.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [