@neuralegion/nextemplate

Template parser used for Authentication Objects in NexPloit

Usage no npm install needed!

<script type="module">
  import neuralegionNextemplate from 'https://cdn.skypack.dev/@neuralegion/nextemplate';
</script>

README

nextemplate

Template parser used for Authentication Objects in NexPloit

Template syntax

The template syntax is developed to control data coordination between the consequent requests and responses.

The interpolation string use the double curly braces {{ and }} as delimiters.

Two types of interpolation expressions are supported inside those curly braces: step references and random data references.

Step references

Example: {{ step1.response.body }}

The first part is step name (any non-empty alphanumeric), the second part - source ('request' or 'response'), the third - location in the source ('url', 'headers' or 'body').

For data transformation there is | as a pipe operator, which supports parameters and chaining.

Example: {{ step1.response.headers | get: '/Set-Cookie' | match: /sid=(.+)/ : 1 }}

Random data references

Example: {{ $faker.datatype.uuid }}

Inspired by Faker.js.

The first part is predefined $faker literal, the second part - faker dataset name (datatype is single supported atm), the third - function name from given dataset (ony uuid and number are supported).

Supported pipes

get

Returns the value associated with the XPath, or undefined if there is none.

Format: {{ step_reference | get : xpath }}

Parameters:

  • xpath - xpath string

Example: {{ step1.response.headers | get: '/Set-Cookie' }}

match

Retrieves the result of matching a string against a regular expression.

Format: {{ step_reference | match : regexp : group }}

Parameters:

  • regexp - regular expression,
  • group - number of the capture group (optional, default 1)

Example: {{ step1.response.body | match: /sid=(.+)/ : 1 }}

encode

Encodes the value to some format.

Format: {{ step_reference | encode : format }}

Parameters:

  • format - base64, url or none (optional, default none)

Example: {{ step1.response.body | encode: 'base64' }}

Install 🚀

npm i --save @neuralegion/nextemplate

API

Main parser function:

parse(template: string): ParseResult

Auxiliary faker wrapper that consumes fakerTemplate (like datatype.uuid) or ParserFakerExpression from parser output:

fakerFn(fakerTemplate: typeof ParserFakerExpression['template'] | ParserFakerExpression): string

where

type ParseResult = (string | ParsedStepExpression | ParserFakerExpression)[];

interface ParsedStepExpression {
  source: string;
  pipes: ParsedPipe[];
}

interface ParsedPipe {
  name: 'get' | 'match' | 'encode';
  args: (string | number)[];
}

interface ParserFakerExpression {
  source: 'faker';
  template: string;
  sample: string;
}

Sample output

Input template string

prefix {{ name.response.headers | get : '/Set-Cookie' | match : /sid=(.+)/ | encode : 'base64' }} {{ $faker.datatype.uuid }} suffix

Parser output

[
  "prefix ",
  {
    "source": "name.response.headers",
    "pipes": [
      {
        "name": "get",
        "args": [
          "/Set-Cookie"
        ]
      },
      {
        "name": "match",
        "args": [
          "/sid=(.+)/",
          1
        ]
      },
      {
        "name": "encode",
        "args": [
          "base64"
        ]
      }
    ]
  },
  " ",
  {
    "template": "datatype.uuid",
    "sample": "28f830ed-a8ac-40ea-85b1-6dbc8e061541",
    "source": "faker"
  },
  " suffix"
]

fakerFn usage samples

> fakerFn('datatype.uuid')
48b0504d-b146-40f7-8fc2-fe19b7b9dc7b

> fakerFn({ template: 'datatype.uuid', sample: '28f830ed-a8ac-40ea-85b1-6dbc8e061541', source: 'faker' })`
b81b54de-735f-401d-aa77-ebd69d4293c2

Usage

ECMAScript 2015, Typescript modules
import { parse } from '@neuralegion/nextemplate';

console.log(parse('some_template'));
NodeJS (CommonJS module)
const parser = require('@neuralegion/nextemplate');

console.log(parser.parse('some_template'));
NodeJS (experimental ESM support)

usage.mjs file:

import parser from '@neuralegion/nextemplate';

console.log(parser.parse('some_template'));

Running: node --experimental-modules ./usage.mjs

Browser (globals from umd bundle)
<script src="./node_modules/@neuralegion/nextemplate/dist/bundle.umd.js"></script>
<script>
  alert(nextemplate.parse('some_template'));
</script>
Browser (ES modules)
<script type="module">
  import { parse } from './node_modules/@neuralegion/nextemplate/dist/bundle.es.js';
  alert(parse('some_template'));
</script>

Development 🛠

Issues and pull requests are highly welcome. 👍

Please, don't forget to lint (npm run lint) and test (npm t) the code.

License

Copyright © 2021 NeuraLegion.

This project is licensed under the MIT License - see the LICENSE file for details.