@autonomys/auto-drive 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +22 -25
  2. package/dist/api/calls/download.d.ts +3 -3
  3. package/dist/api/calls/download.d.ts.map +1 -1
  4. package/dist/api/calls/index.d.ts +26 -19
  5. package/dist/api/calls/index.d.ts.map +1 -1
  6. package/dist/api/calls/read.d.ts +35 -18
  7. package/dist/api/calls/read.d.ts.map +1 -1
  8. package/dist/api/calls/read.js +39 -9
  9. package/dist/api/calls/upload.d.ts +11 -11
  10. package/dist/api/calls/upload.d.ts.map +1 -1
  11. package/dist/api/calls/upload.js +5 -5
  12. package/dist/api/calls/write.d.ts +9 -9
  13. package/dist/api/calls/write.d.ts.map +1 -1
  14. package/dist/api/calls/write.js +4 -4
  15. package/dist/api/connection.d.ts +2 -24
  16. package/dist/api/connection.d.ts.map +1 -1
  17. package/dist/api/connection.js +6 -30
  18. package/dist/api/handler.d.ts +3 -0
  19. package/dist/api/handler.d.ts.map +1 -0
  20. package/dist/api/handler.js +30 -0
  21. package/dist/api/index.d.ts +2 -0
  22. package/dist/api/index.d.ts.map +1 -1
  23. package/dist/api/index.js +2 -0
  24. package/dist/api/models/objects.d.ts +4 -0
  25. package/dist/api/models/objects.d.ts.map +1 -1
  26. package/dist/api/type.d.ts +172 -0
  27. package/dist/api/type.d.ts.map +1 -0
  28. package/dist/api/type.js +8 -0
  29. package/dist/api/types.d.ts +172 -0
  30. package/dist/api/types.d.ts.map +1 -0
  31. package/dist/api/types.js +8 -0
  32. package/dist/api/wrappers.d.ts +2 -115
  33. package/dist/api/wrappers.d.ts.map +1 -1
  34. package/dist/api/wrappers.js +178 -240
  35. package/dist/fs/wrappers.d.ts +1 -2
  36. package/dist/fs/wrappers.d.ts.map +1 -1
  37. package/dist/fs/wrappers.js +18 -19
  38. package/dist/node.d.ts +2 -2
  39. package/dist/node.d.ts.map +1 -1
  40. package/dist/node.js +25 -2
  41. package/package.json +4 -4
  42. package/src/api/calls/download.ts +3 -3
  43. package/src/api/calls/read.ts +61 -18
  44. package/src/api/calls/upload.ts +11 -11
  45. package/src/api/calls/write.ts +10 -10
  46. package/src/api/connection.ts +6 -61
  47. package/src/api/handler.ts +34 -0
  48. package/src/api/index.ts +2 -0
  49. package/src/api/models/objects.ts +5 -0
  50. package/src/api/types.ts +201 -0
  51. package/src/api/wrappers.ts +247 -300
  52. package/src/fs/wrappers.ts +11 -14
  53. package/src/node.ts +2 -2
@@ -1,8 +1,8 @@
1
1
  import { ArgsWithoutPagination } from '../../utils/types'
2
- import { AutoDriveApi } from '../connection'
2
+ import { AutoDriveApiHandler } from '../types'
3
3
 
4
4
  export const downloadObject = async (
5
- api: AutoDriveApi,
5
+ api: AutoDriveApiHandler,
6
6
  query: ArgsWithoutPagination<{ cid: string }>,
7
7
  ): Promise<ReadableStream<Uint8Array>> => {
8
8
  const response = await api.sendRequest(`/objects/${query.cid}/download`, {
@@ -20,6 +20,6 @@ export const downloadObject = async (
20
20
  return response.body
21
21
  }
22
22
 
23
- export const publicDownloadUrl = (api: AutoDriveApi, cid: string): string => {
23
+ export const publicDownloadUrl = (api: AutoDriveApiHandler, cid: string): string => {
24
24
  return `${api.baseUrl}/objects/${cid}/public`
25
25
  }
@@ -1,19 +1,19 @@
1
1
  import { ArgsWithoutPagination, ArgsWithPagination } from '../../utils/types'
2
- import { AutoDriveApi } from '../connection'
3
2
  import { PaginatedResult } from '../models/common'
4
- import { ObjectInformation, ObjectSummary, Scope } from '../models/objects'
3
+ import { ObjectInformation, ObjectSearchResult, ObjectSummary, Scope } from '../models/objects'
5
4
  import { UserInfo } from '../models/user'
5
+ import { AutoDriveApiHandler } from '../types'
6
6
 
7
7
  /**
8
8
  * Retrieves the root objects based on the specified scope.
9
9
  *
10
- * @param {AutoDriveApi} api - The API instance used to send requests.
10
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
11
11
  * @param {ArgsWithPagination<{ scope: Scope }>} query - The query parameters including scope, limit, and offset.
12
12
  * @returns {Promise<ObjectSummary[]>} - A promise that resolves to an array of ObjectSummary representing the root objects.
13
13
  * @throws {Error} - Throws an error if the request fails.
14
14
  */
15
15
  export const getRoots = async (
16
- api: AutoDriveApi,
16
+ api: AutoDriveApiHandler,
17
17
  query: ArgsWithPagination<{ scope: Scope }>,
18
18
  ): Promise<PaginatedResult<ObjectSummary>> => {
19
19
  const response = await api.sendRequest(
@@ -36,13 +36,13 @@ export const getRoots = async (
36
36
  * This method sends a request to the server to fetch a list of objects
37
37
  * that are shared with the user, based on the specified pagination parameters.
38
38
  *
39
- * @param {AutoDriveApi} api - The API instance used to send requests.
39
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
40
40
  * @param {ArgsWithPagination} query - The query parameters including limit and offset for pagination.
41
41
  * @returns {Promise<ObjectSummary[]>} - A promise that resolves to an array of ObjectSummary representing the shared objects.
42
42
  * @throws {Error} - Throws an error if the request fails.
43
43
  */
44
44
  export const getSharedWithMe = async (
45
- api: AutoDriveApi,
45
+ api: AutoDriveApiHandler,
46
46
  query: ArgsWithPagination,
47
47
  ): Promise<PaginatedResult<ObjectSummary>> => {
48
48
  const response = await api.sendRequest(
@@ -59,19 +59,37 @@ export const getSharedWithMe = async (
59
59
  return response.json()
60
60
  }
61
61
 
62
+ export const searchByNameOrCID = async (
63
+ api: AutoDriveApiHandler,
64
+ query: ArgsWithoutPagination<{ value: string; scope: Scope }>,
65
+ ): Promise<ObjectSearchResult[]> => {
66
+ const response = await api.sendRequest(
67
+ `/objects/search?cid=${encodeURIComponent(query.value)}&scope=${query.scope}`,
68
+ {
69
+ method: 'GET',
70
+ },
71
+ )
72
+
73
+ if (!response.ok) {
74
+ throw new Error(`Failed to search by name or CID: ${response.statusText}`)
75
+ }
76
+
77
+ return response.json()
78
+ }
79
+
62
80
  /**
63
81
  * Retrieves the objects that have been marked as deleted.
64
82
  *
65
83
  * This method sends a request to the server to fetch a list of objects
66
84
  * that have been deleted, based on the specified pagination parameters.
67
85
  *
68
- * @param {AutoDriveApi} api - The API instance used to send requests.
86
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
69
87
  * @param {ArgsWithPagination} query - The query parameters including limit and offset for pagination.
70
88
  * @returns {Promise<ObjectSummary[]>} - A promise that resolves to an array of ObjectSummary representing the deleted objects.
71
89
  * @throws {Error} - Throws an error if the request fails.
72
90
  */
73
91
  export const getDeleted = async (
74
- api: AutoDriveApi,
92
+ api: AutoDriveApiHandler,
75
93
  query: ArgsWithPagination,
76
94
  ): Promise<PaginatedResult<ObjectSummary>> => {
77
95
  const response = await api.sendRequest(
@@ -94,13 +112,13 @@ export const getDeleted = async (
94
112
  * This method sends a request to the server to fetch details about the
95
113
  * object, including its metadata and other relevant information.
96
114
  *
97
- * @param {AutoDriveApi} api - The API instance used to send requests.
115
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
98
116
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object to retrieve.
99
117
  * @returns {Promise<ObjectInformation>} - A promise that resolves to the information of the requested object.
100
118
  * @throws {Error} - Throws an error if the request fails.
101
119
  */
102
120
  export const getObject = async (
103
- api: AutoDriveApi,
121
+ api: AutoDriveApiHandler,
104
122
  query: ArgsWithoutPagination<{ cid: string }>,
105
123
  ): Promise<ObjectInformation> => {
106
124
  const response = await api.sendRequest(`/objects/${query.cid}`, {
@@ -114,6 +132,31 @@ export const getObject = async (
114
132
  return response.json()
115
133
  }
116
134
 
135
+ /**
136
+ * Retrieves the summary of a specific object identified by its CID.
137
+ *
138
+ * This method sends a request to the server to fetch the summary of the object.
139
+ *
140
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
141
+ * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object whose upload status is to be retrieved.
142
+ * @returns {Promise<ObjectSummary>} - A promise that resolves to the summary of the requested object.
143
+ * @throws {Error} - Throws an error if the request fails.
144
+ */
145
+ export const getObjectSummary = async (
146
+ api: AutoDriveApiHandler,
147
+ query: ArgsWithoutPagination<{ cid: string }>,
148
+ ): Promise<ObjectSummary> => {
149
+ const response = await api.sendRequest(`/objects/${query.cid}/summary`, {
150
+ method: 'GET',
151
+ })
152
+
153
+ if (!response.ok) {
154
+ throw new Error(`Failed to get object summary: ${response.statusText}`)
155
+ }
156
+
157
+ return response.json()
158
+ }
159
+
117
160
  /**
118
161
  * Retrieves the upload status of a specific object identified by its CID.
119
162
  *
@@ -121,13 +164,13 @@ export const getObject = async (
121
164
  * of the object, which can indicate whether the upload is pending, completed,
122
165
  * or failed.
123
166
  *
124
- * @param {AutoDriveApi} api - The API instance used to send requests.
167
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
125
168
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object whose upload status is to be retrieved.
126
169
  * @returns {Promise<ObjectInformation['uploadStatus']>} - A promise that resolves to the upload status of the requested object.
127
170
  * @throws {Error} - Throws an error if the request fails.
128
171
  */
129
172
  export const getObjectUploadStatus = async (
130
- api: AutoDriveApi,
173
+ api: AutoDriveApiHandler,
131
174
  query: ArgsWithoutPagination<{ cid: string }>,
132
175
  ): Promise<ObjectInformation['uploadStatus']> => {
133
176
  const response = await api.sendRequest(`/objects/${query.cid}/status`, {
@@ -148,13 +191,13 @@ export const getObjectUploadStatus = async (
148
191
  * associated with the object. The owners can provide insights into who
149
192
  * has access to or control over the object.
150
193
  *
151
- * @param {AutoDriveApi} api - The API instance used to send requests.
194
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
152
195
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object whose owners are to be retrieved.
153
196
  * @returns {Promise<ObjectInformation['owners']>} - A promise that resolves to the list of owners of the requested object.
154
197
  * @throws {Error} - Throws an error if the request fails.
155
198
  */
156
199
  export const getObjectOwners = async (
157
- api: AutoDriveApi,
200
+ api: AutoDriveApiHandler,
158
201
  query: ArgsWithoutPagination<{ cid: string }>,
159
202
  ): Promise<ObjectInformation['owners']> => {
160
203
  const response = await api.sendRequest(`/objects/${query.cid}/owners`, {
@@ -175,13 +218,13 @@ export const getObjectOwners = async (
175
218
  * with the object. The metadata can include various details about the object,
176
219
  * such as its name, type, size, and other relevant information.
177
220
  *
178
- * @param {AutoDriveApi} api - The API instance used to send requests.
221
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
179
222
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object whose metadata is to be retrieved.
180
223
  * @returns {Promise<ObjectInformation['metadata']>} - A promise that resolves to the metadata of the requested object.
181
224
  * @throws {Error} - Throws an error if the request fails.
182
225
  */
183
226
  export const getObjectMetadata = async (
184
- api: AutoDriveApi,
227
+ api: AutoDriveApiHandler,
185
228
  query: ArgsWithoutPagination<{ cid: string }>,
186
229
  ): Promise<ObjectInformation['metadata']> => {
187
230
  const response = await api.sendRequest(`/objects/${query.cid}/metadata`, {
@@ -198,11 +241,11 @@ export const getObjectMetadata = async (
198
241
  /**
199
242
  * Get upload and download limits of the user
200
243
  *
201
- * @param {AutoDriveApi} api - The API instance used to send requests.
244
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
202
245
  * @returns {Promise<UserInfo>} - A promise that resolves to the user info.
203
246
  * @throws {Error} - Throws an error if the request fails.
204
247
  */
205
- export const getMe = async (api: AutoDriveApi): Promise<UserInfo> => {
248
+ export const getMe = async (api: AutoDriveApiHandler): Promise<UserInfo> => {
206
249
  const response = await api.sendRequest('@me', {
207
250
  method: 'GET',
208
251
  })
@@ -1,5 +1,4 @@
1
1
  import { ArgsWithoutPagination } from '../../utils/types'
2
- import { AutoDriveApi } from '../connection'
3
2
  import { FolderTree } from '../models/folderTree'
4
3
  import {
5
4
  CompleteUploadResponse,
@@ -7,11 +6,12 @@ import {
7
6
  FileUploadOptions,
8
7
  FolderUpload,
9
8
  } from '../models/uploads'
9
+ import { AutoDriveApiHandler } from '../types'
10
10
 
11
11
  /**
12
12
  * Initiates a file upload to the server.
13
13
  *
14
- * @param {AutoDriveApi} api - The API instance used to send requests.
14
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
15
15
  * @param {ArgsWithoutPagination<{ mimeType?: string; filename: string; uploadOptions: FileUploadOptions | null }>} args - The arguments for the file upload.
16
16
  * @param {string} args.mimeType - The MIME type of the file (optional).
17
17
  * @param {string} args.filename - The name of the file to be uploaded.
@@ -20,7 +20,7 @@ import {
20
20
  * @throws {Error} - Throws an error if the upload fails.
21
21
  */
22
22
  export const createFileUpload = async (
23
- api: AutoDriveApi,
23
+ api: AutoDriveApiHandler,
24
24
  {
25
25
  mimeType,
26
26
  filename,
@@ -52,7 +52,7 @@ export const createFileUpload = async (
52
52
  /**
53
53
  * Initiates a folder upload to the server.
54
54
  *
55
- * @param {AutoDriveApi} api - The API instance used to send requests.
55
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
56
56
  * @param {ArgsWithoutPagination<{ fileTree: FolderTree; uploadOptions: FileUploadOptions }>} args - The arguments for the folder upload.
57
57
  * @param {FolderTree} args.fileTree - The structure of the folder and its contents to be uploaded.
58
58
  * @param {FileUploadOptions} args.uploadOptions - Additional options for the folder upload.
@@ -60,7 +60,7 @@ export const createFileUpload = async (
60
60
  * @throws {Error} - Throws an error if the upload fails.
61
61
  */
62
62
  export const createFolderUpload = async (
63
- api: AutoDriveApi,
63
+ api: AutoDriveApiHandler,
64
64
  {
65
65
  fileTree,
66
66
  uploadOptions = {},
@@ -87,7 +87,7 @@ export const createFolderUpload = async (
87
87
  /**
88
88
  * Creates a file upload within an existing folder upload.
89
89
  *
90
- * @param {AutoDriveApi} api - The API instance used to send requests.
90
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
91
91
  * @param {ArgsWithoutPagination<{ uploadId: string; name: string; mimeType: string; relativeId: string; uploadOptions: FileUploadOptions }>} args - The arguments for the file upload.
92
92
  * @param {string} args.uploadId - The ID of the folder upload to which the file will be added.
93
93
  * @param {string} args.name - The name of the file to be uploaded.
@@ -98,7 +98,7 @@ export const createFolderUpload = async (
98
98
  * @throws {Error} - Throws an error if the upload fails.
99
99
  */
100
100
  export const createFileUploadWithinFolderUpload = async (
101
- api: AutoDriveApi,
101
+ api: AutoDriveApiHandler,
102
102
  {
103
103
  uploadId,
104
104
  name,
@@ -139,7 +139,7 @@ export const createFileUploadWithinFolderUpload = async (
139
139
  * index to the server, which can be used to reconstruct the file on the
140
140
  * server side.
141
141
  *
142
- * @param {AutoDriveApi} api - The API instance used to send requests.
142
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
143
143
  * @param {ArgsWithoutPagination<{ uploadId: string; chunk: Buffer; index: number }>} args - The arguments for the file chunk upload.
144
144
  * @param {string} args.uploadId - The ID of the upload session.
145
145
  * @param {Buffer} args.chunk - The chunk of the file to be uploaded.
@@ -148,7 +148,7 @@ export const createFileUploadWithinFolderUpload = async (
148
148
  * @throws {Error} - Throws an error if the upload fails.
149
149
  */
150
150
  export const uploadFileChunk = async (
151
- api: AutoDriveApi,
151
+ api: AutoDriveApiHandler,
152
152
  {
153
153
  uploadId,
154
154
  chunk,
@@ -180,14 +180,14 @@ export const uploadFileChunk = async (
180
180
  * typically called after all file chunks have been uploaded. This method
181
181
  * can be used to complete both file and folder uploads.
182
182
  *
183
- * @param {AutoDriveApi} api - The API instance used to send requests.
183
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
184
184
  * @param {ArgsWithoutPagination<{ uploadId: string }>} args - The arguments for completing the upload.
185
185
  * @param {string} args.uploadId - The ID of the upload session to complete.
186
186
  * @returns {Promise<any>} - A promise that resolves to the response from the server.
187
187
  * @throws {Error} - Throws an error if the completion of the upload fails.
188
188
  */
189
189
  export const completeUpload = async (
190
- api: AutoDriveApi,
190
+ api: AutoDriveApiHandler,
191
191
  { uploadId }: ArgsWithoutPagination<{ uploadId: string }>,
192
192
  ): Promise<CompleteUploadResponse> => {
193
193
  const response = await api.sendRequest(`/uploads/${uploadId}/complete`, {
@@ -1,5 +1,5 @@
1
- import { ArgsWithoutPagination } from '../../utils/types';
2
- import { AutoDriveApi } from '../connection';
1
+ import { ArgsWithoutPagination } from '../../utils/types'
2
+ import { AutoDriveApiHandler } from '../types'
3
3
 
4
4
  /**
5
5
  * Shares an object with a specified public ID.
@@ -8,13 +8,13 @@ import { AutoDriveApi } from '../connection';
8
8
  * by its CID. The object will be shared with the provided public ID, allowing
9
9
  * access to the object for users with that ID.
10
10
  *
11
- * @param {AutoDriveApi} api - The API instance used to send requests.
11
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
12
12
  * @param {ArgsWithoutPagination<{ cid: string; publicId: string }>} query - The query parameters containing the CID of the object to share and the public ID to share it with.
13
13
  * @returns {Promise<any>} - A promise that resolves to the response from the server.
14
14
  * @throws {Error} - Throws an error if the sharing process fails.
15
15
  */
16
16
  export const shareObject = async (
17
- api: AutoDriveApi,
17
+ api: AutoDriveApiHandler,
18
18
  query: ArgsWithoutPagination<{ cid: string; publicId: string }>,
19
19
  ) => {
20
20
  const response = await api.sendRequest(
@@ -42,13 +42,13 @@ export const shareObject = async (
42
42
  * by its CID as deleted. This action is typically irreversible and should
43
43
  * be used with caution.
44
44
  *
45
- * @param {AutoDriveApi} api - The API instance used to send requests.
45
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
46
46
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object to mark as deleted.
47
47
  * @returns {Promise<void>} - A promise that resolves when the object has been marked as deleted.
48
48
  * @throws {Error} - Throws an error if the marking process fails.
49
49
  */
50
50
  export const markObjectAsDeleted = async (
51
- api: AutoDriveApi,
51
+ api: AutoDriveApiHandler,
52
52
  query: ArgsWithoutPagination<{ cid: string }>,
53
53
  ): Promise<void> => {
54
54
  const response = await api.sendRequest(`/objects/${query.cid}/delete`, {
@@ -67,13 +67,13 @@ export const markObjectAsDeleted = async (
67
67
  * by its CID. The restoration process may depend on the server's implementation
68
68
  * and the object's current state.
69
69
  *
70
- * @param {AutoDriveApi} api - The API instance used to send requests.
70
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
71
71
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object to restore.
72
72
  * @returns {Promise<void>} - A promise that resolves when the object has been successfully restored.
73
73
  * @throws {Error} - Throws an error if the restoration process fails.
74
74
  */
75
75
  export const restoreObject = async (
76
- api: AutoDriveApi,
76
+ api: AutoDriveApiHandler,
77
77
  query: ArgsWithoutPagination<{ cid: string }>,
78
78
  ): Promise<void> => {
79
79
  const response = await api.sendRequest(`/objects/${query.cid}/restore`, {
@@ -94,13 +94,13 @@ export const restoreObject = async (
94
94
  * by its CID. The publication process may depend on the server's implementation
95
95
  * and the object's current state.
96
96
  *
97
- * @param {AutoDriveApi} api - The API instance used to send requests.
97
+ * @param {AutoDriveApiHandler} api - The API instance used to send requests.
98
98
  * @param {ArgsWithoutPagination<{ cid: string }>} query - The query parameters containing the CID of the object to publish.
99
99
  * @returns {Promise<void>} - A promise that resolves when the object has been successfully published.
100
100
  * @throws {Error} - Throws an error if the publication process fails.
101
101
  */
102
102
  export const publishObject = async (
103
- api: AutoDriveApi,
103
+ api: AutoDriveApiHandler,
104
104
  query: ArgsWithoutPagination<{ cid: string }>,
105
105
  ): Promise<{ result: string }> => {
106
106
  const response = await api.sendRequest(`/objects/${query.cid}/publish`, {
@@ -1,63 +1,8 @@
1
- import { NetworkId } from '@autonomys/auto-utils'
2
- import { AutoDriveNetwork, getNetworkUrl, networks } from './networks'
1
+ import { createApiRequestHandler } from './handler'
2
+ import { AutoDriveApi, ConnectionOptions } from './types'
3
+ import { createApiInterface } from './wrappers'
3
4
 
4
- export interface AutoDriveApi {
5
- sendRequest: (
6
- relativeUrl: string,
7
- request: Partial<Request>,
8
- body?: BodyInit,
9
- ) => Promise<Response>
10
- baseUrl: string
11
- }
12
-
13
- export enum OAuthProvider {
14
- GOOGLE = 'google',
15
- DISCORD = 'discord',
16
- }
17
-
18
- export type ApiKeyAuthProvider = 'apikey'
19
- export type AuthProvider = ApiKeyAuthProvider | 'oauth'
20
-
21
- type ConnectionOptions =
22
- | {
23
- provider?: AuthProvider
24
- apiKey?: string
25
- url?: null
26
- network: AutoDriveNetwork
27
- }
28
- | {
29
- provider?: AuthProvider
30
- apiKey?: string
31
- url: string
32
- network?: null
33
- }
34
-
35
- export const createAutoDriveApi = ({
36
- provider = 'apikey',
37
- apiKey,
38
- url = null,
39
- network,
40
- }: ConnectionOptions): AutoDriveApi => {
41
- const baseUrl = !network ? url : getNetworkUrl(network)
42
- if (!baseUrl) {
43
- throw new Error('No base URL provided')
44
- }
45
-
46
- return {
47
- sendRequest: async (relativeUrl: string, request: Partial<Request>, body?: BodyInit) => {
48
- const headers = new Headers({
49
- ...Object.fromEntries(request.headers?.entries() || []),
50
- 'x-auth-provider': provider,
51
- Authorization: `Bearer ${apiKey}`,
52
- })
53
- const fullRequest = {
54
- ...request,
55
- headers: new Headers(headers),
56
- body,
57
- }
58
-
59
- return fetch(`${baseUrl}${relativeUrl}`, fullRequest)
60
- },
61
- baseUrl,
62
- }
5
+ export const createAutoDriveApi = (options: ConnectionOptions): AutoDriveApi => {
6
+ const apiHandler = createApiRequestHandler(options)
7
+ return createApiInterface(apiHandler)
63
8
  }
@@ -0,0 +1,34 @@
1
+ import { getNetworkUrl } from './networks'
2
+ import { AutoDriveApiHandler, ConnectionOptions } from './types'
3
+
4
+ export const createApiRequestHandler = ({
5
+ provider = 'apikey',
6
+ apiKey,
7
+ url = null,
8
+ network,
9
+ }: ConnectionOptions): AutoDriveApiHandler => {
10
+ const baseUrl = !network ? url : getNetworkUrl(network)
11
+ if (!baseUrl) {
12
+ throw new Error('No base URL provided')
13
+ }
14
+
15
+ const api = {
16
+ sendRequest: async (relativeUrl: string, request: Partial<Request>, body?: BodyInit) => {
17
+ const headers = new Headers({
18
+ ...Object.fromEntries(request.headers?.entries() || []),
19
+ 'x-auth-provider': provider,
20
+ Authorization: `Bearer ${apiKey}`,
21
+ })
22
+ const fullRequest = {
23
+ ...request,
24
+ headers: new Headers(headers),
25
+ body,
26
+ }
27
+
28
+ return fetch(`${baseUrl}${relativeUrl}`, fullRequest)
29
+ },
30
+ baseUrl,
31
+ }
32
+
33
+ return api
34
+ }
package/src/api/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './calls/index'
2
2
  export * from './connection'
3
+ export * from './handler'
3
4
  export * from './models/index'
5
+ export * from './types'
4
6
  export * from './wrappers'
@@ -56,6 +56,11 @@ export interface ObjectUploadStatus {
56
56
  maximumBlockDepth: number | null
57
57
  }
58
58
 
59
+ export interface ObjectSearchResult {
60
+ cid: string
61
+ name: string
62
+ }
63
+
59
64
  export type ObjectSummary = {
60
65
  headCid: string
61
66
  name?: string