@jsynple/core
Version:
All the core modules and types for the Synple application
29 lines (28 loc) • 1.23 kB
JavaScript
/**
* A repository links a given model to a set of methods on the API. It provides easy methods
* to create, update, delete or get model instances by infering URLs on the API.
*/
export class BaseRepository {
/**
* @param resource The base URI to build all URLs from, designating the resource to manipulate.
* @param api The Requestable object making requests on the API, used to format and fetch requests.
*/
constructor(resource, api) {
// The root of all the APIs concerning this resource, with a leading slash character.
this.resource = '';
this.resource = resource;
this.api = api;
}
/**
* Formats the URL needed to access the given resource. Either provide an UUID to access
* a specific instance of the resource, or no URL to get a complete list, or a create URL.
*
* @param appended often times the identifier of the resource you're trying to query.
* @returns a url to access the resource you're trying to query.
*/
uri(appended = '') {
const resource = this.resource === '' ? [] : [this.resource];
const ending = appended === '' ? [] : [appended];
return [...resource, ...ending].join('/');
}
}