generated from imranbarbhuiya/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47c3e60
commit ea0cfe1
Showing
5 changed files
with
56 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { SafeObjectTransformer } from '../Transformer'; | ||
import type { IParser } from '../interfaces'; | ||
import type { Context } from '../Interpreter'; | ||
import { BaseParser } from './Base'; | ||
|
||
/** | ||
* JSON is useful when using fetch. You can get all the properties of a JSON object using parameters. | ||
* @usage | ||
* ```yaml | ||
* {json(name):value} | ||
* ``` | ||
* @example | ||
* ```yaml | ||
* {json(data):{"name": "John Doe", "age": 30}} | ||
* Your age is `{data.age}`. | ||
* ``` | ||
*/ | ||
export class JSONVarParser extends BaseParser implements IParser { | ||
public constructor() { | ||
super(['json'], true, true); | ||
} | ||
|
||
public parse(ctx: Context) { | ||
ctx.response.variables[ctx.tag.parameter!] = new SafeObjectTransformer(ctx.tag.payload!); | ||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Interpreter, JSONVarParser, Response, SafeObjectTransformer, StrictVarsParser } from '../../src'; | ||
|
||
describe('JSONVar', () => { | ||
test('GIVEN a JSON in json var THEN store it as an object and show results using the parameter', async () => { | ||
const ts = new Interpreter(new JSONVarParser(), new StrictVarsParser()); | ||
|
||
const text = '{json(data):{"name": "John Doe", "age": 30}}'; | ||
|
||
expect(await ts.run(text)).toStrictEqual( | ||
new Response({ | ||
data: new SafeObjectTransformer('{"name": "John Doe", "age": 30}') | ||
}).setValues('', text) | ||
); | ||
|
||
const text1 = `${text}{data.name}`; | ||
|
||
expect(await ts.run(text1)).toStrictEqual( | ||
new Response({ | ||
data: new SafeObjectTransformer('{"name": "John Doe", "age": 30}') | ||
}).setValues('John Doe', text1) | ||
); | ||
}); | ||
}); |