@data-fair/lib-node 1.2.0 → 1.3.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,4 +1,5 @@
1
1
  import type { AxiosInstance } from 'axios';
2
+ import { CookieJar } from 'tough-cookie';
2
3
  export interface AxiosAuthOptions {
3
4
  email: string;
4
5
  password: string;
@@ -9,5 +10,9 @@ export interface AxiosAuthOptions {
9
10
  axiosOpts?: any;
10
11
  orgStorage?: boolean;
11
12
  }
12
- export declare function axiosAuth(opts: AxiosAuthOptions): Promise<AxiosInstance>;
13
+ export type AxiosAuthInstance = AxiosInstance & {
14
+ cookieJar: CookieJar;
15
+ setOrg: (org: string, dep?: string) => void;
16
+ };
17
+ export declare function axiosAuth(opts: AxiosAuthOptions): Promise<AxiosAuthInstance>;
13
18
  export default axiosAuth;
package/axios-auth.js CHANGED
@@ -2,6 +2,7 @@
2
2
  // used for integration testing
3
3
  import { Agent } from 'node:http'
4
4
  import { axiosBuilder, axiosInstance } from './axios.js'
5
+ import { CookieJar } from 'tough-cookie'
5
6
  export async function axiosAuth (opts) {
6
7
  const body = { email: opts.email, password: opts.password }
7
8
  if (opts.org) { body.org = opts.org }
@@ -10,6 +11,7 @@ export async function axiosAuth (opts) {
10
11
  if (opts.orgStorage) { body.orgStorage = opts.orgStorage }
11
12
  const axiosOpts = {
12
13
  httpAgent: new Agent({ keepAlive: false }),
14
+ maxRedirects: 0,
13
15
  ...opts.axiosOpts
14
16
  }
15
17
  const directoryUrl = opts.directoryUrl ?? 'http://localhost:8080'
@@ -17,14 +19,38 @@ export async function axiosAuth (opts) {
17
19
  if (callbackUrl.startsWith(directoryUrl + '/simple-directory')) {
18
20
  callbackUrl = callbackUrl.replace(directoryUrl + '/simple-directory', directoryUrl)
19
21
  }
22
+ const cookieJar = new CookieJar()
23
+ const origin = new URL(directoryUrl).origin
20
24
  try {
21
25
  await axiosInstance.get(callbackUrl, { maxRedirects: 0 })
22
26
  } catch (err) {
23
27
  if (err.status !== 302) { throw err }
24
- axiosOpts.headers = axiosOpts.headers ?? {}
25
- axiosOpts.headers.common = axiosOpts.headers.common ?? {}
26
- axiosOpts.headers.common.Cookie = err.headers['set-cookie'].map((s) => s.split(';')[0]).join(';')
28
+ /* axiosOpts.headers = axiosOpts.headers ?? {}
29
+ axiosOpts.headers.common = axiosOpts.headers.common ?? {}
30
+ axiosOpts.headers.common.Cookie = err.headers['set-cookie'].map((s: string) => s.split(';')[0]).join(';') */
31
+ for (const cookie of err.headers['set-cookie']) { cookieJar.setCookie(cookie, origin) }
27
32
  }
28
- return axiosBuilder(axiosOpts)
33
+ const ax = axiosBuilder(axiosOpts, (ax) => {
34
+ ax.interceptors.request.use(async (config) => {
35
+ config.headers.Cookie = await cookieJar.getCookies(origin)
36
+ return config
37
+ })
38
+ ax.interceptors.response.use(async (res) => {
39
+ for (const cookie of res.headers['set-cookie'] ?? []) { cookieJar.setCookie(cookie, origin) }
40
+ return res
41
+ }, async (error) => {
42
+ const res = error.response
43
+ if (res) {
44
+ for (const cookie of res.headers['set-cookie'] ?? []) { cookieJar.setCookie(cookie, origin) }
45
+ }
46
+ return Promise.reject(error)
47
+ })
48
+ })
49
+ ax.cookieJar = cookieJar
50
+ ax.setOrg = (org, dep) => {
51
+ cookieJar.setCookie(`id_token_org=${org}`, origin)
52
+ cookieJar.setCookie(`id_token_dep=${dep ?? ''}`, origin)
53
+ }
54
+ return ax
29
55
  }
30
56
  export default axiosAuth
package/axios.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AxiosInstance } from 'axios';
2
- export declare function axiosBuilder(opts?: object): AxiosInstance;
1
+ import type { AxiosInstance, CreateAxiosDefaults } from 'axios';
2
+ export declare function axiosBuilder(opts?: CreateAxiosDefaults, beforeInterceptors?: (ax: AxiosInstance) => void): AxiosInstance;
3
3
  export declare const axiosInstance: AxiosInstance;
4
4
  export default axiosInstance;
package/axios.js CHANGED
@@ -19,12 +19,13 @@ const requestInterceptor = (config) => {
19
19
  config.errorContext = errorContext
20
20
  return config
21
21
  }
22
- export function axiosBuilder (opts = {}) {
22
+ export function axiosBuilder (opts = {}, beforeInterceptors) {
23
23
  const ax = axios.create({
24
24
  httpAgent,
25
25
  httpsAgent,
26
26
  ...opts
27
27
  })
28
+ if (beforeInterceptors) { beforeInterceptors(ax) }
28
29
  ax.interceptors.request.use(requestInterceptor)
29
30
  // shorter stack traces
30
31
  ax.interceptors.response.use(response => response, error => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -26,7 +26,8 @@
26
26
  "peerDependencies": {
27
27
  "mongodb": "6",
28
28
  "prom-client": "15",
29
- "ws": "8"
29
+ "ws": "8",
30
+ "tough-cookie": "5"
30
31
  },
31
32
  "peerDependenciesMeta": {
32
33
  "mongodb": {
@@ -37,12 +38,16 @@
37
38
  },
38
39
  "ws": {
39
40
  "optional": true
41
+ },
42
+ "tough-cookie": {
43
+ "optional": true
40
44
  }
41
45
  },
42
46
  "type": "module",
43
47
  "devDependencies": {
44
48
  "mongodb": "^6.9.0",
45
49
  "prom-client": "^15.1.3",
46
- "ws": "^8.18.0"
50
+ "ws": "^8.18.0",
51
+ "tough-cookie": "^5.0.0"
47
52
  }
48
53
  }