backend.ts 773 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { postJSON } from '../../../../infrastructure/fetch-json'
  2. import { Word } from './spellchecker'
  3. const apiUrl = (path: string) => {
  4. return `/spelling${path}`
  5. }
  6. export async function learnWordRequest(word?: Word) {
  7. if (!word || !word.text) {
  8. throw new Error(`Invalid word supplied: ${word}`)
  9. }
  10. return await postJSON(apiUrl('/learn'), {
  11. body: {
  12. word: word.text,
  13. },
  14. })
  15. }
  16. export function spellCheckRequest(
  17. language: string,
  18. words: Word[],
  19. controller: AbortController
  20. ) {
  21. const signal = controller.signal
  22. const textWords = words.map(w => w.text)
  23. return postJSON<{
  24. misspellings: { index: number; suggestions: string[] }[]
  25. }>(apiUrl('/check'), {
  26. body: {
  27. language,
  28. words: textWords,
  29. },
  30. signal,
  31. })
  32. }