{"name":"openapi-path-templating","version":"2.1.0","description":"OpenAPI Path Templating parser, validator, resolver and matcher.","main":"./cjs/index.cjs","types":"./types/index.d.ts","exports":{".":{"types":"./types/index.d.ts","import":"./es/index.mjs","require":"./cjs/index.cjs"},"./package.json":"./package.json","./path-templating.bnf":"./src/path-templating.bnf"},"watch":{"test":"{src,test}/*.js"},"scripts":{"prepublishOnly":"npm run build","grammar:compile":"node ./scripts/apg-js.js --lite --in=./src/path-templating.bnf --out=./src/path-templating.js && cd ./src","build":"npm run grammar:compile && npm run build:es && npm run build:cjs && npm run build:cjs:apg-lite","build:es":"cross-env BABEL_ENV=es babel src --out-dir es --extensions '.js' --out-file-extension '.mjs'","build:cjs":"cross-env BABEL_ENV=cjs babel src --out-dir cjs --extensions '.js' --out-file-extension '.cjs'","build:cjs:apg-lite":"cross-env BABEL_ENV=cjs babel node_modules/apg-lite/lib/parser.js --out-file ./cjs/apg-lite.cjs","test":"mocha","test:watch":"npm-watch test","watch":"npm-watch"},"engines":{"node":">=12.20.0"},"type":"module","repository":{"type":"git","url":"git+https://github.com/swaggerexpert/openapi-path-templating.git"},"keywords":["openapi","path","templating","parser","validator","resolver","matcher","matching"],"author":{"name":"Vladimír Gorej","email":"vladimir.gorej@gmail.com"},"license":"Apache-2.0","bugs":{"url":"https://github.com/swaggerexpert/openapi-path-templating/issues"},"homepage":"https://github.com/swaggerexpert/openapi-path-templating#readme","dependencies":{"apg-lite":"^1.0.4"},"devDependencies":{"@babel/cli":"=7.26.4","@babel/core":"=7.26.0","@babel/preset-env":"=7.26.0","@commitlint/cli":"=19.6.1","@commitlint/config-conventional":"=19.6.0","apg-js":"^4.4.0","babel-plugin-module-resolver":"^5.0.2","chai":"=5.1.2","cross-env":"^7.0.3","husky":"=9.1.7","mocha":"=11.0.1","npm-watch":"^0.13.0","prettier":"^3.1.1"},"_id":"openapi-path-templating@2.1.0","gitHead":"817de63ac7f1c0db7f7fba45733a2013a8112136","_nodeVersion":"20.18.0","_npmVersion":"10.8.2","dist":{"integrity":"sha512-fLs5eJmLyU8wPRz+JSH5uLE7TE4Ohg6VHOtj0C0AlD3GTCCcw2LgKW6MSN1A8ZBKHEg2O4/d02knmVU1nvGAKQ==","shasum":"cffdbca552d8ba41321667868ec6ddf5ff68db0e","tarball":"http://registry-npm.ywwl.com/openapi-path-templating/download/openapi-path-templating-2.1.0.tgz","fileCount":29,"unpackedSize":101468,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCRTIzUadLpTeAlYJubHcdpFOe1JfjF5aIt4bg2kvxt5QIgVnXn0XrD1S5F1wHp9Xif1vvUqHs67DJ5RzOjAlsZ6lA="}],"size":25016,"noattachment":false,"key":"/openapi-path-templating/-/openapi-path-templating-2.1.0.tgz"},"_npmUser":{"name":"char0n","email":"vladimir.gorej@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/openapi-path-templating_2.1.0_1735656823766_0.08415447498399109"},"_hasShrinkwrap":false,"readme":"# openapi-path-templating\n\n[![npmversion](https://badge.fury.io/js/openapi-path-templating.svg)](https://www.npmjs.com/package/openapi-path-templating)\n[![npm](https://img.shields.io/npm/dm/openapi-path-templating.svg)](https://www.npmjs.com/package/openapi-path-templating)\n[![Test workflow](https://github.com/swaggerexpert/openapi-path-templating/actions/workflows/test.yml/badge.svg)](https://github.com/swaggerexpert/openapi-path-templating/actions)\n[![Dependabot enabled](https://img.shields.io/badge/Dependabot-enabled-blue.svg)](https://dependabot.com/)\n[![try on RunKit](https://img.shields.io/badge/try%20on-RunKit-brightgreen.svg?style=flat)](https://npm.runkit.com/openapi-path-templating)\n[![Tidelift](https://tidelift.com/badges/package/npm/openapi-path-templating)](https://tidelift.com/subscription/pkg/npm-openapi-path-templating?utm_source=npm-openapi-path-templating&utm_medium=referral&utm_campaign=readme)\n\n[OpenAPI Path Templating](https://spec.openapis.org/oas/v3.1.1.html#path-templating) refers to the usage of template expressions, delimited by curly braces (`{}`), to mark a section of a URL path as replaceable using path parameters.\nEach template expression in the path MUST correspond to a path parameter that is included in the [Path Item](https://spec.openapis.org/oas/v3.1.1.html#path-item-object) itself and/or in each of the Path Item's [Operations](https://spec.openapis.org/oas/v3.1.1.html#operation-object).\nAn exception is if the path item is empty, for example due to ACL constraints, matching path parameters are not required.\n\n`openapi-path-templating` is a **parser**, **validator**, **resolver** and **matcher** for OpenAPI Path Templating,\nwhich played a [foundational role](https://github.com/OAI/OpenAPI-Specification/pull/4244) in defining the official ANBF grammar for OpenAPI Path Templating.\n\nIt supports Path Templating defined in following OpenAPI specification versions:\n\n- [OpenAPI 2.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathTemplating)\n- [OpenAPI 3.0.0](https://spec.openapis.org/oas/v3.0.0.html#path-templating)\n- [OpenAPI 3.0.1](https://spec.openapis.org/oas/v3.0.1.html#path-templating)\n- [OpenAPI 3.0.2](https://spec.openapis.org/oas/v3.0.2.html#path-templating)\n- [OpenAPI 3.0.3](https://spec.openapis.org/oas/v3.0.3.html#path-templating)\n- [OpenAPI 3.0.4](https://spec.openapis.org/oas/v3.0.4.html#path-templating)\n- [OpenAPI 3.1.0](https://spec.openapis.org/oas/v3.1.0.html#path-templating)\n- [OpenAPI 3.1.1](https://spec.openapis.org/oas/v3.1.1.html#path-templating)\n\n\n<table>\n  <tr>\n    <td align=\"right\" valign=\"middle\">\n        <img src=\"https://cdn2.hubspot.net/hubfs/4008838/website/logos/logos_for_download/Tidelift_primary-shorthand-logo.png\" alt=\"Tidelift\" width=\"60\" />\n      </td>\n      <td valign=\"middle\">\n        <a href=\"https://tidelift.com/subscription/pkg/npm-openapi-path-templating?utm_source=npm-openapi-path-templating&utm_medium=referral&utm_campaign=readme\">\n            Get professionally supported openapi-path-templating with Tidelift Subscription.\n        </a>\n      </td>\n  </tr>\n</table>\n\n## Table of Contents\n\n- [Getting started](#getting-started)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [Parsing](#parsing)\n    - [Validation](#validation)\n    - [Resolution](#resolution)\n    - [Matching](#matching)\n    - [Grammar](#grammar)\n- [More about OpenAPI Path Templating](#more-about-openapi-path-templating)\n- [License](#license)\n\n\n## Getting started\n\n### Installation\n\nYou can install `openapi-path-templating` using `npm`:\n\n```sh\n $ npm install openapi-path-templating\n```\n\n### Usage\n\n`openapi-path-templating` currently supports **parsing**, **validation**, **resolution** and **matching**.\nBoth parser and validator are based on a superset of [ABNF](https://www.rfc-editor.org/rfc/rfc5234) ([SABNF](https://github.com/ldthomas/apg-js2/blob/master/SABNF.md))\nand use [apg-lite](https://github.com/ldthomas/apg-lite) parser generator.\n\n#### Parsing\n\nParsing a Path Templating is as simple as importing the **parse** function\nand calling it.\n\n```js\nimport { parse } from 'openapi-path-templating';\n\nconst parseResult = parse('/pets/{petId}');\nparseResult.result.success; // => true\n```\n\n**parseResult** variable has the following shape:\n\n```\n{\n  result: {\n    success: true,\n    state: 101,\n    stateName: 'MATCH',\n    length: 13,\n    matched: 13,\n    maxMatched: 13,\n    maxTreeDepth: 20,\n    nodeHits: 232\n  },\n  ast: fnast {\n    callbacks: [\n      'path-template': [Function: pathTemplate],\n      slash: [Function: slash],\n      'path-literal': [Function: pathLiteral],\n      'template-expression': [Function: templateExpression],\n      'template-expression-param-name': [Function: templateExpressionParamName]\n    ],\n    init: [Function (anonymous)],\n    ruleDefined: [Function (anonymous)],\n    udtDefined: [Function (anonymous)],\n    down: [Function (anonymous)],\n    up: [Function (anonymous)],\n    translate: [Function (anonymous)],\n    setLength: [Function (anonymous)],\n    getLength: [Function (anonymous)],\n    toXml: [Function (anonymous)]\n  }\n}\n```\n\n###### Interpreting AST as list of entries\n\n```js\nimport { parse } from 'openapi-path-templating';\n\nconst parseResult = parse('/pets/{petId}');\nconst parts = [];\n\nparseResult.ast.translate(parts);\n```\n\nAfter running the above code, **parts** variable has the following shape:\n\n```js\n[\n  [ 'path-template', '/pets/{petId}' ],\n  [ 'slash', '/' ],\n  [ 'path-literal', 'pets' ],\n  [ 'slash', '/' ],\n  [ 'template-expression', '{petId}' ],\n  [ 'template-expression-param-name', 'petId' ]\n]\n```\n\n###### Interpreting AST as XML\n\n```js\nimport { parse } from 'openapi-path-templating';\n\nconst parseResult = parse('/pets/{petId}');\nconst xml = parseResult.ast.toXml();\n```\n\nAfter running the above code, **xml** variable has the following content:\n\n```xml\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root nodes=\"6\" characters=\"13\">\n  <!-- input string -->\n  /pets/{petId}\n  <node name=\"path-template\" index=\"0\" length=\"13\">\n    /pets/{petId}\n    <node name=\"slash\" index=\"0\" length=\"1\">\n      /\n    </node><!-- name=\"slash\" -->\n    <node name=\"path-literal\" index=\"1\" length=\"4\">\n      pets\n    </node><!-- name=\"path-literal\" -->\n    <node name=\"slash\" index=\"5\" length=\"1\">\n      /\n    </node><!-- name=\"slash\" -->\n    <node name=\"template-expression\" index=\"6\" length=\"7\">\n      {petId}\n      <node name=\"template-expression-param-name\" index=\"7\" length=\"5\">\n        petId\n      </node><!-- name=\"template-expression-param-name\" -->\n    </node><!-- name=\"template-expression\" -->\n  </node><!-- name=\"path-template\" -->\n</root>\n```\n\n> NOTE: AST can also be traversed in classical way using [depth first traversal](https://www.tutorialspoint.com/data_structures_algorithms/depth_first_traversal.htm). For more information about this option please refer to [apg-js](https://github.com/ldthomas/apg-js) and [apg-js-examples](https://github.com/ldthomas/apg-js-examples).\n\n#### Validation\n\nValidating a Path Templating is as simple as importing the **test** function and calling it.\n\n\n```js\nimport { test } from 'openapi-path-templating';\n\ntest('/pets/{petId}'); // => true\ntest('/a{petId}'); // => true\ntest('/pets'); // => true\ntest('/pets', { strict: true }); // => false (doesn't contain any template-expression)\n```\n\n#### Resolution\n\nResolving a Path Templating is as simple as importing the **resolve** function and calling it.\n\n```js\nimport { resolve } from 'openapi-path-templating';\n\nresolve('/pets/{petId}', { petId: 3 }); // => \"/pets/3\"\n```\n\nResolved Path Templating is automatically encoded using [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) function.\nIt is possible to provide a custom encoder.\n\n```js\nimport { resolve } from 'openapi-path-templating';\n\nresolve('/pets/{petId}', { petId: '/?#' }, {\n  encoder: (component) => component, // no encoding\n}); // => \"/pets//?#\"\n```\n\n#### Matching\n\nPath templating matching in OpenAPI prioritizes concrete paths over parameterized ones,\ntreats paths with identical structures but different parameter names as invalid,\nand considers paths with overlapping patterns that could match the same request as potentially ambiguous.\n\n##### Predicates\n\n`isIdentical`\n\nDetermines whether two path templates are structurally identical, meaning they have the same sequence\nof literals and template expressions, regardless of template expression names. In the OpenAPI context,\nsuch identical paths are considered invalid due to potential conflicts in routing.\n\n```js\nimport { isIdentical } from 'openapi-path-templating';\n\nisIdentical('/pets/{petId}', '/pets/{name}'); // => true\nisIdentical('/pets/{petId}', '/animals/{name}'); // => false\n```\n\n#### Grammar\n\nNew grammar instance can be created in following way:\n\n```js\nimport { Grammar } from 'openapi-path-templating';\n\nconst grammar = new Grammar();\n```\n\nTo obtain original ABNF (SABNF) grammar as a string:\n\n```js\nimport { Grammar } from 'openapi-path-templating';\n\nconst grammar = new Grammar();\n\ngrammar.toString();\n// or\nString(grammar);\n```\n\n## More about OpenAPI Path Templating\n\nThe Path Templating is defined by the following [ABNF](https://tools.ietf.org/html/rfc5234) syntax\n\n```abnf\n; OpenAPI Path Templating ABNF syntax\n; variant of https://datatracker.ietf.org/doc/html/rfc3986#section-3.3\npath-template                  = slash *( path-segment slash ) [ path-segment ]\npath-segment                   = 1*( path-literal / template-expression )\nslash                          = \"/\"\npath-literal                   = 1*pchar\ntemplate-expression            = \"{\" template-expression-param-name \"}\"\ntemplate-expression-param-name = 1*( %x00-7A / %x7C / %x7E-10FFFF ) ; every UTF8 character except { and } (from OpenAPI)\n\n; https://datatracker.ietf.org/doc/html/rfc3986#section-3.3\npchar               = unreserved / pct-encoded / sub-delims / \":\" / \"@\"\nunreserved          = ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\"\n                    ; https://datatracker.ietf.org/doc/html/rfc3986#section-2.3\npct-encoded         = \"%\" HEXDIG HEXDIG\n                    ; https://datatracker.ietf.org/doc/html/rfc3986#section-2.1\nsub-delims          = \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\"\n                    / \"*\" / \"+\" / \",\" / \";\" / \"=\"\n                    ; https://datatracker.ietf.org/doc/html/rfc3986#section-2.2\n\n; https://datatracker.ietf.org/doc/html/rfc5234#appendix-B.1\nALPHA               = %x41-5A / %x61-7A   ; A-Z / a-z\nDIGIT               = %x30-39            ; 0-9\nHEXDIG              = DIGIT / \"A\" / \"B\" / \"C\" / \"D\" / \"E\" / \"F\"\n```\n\n## License\n\n`openapi-path-templating` is licensed under [Apache 2.0 license](https://github.com/swaggerexpert/openapi-path-templating/blob/main/LICENSE).\n`openapi-path-templating` comes with an explicit [NOTICE](https://github.com/swaggerexpert/openapi-path-templating/blob/main/NOTICE) file\ncontaining additional legal notices and information.\n","publish_time":1735656823970,"maintainers":[{"name":"char0n","email":"vladimir.gorej@gmail.com"}],"_cnpm_publish_time":1735656823970,"dist-tags":{"latest":"2.3.0"}}