diff --git a/src/copy/en.js b/src/copy/en.js index ef50d69..8c3350b 100644 --- a/src/copy/en.js +++ b/src/copy/en.js @@ -5,7 +5,8 @@ export default { onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:sheet/:tab/:resource.', noSheet: sheet => `The sheet ${sheet} is not available in this server.`, noResource: prts => `The resource '${prts[2]}' does not exists in the tab '${prts[1]}' in this sheet.`, - noFragment: prts => `Fragment index does not exist` + noFragment: prts => `Fragment index does not exist`, + modelLayer: prts => `Something went wrong at the model layer` }, success: { update: 'All sheets updated' diff --git a/src/lib/errors.js b/src/lib/errors.js new file mode 100644 index 0000000..d15b7f0 --- /dev/null +++ b/src/lib/errors.js @@ -0,0 +1,13 @@ +import copy from '../copy/en' + +export function modelLayerGeneric (parts) { + return new Error(copy.errors.modelLayer(parts)) +} + +export function noFragment (parts) { + return new Error(copy.errors.noFragment(parts)) +} + +export function noResource (parts) { + return new Error(copy.errors.noResource(parts)) +} diff --git a/src/models/Interface.js b/src/models/Interface.js index 60f98dd..71854fc 100644 --- a/src/models/Interface.js +++ b/src/models/Interface.js @@ -1,30 +1,41 @@ /* eslint-disable */ /** - * Model is a class whose sole responsibility is to save and load blueprints. - * It allows for different storage mechanisms for different kinds of blueprints. + * Model is a class whose sole responsibility is to save and load data through a custom URL format. + * As an interfacce, it allows for different storage mechanisms, and different scale/performance for different kinds of data. * * ERRORS: * When a load function fails, it should throw either: - * 1. A __ error if the resource doesn't exist on that sheet/tab. - * 2. A __ error if a fragment lookup fails because it doesn't exist. - * 3. A __ error if something else goes wrong. + * 1. noResource(parts) if the resource doesn't exist on that sheet/tab. + * 2. noFragment(parts) if a fragment lookup fails because it doesn't exist. + * 3. modelLayerGeneric(parts) if something else goes wrong. + * + * This is a WIP layer. See StoreJson.js for an example in action. */ class Model { /** - * save - save a Blueprint, using the information it contains. - * - * @param {type} blueprint the Blueprint to be saved. - * @return {type} Promise which returns True. + * Index the data stored by this model, returning a list of the available URLs. + * @return {Promise(boolean)} Unpacks to a list of available URLs if successful, throws an error otherwise. */ - save (blueprint) {} + index () {} + + /** + * Save data at a URL. The URL is in the format + * /:fetcherID/:tab/:resource + * Fetcher IDs must be unique, tabs and resources can be duplicated across + * different fetchers. + * + * @param {string} url - the URL at which to save the data. + * @param {object} data - the data to be saved. + * @return {Promise(boolean)} Unpacks to true if the update was successful, false if otherwise. + */ + save (url, data) {} /** - * load - load a resource from a data model, using a Blueprint object as - * well as a REST-like URL of the format /:source/:tab/:resource. - * - * @param {type} url String that represents the path to resource. - * @param {type} blueprint Blueprint object (desaturated?). - * @return {type} Object containing the resource data. + * Load data from a URL, in the format + * /:fetcherID/:tab/:resource + * + * @param {string} url - the URL at which to load the data. + * @return {Promise(object)} a Promise that unpacks to the data retrieved. An error will be thrown if the URL is invalid. */ - load (url, blueprint) {} + load (url) {} } diff --git a/src/models/StoreJson.js b/src/models/StoreJson.js index 1124960..72ca593 100644 --- a/src/models/StoreJson.js +++ b/src/models/StoreJson.js @@ -1,5 +1,5 @@ import fs from 'mz/fs' -import copy from '../copy/en' +import errors from '../lib/errors' const STORAGE_DIRNAME = 'temp' @@ -45,7 +45,7 @@ class StoreJson { if (!isNaN(id) && id >= 0 && id < data.length) { return data[id] } else { - throw new Error(copy.errors.noFragment(parts)) + throw errors.noFragment(parts) } } else { // Do a lookup if fragment is included to filter a relevant item @@ -53,12 +53,12 @@ class StoreJson { if (!isNaN(index) && index >= 0 && index < data.length) { return data.filter((vl, idx) => idx === index)[0] } else { - throw new Error(copy.errors.noFragment(parts)) + throw errors.noFragment(parts) } } }) } else { - return Promise.reject(new Error(copy.errors.noResource(parts))) + return Promise.reject(errors.noResource(parts)) } }