-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: update createUpload to accept environment id in its params [INTEG-1492] #2030
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No expert on the changes that have been introduced to the upload endpoint, so happy to discuss this further async as well :)
lib/create-environment-api.ts
Outdated
@@ -1152,11 +1152,13 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) { | |||
*/ | |||
createUpload: function createUpload(data: { file: string | ArrayBuffer | Stream }) { | |||
const raw = this.toPlainObject() as EnvironmentProps | |||
const envId = raw.sys.aliasedEnvironment?.sys.id || '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not targeting the public environment id. As far as I understand the code this either:
-
in the case that we are looking at an alias route, using the underlying environment id
-
in the case that no aliases are used, just providing an empty string.
Not sure about all the details of how the upload endpoint has been adjusted, but I am assuming it will always use either the alias-id or the environment-id, but never the environment-id that is being targeted by the alias, not empty string.
I think this is the file that the upload endpoint is properly handled and that would need to be able to handle the environment as part of the route. |
5f77acd
to
e613dc2
Compare
) => { | ||
const httpUpload = getUploadHttpClient(http) | ||
|
||
return raw.get(httpUpload, `/spaces/${params.spaceId}/uploads/${params.uploadId}`) | ||
const spacePath = `/spaces/${params.spaceId}/uploads/${params.uploadId}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the third repetition of the same code and I think it would not be premature to abstract it into a helper function (eg in this file)
15a9896
to
c39d8a2
Compare
ce7488e
to
2b28653
Compare
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand this correctly, this now changes the behavior to also on post
requests use the asset id as part of the route, whereas before we would never use the uploadId on post requests. Is that in accordance with the CEP changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I don't think so. The path for the post route would look like /spaces/${params.spaceId}/uploads
OR like /spaces/${params.spaceId}/environments/${params.environmentId}/uploads
. So no asset ids, or upload ids in the route being added here. This is basically just a refactoring of the path logic since the post doesn't need an upload id but get and deletes do.
The asset itself is passed as a function parameter like this
const path = getUploadUrlPath(params)
return raw.post(httpUpload, path, file, {
headers: {
'Content-Type': 'application/octet-stream',
},
})
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 But then we shouldn't add the uploadId
to the path here, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, const path = getUploadUrlPath(params)
will ensure that the path will contain the upload id for get/delete but not for the create operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. 👍🏻 A bit of a nit but I think I would prefer to have this reflected in the types of the create
method then, basically the create
method then should not have GetSpaceEnvironmentUploadParams
as type, because it will never accept the optional uploadId
(or if it did, it would result in a 400 api call), but only GetSpaceEnvironmentParams
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I agree, the create method should take in only the GetSpaceEnvironmentParams
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I have a few cents to add here as well... I think the comments and the suggestions on the approach make a lot of sense.
My opinion is that we should not overload the method here to handle both uses cases but treat them as different things. This also makes the type definition itself a bit cleaner.
So to give you an example picked at random from the same folder we're in here (REST/endpoints):
contentful-management.js/lib/adapters/REST/endpoints/organization-membership.ts
Lines 15 to 19 in cebe70a
const getBaseUrl = (params: GetOrganizationParams) => | |
`/organizations/${params.organizationId}/organization_memberships` | |
const getEntityUrl = (params: GetOrganizationMembershipParams) => | |
`${getBaseUrl(params)}/${params.organizationMembershipId}` |
In general this is true of every "restful" resource, there will always be two endpoints the "single" (for read/update/delete) and the "collection" (for create/query). So I suggest we embrace it, create two methods which have different input types, and then update GetSpaceEnvironmentUploadParams
type to require the uploadid (so it can be reused throughout).
I think with this small change you might even end up with less lines of code, certainly one less conditional, and hopefully a cleaner solution.
Pretty small thing at the end of the day but i think worth getting to a nice clean state.
9bcac61
to
018840f
Compare
@@ -99,7 +99,8 @@ describe('Asset api', function () { | |||
await unarchivedAsset.delete() | |||
}) | |||
|
|||
test('Create and process asset with multiple locales', async () => { | |||
// Skip because this is a flakey test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are something the DX team will fix once they take ownership of this repo - ticket has been created in their backlog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 🎉
I would double check this with the owners of the upload api as well though!
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. 👍🏻 A bit of a nit but I think I would prefer to have this reflected in the types of the create
method then, basically the create
method then should not have GetSpaceEnvironmentUploadParams
as type, because it will never accept the optional uploadId
(or if it did, it would result in a 400 api call), but only GetSpaceEnvironmentParams
right?
export const create: RestEndpoint<'Upload', 'create'> = ( | ||
http: AxiosInstance, | ||
params: GetSpaceParams, | ||
params: GetSpaceEnvironmentUploadParams, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically this should then be GetSpaceEnvironmentParams
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right!
018840f
to
cebe70a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some more thoughts in this thread of discussion around the path method and types, LMK if makes sense!
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I have a few cents to add here as well... I think the comments and the suggestions on the approach make a lot of sense.
My opinion is that we should not overload the method here to handle both uses cases but treat them as different things. This also makes the type definition itself a bit cleaner.
So to give you an example picked at random from the same folder we're in here (REST/endpoints):
contentful-management.js/lib/adapters/REST/endpoints/organization-membership.ts
Lines 15 to 19 in cebe70a
const getBaseUrl = (params: GetOrganizationParams) => | |
`/organizations/${params.organizationId}/organization_memberships` | |
const getEntityUrl = (params: GetOrganizationMembershipParams) => | |
`${getBaseUrl(params)}/${params.organizationMembershipId}` |
In general this is true of every "restful" resource, there will always be two endpoints the "single" (for read/update/delete) and the "collection" (for create/query). So I suggest we embrace it, create two methods which have different input types, and then update GetSpaceEnvironmentUploadParams
type to require the uploadid (so it can be reused throughout).
I think with this small change you might even end up with less lines of code, certainly one less conditional, and hopefully a cleaner solution.
Pretty small thing at the end of the day but i think worth getting to a nice clean state.
82a542f
to
155bac7
Compare
155bac7
to
a58bfff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes, look great! 😎
🎉 This PR is included in version 11.5.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 11.5.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Somewhat recently there was a CEP for updating the upload-api that was approved and a subsequent PR that was merged. This effectively means the Upload API now takes in both the Environment Id and Space Id for a given request.