slash-command-parser.ts 743 B

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