naolib_trips 1.5.0 → 1.6.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.
@@ -0,0 +1,2 @@
1
+ export declare function buildHeaders(): Promise<Record<string, string>>;
2
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAgCA,wBAAsB,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAKpE"}
package/dist/client.js ADDED
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.buildHeaders = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const BASE_ORIGIN = "https://plan.naolib.fr";
18
+ let sessionCookie = null;
19
+ let pending = null;
20
+ function fetchSessionCookie() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ try {
23
+ const response = yield axios_1.default.get(BASE_ORIGIN + "/", {
24
+ validateStatus: () => true,
25
+ });
26
+ const raw = response.headers["set-cookie"];
27
+ if (!raw)
28
+ return null;
29
+ const cookies = Array.isArray(raw) ? raw : [raw];
30
+ for (const cookie of cookies) {
31
+ const match = cookie.match(/SERVERID=[^;]+/);
32
+ if (match)
33
+ return match[0];
34
+ }
35
+ return null;
36
+ }
37
+ catch (_a) {
38
+ return null;
39
+ }
40
+ });
41
+ }
42
+ function getSessionCookie() {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ if (sessionCookie !== null)
45
+ return sessionCookie;
46
+ if (!pending)
47
+ pending = fetchSessionCookie();
48
+ sessionCookie = yield pending;
49
+ pending = null;
50
+ return sessionCookie;
51
+ });
52
+ }
53
+ function buildHeaders() {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const cookie = yield getSessionCookie();
56
+ const headers = { Referer: BASE_ORIGIN + "/" };
57
+ if (cookie)
58
+ headers.Cookie = cookie;
59
+ return headers;
60
+ });
61
+ }
62
+ exports.buildHeaders = buildHeaders;
@@ -1 +1 @@
1
- {"version":3,"file":"infotrafic.d.ts","sourceRoot":"","sources":["../../src/infotrafic/infotrafic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAIrD,wBAAsB,aAAa,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAKjE"}
1
+ {"version":3,"file":"infotrafic.d.ts","sourceRoot":"","sources":["../../src/infotrafic/infotrafic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAKrD,wBAAsB,aAAa,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAKjE"}
@@ -14,11 +14,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getInfotrafic = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
+ const client_js_1 = require("../client.js");
17
18
  const BASE_URL = "https://plan.naolib.fr/api/poi/infotrafic";
18
19
  function getInfotrafic() {
19
20
  return __awaiter(this, void 0, void 0, function* () {
20
21
  const response = yield axios_1.default.get(BASE_URL, {
21
- headers: { Referer: "https://plan.naolib.fr/", "X-Requested-With": "XMLHttpRequest" },
22
+ headers: yield (0, client_js_1.buildHeaders)(),
22
23
  });
23
24
  return response.data;
24
25
  });
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/search/search.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAI/D,wBAAsB,MAAM,CAC1B,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,cAAc,CAAC,CAMzB"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/search/search.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAK/D,wBAAsB,MAAM,CAC1B,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,cAAc,CAAC,CAMzB"}
@@ -14,12 +14,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.search = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
+ const client_js_1 = require("../client.js");
17
18
  const BASE_URL = "https://plan.naolib.fr/api/autocomplete/itinerary";
18
19
  function search(params) {
19
20
  return __awaiter(this, void 0, void 0, function* () {
20
21
  const response = yield axios_1.default.get(BASE_URL, {
21
22
  params,
22
- headers: { Referer: "https://plan.naolib.fr/", "X-Requested-With": "XMLHttpRequest" },
23
+ headers: yield (0, client_js_1.buildHeaders)(),
23
24
  });
24
25
  return response.data;
25
26
  });
@@ -1 +1 @@
1
- {"version":3,"file":"trip.d.ts","sourceRoot":"","sources":["../../src/trip/trip.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAI3D,wBAAsB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,CAM1E"}
1
+ {"version":3,"file":"trip.d.ts","sourceRoot":"","sources":["../../src/trip/trip.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAK3D,wBAAsB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,CAM1E"}
package/dist/trip/trip.js CHANGED
@@ -14,12 +14,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.searchTrip = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
+ const client_js_1 = require("../client.js");
17
18
  const BASE_URL = "https://plan.naolib.fr/api/itinerary/search";
18
19
  function searchTrip(params) {
19
20
  return __awaiter(this, void 0, void 0, function* () {
20
21
  const response = yield axios_1.default.get(BASE_URL, {
21
22
  params,
22
- headers: { Referer: "https://plan.naolib.fr/", "X-Requested-With": "XMLHttpRequest" },
23
+ headers: yield (0, client_js_1.buildHeaders)(),
23
24
  });
24
25
  return response.data;
25
26
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naolib_trips",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "A npm package for Naolib (Nantes) public transportation route finder",
5
5
  "license": "ISC",
6
6
  "author": "",
package/src/client.ts ADDED
@@ -0,0 +1,38 @@
1
+ import axios from "axios";
2
+
3
+ const BASE_ORIGIN = "https://plan.naolib.fr";
4
+ let sessionCookie: string | null = null;
5
+ let pending: Promise<string | null> | null = null;
6
+
7
+ async function fetchSessionCookie(): Promise<string | null> {
8
+ try {
9
+ const response = await axios.get(BASE_ORIGIN + "/", {
10
+ validateStatus: () => true,
11
+ });
12
+ const raw: string | string[] | undefined = response.headers["set-cookie"];
13
+ if (!raw) return null;
14
+ const cookies = Array.isArray(raw) ? raw : [raw];
15
+ for (const cookie of cookies) {
16
+ const match = cookie.match(/SERVERID=[^;]+/);
17
+ if (match) return match[0];
18
+ }
19
+ return null;
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+
25
+ async function getSessionCookie(): Promise<string | null> {
26
+ if (sessionCookie !== null) return sessionCookie;
27
+ if (!pending) pending = fetchSessionCookie();
28
+ sessionCookie = await pending;
29
+ pending = null;
30
+ return sessionCookie;
31
+ }
32
+
33
+ export async function buildHeaders(): Promise<Record<string, string>> {
34
+ const cookie = await getSessionCookie();
35
+ const headers: Record<string, string> = { Referer: BASE_ORIGIN + "/" };
36
+ if (cookie) headers.Cookie = cookie;
37
+ return headers;
38
+ }
@@ -1,11 +1,12 @@
1
1
  import axios from "axios";
2
2
  import type { InfotraficResponse } from "./types.js";
3
+ import { buildHeaders } from "../client.js";
3
4
 
4
5
  const BASE_URL = "https://plan.naolib.fr/api/poi/infotrafic";
5
6
 
6
7
  export async function getInfotrafic(): Promise<InfotraficResponse> {
7
8
  const response = await axios.get<InfotraficResponse>(BASE_URL, {
8
- headers: { Referer: "https://plan.naolib.fr/", "X-Requested-With": "XMLHttpRequest" },
9
+ headers: await buildHeaders(),
9
10
  });
10
11
  return response.data;
11
12
  }
@@ -1,5 +1,6 @@
1
1
  import axios from "axios";
2
2
  import type { SearchParams, SearchResponse } from "./types.js";
3
+ import { buildHeaders } from "../client.js";
3
4
 
4
5
  const BASE_URL = "https://plan.naolib.fr/api/autocomplete/itinerary";
5
6
 
@@ -8,7 +9,7 @@ export async function search(
8
9
  ): Promise<SearchResponse> {
9
10
  const response = await axios.get<SearchResponse>(BASE_URL, {
10
11
  params,
11
- headers: { Referer: "https://plan.naolib.fr/", "X-Requested-With": "XMLHttpRequest" },
12
+ headers: await buildHeaders(),
12
13
  });
13
14
  return response.data;
14
15
  }
package/src/trip/trip.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import axios from "axios";
2
2
  import type { TripParams, TripResponse } from "./types.js";
3
+ import { buildHeaders } from "../client.js";
3
4
 
4
5
  const BASE_URL = "https://plan.naolib.fr/api/itinerary/search";
5
6
 
6
7
  export async function searchTrip(params: TripParams): Promise<TripResponse> {
7
8
  const response = await axios.get<TripResponse>(BASE_URL, {
8
9
  params,
9
- headers: { Referer: "https://plan.naolib.fr/", "X-Requested-With": "XMLHttpRequest" },
10
+ headers: await buildHeaders(),
10
11
  });
11
12
  return response.data;
12
13
  }