• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Gestell-AI / node-sdk / #7

22 Dec 2024 12:25AM UTC coverage: 91.861% (-5.0%) from 96.871%
#7

push

ChrisCates
1.1.21 - Fix Buffer Encoding (document.upload())
Update README Guide Info

27 of 73 new or added lines in 1 file covered. (36.99%)

11 existing lines in 1 file now uncovered.

1535 of 1671 relevant lines covered (91.86%)

24.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.54
/src/document/upload.ts
1
import mime from 'mime-types'
30✔
2
import { createDocument } from '@gestell/document/create'
58✔
3
import { presignDocument } from '@gestell/document/presign'
60✔
4
import type { BaseRequest, BaseResponse } from '@gestell/types/base'
5

6
export interface UploadDocumentRequest {
7
  collectionId: string
8
  name: string
9
  type?: string
10
  file: string | Buffer | File
11
  instructions?: string
12
  job?: boolean
13
}
14

15
export interface UploadDocumentResponse extends BaseResponse {
16
  id: string
17
}
18

19
export async function uploadDocument({
9✔
20
  apiKey,
9✔
21
  apiUrl,
9✔
22
  debug,
8✔
23
  collectionId,
15✔
24
  name,
7✔
25
  file,
7✔
26
  type,
7✔
27
  instructions = '',
20✔
28
  job = true
12✔
29
}: UploadDocumentRequest & BaseRequest): Promise<UploadDocumentResponse> {
2✔
30
  const fileType =
18✔
31
    type ||
9✔
32
    (file instanceof File ? file.type : mime.lookup(file as string)) ||
45✔
33
    'text/plain'
14✔
34

35
  const { status, message, path, url } = await presignDocument({
67✔
36
    apiKey,
11✔
37
    apiUrl,
11✔
38
    debug,
10✔
39
    collectionId,
17✔
40
    type: fileType,
19✔
41
    filename: name
16✔
42
  })
4✔
43

44
  if (status !== 'OK') {
23✔
45
    return {
17✔
46
      status,
13✔
47
      message,
14✔
48
      id: ''
10✔
49
    }
1✔
50
  }
1✔
51

52
  if (typeof file === 'string' || file instanceof Buffer) {
58✔
53
    const { default: fetch } = await import('node-fetch')
58✔
54
    const { readFileSync } = await import('node:fs')
53✔
55
    if (typeof file === 'string') {
34✔
56
      try {
11✔
57
        const upload = await fetch(url, {
50✔
58
          method: 'PUT',
24✔
59
          headers: {
22✔
60
            'Content-Type': fileType || 'application/octet-stream'
64✔
61
          },
12✔
62
          body: readFileSync(file)
32✔
63
        })
4✔
64
        if (!upload.ok) {
22✔
NEW
65
          console.log(await upload.text())
×
NEW
66
          return {
×
NEW
67
            status: 'ERROR',
×
NEW
68
            message:
×
NEW
69
              'Error uploading document, failed to upload to the presigned url',
×
NEW
70
            id: ''
×
NEW
71
          }
×
72
        }
6✔
NEW
73
      } catch {
×
NEW
74
        return {
×
NEW
75
          status: 'ERROR',
×
NEW
76
          message: 'Error uploading document',
×
NEW
77
          id: ''
×
78
        }
4✔
79
      }
80
    } else {
8✔
81
      try {
11✔
82
        const upload = await fetch(url, {
50✔
83
          method: 'PUT',
24✔
84
          headers: {
22✔
85
            'Content-Type': fileType || 'application/octet-stream'
64✔
86
          },
12✔
87
          body: file
18✔
88
        })
4✔
89
        if (!upload.ok) {
22✔
NEW
90
          return {
×
NEW
91
            status: 'ERROR',
×
NEW
92
            message:
×
NEW
93
              'Error uploading document, failed to upload to the presigned url',
×
NEW
94
            id: ''
×
NEW
95
          }
×
96
        }
6✔
NEW
97
      } catch {
×
NEW
98
        return {
×
NEW
99
          status: 'ERROR',
×
NEW
100
          message: 'Error uploading document',
×
NEW
101
          id: ''
×
102
        }
6✔
103
      }
104
    }
NEW
105
  } else if (typeof window !== 'undefined' && file instanceof File) {
×
NEW
106
    try {
×
NEW
107
      const formData = new FormData()
×
NEW
108
      formData.append('file', file)
×
109

NEW
110
      const upload = await fetch(url, {
×
NEW
111
        method: 'PUT',
×
NEW
112
        body: formData as unknown as undefined //This is a workaround. FormData is the correct type, but typescript doesn't know this
×
NEW
113
      })
×
NEW
114
      if (!upload.ok) {
×
NEW
115
        return {
×
NEW
116
          status: 'ERROR',
×
NEW
117
          message:
×
NEW
118
            'Error uploading document, failed to upload to the presigned url',
×
NEW
119
          id: ''
×
NEW
120
        }
×
NEW
121
      }
×
NEW
122
    } catch {
×
NEW
123
      return {
×
NEW
124
        status: 'ERROR',
×
NEW
125
        message: 'Error uploading document',
×
NEW
126
        id: ''
×
NEW
127
      }
×
128
    }
129
  } else {
×
130
    return {
×
131
      status: 'ERROR',
×
132
      message:
×
NEW
133
        'Invalid file type provided, must be a string (path), Buffer (Node) or a File (Client).',
×
134
      id: ''
×
135
    }
2✔
136
  }
137

138
  return (await createDocument({
34✔
139
    apiKey,
11✔
140
    apiUrl,
11✔
141
    debug,
10✔
142
    collectionId,
17✔
143
    name,
9✔
144
    path,
9✔
145
    type: fileType,
19✔
146
    instructions,
17✔
147
    job
5✔
148
  })) as UploadDocumentResponse
2✔
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc