{"name":"openapi-path-templating","version":"1.4.0","keywords":["openapi","path","templating","parser","validator","resolver"],"author":{"name":"Vladimír Gorej","email":"vladimir.gorej@gmail.com"},"license":"Apache-2.0","_id":"openapi-path-templating@1.4.0","maintainers":[{"name":"char0n","email":"vladimir.gorej@gmail.com"}],"homepage":"https://github.com/char0n/openapi-path-templating#readme","bugs":{"url":"https://github.com/char0n/openapi-path-templating/issues"},"dist":{"shasum":"c52a9d9bd0c350a16cd6acd7e6b7f5a7b5037c9d","tarball":"http://registry-npm.ywwl.com/openapi-path-templating/download/openapi-path-templating-1.4.0.tgz","fileCount":39,"integrity":"sha512-Yves8IYEtqwhvjoMQx3LFjnA69sh/r61r2yJlEUmPlhCemh2EVuTPDdwfNOqbHV8Fl1hg1qaLWQ4xzCMIX7wPw==","signatures":[{"sig":"MEYCIQCqc2jYIJFQzWXEuuqToC4CIxl2OLVUHOF1TPpD7/yBvgIhAPCVmHC1f9/Pib84JjZTsig22aMg72HK8Vijq/xeJxVw","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":110134,"size":23740,"noattachment":false,"key":"/openapi-path-templating/-/openapi-path-templating-1.4.0.tgz"},"type":"module","types":"./types/index.d.ts","engines":{"node":">=14.16"},"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"},"gitHead":"0af62f62de4fc3016bd107451e38a4b322aff522","imports":{"#apg-lite":{"import":"apg-lite","default":"apg-lite","require":"./cjs/apg-lite.cjs"}},"scripts":{"test":"mocha","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'","prepublishOnly":"npm run build","grammar:compile":"./node_modules/apg-js/bin/apg.sh --lite --in=./src/path-templating.bnf --out=./src/path-templating.js && cd ./src","build:cjs:apg-lite":"cross-env BABEL_ENV=cjs babel node_modules/apg-lite/lib/parser.js --out-file ./cjs/apg-lite.cjs"},"_npmUser":{"name":"char0n","email":"vladimir.gorej@gmail.com"},"repository":{"url":"git+https://github.com/char0n/openapi-path-templating.git","type":"git"},"_npmVersion":"9.8.0","description":"OpenAPI Path Templating parser, validator and resolver.","directories":{},"_nodeVersion":"20.5.0","dependencies":{"apg-lite":"^1.0.2","prettier":"^3.1.1"},"_hasShrinkwrap":false,"devDependencies":{"chai":"=5.0.0","husky":"=8.0.3","mocha":"=10.2.0","apg-js":"^4.3.0","cross-env":"^7.0.3","@babel/cli":"=7.23.4","@babel/core":"=7.23.7","@commitlint/cli":"=18.4.3","@babel/preset-env":"=7.23.7","@commitlint/config-conventional":"=18.4.3"},"_npmOperationalInternal":{"tmp":"tmp/openapi-path-templating_1.4.0_1704196774992_0.23005103842365182","host":"s3://npm-registry-packages"},"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[Path Templating](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathTemplating) allow defining values based on information that will only be available within the HTTP message in an actual API call.\nThis mechanism is used by [Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#paths-object)\nof [OpenAPI specification](https://github.com/OAI/OpenAPI-Specification).\n\n`openapi-path-templating` is a **parser**, **validator** and **resolver** for OpenAPI Path Templating. It supports\nPath 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://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#pathTemplating)\n- [OpenAPI 3.0.1](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.1.md#pathTemplating)\n- [OpenAPI 3.0.2](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#pathTemplating)\n- [OpenAPI 3.0.3](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#pathTemplating)\n- [OpenAPI 3.1.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathTemplating)\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    - [Extraction](#extraction)\n    - [Parsing](#parsing)\n    - [Validation](#validation)\n    - [Resolution](#resolution)\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** and **resolution**.\nBoth parser and validator are based on a superset of [ABNF](https://www.rfc-editor.org/rfc/rfc5234) ([SABNF](https://cs.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: 18,\n    nodeHits: 324\n  },\n  ast: fnast {\n    callbacks: [\n      'path-template': [Function: pathTemplate],\n      path: [Function: path],\n      query: [Function: query],\n      'query-marker': [Function: queryMarker],\n      fragment: [Function: fragment],\n      'fragment-marker': [Function: fragmentMarker],\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  [ 'path', '/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=\"7\" characters=\"13\">\n  <!-- input string -->\n  /pets/{petId}\n  <node name=\"path-template\" index=\"0\" length=\"13\">\n    /pets/{petId}\n    <node name=\"path\" 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\" -->\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#### 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\npath-template                  = path [ query-marker query ] [ fragment-marker fragment ]\npath                           = slash *( path-segment slash ) [ path-segment ]\npath-segment                   = 1*( path-literal / template-expression )\nquery                          = *( query-literal )\nquery-literal                  = 1*( unreserved / pct-encoded / sub-delims / \":\" / \"@\" / \"/\" / \"?\" / \"&\" / \"=\" )\nquery-marker                   = \"?\"\nfragment                       = *( fragment-literal )\nfragment-literal               = 1*( unreserved / pct-encoded / sub-delims / \":\" / \"@\" / \"/\" / \"?\" )\nfragment-marker                = \"#\"\nslash                          = \"/\"\npath-literal                   = 1*( unreserved / pct-encoded / sub-delims / \":\" / \"@\" )\ntemplate-expression            = \"{\" template-expression-param-name \"}\"\ntemplate-expression-param-name = 1*( unreserved / pct-encoded / sub-delims / \":\" / \"@\" )\n\n; Characters definitions (from RFC 3986)\nunreserved          = ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\"\npct-encoded         = \"%\" HEXDIG HEXDIG\nsub-delims          = \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\"\n                    / \"*\" / \"+\" / \",\" / \";\" / \"=\"\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":1704196775159,"_cnpm_publish_time":1704196775159,"dist-tags":{"latest":"2.3.0"}}