slash-command-parser.ts 717 B

12345678910111213141516171819202122
  1. import { GrowiCommand } from '../interfaces/growi-command';
  2. import { InvalidGrowiCommandError } from '../models/errors';
  3. export const parseSlashCommand = (slashCommand:{[key:string]:string}): GrowiCommand => {
  4. if (slashCommand.text == null) {
  5. throw new InvalidGrowiCommandError('The SlashCommand.text is null');
  6. }
  7. const trimmedText = slashCommand.text.trim();
  8. const splitted = trimmedText.split(' ');
  9. if (splitted[0] === '') {
  10. throw new InvalidGrowiCommandError('The SlashCommand.text does not specify GrowiCommand type');
  11. }
  12. return {
  13. text: slashCommand.text,
  14. responseUrl: slashCommand.response_url,
  15. growiCommandType: splitted[0],
  16. growiCommandArgs: splitted.slice(1),
  17. };
  18. };