-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON field transformation (#163)
This PR allows to use JSONPath transformation on fields. JSONPath system (quite similar to jq) is explained here: https://github.com/JSONPath-Plus/JSONPath#syntax-through-examples
- Loading branch information
Showing
4 changed files
with
32 additions
and
14 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
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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import { JSONPath } from 'jsonpath-plus'; | ||
import { authenticator } from 'otplib'; | ||
|
||
export const transformOtp = (secret: string) => { | ||
return authenticator.generate(secret); | ||
}; | ||
|
||
export const transformJsonPath = (json: string, path: string) => { | ||
const result = JSONPath<unknown>({ path, json: JSON.parse(json) as object, wrap: false }); | ||
if (result === undefined) { | ||
throw new Error(`No matching json path found for "${path}"`); | ||
} | ||
|
||
if (typeof result === 'string') { | ||
return result; | ||
} | ||
return JSON.stringify(result); | ||
}; |
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