Rename Api to PasswordsClient to match package name

Signed-off-by: Marius David Wieschollek's avatarMarius David Wieschollek <passwords.public@mdns.eu>
parent b73a1bdb
......@@ -4,24 +4,11 @@ Cou can find the API documentation [here](https://git.mdns.eu/nextcloud/password
### Using the client
You can use the enhanced version of the client in your project like this:
```javascript
import EnhancedApi from 'passwords-client';
import PasswordsClient from 'passwords-client';
let api = new EnhancedApi();
api.initialize({baseUrl:'https://cloud.example.com', user:'user', password:'password'});
```
#### Using the simple api
There is a "slim" version of the api.
This version will just communicate with the api but does no processing or encryption of the objects.
```javascript
import EventEmitter from 'eventemitter3';
import {SimpleApi} from 'passwords-client';
let events = new EventEmitter(),
api = new SimpleApi();
api.initialize({apiUrl:'https://cloud.example.com/index.php/apps/passwords/', user:'user', password:'password', events});
let client = new PasswordsClient({baseUrl:'https://cloud.example.com/', user:'user', token:'12345-12345-12345-12345-12345'});
let passwordsRepository = client.getPasswordRepository();
let passwordCollection = await passwordsRepository.findAll();
```
......
import SessionAuthorization from '../Authorization/SessionAuthorization';
import Server from '../Model/Server/Server';
import BaseClassLoader from "../ClassLoader/BaseClassLoader";
import DefaultClassLoader from "../ClassLoader/DefaultClassLoader";
import BaseApi from "./BaseApi";
import PasswordsClient from "../Client/PasswordsClient";
export default class Api extends BaseApi {
/**
*
* @param {Server} server
* @param {Object} [config={}]
* @param {Object} [classes={}]
*/
constructor(server, config = {}, classes = {}) {
if(!(classes instanceof BaseClassLoader)) {
classes = new DefaultClassLoader(classes);
}
super(server, config, classes);
}
/**
*
* @returns {SessionAuthorization}
* @deprecated
*/
getSessionAuthorisation() {
console.trace('API.getSessionAuthorisation() is deprecated. Use API.getSessionAuthorization() instead');
return this.getSessionAuthorization();
}
/**
* @deprecated
*/
export default class Api extends PasswordsClient {
}
\ No newline at end of file
import ConfigurationError from "../Exception/ConfigruationError";
import BasicPasswordsClient from "../Client/BasicPasswordsClient";
export default class BaseApi {
/**
*
* @param server
* @param config
* @param classes
*/
constructor(server, config, classes) {
this._setConfig(config);
this._classes = classes;
this._classes.setInstance('model.server', server);
this._classes.setInstance('api', this);
this._server = server;
this._session = this._classes.getInstance('model.session', server.getUser(), server.getToken());
this._events = this._classes.getInstance('event.event');
}
/**
*
* @param {String} event
* @param {Function} listener
*/
on(event, listener) {
this._events.on(event, listener);
}
/**
*
* @param {String} event
* @param {Function} listener
*/
once(event, listener) {
this._events.once(event, listener);
}
/**
*
* @param {String} event
* @param {Function} listener
*/
off(event, listener) {
this._events.off(event, listener);
}
/**
*
* @param {String} event
* @param {Object} data
*/
emit(event, data) {
this._events.emit(event, data);
}
/**
*
* @return {Server}
*/
getServer() {
return this._server;
}
/**
*
* @return {ApiRequest}
*/
getRequest() {
/** @type {ApiRequest} **/
let request = this.getClass('network.request', this, this._server.getApiUrl(), this.getSession());
if(this._config.userAgent !== null) {
request.setUserAgent(this._config.userAgent);
}
return request;
}
/**
* @returns {Session}
*/
getSession() {
return this._session
.setUser(this._server.getUser())
.setToken(this._server.getToken());
}
/**
*
* @returns {SessionAuthorization}
*/
getSessionAuthorization() {
return this.getInstance('authorization.session');
}
/**
*
* @returns {PasswordRepository}
*/
getPasswordRepository() {
return this.getInstance('repository.password');
}
/**
*
* @returns {FolderRepository}
*/
getFolderRepository() {
return this.getInstance('repository.folder');
}
/**
*
* @returns {TagRepository}
*/
getTagRepository() {
return this.getInstance('repository.tag');
}
/**
*
* @returns {CSEv1Encryption}
*/
getCseV1Encryption() {
return this.getInstance('encryption.csev1');
}
/**
*
* @returns {CSEv1Encryption}
*/
getDefaultEncryption() {
let mode = 'auto';
if(this._config.hasOwnProperty('defaultEncryption')) {
mode = this._config.defaultEncryption;
}
if(mode === 'none') {
return this.getInstance('encryption.none');
}
if(mode === 'csev1') {
return this.getInstance('encryption.csev1');
}
let csev1 = this.getInstance('encryption.csev1');
if(csev1.enabled()) return csev1;
return this.getInstance('encryption.none');
}
/**
*
* @param parameters
* @return {*}
*/
getInstance(...parameters) {
return this._classes.getInstance(...parameters)
}
/**
*
* @param parameters
* @return {*}
*/
setInstance(...parameters) {
return this._classes.setInstance(...parameters)
}
/**
*
* @param parameters
* @return {*}
*/
getClass(...parameters) {
return this._classes.getClass(...parameters)
}
/**
*
* @param config
* @private
*/
_setConfig(config) {
if(!config.hasOwnProperty('userAgent')) {
config.userAgent = null;
}
if(config.hasOwnProperty('defaultEncryption') && ['auto', 'none', 'csev1'].indexOf(config.defaultEncryption) === -1) {
throw new ConfigurationError('Invalid default encryption');
} else {
config.defaultEncryption = 'auto';
}
this._config = config;
}
/**
* @deprecated
*/
export default class BaseApi extends BasicPasswordsClient {
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ export default class SessionAuthorization {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
this._api = api;
......
......@@ -2,7 +2,7 @@ export default class AbstractToken {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {String} id
* @param {String} label
* @param {String} description
......
import ObjectMerger from "../Utility/ObjectMerger";
export default class BaseClassLoader {
export default class BasicClassLoader {
constructor(classes = {}) {
this._classes = ObjectMerger.merge(this._getDefaultClasses(), classes);
......@@ -27,7 +27,7 @@ export default class BaseClassLoader {
*
* @param {String} name
* @param {Object} object
* @return {BaseClassLoader}
* @return {BasicClassLoader}
* @api
*/
setInstance(name, object) {
......@@ -65,7 +65,7 @@ export default class BaseClassLoader {
*
* @param {String} name
* @param {Object} constructor
* @return {BaseClassLoader}
* @return {BasicClassLoader}
* @api
*/
registerClass(name, constructor) {
......
......@@ -55,11 +55,11 @@ import InternalServerError from "../Exception/Http/InternalServerError";
import BadGatewayError from "../Exception/Http/BadGatewayError";
import ServiceUnavailableError from "../Exception/Http/ServiceUnavailableError";
import GatewayTimeoutError from "../Exception/Http/GatewayTimeoutError";
import BaseClassLoader from "./BaseClassLoader";
import BasicClassLoader from "./BasicClassLoader";
import ModelService from "../Services/ModelService";
import PasswordService from "../Services/PasswordService";
export default class DefaultClassLoader extends BaseClassLoader {
export default class DefaultClassLoader extends BasicClassLoader {
/**
*
......
......@@ -107,7 +107,8 @@ export default class SimpleApi {
if(config.user !== null && config.password !== null) {
this._headers.Authorization = `Basic ${btoa(`${config.user}:${config.password}`)}`;
} else {
throw new Error('Api username or password missing');
// @TODO Use custom error here
throw new Error('API username or password missing');
}
this._enabled = true;
......
import ConfigurationError from "../Exception/ConfigruationError";
export default class BasicPasswordsClient {
/**
*
* @param server
* @param config
* @param classes
*/
constructor(server, config, classes) {
this._setConfig(config);
this._classes = classes;
this._classes.setInstance('model.server', server);
this._classes.setInstance('api', this);
this._server = server;
this._session = this._classes.getInstance('model.session', server.getUser(), server.getToken());
this._events = this._classes.getInstance('event.event');
}
/**
*
* @param {String} event
* @param {Function} listener
*/
on(event, listener) {
this._events.on(event, listener);
}
/**
*
* @param {String} event
* @param {Function} listener
*/
once(event, listener) {
this._events.once(event, listener);
}
/**
*
* @param {String} event
* @param {Function} listener
*/
off(event, listener) {
this._events.off(event, listener);
}
/**
*
* @param {String} event
* @param {Object} data
*/
emit(event, data) {
this._events.emit(event, data);
}
/**
*
* @return {Server}
*/
getServer() {
return this._server;
}
/**
*
* @return {ApiRequest}
*/
getRequest() {
/** @type {ApiRequest} **/
let request = this.getClass('network.request', this, this._server.getApiUrl(), this.getSession());
if(this._config.userAgent !== null) {
request.setUserAgent(this._config.userAgent);
}
return request;
}
/**
* @returns {Session}
*/
getSession() {
return this._session
.setUser(this._server.getUser())
.setToken(this._server.getToken());
}
/**
*
* @returns {SessionAuthorization}
*/
getSessionAuthorization() {
return this.getInstance('authorization.session');
}
/**
*
* @returns {PasswordRepository}
*/
getPasswordRepository() {
return this.getInstance('repository.password');
}
/**
*
* @returns {FolderRepository}
*/
getFolderRepository() {
return this.getInstance('repository.folder');
}
/**
*
* @returns {TagRepository}
*/
getTagRepository() {
return this.getInstance('repository.tag');
}
/**
*
* @returns {CSEv1Encryption}
*/
getCseV1Encryption() {
return this.getInstance('encryption.csev1');
}
/**
*
* @returns {CSEv1Encryption}
*/
getDefaultEncryption() {
let mode = 'auto';
if(this._config.hasOwnProperty('defaultEncryption')) {
mode = this._config.defaultEncryption;
}
if(mode === 'none') {
return this.getInstance('encryption.none');
}
if(mode === 'csev1') {
return this.getInstance('encryption.csev1');
}
let csev1 = this.getInstance('encryption.csev1');
if(csev1.enabled()) return csev1;
return this.getInstance('encryption.none');
}
/**
*
* @param parameters
* @return {*}
*/
getInstance(...parameters) {
return this._classes.getInstance(...parameters)
}
/**
*
* @param parameters
* @return {*}
*/
setInstance(...parameters) {
return this._classes.setInstance(...parameters)
}
/**
*
* @param parameters
* @return {*}
*/
getClass(...parameters) {
return this._classes.getClass(...parameters)
}
/**
*
* @param config
* @private
*/
_setConfig(config) {
if(!config.hasOwnProperty('userAgent')) {
config.userAgent = null;
}
if(config.hasOwnProperty('defaultEncryption') && ['auto', 'none', 'csev1'].indexOf(config.defaultEncryption) === -1) {
throw new ConfigurationError('Invalid default encryption');
} else {
config.defaultEncryption = 'auto';
}
this._config = config;
}
}
\ No newline at end of file
import BasicClassLoader from "../ClassLoader/BasicClassLoader";
import DefaultClassLoader from "../ClassLoader/DefaultClassLoader";
import BasicPasswordsClient from "./BasicPasswordsClient";
export default class PasswordsClient extends BasicPasswordsClient {
/**
*
* @param {(Object|Server)} server
* @param {Object} [config={}]
* @param {(Object|BasicClassLoader|DefaultClassLoader)} [classes={}]
*/
constructor(server, config = {}, classes = {}) {
if(!(classes instanceof BasicClassLoader)) {
classes = new DefaultClassLoader(classes);
}
if(!server.getApiUrl || typeof server.getApiUrl !== "function") {
server = classes.getInstance('model.server', server)
}
super(server, config, classes);
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ import ObjectClone from '../Utility/ObjectClone';
export default class AbstractConverter {
/**
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {String} type
*/
constructor(api, type) {
......
export default class CustomFieldConverter {
/**
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
this._api = api;
......
......@@ -3,7 +3,7 @@ import AbstractConverter from './AbstractConverter';
export default class FolderConverter extends AbstractConverter {
/**
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
super(api, 'folder');
......
......@@ -4,7 +4,7 @@ import ObjectClone from '../Utility/ObjectClone';
export default class PasswordConverter extends AbstractConverter {
/**
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
super(api, 'password');
......
export default class SettingConverter {
/**
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
this._api = api;
......
......@@ -3,7 +3,7 @@ import AbstractConverter from './AbstractConverter';
export default class TagConverter extends AbstractConverter {
/**
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
super(api, 'tag');
......
......@@ -7,7 +7,7 @@ export default class Folder extends EnhancedFolder {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {Object} [data={}]
*/
constructor(api, data = {}) {
......
......@@ -5,7 +5,7 @@ export default class EnhancedFolder extends Folder {
/**
*
* @param {Object} [data={}]
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(data = {}, api) {
super(data);
......
......@@ -7,7 +7,7 @@ export default class Password extends EnhancedPassword {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {Object} [data={}]
*/
constructor(api, data = {}) {
......
......@@ -5,7 +5,7 @@ export default class EnhancedPassword extends Password {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {Object} [data={}]
*/
constructor(data = {}, api) {
......
......@@ -7,7 +7,7 @@ export default class Tag extends EnhancedTag {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {Object} [data={}]
*/
constructor(api, data = {}) {
......
......@@ -5,7 +5,7 @@ export default class EnhancedTag extends Tag {
/**
*
* @param {Object} [data={}]
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(data = {}, api) {
super(data);
......
......@@ -4,7 +4,7 @@ export default class ApiRequest {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
* @param {String} [url=null]
* @param {Session} [session=null]
*/
......
......@@ -22,7 +22,7 @@ export default class AbstractRepository {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
this._api = api;
......
......@@ -2,7 +2,7 @@ export default class SettingRepository {
/**
*
* @param {Api} api
* @param {BasicPasswordsClient} api
*/
constructor(api) {
this._api = api;
......
......@@ -2,7 +2,7 @@ export default class ModelService {
/**
*
* @param {BaseClassLoader} cl
* @param {BasicClassLoader} cl
*/
constructor(cl) {
this._cl = cl;
......
import Api from './Api/Api';
import PassLink from './PassLink/PassLink';
import PasswordsClient from './Client/PasswordsClient';
import BasicPasswordsClient from './Client/BasicPasswordsClient';
import EnhancedClassLoader from "./ClassLoader/EnhancedClassLoader";
import DefaultClassLoader from "./ClassLoader/DefaultClassLoader";
import BasicClassLoader from "./ClassLoader/BasicClassLoader";
export default Api;
export {Api, PassLink};
/** @deprecated **/
const Api = PasswordsClient;
export default PasswordsClient;
export {PasswordsClient, BasicPasswordsClient, EnhancedClassLoader, DefaultClassLoader, BasicClassLoader, PassLink, Api};