|
1 | | -import { Entry, NightscoutAPI, NightscoutConfig } from './interface'; |
| 1 | +import axios, { RawAxiosRequestHeaders } from "axios"; |
| 2 | +import { logger } from ".."; |
| 3 | +import { Entry, NightscoutAPI, NightscoutConfig } from "./interface"; |
| 4 | + |
| 5 | +interface NightscoutApiV3HttpHeaders extends RawAxiosRequestHeaders { |
| 6 | + Authorization: string; |
| 7 | +} |
2 | 8 |
|
3 | 9 | export class Client implements NightscoutAPI { |
4 | | - constructor(config: NightscoutConfig) { |
5 | | - throw new Error('Not implemented'); |
6 | | - } |
| 10 | + readonly baseUrl: string; |
| 11 | + readonly headers: NightscoutApiV3HttpHeaders; |
| 12 | + readonly device: string; |
| 13 | + readonly accessToken: string; |
| 14 | + readonly app: string; |
| 15 | + |
| 16 | + constructor(config: NightscoutConfig) { |
| 17 | + this.baseUrl = config.nightscoutBaseUrl; |
| 18 | + this.headers = { |
| 19 | + "User-Agent": "FreeStyle LibreLink Up NightScout Uploader", |
| 20 | + "Content-Type": "application/json", |
| 21 | + Authorization: "", |
| 22 | + }; |
| 23 | + this.device = config.nightscoutDevice; |
| 24 | + this.accessToken = config.nightscoutApiToken; |
| 25 | + this.app = config.nightscoutApp; |
| 26 | + } |
| 27 | + |
| 28 | + private addBearerJwtToken(jwtToken: string): string { |
| 29 | + if (!jwtToken) { |
| 30 | + throw Error("No jwtToken found"); |
| 31 | + } |
| 32 | + return `Bearer ${jwtToken}`; |
| 33 | + } |
| 34 | + |
| 35 | + async getJwtToken(): Promise<string> { |
| 36 | + let newToken = ""; |
| 37 | + try { |
| 38 | + const url = new URL( |
| 39 | + `/api/v2/authorization/request/${this.accessToken}`, |
| 40 | + this.baseUrl |
| 41 | + ).toString(); |
| 42 | + const resp = await axios.get(url, { |
| 43 | + headers: { |
| 44 | + ...this.headers, |
| 45 | + Accept: "application/json", |
| 46 | + }, |
| 47 | + }); |
| 48 | + if (resp.status !== 200 || !resp.data.token) { |
| 49 | + throw Error(`Error getting JWT token: ${resp.statusText} `); |
| 50 | + } |
| 51 | + newToken = await resp.data.token; |
| 52 | + } catch (error) { |
| 53 | + logger.error("Error getting JWT token:", error); |
| 54 | + } |
| 55 | + return newToken; |
| 56 | + } |
| 57 | + |
| 58 | + async lastEntry(): Promise<Entry | null> { |
| 59 | + const jwtToken = await this.getJwtToken(); |
| 60 | + const url = new URL( |
| 61 | + "/api/v3/entries?limit=1&sort$desc=date", |
| 62 | + this.baseUrl |
| 63 | + ).toString(); |
| 64 | + const resp = await axios.get(url, { |
| 65 | + headers: { |
| 66 | + ...this.headers, |
| 67 | + Authorization: this.addBearerJwtToken(jwtToken), |
| 68 | + } as NightscoutApiV3HttpHeaders, |
| 69 | + }); |
| 70 | + if (resp.status !== 200) { |
| 71 | + throw Error(`Failed to get last entry: ${resp.statusText}`); |
| 72 | + } |
| 73 | + if (!resp.data.result || resp.data.result.length === 0) { |
| 74 | + throw Error( |
| 75 | + `Last entry not found in response data: ${JSON.stringify(resp.data)}` |
| 76 | + ); |
| 77 | + } |
| 78 | + return resp.data.result.pop(); |
| 79 | + } |
| 80 | + |
| 81 | + async uploadEntries(entries: Entry[]): Promise<void> { |
| 82 | + const jwtToken = await this.getJwtToken(); |
| 83 | + const url = new URL("/api/v3/entries", this.baseUrl).toString(); |
| 84 | + |
| 85 | + if (!entries.length) { |
| 86 | + throw Error(`No entries to upload`); |
| 87 | + } |
| 88 | + |
| 89 | + const entryPayloads = entries.map((entry) => ({ |
| 90 | + type: "sgv", |
| 91 | + sgv: entry.sgv, |
| 92 | + direction: entry.direction?.toString(), |
| 93 | + device: this.device, |
| 94 | + date: entry.date.getTime(), |
| 95 | + app: this.app, |
| 96 | + })); |
7 | 97 |
|
8 | | - async lastEntry(): Promise<Entry | null> { |
9 | | - throw new Error('Not implemented'); |
10 | | - } |
| 98 | + // APIv3 accepts only single entries |
| 99 | + const responses = await Promise.all( |
| 100 | + entryPayloads.map((entryV3) => |
| 101 | + axios.post(url, entryV3, { |
| 102 | + headers: { |
| 103 | + ...this.headers, |
| 104 | + Authorization: this.addBearerJwtToken(jwtToken), |
| 105 | + } as NightscoutApiV3HttpHeaders, |
| 106 | + }) |
| 107 | + ) |
| 108 | + ); |
11 | 109 |
|
12 | | - async uploadEntries(entries: Entry[]): Promise<void> { |
13 | | - throw new Error('Not implemented'); |
14 | | - } |
| 110 | + responses.forEach((resp) => { |
| 111 | + if (resp.status !== 201) { |
| 112 | + throw Error( |
| 113 | + `failed to post new entries: ${resp.statusText} ${resp.status}` |
| 114 | + ); |
| 115 | + } |
| 116 | + }); |
| 117 | + return; |
| 118 | + } |
15 | 119 | } |
0 commit comments