UNPKG

rentdynamics

Version:

Package to help facilitate communicating with the Rent Dynamics API

168 lines 7.07 kB
/** Payload is known as the "body" of a Request. The payload must be JSON serializable. */ type Payload = any; /** * Client is a convenience class for interacting with the Rent Dynamics services. * * Note if this class does not suit your needs, it may be wise to roll your own implementation using * the {@linkcode ClientHelpers} class. */ export declare class Client { /** * authToken if defined, authToken is used to make authenticated requests. Note authToken is not * typically get/set manually, but instead is managed through the login/logout methods. */ authToken: string | undefined; private readonly helpers; constructor(options: ClientOptions); /** * get wraps a request library to work with the Rent Dynamics API. * @param endpoint the path following the baseUrl. * @example get('/foo'); */ get(endpoint: string): Promise<Response>; /** * put wraps a request library to work with the Rent Dynamics API. * @param endpoint the path following the baseUrl. * @param payload a JSON serializable object. * @example put('/foo', { bar: 1 }); */ put(endpoint: string, payload: Payload): Promise<Response>; /** * post wraps a request library to work with the Rent Dynamics API. * @param endpoint the path following the baseUrl. * @param payload a JSON serializable object. * @example post('/foo', { bar: 1 }); */ post(endpoint: string, payload: Payload): Promise<Response>; /** * delete wraps a request library to work with the Rent Dynamics API. * @param endpoint the path following the baseUrl. * @example delete('/foo/1'); */ delete(endpoint: string): Promise<Response>; /** * login enables an instance of {@linkcode Client} to make authenticated requests to the Rent * Dynamics API. */ login(username: string, password: string): Promise<Response>; /** logout invalidates the users session generated by {@linkcode login}. */ logout(): Promise<Response>; } /** BASE_URL is a collection of base urls for each dev/prod Rent Dynamics service. */ export declare enum BASE_URL { DEV_RD = "https://api.rentdynamics.dev", PROD_RD = "https://api.rentdynamics.com", DEV_RP = "https://api.rentplus.dev", PROD_RP = "https://api.rentplus.com" } export type RdEncoder = Pick<TextEncoder, 'encode'>; export type RdCryptographer = Pick<SubtleCrypto, 'importKey'> & Pick<SubtleCrypto, 'sign'> & Pick<SubtleCrypto, 'digest'>; /** ClientOptions is consumed and updated by {@linkcode ClientHelpers}. */ export declare class ClientOptions { /** apiKey if defined apiKey is used to calculate auth headers. */ apiKey: string | undefined; /** apiSecretKey if defined apiSecretKey is used to calculate auth headers. */ apiSecretKey: string | undefined; /** * baseUrl is the base request url. The default is the development rentdynamics api. A custom * string may be provided beyond the {@linkcode BASE_URL} options. */ baseUrl: BASE_URL | string; /** * getEncoder is used to encode text. The encoder can be overridden as needed. For example in a * node environment. * @example * const options = new ClientOptions(); * options.getEncoder = async () => new (await import('util')).TextEncoder(); */ getEncoder: () => Promise<RdEncoder>; /** * getCryptographer is used for cryptography. The cryptographer can be overridden as needed. For * example in a node environment. * @example * const options = new ClientOptions(); * options.getCryptographer = async () => (await import('crypto')).subtle; */ getCryptographer: () => Promise<RdCryptographer>; } /** * ClientHelpers is a collection of utilities consumed by {@linkcode Client}. ClientHelpers can be * used to calculate headers in case a consumer wants to build their own API client. */ export declare class ClientHelpers { private options; constructor(options: ClientOptions); /** * baseUrl is the base url used throughout the {@linkcode ClientHelpers} instance. It is initially * configured through {@linkcode ClientOptions}. */ get baseUrl(): string; /** * getTimestamp is used to calculate the timestamp header. This method is not likely to be called * on it's own. Instead, it is typically used to mock the current time. */ getTimestamp(): number; /** * getHeaders creates headers for the given params. If an auth token is included, this method will * generate an `Authorization` header. */ getHeaders(endpoint: string, payload?: Payload | undefined, authToken?: string | undefined): Promise<Record<string, string>>; /** formatPayload formats the payload for nonce calculation. */ formatPayload(payload: Payload): Payload; /** getNonce calculates the nonce for the given params. */ getNonce(timestamp: number, url: string, payloadStr?: string): Promise<string>; /** encryptPassword encrypts the password for login. */ encryptPassword(password: string): Promise<string>; } /** * ProofOfPossession encapsulates generic low level logic related to generating proof of possession * tokens and storing their associated key pair. */ export declare class ProofOfPossession { private readonly _encoder; private readonly _keyStoreName; private readonly _keyKey; private readonly _dbName; private readonly _dbVersion; private readonly _algorithm; private readonly indexedDB; /** * constructor creates an instance capable of generating proof of possession tokens. * * @param indexedDB window.indexedDB satisfies this interface. If you're not in a browser * environment, proof of possession tokens are likely not necessary. */ constructor(indexedDB: IDBFactory); /** * generateProof generates a proof of possession JWT with the key pair. The key pair comes from a * get or create method. Where the key pair will continuously be used until evicted with * evictKeyPair. */ generateProof(): Promise<string>; /** * getPublicKey gets or creates a key pair and stores it. The public key component is then * extracted and returned in a base64 url encoded form. */ getPublicKey(): Promise<string>; /** * evictKeyPair evicts the currently stored key pair. No operation is performed if the key pair * does not exist. */ evictKeyPair(): Promise<unknown>; private _getHeader; private _getPayload; private _getOrCreateKey; private _onDatabaseOpened; private _onKeyLookup; private _upgradeDb; private _createKey; } /** encodeSegment base64url encodes a string or JWT segment */ export declare const encodeSegment: (segment: string) => string; /** decodePayload decodes the payload segment of a JWT to it's object representation */ export declare const decodePayload: (token: string) => { exp: number; iat: number; }; export {}; //# sourceMappingURL=index.d.ts.map