UNPKG

feedbaby-client

Version:
80 lines (79 loc) 3.13 kB
/// <reference types="node" /> import { AxiosInstance } from "axios"; import { Feed } from "../models/Feed"; import { AppData } from "./AppData"; import { Growth } from "../models/Growth"; import { Medicine } from "../models/Medicine"; export declare type HttpClientFactory = (baseUrl: string) => AxiosInstance; export declare class Device { readonly id: string; static create(): Device; constructor(id: string); } export interface Authentication { /** * Alphanumeric string used by all devices sharing the data */ readonly passphrase: string; /** * Date of birth used by all devices sharing the data */ readonly dateOfBirth: Date; } export interface Version { readonly version: string; readonly dateOfLastSync: Date; } export interface AppDataZip { getFeeds: () => Promise<Feed[]>; getGrowths: () => Promise<Growth[]>; getMedicines: () => Promise<Medicine[]>; getBuffer: () => Buffer; } export interface AppDataZipReader { read(zip: Buffer): AppDataZip; } export interface AppDataZipCreator { create(appData: AppData): Buffer; } export declare class FeedBabyClient { readonly host: string; readonly httpClientFactory: HttpClientFactory; private readonly appDataCreator; private readonly appDataReader; private static readonly HOST; private static readonly BASE_PATH; private static readonly PING_ENDPOINT; private static readonly CHECK_VERSION_ENDPOINT; private static readonly MERGE_ENDPOINT; private static readonly REGISTER_DEVICE_TOKEN_ENDPOINT; private client; private readonly baseParameters; private readonly syncParameters; private readonly authParameters; private readonly deviceParameters; constructor(host?: string, httpClientFactory?: HttpClientFactory, appDataCreator?: AppDataZipCreator, appDataReader?: AppDataZipReader); ping(): Promise<boolean>; /** * Determines the latest 'version' synchronisation, which is just a unix timestamp of the last sync. * * Contrary to what I thought this is not the same date returned in the Date header of the merge. */ checkVersion(authentication: Authentication): Promise<Version>; registerDevice(auth: Authentication, device: Device, deviceToken: string): Promise<string>; /** * WARNING: This method is destructive! * * The first time a new device syncs with the server no data will be erased from the server, only added. What is * returned is all the data that the server has of the passphrase + date combination. * * Subsequent syncs for a device the server knows could result in data being erased from the account. i.e. if the * ZIP from the device doesn't contain data that the server knows it previously sent to the device, then the server * will assume the user of the device deleted this data, thus update the data on the server accordingly. * * @return Returns the merged sync */ merge(auth: Authentication, device: Device, zip?: Buffer): Promise<AppDataZip>; private mergeZip; private createZipFromAppFirstStart; }