aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/http/resolvePaste.js
blob: aa4f8b6a6e6697e1c5df54b95fd1d77faf836e6f (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
44
45
46
import {useEffect, useState} from 'react'
import {fetchPaste, fmtDateStr} from './shared'
import {LANGS} from "../components/renderers/Code";

export const defaultResponse = {
  data: {
    title: '',
    content: '',
    language: LANGS.detect,
    expiry: '',
  },
  unauthorized: false,
  error: '',
}

const resolvePaste = async (id, password = "") => {
  const response = {...defaultResponse}
  try {
    return await fetchPaste(id, password)
      .then(resp => {
        const data = resp.data
        response.data = {
          ...data,
          expiry: fmtDateStr(data.expiry)
        }
        return response
      })
  } catch (err) {
    const resp = err.response
    if (!resp) {
      response.error = 'network error'
      return response
    }

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

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

export default resolvePaste