import { FullItem } from "../model/fullItem" ; import { Item as SimpleItem } from "../model/item" ; import { Vault } from "../model/vault" ; import { Items , Vaults } from "./resources" ; import { HTTPClient , IRequestClient } from "./client" ; import { RequestAdapter } from "./requests" ; export interface OPConfig { serverURL : string ; token : string ; httpClient ? : IRequestClient ; keepAlive ? : boolean ; timeout ? : number ; } param { OPConfig } opts returns { OPConnect } export const newConnectClient = ( opts ) : OPConnect => { if ( opts && typeof opts !== "object" ) { throw new TypeError ( "Options argument must be an object" ) ; } if ( ! opts . serverURL || ! opts . token ) { throw new Error ( "Options serverURL and token are required." ) ; } return new OPConnect ( opts ) ; } ; class OPConnect { private vaults : Vaults ; private items : Items ; private readonly httpAdapter : RequestAdapter ; public constructor ( opts ) { this . httpAdapter = new RequestAdapter ( opts . httpClient ? opts . httpClient : new HTTPClient ( opts ) , { serverURL : opts . serverURL , token : opts . token } ) ; this . vaults = new Vaults ( this . httpAdapter ) ; this . items = new Items ( this . httpAdapter ) ; } returns { Promise < Vault [ ] > } public async listVaults ( ) { return await this . vaults . list ( ) ; } param { string } vaultId returns { Promise < Vault > } public async getVault ( vaultId ) { return await this . vaults . getVault ( vaultId ) ; } param { string } vaultId returns { Promise < SimpleItem [ ] > } public async listItems ( vaultId ) { return await this . vaults . listItems ( vaultId ) ; } param { string } vaultId param { string } itemId returns { Promise < FullItem > } public async getItem ( vaultId , itemId ) { return await this . items . get ( vaultId , { itemId } ) ; } param { string } vaultId param { string } title returns { Promise < FullItem > } public async getItemByTitle ( vaultId , title ) { return await this . items . get ( vaultId , { title } ) ; } param { string } vaultId param { FullItem } item returns { Promise < FullItem > } public async createItem ( vaultId , item ) { return this . items . create ( vaultId , item ) ; } param { string } vaultId param { FullItem } item returns { Promise < FullItem > } public async updateItem ( vaultId , item ) { if ( ! item . id ) throw Error ( "Item ID must be defined." ) ; return await this . items . update ( vaultId , item ) ; } param { string } vaultId param { string } itemId returns { Promise < void > } public async deleteItem ( vaultId , itemId ) { await this . items . delete ( vaultId , itemId ) ; return ; } }