All files / src/operation load-node-from-url.ts

14.29% Statements 3/21
0% Branches 0/6
0% Functions 0/5
14.29% Lines 3/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  18x 18x                                                                                                           18x  
import Node from 'type/node';
import Operation, { OperationInput } from 'operation';
import NotFoundError from 'http/error/not-found';
 
interface Input extends OperationInput {
	readonly url: string;
}
 
class LoadNodeFromUrlOperation extends Operation<Input, Node> {
	protected performInternal(): Promise<Node> {
		if (this.isLocalNode()) {
			return this.loadLocalNode();
		} else {
			return this.loadRemoteNode();
		}
	}
 
	private async loadLocalNode(): Promise<Node> {
		const repository = this.getRepository();
		const url = this.getUrl();
		const node_parameters = repository.getNodeParametersForUrl(url);
 
		if (node_parameters === undefined) {
			throw new Error(`Invalid local node url: ${url}`);
		}
 
		const node = await repository.fetchNode(
			node_parameters.type_id,
			node_parameters.id
		);
 
		if (node === undefined) {
			throw new NotFoundError();
		}
 
		return node;
	}
 
	private loadRemoteNode(): Promise<Node> {
		throw new Error('not implemented');
	}
 
	private isLocalNode(): boolean {
		const url = this.getUrl();
		const repository = this.getRepository();
 
		return repository.isLocalUrl(url);
	}
 
	private getUrl(): string {
		const input = this.getInput();
 
		return input.url;
	}
}
 
export default LoadNodeFromUrlOperation;