Skip to content

Commit

Permalink
fix: update createUpload to accept environment id in its params
Browse files Browse the repository at this point in the history
  • Loading branch information
ryunsong-contentful committed Nov 8, 2023
1 parent 1dca868 commit 15a9896
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
29 changes: 20 additions & 9 deletions lib/adapters/REST/endpoints/upload.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import type { AxiosInstance } from 'contentful-sdk-core'
import { Stream } from 'stream'
import { GetSpaceParams } from '../../../common-types'
import { GetSpaceEnvironmentParams, GetSpaceParams } from '../../../common-types'
import { getUploadHttpClient } from '../../../upload-http-client'
import { RestEndpoint } from '../types'
import * as raw from './raw'

const getUploadUrlPath = (params: GetSpaceEnvironmentParams & { uploadId?: string }) => {
const spacePath = `/spaces/${params.spaceId}/uploads`
const environmentPath = `/spaces/${params.spaceId}/environments/${params.environmentId}/uploads`
const path = params.environmentId ? environmentPath : spacePath

// Return path as is if uploadId is not provided, otherwise append it to the path
if (!params.uploadId) return path
return path + `/${params.uploadId}`
}

export const create: RestEndpoint<'Upload', 'create'> = (
http: AxiosInstance,
params: GetSpaceParams,
params: GetSpaceEnvironmentParams,
data: { file: string | ArrayBuffer | Stream }
) => {
const httpUpload = getUploadHttpClient(http)
Expand All @@ -16,7 +26,8 @@ export const create: RestEndpoint<'Upload', 'create'> = (
if (!file) {
return Promise.reject(new Error('Unable to locate a file to upload.'))
}
return raw.post(httpUpload, `/spaces/${params.spaceId}/uploads`, file, {
const path = getUploadUrlPath(params)
return raw.post(httpUpload, path, file, {
headers: {
'Content-Type': 'application/octet-stream',
},
Expand All @@ -25,18 +36,18 @@ export const create: RestEndpoint<'Upload', 'create'> = (

export const del: RestEndpoint<'Upload', 'delete'> = (
http: AxiosInstance,
params: GetSpaceParams & { uploadId: string }
params: GetSpaceEnvironmentParams & { uploadId: string }
) => {
const httpUpload = getUploadHttpClient(http)

return raw.del(httpUpload, `/spaces/${params.spaceId}/uploads/${params.uploadId}`)
const path = getUploadUrlPath(params)
return raw.del(httpUpload, path)
}

export const get: RestEndpoint<'Upload', 'get'> = (
http: AxiosInstance,
params: GetSpaceParams & { uploadId: string }
params: GetSpaceEnvironmentParams & { uploadId: string }
) => {
const httpUpload = getUploadHttpClient(http)

return raw.get(httpUpload, `/spaces/${params.spaceId}/uploads/${params.uploadId}`)
const path = getUploadUrlPath(params)
return raw.get(httpUpload, path)
}
6 changes: 3 additions & 3 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1663,13 +1663,13 @@ export type MRActions = {
update: { params: GetUIConfigParams; payload: UIConfigProps; return: UIConfigProps }
}
Upload: {
get: { params: GetSpaceParams & { uploadId: string }; return: any }
get: { params: GetSpaceEnvironmentParams & { uploadId: string }; return: any }
create: {
params: GetSpaceParams
params: GetSpaceEnvironmentParams
payload: { file: string | ArrayBuffer | Stream }
return: any
}
delete: { params: GetSpaceParams & { uploadId: string }; return: any }
delete: { params: GetSpaceEnvironmentParams & { uploadId: string }; return: any }
}
Usage: {
getManyForSpace: {
Expand Down
1 change: 1 addition & 0 deletions lib/create-environment-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
action: 'create',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
payload: data,
}).then((data) => wrapUpload(makeRequest, data))
Expand Down
29 changes: 28 additions & 1 deletion test/unit/adapters/REST/endpoints/upload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,34 @@ function setup(promise, params = {}) {
}

describe('Rest Upload', async () => {
test('API call createUpload', async () => {
test('API call createUpload with envId', async () => {
const { adapterMock, httpMock } = setup(Promise.resolve({}))

return adapterMock
.makeRequest({
entityType: 'Upload',
action: 'create',
params: {
spaceId: 'id',
environmentId: 'envId',
},
payload: {
contentType: 'image/svg',
fileName: 'filename.svg',
file: '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>',
},
})
.then(() => {
expect(httpMock.post.args[0][0]).equals('/spaces/id/environments/envId/uploads')
expect(httpMock.post.args[0][2].headers['Content-Type']).equals('application/octet-stream')
expect(httpMock.post.args[0][1]).equals(
'<svg><path fill="red" d="M50 50h150v50H50z"/></svg>',
'uploads file to upload endpoint'
)
})
})

test('API call createUpload without envId', async () => {
const { adapterMock, httpMock } = setup(Promise.resolve({}))

return adapterMock
Expand Down

0 comments on commit 15a9896

Please sign in to comment.