Gathering detailed insights and metrics for openapi-path-templating
Gathering detailed insights and metrics for openapi-path-templating
Gathering detailed insights and metrics for openapi-path-templating
Gathering detailed insights and metrics for openapi-path-templating
openapi-server-url-templating
OpenAPI Server URL templating parser, validator and substitution mechanism.
openapi-typescript
Convert OpenAPI 3.0 & 3.1 schemas to TypeScript
@octokit/openapi-types
Generated TypeScript definitions based on GitHub's OpenAPI spec for api.github.com
@openapitools/openapi-generator-cli
A npm package wrapper for OpenAPI Generator (https://github.com/OpenAPITools/openapi-generator), generates which API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
OpenAPI Path Templating parser, validator and resolver.
npm install openapi-path-templating
99.6
Supply Chain
100
Quality
82.5
Maintenance
100
Vulnerability
87.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
156 Commits
2 Watching
1 Branches
2 Contributors
Updated on 26 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-0.5%
52,721
Compared to previous day
Last week
-1.4%
283,120
Compared to previous week
Last month
10.2%
1,194,183
Compared to previous month
Last year
0%
5,826,470
Compared to previous year
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:
Get professionally supported openapi-path-templating with Tidelift Subscription. |
You can install openapi-path-templating
using npm
:
1 $ npm install openapi-path-templating
openapi-path-templating
currently supports parsing, validation and resolution.
Both parser and validator are based on a superset of ABNF (SABNF)
and use apg-lite parser generator.
Parsing a Path Templating is as simple as importing the parse function and calling it.
1import { parse } from 'openapi-path-templating'; 2 3const parseResult = parse('/pets/{petId}'); 4parseResult.result.success; // => true
parseResult variable has the following shape:
{
result: {
success: true,
state: 101,
stateName: 'MATCH',
length: 13,
matched: 13,
maxMatched: 13,
maxTreeDepth: 18,
nodeHits: 324
},
ast: fnast {
callbacks: [
'path-template': [Function: pathTemplate],
path: [Function: path],
query: [Function: query],
'query-marker': [Function: queryMarker],
fragment: [Function: fragment],
'fragment-marker': [Function: fragmentMarker],
slash: [Function: slash],
'path-literal': [Function: pathLiteral],
'template-expression': [Function: templateExpression],
'template-expression-param-name': [Function: templateExpressionParamName]
],
init: [Function (anonymous)],
ruleDefined: [Function (anonymous)],
udtDefined: [Function (anonymous)],
down: [Function (anonymous)],
up: [Function (anonymous)],
translate: [Function (anonymous)],
setLength: [Function (anonymous)],
getLength: [Function (anonymous)],
toXml: [Function (anonymous)]
}
}
1import { parse } from 'openapi-path-templating'; 2 3const parseResult = parse('/pets/{petId}'); 4const parts = []; 5 6parseResult.ast.translate(parts);
After running the above code, parts variable has the following shape:
1[ 2 [ 'path-template', '/pets/{petId}' ], 3 [ 'path', '/pets/{petId}' ], 4 [ 'slash', '/' ], 5 [ 'path-literal', 'pets' ], 6 [ 'slash', '/' ], 7 [ 'template-expression', '{petId}' ], 8 [ 'template-expression-param-name', 'petId' ] 9]
1import { parse } from 'openapi-path-templating'; 2 3const parseResult = parse('/pets/{petId}'); 4const xml = parseResult.ast.toXml();
After running the above code, xml variable has the following content:
1<?xml version="1.0" encoding="utf-8"?> 2<root nodes="7" characters="13"> 3 <!-- input string --> 4 /pets/{petId} 5 <node name="path-template" index="0" length="13"> 6 /pets/{petId} 7 <node name="path" index="0" length="13"> 8 /pets/{petId} 9 <node name="slash" index="0" length="1"> 10 / 11 </node><!-- name="slash" --> 12 <node name="path-literal" index="1" length="4"> 13 pets 14 </node><!-- name="path-literal" --> 15 <node name="slash" index="5" length="1"> 16 / 17 </node><!-- name="slash" --> 18 <node name="template-expression" index="6" length="7"> 19 {petId} 20 <node name="template-expression-param-name" index="7" length="5"> 21 petId 22 </node><!-- name="template-expression-param-name" --> 23 </node><!-- name="template-expression" --> 24 </node><!-- name="path" --> 25 </node><!-- name="path-template" --> 26</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.
Validating a Path Templating is as simple as importing the test function and calling it.
1import { test } from 'openapi-path-templating'; 2 3test('/pets/{petId}'); // => true 4test('/a{petId}'); // => true 5test('/pets'); // => true 6test('/pets', { strict: true }); // => false (doesn't contain any template-expression)
Resolving a Path Templating is as simple as importing the resolve function and calling it.
1import { resolve } from 'openapi-path-templating'; 2 3resolve('/pets/{petId}', { petId: 3 }); // => "/pets/3"
Resolved Path Templating is automatically encoded using encodeURIComponent function. It is possible to provide a custom encoder.
1import { resolve } from 'openapi-path-templating'; 2 3resolve('/pets/{petId}', { petId: '/?#' }, { 4 encoder: (component) => component, // no encoding 5}); // => "/pets//?#"
New grammar instance can be created in following way:
1import { Grammar } from 'openapi-path-templating'; 2 3const grammar = new Grammar();
To obtain original ABNF (SABNF) grammar as a string:
1import { Grammar } from 'openapi-path-templating'; 2 3const grammar = new Grammar(); 4 5grammar.toString(); 6// or 7String(grammar);
The Path Templating is defined by the following ABNF syntax
1; OpenAPI Path Templating ABNF syntax 2path-template = path [ query-marker query ] [ fragment-marker fragment ] 3path = slash *( path-segment slash ) [ path-segment ] 4path-segment = 1*( path-literal / template-expression ) 5query = *( query-literal ) 6query-literal = 1*( unreserved / pct-encoded / sub-delims / ":" / "@" / "/" / "?" / "&" / "=" ) 7query-marker = "?" 8fragment = *( fragment-literal ) 9fragment-literal = 1*( unreserved / pct-encoded / sub-delims / ":" / "@" / "/" / "?" ) 10fragment-marker = "#" 11slash = "/" 12path-literal = 1*( unreserved / pct-encoded / sub-delims / ":" / "@" ) 13template-expression = "{" template-expression-param-name "}" 14template-expression-param-name = 1*( unreserved / pct-encoded / sub-delims / ":" / "@" ) 15 16; Characters definitions (from RFC 3986) 17unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" 18pct-encoded = "%" HEXDIG HEXDIG 19sub-delims = "!" / "$" / "&" / "'" / "(" / ")" 20 / "*" / "+" / "," / ";" / "=" 21ALPHA = %x41-5A / %x61-7A ; A-Z / a-z 22DIGIT = %x30-39 ; 0-9 23HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
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.
No vulnerabilities found.
No security vulnerabilities found.