From 8cfed972e3a181fe3d2f3518000100dd44e388d1 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:34:36 +0200 Subject: [PATCH] fixing some more codacy issues --- src/models/processes/callback.js | 2 +- src/models/processes/execution.js | 14 ++-- src/models/processes/job.js | 18 +++-- src/models/processes/jobs.js | 11 +-- src/models/processes/process.js | 106 +++++++++++++++++----------- src/models/processes/processes.js | 111 +++++++++++++++++++----------- src/models/processes/results.js | 2 +- 7 files changed, 169 insertions(+), 95 deletions(-) diff --git a/src/models/processes/callback.js b/src/models/processes/callback.js index f42dd02..9b634be 100644 --- a/src/models/processes/callback.js +++ b/src/models/processes/callback.js @@ -21,7 +21,7 @@ function post(neutralUrl, jobId, query, callback) { undefined ); - /* + /* switch (query.type) { } */ diff --git a/src/models/processes/execution.js b/src/models/processes/execution.js index 5763386..dba2e3f 100644 --- a/src/models/processes/execution.js +++ b/src/models/processes/execution.js @@ -54,11 +54,14 @@ function post(neutralUrl, processId, parameters, prefer, callback) { // check parameters against the process input parameter definition for (let [key, processInput] of Object.entries(process.inputs)) { - if (parameters.inputs[key] == undefined) - return callback({ code: 400, description: `${key} not found` }, undefined); + if (parameters.inputs[key] == undefined) + return callback( + { code: 400, description: `${key} not found` }, + undefined + ); switch (processInput.schema.type) { case "number": - if (typeof parameters.inputs[key] !== "number") + if (typeof parameters.inputs[key] !== "number") return callback( { code: 400, @@ -73,7 +76,7 @@ function post(neutralUrl, processId, parameters, prefer, callback) { } for (let [key, processInput] of Object.entries(parameters.inputs)) { - if (process.inputs[key] == undefined) + if (process.inputs[key] == undefined) return callback( { code: 400, description: `${key} not found in process definition` }, undefined @@ -130,8 +133,7 @@ function post(neutralUrl, processId, parameters, prefer, callback) { prefer.includes("async"), parameters, function (err, content) { - if (err) - return callback(err, undefined); + if (err) return callback(err, undefined); // indication in the header of the location of the // newly created job resource diff --git a/src/models/processes/job.js b/src/models/processes/job.js index 59ab855..a97e9d9 100644 --- a/src/models/processes/job.js +++ b/src/models/processes/job.js @@ -11,10 +11,20 @@ function getLinks(neutralUrl, format, jobId, links) { return _encodings[i]; } - links.push({ href: urlJoin(neutralUrl, jobId, `?f=${format}`), rel: `self`, type: getTypeFromFormat(format), title: `Job information as ${format}` }) - utils.getAlternateFormats(format, ['json', 'html']).forEach(altFormat => { - links.push({ href: urlJoin(neutralUrl, jobId, `?f=${altFormat}`), rel: `alternate`, type: getTypeFromFormat(altFormat), title: `Job information as ${altFormat}` }) - }) + links.push({ + href: urlJoin(neutralUrl, jobId, `?f=${format}`), + rel: `self`, + type: getTypeFromFormat(format), + title: `Job information as ${format}`, + }); + utils.getAlternateFormats(format, ["json", "html"]).forEach((altFormat) => { + links.push({ + href: urlJoin(neutralUrl, jobId, `?f=${altFormat}`), + rel: `alternate`, + type: getTypeFromFormat(altFormat), + title: `Job information as ${altFormat}`, + }); + }); links.push({ href: urlJoin(neutralUrl, jobId, "results", `?f=${format}`), diff --git a/src/models/processes/jobs.js b/src/models/processes/jobs.js index e1a7e37..b303917 100644 --- a/src/models/processes/jobs.js +++ b/src/models/processes/jobs.js @@ -49,11 +49,14 @@ export function create(processId, isAsync) { } function e7() { - return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c => - (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16) + return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c) => + ( + +c ^ + (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4))) + ).toString(16) ); } - + let job = {}; job.processID = processId; job.jobID = e7(); @@ -80,7 +83,7 @@ function get(neutralUrl, format, callback) { // (OAPIC P2) Requirement 3A: The content of that response SHALL be based upon the JSON schema collections.yaml. var content = {}; - content.links = getContent(neutralUrl, format) + content.links = getContent(neutralUrl, format); content.jobs = []; diff --git a/src/models/processes/process.js b/src/models/processes/process.js index 34305f9..6c2da14 100644 --- a/src/models/processes/process.js +++ b/src/models/processes/process.js @@ -1,62 +1,90 @@ -import urlJoin from 'url-join' -import utils from '../../utils/utils.js' -import { getProcesses } from '../../database/processes.js' +import urlJoin from "url-join"; +import utils from "../../utils/utils.js"; +import { getProcesses } from "../../database/processes.js"; function getLinks(neutralUrl, format, name, links) { - function getTypeFromFormat(format) { - var _formats = ['json', 'html'] - var _encodings = ['application/json', 'text/html'] - + var _formats = ["json", "html"]; + var _encodings = ["application/json", "text/html"]; + var i = _formats.indexOf(format); - return _encodings[i] + return _encodings[i]; } - links.push({ href: urlJoin(neutralUrl,), rel: `self`, type: getTypeFromFormat(format), title: `Process description as ${format}` }) - utils.getAlternateFormats(format, ['json', 'html']).forEach(altFormat => { - links.push({ href: urlJoin(neutralUrl, `?f=${altFormat}`), rel: `alternate`, type: getTypeFromFormat(altFormat), title: `Process description as ${altFormat}` }) - }) - - let serviceUrl = neutralUrl.substring(0, neutralUrl.indexOf('processes')); - - links.push({ href: urlJoin(serviceUrl, 'jobs?f=html'), rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, type: 'text/html', title: `Jobs list as HTML` }) - links.push({ href: urlJoin(serviceUrl, 'jobs?f=json'), rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, type: 'application/json', title: `Jobs list as JSON` }) - - links.push({ href: urlJoin(neutralUrl, 'execution'), rel: `http://www.opengis.net/def/rel/ogc/1.0/execute`, title: `Execute endpoint` }) + links.push({ + href: urlJoin(neutralUrl), + rel: `self`, + type: getTypeFromFormat(format), + title: `Process description as ${format}`, + }); + utils.getAlternateFormats(format, ["json", "html"]).forEach((altFormat) => { + links.push({ + href: urlJoin(neutralUrl, `?f=${altFormat}`), + rel: `alternate`, + type: getTypeFromFormat(altFormat), + title: `Process description as ${altFormat}`, + }); + }); + + let serviceUrl = neutralUrl.substring(0, neutralUrl.indexOf("processes")); + + links.push({ + href: urlJoin(serviceUrl, "jobs?f=html"), + rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, + type: "text/html", + title: `Jobs list as HTML`, + }); + links.push({ + href: urlJoin(serviceUrl, "jobs?f=json"), + rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, + type: "application/json", + title: `Jobs list as JSON`, + }); + + links.push({ + href: urlJoin(neutralUrl, "execution"), + rel: `http://www.opengis.net/def/rel/ogc/1.0/execute`, + title: `Execute endpoint`, + }); } function getContent(neutralUrl, format, name, process) { + var content = {}; + content.id = process.id; + content.title = process.title; + content.description = process.description; + content.version = process.version; + content.jobControlOptions = process.jobControlOptions; + content.outputTransmission = process.outputTransmission; - var content = {} - content.id = process.id - content.title = process.title - content.description = process.description - content.version = process.version - content.jobControlOptions = process.jobControlOptions - content.outputTransmission = process.outputTransmission - - content.inputs = process.inputs - content.outputs = process.outputs + content.inputs = process.inputs; + content.outputs = process.outputs; - content.links = [] + content.links = []; - getLinks(neutralUrl, format, name, content.links) + getLinks(neutralUrl, format, name, content.links); - return content + return content; } function get(neutralUrl, format, processId, callback) { - - var processes = getProcesses() - var process = processes[processId] + var processes = getProcesses(); + var process = processes[processId]; if (!process) - return callback({ 'httpCode': 404, 'code': `Process not found: ${processId}`, 'description': 'Make sure you use an existing processId. See /processes' }, undefined); + return callback( + { + httpCode: 404, + code: `Process not found: ${processId}`, + description: "Make sure you use an existing processId. See /processes", + }, + undefined + ); - var content = getContent(neutralUrl, format, processId, process) + var content = getContent(neutralUrl, format, processId, process); return callback(undefined, content); } export default { - get -} \ No newline at end of file + get, +}; diff --git a/src/models/processes/processes.js b/src/models/processes/processes.js index 529d967..d039a02 100644 --- a/src/models/processes/processes.js +++ b/src/models/processes/processes.js @@ -1,65 +1,96 @@ -import urlJoin from 'url-join' -import utils from '../../utils/utils.js' -import { getProcesses } from '../../database/processes.js' - -function getLinks(neutralUrl, format, name, links) { +import urlJoin from "url-join"; +import utils from "../../utils/utils.js"; +import { getProcesses } from "../../database/processes.js"; +function getLinks(neutralUrl, format, name, links) { function getTypeFromFormat(format) { - var _formats = ['json', 'html'] - var _encodings = ['application/json', 'text/html'] - + var _formats = ["json", "html"]; + var _encodings = ["application/json", "text/html"]; + var i = _formats.indexOf(format); - return _encodings[i] + return _encodings[i]; } - links.push({ href: urlJoin(neutralUrl, name ), type: getTypeFromFormat(format), rel: `self`, title: `process description as ${format}` }) - utils.getAlternateFormats(format, ['json', 'html']).forEach(altFormat => { - links.push({ href: urlJoin(neutralUrl, name, `?f=${altFormat}`), rel: `alternate`, type: getTypeFromFormat(altFormat), title: `Process description as ${altFormat}` }) - }) - - let serviceUrl = neutralUrl.substring(0, neutralUrl.indexOf('processes')); - - links.push({ href: urlJoin(serviceUrl, 'jobs?f=html'), rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, type: 'text/html', title: `Jobs list as HTML` }) - links.push({ href: urlJoin(serviceUrl, 'jobs?f=json'), rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, type: 'application/json', title: `Jobs list as JSON` }) - - links.push({ href: urlJoin(neutralUrl, name, 'execution'), rel: `http://www.opengis.net/def/rel/ogc/1.0/execute`, title: `Execute endpoint` }) + links.push({ + href: urlJoin(neutralUrl, name), + type: getTypeFromFormat(format), + rel: `self`, + title: `process description as ${format}`, + }); + utils.getAlternateFormats(format, ["json", "html"]).forEach((altFormat) => { + links.push({ + href: urlJoin(neutralUrl, name, `?f=${altFormat}`), + rel: `alternate`, + type: getTypeFromFormat(altFormat), + title: `Process description as ${altFormat}`, + }); + }); + + let serviceUrl = neutralUrl.substring(0, neutralUrl.indexOf("processes")); + + links.push({ + href: urlJoin(serviceUrl, "jobs?f=html"), + rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, + type: "text/html", + title: `Jobs list as HTML`, + }); + links.push({ + href: urlJoin(serviceUrl, "jobs?f=json"), + rel: `http://www.opengis.net/def/rel/ogc/1.0/job-list`, + type: "application/json", + title: `Jobs list as JSON`, + }); + + links.push({ + href: urlJoin(neutralUrl, name, "execution"), + rel: `http://www.opengis.net/def/rel/ogc/1.0/execute`, + title: `Execute endpoint`, + }); } function getContent(neutralUrl, format, name, document) { + var content = {}; + content.id = document.id; + content.title = document.title; + content.description = document.description; + content.version = document.version; + content.jobControlOptions = document.jobControlOptions; + content.outputTransmission = document.outputTransmission; - var content = {} - content.id = document.id - content.title = document.title - content.description = document.description - content.version = document.version - content.jobControlOptions = document.jobControlOptions - content.outputTransmission = document.outputTransmission - - content.links = [] + content.links = []; - getLinks(neutralUrl, format, name, content.links) + getLinks(neutralUrl, format, name, content.links); - return content + return content; } function get(neutralUrl, format, callback) { - var content = {}; - content.links = [] + content.links = []; // (OAPIC P2) Requirement 2B. The API SHALL support the HTTP GET operation on all links to a Collections Resource that have the relation type - content.links.push({ href: urlJoin(neutralUrl, `?f=${format}`), rel: `self`, type: utils.getTypeFromFormat(format), title: `This document` }) - utils.getAlternateFormats(format, ['json', 'html']).forEach(altFormat => { - content.links.push({ href: urlJoin(neutralUrl, `?f=${altFormat}`), rel: `alternate`, type: utils.getTypeFromFormat(altFormat), title: `This document as ${altFormat}` }) - }) + content.links.push({ + href: urlJoin(neutralUrl, `?f=${format}`), + rel: `self`, + type: utils.getTypeFromFormat(format), + title: `This document`, + }); + utils.getAlternateFormats(format, ["json", "html"]).forEach((altFormat) => { + content.links.push({ + href: urlJoin(neutralUrl, `?f=${altFormat}`), + rel: `alternate`, + type: utils.getTypeFromFormat(altFormat), + title: `This document as ${altFormat}`, + }); + }); content.processes = []; - var processes = getProcesses() + var processes = getProcesses(); // get content per :process for (var name in processes) { - var process = getContent(neutralUrl, format, name, processes[name]) + var process = getContent(neutralUrl, format, name, processes[name]); content.processes.push(process); } @@ -68,4 +99,4 @@ function get(neutralUrl, format, callback) { export default { get, -} \ No newline at end of file +}; diff --git a/src/models/processes/results.js b/src/models/processes/results.js index 1c39cd4..e3cc5fd 100644 --- a/src/models/processes/results.js +++ b/src/models/processes/results.js @@ -28,7 +28,7 @@ function getLinks(neutralUrl, format, links) { } export function getContent(neutralUrl, format, job) { - var content = job.results + var content = job.results; content.links = []; getLinks(neutralUrl, format, content.links);