aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/http/resolvePaste.js
blob: 8d40cbe14bb3843d6a94728b7d6c264b82e023ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {useEffect, useState} from 'react'
import {fetchPaste, fmtDateStr} from './shared'
import {LANGS} from "../components/renderers/Code";

const resolvePaste = (id, password = "") => {
  const response = {
    data: {
      title: '',
      content: '',
      language: LANGS.detect,
      expiry: '',
    },
    unauthorized: false,
    error: '',
  }
  return fetchPaste(id, password)
    .then(resp => {
      const data = resp.data
      response.data = {
        ...data,
        expiry: fmtDateStr(data.expiry)
      }
      return response
    })
    .catch(error => {
      const resp = error.response
      if (!resp) {
        response.error = 'network error'
        return
      }

      if (resp.status === 401) {
        response.error = 'unauthorized'
        response.unauthorized = true
        return
      }

      response.error = `${resp.status}: ${resp.data}`
      return response
    })
}

export default resolvePaste