npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solenoden/endpoint-utils-demo

v1.0.0

Published

Common utilities revolving around endpoints, such as the validation of request data.

Downloads

2

Readme

1Life Endpoint Utils

A JavaScript package which contains utilities, pertaining to endpoints, common among 1Life WebCore services.

Getting Started

  1. Connect to the WebCorePackages feed.

  2. Generate or refresh your VSTS token.

  3. Run npm install 1life-endpoint-utils.

  4. Import and use the exposed classes to validate your endpoints.

Connecting to the WebCorePackages feed

To connect to the WebCorePackages feed you need to create a .npmrc file in your project's root folder (the folder containing the package.json).

The .npmrc file should contain the following code:

registry=https://pkgs.dev.azure.com/Telesure/1Life/_packaging/WebCorePackages/npm/registry/

always-auth=true

Generate or refresh your VSTS token

This package is protected using Visual Studio Team Services (VSTS) and will require you to have a valid VSTS token

If you haven't used Azure Artifacts with npm you will need to do the following:

  1. Install node and npm.
  2. Install vsts auth for npm with the following command: npm install -g vsts-npm-auth --registry https://registry.npmjs.com --always-auth false
  3. Retrieve a vsts token by running the following command: npm run refreshVSToken

If you already have used packages in the WebCorePackages feed you might need to refresh your VSTS token. You can do so by running:

npm run refreshVSToken

Usage

This package currently contains utilities to:

Validate an endpoint

To validate an endpoint, start by importing the EndpointValidator and ValidatableParameter classes.

const { ValidatableParameter, EndpointValidator} = require('1life-endpoint-utils')

Define all the required parameters for your endpoint:

const requiredParameters = [
    new ValidatableParameter('name', true),
    new ValidatableParameter('description', true),
    // Validation can be nested
    new ValidatableParameter('language', true, [
        new ValidatableParameter('code', true),
        new ValidatableParameter('name', true)
    ])
]

To determine if an endpoint is valid, do the following:

const endpointIsValid = EndpointValidator.determineEndpointHasValidParameters(request.body, requiredParameters)

To provide an error message informing the endpoint consumer which field is missing, do the following:

const validationErrorMessage = EndpointValidator.determineEndpointMissingParameterMessage('body', request.body, requiredParameters)
response.status(400).send(validationErrorMessage)

Here is a (somewhat) full, Express implementation of validating an endpoint.

const { ValidatableParameter, EndpointValidator} = require('1life-endpoint-utils')

//
// Define routes
//

function addBook(request, response) {
    const requiredParameters = [
        new ValidatableParameter('name', true),
        new ValidatableParameter('description', true),
        // Validation can be nested
        new ValidatableParameter('language', true, [
            new ValidatableParameter('code', true),
            new ValidatableParameter('name', true)
        ])
    ]

    if (!EndpointValidator.determineEndpointHasValidParameters(request.body, requiredParameters)) {
        const validationErrorMessage = EndpointValidator.determineEndpointMissingParameterMessage('body', request.body, requiredParameters)
        response.status(400).send(validationErrorMessage)
    }

    //
    // Rest of the endpoint logic
    //
}

API Reference

Models

ValidatableParameter

A parameter on an endpoint which can be validated.

A parameter on an endpoint can come from the query, params or body of the request.

Currently, only supports validation to check if the parameter is present, type validation is not yet supported.

source code

Fields

  • name: String field name of the validatable parameter

  • checkTruthiness: boolean Determines if a truthiness check must be done. If true then the parameter cannot be an empty string, zero, an empty array or undefined. If false the parameter cannot be undefined but can be an empty string, zero or an empty array.

  • requiredFields: ValidatableParameter[ ] If the parameter is an object, use this field to declare the required fields of the object so that they too may be validated

Classes

EndpointValidator

Contains logic pertaining to the validation of (request data on) endpoints

source code

Methods

  • determineEndpointHasValidParameters(parameters, requiredParams): boolean Determines if the supplied parameters are valid. @parameters: any[] - The parameters of the endpoint, can be from the request query, params or body. @requiredParams: ValidatableParameter[ ] - The required parameters of the endpoint.

  • determineEndpointMissingParameterMessage(parentObjectName, parameters, requiredParams): String Provides an error message for the missing parameter. @parentObjectName: String - The name of the parent object of the supplied parameters, typically query or body. @parameters: any[] - The parameters of the endpoint, can be from the request query, params or body. @requiredParams: ValidatableParameter[ ] - The required parameters of the endpoint.