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

Gestell-AI / node-sdk / #11

15 May 2025 11:50PM UTC coverage: 91.743%. Remained the same
#11

push

ChrisCates
v1.4.0 - Refactored Shapes & SDK

- Expose types with clearer shapes
- Add PII controls
- Support single-entry categories
- Update table records & prompt features
- Refactor code & IntelliSense
- Enhance API shape tests
- Cleaned project structure

243 of 244 new or added lines in 19 files covered. (99.59%)

46 existing lines in 6 files now uncovered.

1400 of 1526 relevant lines covered (91.74%)

21.06 hits per line

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

59.2
/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
/**
7
 * Request parameters for uploading a document to a collection.
8
 */
9
export interface UploadDocumentRequest {
10
  /** ID of the collection to upload the document to */
11
  collectionId: string
12
  /** Name of the document */
13
  name: string
14
  /** Optional MIME type; auto-detected if not provided */
15
  type?: string
16
  /** File content as path, Buffer, or File */
17
  file: string | Buffer | File
18
  /** Optional instructions for the upload */
19
  instructions?: string
20
  /** Whether to process upload as a background job */
21
  job?: boolean
22
  /** Whether to include tables in the document */
23
  tables?: boolean
24
}
25

26
/**
27
 * Response data from a document upload operation.
28
 */
29
export interface UploadDocumentResponse extends BaseResponse {
30
  /** ID of the newly uploaded document */
31
  id: string
32
}
33

34
export async function uploadDocument({
9✔
35
  apiKey,
9✔
36
  apiUrl,
9✔
37
  debug,
8✔
38
  collectionId,
15✔
39
  name,
7✔
40
  file,
7✔
41
  type,
7✔
42
  instructions = '',
20✔
43
  job = true,
13✔
44
  tables = false
16✔
45
}: UploadDocumentRequest & BaseRequest): Promise<UploadDocumentResponse> {
3✔
46
  const fileType =
17✔
47
    type ||
9✔
48
    (file instanceof File ? file.type : mime.lookup(file as string)) ||
45✔
49
    'text/plain'
15✔
50

51
  const { status, message, path, url } = await presignDocument({
66✔
52
    apiKey,
11✔
53
    apiUrl,
11✔
54
    debug,
10✔
55
    collectionId,
17✔
56
    type: fileType,
19✔
57
    filename: name
16✔
58
  })
5✔
59

60
  if (status !== 'OK') {
25✔
61
    return {
14✔
62
      status,
13✔
63
      message,
14✔
64
      id: ''
10✔
65
    }
1✔
66
  }
2✔
67

68
  if (typeof file === 'string' || file instanceof Buffer) {
60✔
69
    const { default: fetch } = await import('node-fetch')
58✔
70
    const { readFileSync } = await import('node:fs')
53✔
71
    if (typeof file === 'string') {
36✔
72
      try {
13✔
73
        const upload = await fetch(url, {
43✔
74
          method: 'PUT',
24✔
75
          headers: {
22✔
76
            'Content-Type': fileType || 'application/octet-stream'
64✔
77
          },
12✔
78
          body: readFileSync(file)
32✔
79
        })
11✔
80
        if (!upload.ok) {
15✔
UNCOV
81
          console.log(await upload.text())
×
UNCOV
82
          return {
×
UNCOV
83
            status: 'ERROR',
×
UNCOV
84
            message:
×
UNCOV
85
              'Error uploading document, failed to upload to the presigned url',
×
UNCOV
86
            id: ''
×
UNCOV
87
          }
×
88
        }
6✔
UNCOV
89
      } catch {
×
UNCOV
90
        return {
×
UNCOV
91
          status: 'ERROR',
×
UNCOV
92
          message: 'Error uploading document',
×
UNCOV
93
          id: ''
×
94
        }
4✔
95
      }
96
    } else {
13✔
97
      try {
13✔
98
        const upload = await fetch(url, {
43✔
99
          method: 'PUT',
24✔
100
          headers: {
22✔
101
            'Content-Type': fileType || 'application/octet-stream'
64✔
102
          },
12✔
103
          body: file
18✔
104
        })
11✔
105
        if (!upload.ok) {
15✔
106
          return {
×
107
            status: 'ERROR',
×
UNCOV
108
            message:
×
UNCOV
109
              'Error uploading document, failed to upload to the presigned url',
×
UNCOV
110
            id: ''
×
UNCOV
111
          }
×
112
        }
6✔
UNCOV
113
      } catch {
×
UNCOV
114
        return {
×
UNCOV
115
          status: 'ERROR',
×
UNCOV
116
          message: 'Error uploading document',
×
UNCOV
117
          id: ''
×
118
        }
6✔
119
      }
120
    }
121
  } else if (typeof window !== 'undefined' && file instanceof File) {
×
122
    try {
×
123
      const formData = new FormData()
×
124
      formData.append('file', file)
×
125

UNCOV
126
      const upload = await fetch(url, {
×
127
        method: 'PUT',
×
128
        body: formData as unknown as undefined //This is a workaround. FormData is the correct type, but typescript doesn't know this
×
129
      })
×
130
      if (!upload.ok) {
×
131
        return {
×
UNCOV
132
          status: 'ERROR',
×
UNCOV
133
          message:
×
UNCOV
134
            'Error uploading document, failed to upload to the presigned url',
×
135
          id: ''
×
136
        }
×
137
      }
×
138
    } catch {
×
UNCOV
139
      return {
×
140
        status: 'ERROR',
×
141
        message: 'Error uploading document',
×
142
        id: ''
×
143
      }
×
144
    }
145
  } else {
×
146
    return {
×
147
      status: 'ERROR',
×
148
      message:
×
149
        'Invalid file type provided, must be a string (path), Buffer (Node) or a File (Client).',
×
150
      id: ''
×
151
    }
3✔
152
  }
153

154
  return (await createDocument({
33✔
155
    apiKey,
11✔
156
    apiUrl,
11✔
157
    debug,
10✔
158
    collectionId,
17✔
159
    name,
9✔
160
    path,
9✔
161
    type: fileType,
19✔
162
    instructions,
17✔
163
    job,
8✔
164
    tables
8✔
165
  })) as UploadDocumentResponse
2✔
166
}
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