chuvsu-js 2.8.1 → 2.8.2

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.
@@ -78,6 +78,7 @@ export declare class TtClient {
78
78
  audienceId: number;
79
79
  period: Period;
80
80
  }): Promise<Schedule>;
81
+ private getCachedAudienceImage;
81
82
  /** Get the audience photo (audimage). Returns null if missing. */
82
83
  getAudienceImage(audienceId: number): Promise<Buffer | null>;
83
84
  /** Get the building exterior image (blockimage). Returns null if missing. */
package/dist/tt/client.js CHANGED
@@ -259,29 +259,38 @@ export class TtClient {
259
259
  schedules.set(opts.period, days);
260
260
  return new Schedule(opts.audienceId, schedules, opts.period, this.educationType);
261
261
  }
262
+ async getCachedAudienceImage(cacheKey, fetchUrl) {
263
+ const cached = this.cache?.get("audienceImages", cacheKey);
264
+ if (cached !== null && cached !== undefined) {
265
+ const entry = cached;
266
+ return entry.data ? Buffer.from(entry.data, "base64") : null;
267
+ }
268
+ const url = await fetchUrl();
269
+ if (!url) {
270
+ this.cache?.set("audienceImages", cacheKey, { data: null });
271
+ return null;
272
+ }
273
+ const buf = await this.authGetBuffer(`${BASE}${url}`);
274
+ if (buf.length === 0) {
275
+ this.cache?.set("audienceImages", cacheKey, { data: null });
276
+ return null;
277
+ }
278
+ this.cache?.set("audienceImages", cacheKey, {
279
+ data: buf.toString("base64"),
280
+ });
281
+ return buf;
282
+ }
262
283
  /** Get the audience photo (audimage). Returns null if missing. */
263
284
  async getAudienceImage(audienceId) {
264
- const info = await this.getAudienceInfo(audienceId);
265
- if (!info?.audImageUrl)
266
- return null;
267
- const buf = await this.authGetBuffer(`${BASE}${info.audImageUrl}`);
268
- return buf.length > 0 ? buf : null;
285
+ return this.getCachedAudienceImage(`aud:${audienceId}`, async () => (await this.getAudienceInfo(audienceId))?.audImageUrl);
269
286
  }
270
287
  /** Get the building exterior image (blockimage). Returns null if missing. */
271
288
  async getAudienceBlockImage(audienceId) {
272
- const info = await this.getAudienceInfo(audienceId);
273
- if (!info?.blockImageUrl)
274
- return null;
275
- const buf = await this.authGetBuffer(`${BASE}${info.blockImageUrl}`);
276
- return buf.length > 0 ? buf : null;
289
+ return this.getCachedAudienceImage(`block:${audienceId}`, async () => (await this.getAudienceInfo(audienceId))?.blockImageUrl);
277
290
  }
278
291
  /** Get the floor plan image for the audience. Returns null if missing. */
279
292
  async getAudienceFloorplan(audienceId) {
280
- const info = await this.getAudienceInfo(audienceId);
281
- if (!info?.floorplanUrl)
282
- return null;
283
- const buf = await this.authGetBuffer(`${BASE}${info.floorplanUrl}`);
284
- return buf.length > 0 ? buf : null;
293
+ return this.getCachedAudienceImage(`floor:${audienceId}`, async () => (await this.getAudienceInfo(audienceId))?.floorplanUrl);
285
294
  }
286
295
  async searchTeacher(opts) {
287
296
  const { body } = await this.authPost(`${BASE}/`, {
@@ -147,6 +147,7 @@ export interface CacheConfig {
147
147
  teacherInfo?: number;
148
148
  teacherPhotos?: number;
149
149
  audienceInfo?: number;
150
+ audienceImages?: number;
150
151
  }
151
152
  export interface TtClientOptions {
152
153
  educationType?: EducationType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chuvsu-js",
3
- "version": "2.8.1",
3
+ "version": "2.8.2",
4
4
  "description": "Node.js library for ChuvSU student portal (lk.chuvsu.ru) and schedule (tt.chuvsu.ru)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",