import { FullItem } from "../model/fullItem" ; import { Item as SimpleItem , ObjectSerializer } from "../model/models" ; import { Vault } from "../model/vault" ; import { RequestAdapter , Response } from "./requests" ; class OPResource { protected adapter : RequestAdapter ; public constructor ( adapter ) { this . adapter = adapter ; } } export class Vaults extends OPResource { private basePath = "v1/vaults" ; /**
     * Return all vaults the Service Account has permission to view.
     */ public async list ( ) { const { data } = await this . adapter . sendRequest ( "get" , `${ this . basePath }/` ) ; return ObjectSerializer . deserialize ( data , "Array<Vault>" ) ; } param vaultId public async listItems ( vaultId ) { const { data } = await this . adapter . sendRequest ( "get" , `${ this . basePath }/${ vaultId }/items` ) ; return ObjectSerializer . deserialize ( data , "Array<Item>" ) ; } param vaultId public async getVault ( vaultId ) { const { data } = await this . adapter . sendRequest ( "get" , `${ this . basePath }/${ vaultId }` ) ; return ObjectSerializer . deserialize ( data , "Vault" ) ; } } export class Items extends OPResource { private basePath = ( vaultId , itemId ? ) : string => itemId && typeof itemId !== "undefined" ? `v1/vaults/${ vaultId }/items/${ itemId }` : `v1/vaults/${ vaultId }/items/` ; public async create ( vaultId , item ) { item . vault = Object . assign ( item . vault || { } , { id : vaultId } ) ; const { data } = await this . adapter . sendRequest ( "post" , this . basePath ( vaultId ) , { data : ObjectSerializer . serialize ( item , "FullItem" ) , } ) ; return ObjectSerializer . deserialize ( data , "FullItem" ) ; } public async update ( vaultId , item ) { const { data } = await this . adapter . sendRequest ( "put" , this . basePath ( vaultId , item . id ) , { data : ObjectSerializer . serialize ( item , "FullItem" ) } ) ; return ObjectSerializer . deserialize ( data , "FullItem" ) ; } public async get ( vaultId , opts ) { if ( ! ( opts . itemId || opts . title ) || ( opts . itemId && opts . title ) ) { throw TypeError ( "Items.get() requires itemId or title" ) ; } const { data } = opts . itemId ? await this . getById ( vaultId , opts . itemId ) : await this . getByTitle ( vaultId , opts . title ) ; return ObjectSerializer . deserialize ( data , "FullItem" ) ; } public async delete ( vaultId , itemId ) { return await this . adapter . sendRequest ( "delete" , this . basePath ( vaultId , itemId ) ) ; } private async getById ( vaultId , itemId ) { return await this . adapter . sendRequest ( "get" , this . basePath ( vaultId , itemId ) ) ; } param { string } vaultId param { string } title returns { Promise < FullItem > } private private async getByTitle ( vaultId , title ) { const queryPath = `${ this . basePath ( vaultId ) }?filter=title eq "${ title }"` ; const { data } = await this . adapter . sendRequest ( "get" , queryPath ) ; if ( ! data ?. length ) { return Promise . reject ( { status : 404 , message : "No Items found with title" , } ) ; } if ( data . length > 1 ) { return Promise . reject ( { status : 400 , message : "Found multiple Items with given title. Provide a more specific Item title" , } ) ; } return this . getById ( data [ 0 ] . vault . id , data [ 0 ] . id ) ; } } type GetItemOptions = { itemId : string ; title ? : never ; } | { title : string ; itemId ? : never ; } ;