-
Notifications
You must be signed in to change notification settings - Fork 97
/
upload.ts
42 lines (36 loc) · 1.27 KB
/
upload.ts
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
import type { AxiosInstance } from 'contentful-sdk-core'
import { Stream } from 'stream'
import { GetSpaceParams } from '../../../common-types'
import { getUploadHttpClient } from '../../../upload-http-client'
import { RestEndpoint } from '../types'
import * as raw from './raw'
export const create: RestEndpoint<'Upload', 'create'> = (
http: AxiosInstance,
params: GetSpaceParams,
data: { file: string | ArrayBuffer | Stream }
) => {
const httpUpload = getUploadHttpClient(http)
const { file } = data
if (!file) {
return Promise.reject(new Error('Unable to locate a file to upload.'))
}
return raw.post(httpUpload, `/spaces/${params.spaceId}/uploads`, file, {
headers: {
'Content-Type': 'application/octet-stream',
},
})
}
export const del: RestEndpoint<'Upload', 'delete'> = (
http: AxiosInstance,
params: GetSpaceParams & { uploadId: string }
) => {
const httpUpload = getUploadHttpClient(http)
return raw.del(httpUpload, `/spaces/${params.spaceId}/uploads/${params.uploadId}`)
}
export const get: RestEndpoint<'Upload', 'get'> = (
http: AxiosInstance,
params: GetSpaceParams & { uploadId: string }
) => {
const httpUpload = getUploadHttpClient(http)
return raw.get(httpUpload, `/spaces/${params.spaceId}/uploads/${params.uploadId}`)
}