Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-path-templating

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-path-templating

OpenAPI Path Templating parser, validator and resolver.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
242K
increased by19.35%
Maintainers
1
Weekly downloads
 
Created
Source

openapi-path-templating

Path Templating allow defining values based on information that will only be available within the HTTP message in an actual API call. This mechanism is used by Paths Object of OpenAPI specification.

openapi-path-templating is a parser, validator and resolver for OpenAPI Path Templating. It supports Path Templating defined in following OpenAPI specification versions:

Table of Contents

Getting started

Installation

You can install openapi-path-templating using npm:

 $ npm install openapi-path-templating

Given that openapi-path-templating is a pure ESM package you can also install it directly from GitHub.

 $ npm install github:char0n/openapi-path-templating

Usage

openapi-path-templating currently supports parsing, validation and resolution. Both parser and validator are based on a superset of ABNF (SABNF) and use apg-js parser generator.

Parsing

Parsing a Path template expression is as simple as importing the parse function and calling it.

import { parse } from 'openapi-path-templating';

const parseResult = parse('/pets/{petId}');
parseResult.result.success; // => true

parseResult variable has the following shape:

{
  result: {
    success: true,
    state: 101,
    length: 13,
    matched: 13,
    maxMatched: 13,
    maxTreeDepth: 15,
    nodeHits: 279,
    inputLength: 13,
    subBegin: 0,
    subEnd: 13,
    subLength: 13
  },
  ast: exportsAst {
    callbacks: [
      'path-template': [Function: pathTemplate],
      slash: [Function: slash],
      'path-literal': [Function: pathLiteral],
      'template-expression': [Function: templateExpression]
    ],
    astObject: 'astObject',
    init: [Function: init],
    ruleDefined: [Function: ruleDefined],
    udtDefined: [Function: udtDefined],
    down: [Function: down],
    up: [Function: up],
    translate: [Function: translate],
    setLength: [Function: setLength],
    getLength: [Function: getLength],
    toXml: [Function: toSml],
    phrases: [Function: phrases]
  }
}
Interpreting AST as list of entries
import { parse } from 'openapi-path-templating';

const parseResult = parse('/pets/{petId}');
const parts = [];

parseResult.ast.translate(parts);

After running the above code, parts variable has the following shape:

[
  [ 'path-template', '/pets/{petId}' ],
  [ 'slash', '/' ],
  [ 'path-literal', 'pets' ],
  [ 'slash', '/' ],
  [ 'template-expression', '{petId}' ]
]
Interpreting AST as XML
import { parse } from 'openapi-path-templating';

const parseResult = parse('/pets/{petId}');
const xml = parseResult.ast.toXml();

After running the above code, xml variable has the following content:

<?xml version="1.0" encoding="utf-8"?>
<root nodes="5" characters="13">
  <!-- input string, decimal integer character codes -->
  47,112,101,116,115,47,123,112,101,116,73,100,125
  <node name="path-template" index="0" length="13">
    47,112,101,116,115,47,123,112,101,116,73,100,125
    <node name="slash" index="0" length="1">
      47
    </node><!-- name="slash" -->
    <node name="path-literal" index="1" length="4">
      112,101,116,115
    </node><!-- name="path-literal" -->
    <node name="slash" index="5" length="1">
      47
    </node><!-- name="slash" -->
    <node name="template-expression" index="6" length="7">
      123,112,101,116,73,100,125
    </node><!-- name="template-expression" -->
  </node><!-- name="path-template" -->
</root>

NOTE: AST can also be traversed in classical way using depth first traversal. For more information about this option please refer to apg-js and apg-js-examples.

Validation

Validating a Path Templating is as simple as importing the test function and calling it.

import { test } from 'openapi-path-templating';

test('/pets/{petId}'); // => true
test('/pets'); // => false

[!IMPORTANT] Note that Relative URIs without path template expressions (like "/pets") always returns false.

Resolution

Resolving a Path Templating is as simple as importing the resolve function and calling it.

import { resolve } from 'openapi-path-templating';

resolve('/pets/{petId}', { petId: 3 }); // => "/pets/3"
Grammar

New grammar instance can be created in following way:

import { Grammar } from 'openapi-path-templating';

const grammar = new Grammar();

To obtain original ABNF (SABNF) grammar as a string:

import { Grammar } from 'openapi-path-templating';

const grammar = new Grammar();

grammar.toString();
// or
String(grammar);

More about OpenAPI Path Templating

The Path Templating is defined by the following ABNF syntax

; OpenAPI Path Templating ABNF syntax
path-template       = slash *( ( path-literal / template-expression ) slash ) [ path-literal / template-expression ]
slash               = "/"
path-literal        = 1*( unreserved / pct-encoded / sub-delims-no-slash / ":" / "@" )
template-expression = "{" param-name "}"
param-name          = 1*( unreserved / pct-encoded / sub-delims-no-slash / ":" / "@" )

; Characters definitions (from RFC 3986)
unreserved          = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded         = "%" HEXDIG HEXDIG
sub-delims-no-slash = "!" / "$" / "&" / "'" / "(" / ")"
                    / "*" / "+" / "," / ";"
ALPHA               = %x41-5A / %x61-7A   ; A-Z / a-z
DIGIT               = %x30-39            ; 0-9
HEXDIG              = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"

License

openapi-path-templating is licensed under Apache 2.0 license. openapi-path-templating comes with an explicit NOTICE file containing additional legal notices and information.

Keywords

FAQs

Package last updated on 12 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc