Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docstrings for endpoint-common service #8079

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions services/endpoint-common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Common functions and utilities for tasks related to endpoint badges.
*
* @module
*/

import Joi from 'joi'
import validate from '../core/base-service/validate.js'
import { InvalidResponse } from './index.js'
Expand All @@ -14,6 +20,11 @@ const optionalNumberWhenAnyLogoPresent = Joi.alternatives()
.conditional('namedLogo', { is: Joi.string().required(), then: Joi.number() })
.conditional('logoSvg', { is: Joi.string().required(), then: Joi.number() })

/**
* Joi schema for validating endpoint.
*
* @type {object}
*/
const endpointSchema = Joi.object({
schemaVersion: 1,
label: Joi.string().allow('').required(),
Expand All @@ -33,9 +44,18 @@ const endpointSchema = Joi.object({
.oxor('namedLogo', 'logoSvg')
.required()

// Strictly validate according to the endpoint schema. This rejects unknown /
// invalid keys. Optionally it prints those keys in the message in order to
// provide detailed feedback.
/**
* Strictly validate the data according to the endpoint schema.
* This rejects unknown/invalid keys.
* Optionally it prints those keys in the message to provide detailed feedback.
*
* @param {object} data Object containing the data for validation
* @param {object} attrs Refer to individual attributes
* @param {string} [attrs.prettyErrorMessage] If provided then error message is set to this value
* @param {boolean} [attrs.includeKeys] If true then includes error details in error message, defaults to false
* @throws {InvalidResponse|Error} Error if Joi validation fails due to invalid or no schema
* @returns {object} Value if Joi validation is success
*/
function validateEndpointData(
data,
{ prettyErrorMessage = 'invalid response data', includeKeys = false } = {}
Expand All @@ -56,6 +76,17 @@ function validateEndpointData(

const anySchema = Joi.any()

/**
* Fetches data from the endpoint and validates the data.
*
* @param {object} serviceInstance Instance of Endpoint class
* @param {object} attrs Refer to individual attributes
* @param {string} attrs.url Endpoint URL
* @param {object} attrs.errorMessages Object containing error messages for different error codes
* @param {string} attrs.validationPrettyErrorMessage If provided then the error message is set to this value
* @param {boolean} attrs.includeKeys If true then includes error details in error message
* @returns {object} Data fetched from endpoint
*/
async function fetchEndpointData(
serviceInstance,
{ url, errorMessages, validationPrettyErrorMessage, includeKeys }
Expand Down