diff --git a/lib/handlers/source.js b/lib/handlers/source.ts similarity index 60% rename from lib/handlers/source.js rename to lib/handlers/source.ts index b07f0321d2a..f737d78d09c 100644 --- a/lib/handlers/source.js +++ b/lib/handlers/source.ts @@ -22,33 +22,40 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +import express from 'express'; import _ from 'underscore'; +import {Source} from '../sources'; + +// TODO(supergrecko): Maybe provide a more elegant way to do this instead of accessing keys? +const ALLOWED_ACTIONS = new Set(['list', 'load']); + export class SourceHandler { - constructor(fileSources, addStaticHeaders) { - this.allowedActions = new Set(['list', 'load', 'save']); - this.sourceToHandler = _.indexBy(fileSources, 'urlpart'); - this.addStaticHeaders = addStaticHeaders; + public constructor(private fileSources: Source[], private addStaticHeaders: (res: express.Response) => void) {} + + private getSourceForHandler(handler: string): Source | null { + const records = _.indexBy(this.fileSources, 'urlpart'); + return records[handler] || null; } - handlerForAction(handler, action) { - return this.allowedActions.has(action) ? handler[action] : null; + private getActionForSource(source: Source, action: string): ((...args: unknown[]) => Promise) | null { + return ALLOWED_ACTIONS.has(action) ? source[action] : null; } - handle(req, res, next) { - const bits = req.url.split('/'); - const handler = this.sourceToHandler[bits[1]]; - if (!handler) { + public handle(req: express.Request, res: express.Response, next: express.NextFunction): void { + // Split URLs with the scheme /source/browser/list into the source and the action to perform + const [_, handler, action, ...rest] = req.url.split('/'); + const source = this.getSourceForHandler(handler); + if (source === null) { next(); return; } - const handlerAction = this.handlerForAction(handler, bits[2]); - if (!handlerAction) { + const callback = this.getActionForSource(source, action); + if (callback === null) { next(); return; } - handlerAction - .apply(handlerAction, bits.slice(3)) + callback(rest) .then(response => { this.addStaticHeaders(res); res.send(response); diff --git a/lib/sources/browser.ts b/lib/sources/browser.ts index a76d35fdff8..d1c4946d783 100644 --- a/lib/sources/browser.ts +++ b/lib/sources/browser.ts @@ -28,7 +28,6 @@ import type {Source, SourceEntry} from './index'; export const browser: Source = { name: 'Browser', urlpart: 'browser', - save: undefined, list(): Promise[]> { return Promise.resolve([]); }, diff --git a/lib/sources/builtin.ts b/lib/sources/builtin.ts index 58534de332c..362b3153eba 100644 --- a/lib/sources/builtin.ts +++ b/lib/sources/builtin.ts @@ -69,5 +69,4 @@ export const builtin: Source = { file: e.file, })); }, - save: null, }; diff --git a/lib/sources/index.ts b/lib/sources/index.ts index 4d0341e609e..d00bcab9b80 100644 --- a/lib/sources/index.ts +++ b/lib/sources/index.ts @@ -41,8 +41,6 @@ export interface Source { urlpart: string; list(): Promise[]>; load(language: string, filename: string): Promise<{file: string}>; - /** Appears to be unused? */ - save: unknown; } export const sources = [browser, builtin]; diff --git a/test/handlers/source-tests.js b/test/handlers/source-tests.js index 29a89bf7382..86a14209e81 100644 --- a/test/handlers/source-tests.js +++ b/test/handlers/source-tests.js @@ -35,7 +35,6 @@ describe('Sources', () => { urlpart: 'moose', list: () => Promise.resolve({moose: 'pig'}), load: name => Promise.resolve({file: `File called ${name}`}), - save: null, }, ], res => res.setHeader('Yibble', 'boing'),