Skip to content

Commit 3d0fca0

Browse files
authored
feat: add upload credentials api to client [] (#2465)
* feat: add upload cred to client [] * chore: add upload cred entity * chore: update base url * chore: fix test * chore: add e2e test * chore: update endpoint
1 parent 3dcacc2 commit 3d0fca0

File tree

12 files changed

+435
-445
lines changed

12 files changed

+435
-445
lines changed

lib/adapters/REST/endpoints/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import * as AccessToken from './access-token'
12
import * as ApiKey from './api-key'
3+
import * as AppAccessToken from './app-access-token'
24
import * as AppAction from './app-action'
35
import * as AppActionCall from './app-action-call'
46
import * as AppBundle from './app-bundle'
57
import * as AppDefinition from './app-definition'
68
import * as AppDetails from './app-details'
9+
import * as AppEventSubscription from './app-event-subscription'
710
import * as AppInstallation from './app-installation'
11+
import * as AppKey from './app-key'
812
import * as AppSignedRequest from './app-signed-request'
913
import * as AppSigningSecret from './app-signing-secret'
10-
import * as AppEventSubscription from './app-event-subscription'
11-
import * as AppKey from './app-key'
12-
import * as AppAccessToken from './app-access-token'
1314
import * as AppUpload from './app-upload'
1415
import * as Asset from './asset'
1516
import * as AssetKey from './asset-key'
@@ -31,7 +32,6 @@ import * as Organization from './organization'
3132
import * as OrganizationInvitation from './organization-invitation'
3233
import * as OrganizationMembership from './organization-membership'
3334
import * as PersonalAccessToken from './personal-access-token'
34-
import * as AccessToken from './access-token'
3535
import * as PreviewApiKey from './preview-api-key'
3636
import * as Release from './release'
3737
import * as ReleaseAction from './release-action'
@@ -51,6 +51,7 @@ import * as TeamMembership from './team-membership'
5151
import * as TeamSpaceMembership from './team-space-membership'
5252
import * as UIConfig from './ui-config'
5353
import * as Upload from './upload'
54+
import * as UploadCredential from './upload-credentials'
5455
import * as Usage from './usage'
5556
import * as User from './user'
5657
import * as UserUIConfig from './user-ui-config'
@@ -113,6 +114,7 @@ export default {
113114
TeamSpaceMembership,
114115
UIConfig,
115116
Upload,
117+
UploadCredential,
116118
Usage,
117119
User,
118120
UserUIConfig,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { AxiosInstance } from 'contentful-sdk-core'
2+
import type { GetSpaceEnvironmentParams } from '../../../common-types'
3+
import { getUploadHttpClient } from '../../../upload-http-client'
4+
import type { RestEndpoint } from '../types'
5+
import * as raw from './raw'
6+
7+
const getBaseUrl = (params: GetSpaceEnvironmentParams) => {
8+
return `/spaces/${params.spaceId}/environments/${
9+
params.environmentId ?? 'master'
10+
}/upload_credentials`
11+
}
12+
13+
export const create: RestEndpoint<'UploadCredential', 'create'> = (
14+
http: AxiosInstance,
15+
params: GetSpaceEnvironmentParams
16+
) => {
17+
const httpUpload = getUploadHttpClient(http)
18+
19+
const path = getBaseUrl(params)
20+
return raw.post(httpUpload, path)
21+
}

lib/common-types.ts

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
import type { RawAxiosRequestConfig, RawAxiosRequestHeaders } from 'axios'
22
import type { OpPatch } from 'json-patch'
33
import type { Stream } from 'stream'
4+
import type {
5+
AccessTokenProps,
6+
CreatePersonalAccessTokenProps as CreatePATProps,
7+
} from './entities/access-token'
8+
import type { ApiKeyProps, CreateApiKeyProps } from './entities/api-key'
49
import type { AppActionProps, CreateAppActionProps } from './entities/app-action'
510
import type {
611
AppActionCallProps,
712
AppActionCallResponse,
813
CreateAppActionCallProps,
914
} from './entities/app-action-call'
1015
import type { AppBundleProps, CreateAppBundleProps } from './entities/app-bundle'
11-
import type { ApiKeyProps, CreateApiKeyProps } from './entities/api-key'
12-
import type { AppDefinitionProps, CreateAppDefinitionProps } from './entities/app-definition'
16+
import type {
17+
AppDefinitionProps,
18+
AppInstallationsForOrganizationProps,
19+
CreateAppDefinitionProps,
20+
} from './entities/app-definition'
21+
import type { AppDetailsProps, CreateAppDetailsProps } from './entities/app-details'
1322
import type { AppInstallationProps, CreateAppInstallationProps } from './entities/app-installation'
23+
import type {
24+
AppSignedRequestProps,
25+
CreateAppSignedRequestProps,
26+
} from './entities/app-signed-request'
27+
import type {
28+
AppSigningSecretProps,
29+
CreateAppSigningSecretProps,
30+
} from './entities/app-signing-secret'
31+
import type { AppUploadProps } from './entities/app-upload'
1432
import type {
1533
AssetFileProp,
1634
AssetProcessingForLocale,
1735
AssetProps,
1836
CreateAssetProps,
1937
} from './entities/asset'
20-
import type { ContentTypeProps, CreateContentTypeProps } from './entities/content-type'
38+
import type { AssetKeyProps, CreateAssetKeyProps } from './entities/asset-key'
39+
import type {
40+
BulkActionProps,
41+
BulkActionPublishPayload,
42+
BulkActionUnpublishPayload,
43+
BulkActionValidatePayload,
44+
} from './entities/bulk-action'
2145
import type {
2246
CommentProps,
2347
CreateCommentParams,
@@ -32,15 +56,16 @@ import type {
3256
UpdateCommentParams,
3357
UpdateCommentProps,
3458
} from './entities/comment'
59+
import type { ContentTypeProps, CreateContentTypeProps } from './entities/content-type'
3560
import type { EditorInterfaceProps } from './entities/editor-interface'
3661
import type { CreateEntryProps, EntryProps, EntryReferenceProps } from './entities/entry'
3762
import type { CreateEnvironmentProps, EnvironmentProps } from './entities/environment'
3863
import type {
3964
CreateEnvironmentAliasProps,
4065
EnvironmentAliasProps,
4166
} from './entities/environment-alias'
67+
import type { CreateExtensionProps, ExtensionProps } from './entities/extension'
4268
import type { CreateLocaleProps, LocaleProps } from './entities/locale'
43-
import type { AppInstallationsForOrganizationProps } from './entities/app-definition'
4469
import type { OrganizationProps } from './entities/organization'
4570
import type {
4671
CreateOrganizationInvitationProps,
@@ -51,11 +76,18 @@ import type {
5176
CreatePersonalAccessTokenProps,
5277
PersonalAccessTokenProps,
5378
} from './entities/personal-access-token'
54-
import type {
55-
AccessTokenProps,
56-
CreatePersonalAccessTokenProps as CreatePATProps,
57-
} from './entities/access-token'
5879
import type { PreviewApiKeyProps } from './entities/preview-api-key'
80+
import type {
81+
ReleasePayload,
82+
ReleaseProps,
83+
ReleaseQueryOptions,
84+
ReleaseValidatePayload,
85+
} from './entities/release'
86+
import type {
87+
ReleaseAction,
88+
ReleaseActionProps,
89+
ReleaseActionQueryOptions,
90+
} from './entities/release-action'
5991
import type { CreateRoleProps, RoleProps } from './entities/role'
6092
import type { ScheduledActionProps } from './entities/scheduled-action'
6193
import type { SnapshotProps } from './entities/snapshot'
@@ -69,7 +101,6 @@ import type {
69101
CreateTeamSpaceMembershipProps,
70102
TeamSpaceMembershipProps,
71103
} from './entities/team-space-membership'
72-
import type { CreateExtensionProps, ExtensionProps } from './entities/extension'
73104
import type { UsageProps } from './entities/usage'
74105
import type { UserProps } from './entities/user'
75106
import type {
@@ -83,34 +114,6 @@ import type {
83114
WebhookRetryPolicyProps,
84115
WebhookSigningSecretProps,
85116
} from './entities/webhook'
86-
import type { AssetKeyProps, CreateAssetKeyProps } from './entities/asset-key'
87-
import type { AppUploadProps } from './entities/app-upload'
88-
import type { AppDetailsProps, CreateAppDetailsProps } from './entities/app-details'
89-
import type {
90-
AppSignedRequestProps,
91-
CreateAppSignedRequestProps,
92-
} from './entities/app-signed-request'
93-
import type {
94-
AppSigningSecretProps,
95-
CreateAppSigningSecretProps,
96-
} from './entities/app-signing-secret'
97-
import type {
98-
BulkActionProps,
99-
BulkActionPublishPayload,
100-
BulkActionUnpublishPayload,
101-
BulkActionValidatePayload,
102-
} from './entities/bulk-action'
103-
import type {
104-
ReleasePayload,
105-
ReleaseProps,
106-
ReleaseQueryOptions,
107-
ReleaseValidatePayload,
108-
} from './entities/release'
109-
import type {
110-
ReleaseAction,
111-
ReleaseActionProps,
112-
ReleaseActionQueryOptions,
113-
} from './entities/release-action'
114117

115118
import type {
116119
CreateTaskParams,
@@ -121,27 +124,14 @@ import type {
121124
UpdateTaskProps,
122125
} from './entities/task'
123126

127+
import type { AppAccessTokenProps, CreateAppAccessTokenProps } from './entities/app-access-token'
124128
import type {
125-
CreateWorkflowDefinitionParams,
126-
CreateWorkflowDefinitionProps,
127-
DeleteWorkflowDefinitionParams,
128-
WorkflowDefinitionProps,
129-
WorkflowDefinitionQueryOptions,
130-
} from './entities/workflow-definition'
131-
import type {
132-
CompleteWorkflowParams,
133-
CreateWorkflowParams,
134-
CreateWorkflowProps,
135-
DeleteWorkflowParams,
136-
WorkflowProps,
137-
WorkflowQueryOptions,
138-
} from './entities/workflow'
139-
import type {
140-
WorkflowsChangelogEntryProps,
141-
WorkflowsChangelogQueryOptions,
142-
} from './entities/workflows-changelog-entry'
143-
import type { UIConfigProps } from './entities/ui-config'
144-
import type { UserUIConfigProps } from './entities/user-ui-config'
129+
AppEventSubscriptionProps,
130+
CreateAppEventSubscriptionProps,
131+
} from './entities/app-event-subscription'
132+
import type { AppKeyProps, CreateAppKeyProps } from './entities/app-key'
133+
import type { ConceptProps, CreateConceptProps } from './entities/concept'
134+
import type { ConceptSchemeProps, CreateConceptSchemeProps } from './entities/concept-scheme'
145135
import type {
146136
CreateEnvironmentTemplateProps,
147137
EnvironmentTemplateProps,
@@ -153,14 +143,7 @@ import type {
153143
ValidateEnvironmentTemplateInstallationProps,
154144
} from './entities/environment-template-installation'
155145
import type { FunctionProps } from './entities/function'
156-
import type {
157-
AppEventSubscriptionProps,
158-
CreateAppEventSubscriptionProps,
159-
} from './entities/app-event-subscription'
160-
import type { AppKeyProps, CreateAppKeyProps } from './entities/app-key'
161-
import type { AppAccessTokenProps, CreateAppAccessTokenProps } from './entities/app-access-token'
162-
import type { ConceptProps, CreateConceptProps } from './entities/concept'
163-
import type { ConceptSchemeProps, CreateConceptSchemeProps } from './entities/concept-scheme'
146+
import type { ResourceProps, ResourceQueryOptions } from './entities/resource'
164147
import type {
165148
ResourceProviderProps,
166149
UpsertResourceProviderProps,
@@ -170,7 +153,27 @@ import type {
170153
SpaceEnvResourceTypeProps,
171154
UpsertResourceTypeProps,
172155
} from './entities/resource-type'
173-
import type { ResourceProps, ResourceQueryOptions } from './entities/resource'
156+
import type { UIConfigProps } from './entities/ui-config'
157+
import type { UserUIConfigProps } from './entities/user-ui-config'
158+
import type {
159+
CompleteWorkflowParams,
160+
CreateWorkflowParams,
161+
CreateWorkflowProps,
162+
DeleteWorkflowParams,
163+
WorkflowProps,
164+
WorkflowQueryOptions,
165+
} from './entities/workflow'
166+
import type {
167+
CreateWorkflowDefinitionParams,
168+
CreateWorkflowDefinitionProps,
169+
DeleteWorkflowDefinitionParams,
170+
WorkflowDefinitionProps,
171+
WorkflowDefinitionQueryOptions,
172+
} from './entities/workflow-definition'
173+
import type {
174+
WorkflowsChangelogEntryProps,
175+
WorkflowsChangelogQueryOptions,
176+
} from './entities/workflows-changelog-entry'
174177

175178
export interface DefaultElements<TPlainObject extends object = object> {
176179
toPlainObject(): TPlainObject
@@ -724,6 +727,8 @@ type MRInternal<UA extends boolean> = {
724727
(opts: MROpts<'Upload', 'create', UA>): MRReturn<'Entry', 'create'>
725728
(opts: MROpts<'Upload', 'delete', UA>): MRReturn<'Entry', 'delete'>
726729

730+
(opts: MROpts<'UploadCredential', 'create', UA>): MRReturn<'UploadCredential', 'create'>
731+
727732
(opts: MROpts<'Usage', 'getManyForSpace', UA>): MRReturn<'Usage', 'getManyForSpace'>
728733
(opts: MROpts<'Usage', 'getManyForOrganization', UA>): MRReturn<'Usage', 'getManyForOrganization'>
729734

@@ -1877,6 +1882,12 @@ export type MRActions = {
18771882
}
18781883
delete: { params: GetSpaceEnvironmentUploadParams; return: any }
18791884
}
1885+
UploadCredential: {
1886+
create: {
1887+
params: GetSpaceEnvironmentParams
1888+
return: any
1889+
}
1890+
}
18801891
Usage: {
18811892
getManyForSpace: {
18821893
params: { organizationId: string } & QueryParams

lib/enhance-with-methods.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default function enhanceWithMethods<
1313
B extends Record<string, unknown>,
1414
M extends Record<string, Function>
1515
>(baseObject: B, methodsObject: M): M & B {
16-
// @ts-expect-error
1716
return Object.keys(methodsObject).reduce((enhancedObject, methodName) => {
1817
Object.defineProperty(enhancedObject, methodName, {
1918
enumerable: false,
@@ -22,5 +21,5 @@ export default function enhanceWithMethods<
2221
value: methodsObject[methodName],
2322
})
2423
return enhancedObject
25-
}, baseObject)
24+
}, baseObject as M & B)
2625
}

lib/entities/upload-credential.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
2+
import copy from 'fast-copy'
3+
import type { DefaultElements, MakeRequest, MetaSysProps, SysLink } from '../common-types'
4+
import enhanceWithMethods from '../enhance-with-methods'
5+
6+
export type UploadCredentialProps = {
7+
/**
8+
* System metadata
9+
*/
10+
sys: MetaSysProps & { space: SysLink; environment?: SysLink }
11+
}
12+
13+
export interface UploadCredential
14+
extends UploadCredentialProps,
15+
DefaultElements<UploadCredentialProps> {
16+
/**
17+
* Deletes this object on the server.
18+
* @return Promise for the deletion. It contains no data, but the Promise error case should be handled.
19+
* @example
20+
* const contentful = require('contentful-management')
21+
*
22+
* const client = contentful.createClient({
23+
* accessToken: '<content_management_api_key>'
24+
* })
25+
*
26+
* client.getSpace('<space_id>')
27+
* .then((space) => space.getEnvironment('<environment_id>'))
28+
* .then((environment) => environment.getUpload('<upload_id>'))
29+
* .then((upload) => upload.delete())
30+
* .then((upload) => console.log(`upload ${upload.sys.id} updated.`))
31+
* .catch(console.error)
32+
*/
33+
create(): Promise<UploadCredentialProps>
34+
}
35+
36+
/**
37+
* @private
38+
*/
39+
function createUploadCredentialsApi(makeRequest: MakeRequest) {
40+
return {
41+
create: async function create() {
42+
const raw = this.toPlainObject() as UploadCredentialProps
43+
return makeRequest({
44+
entityType: 'UploadCredential',
45+
action: 'create',
46+
params: {
47+
spaceId: raw.sys.space.sys.id,
48+
environmentId: raw.sys.id,
49+
},
50+
})
51+
},
52+
}
53+
}
54+
55+
/**
56+
* @private
57+
* @param {function} makeRequest - function to make requests via an adapter
58+
* @param {object} data - Raw upload credential data
59+
* @return {UploadCredential} Wrapped upload credential data
60+
*/
61+
export function wrapUploadCredential(
62+
makeRequest: MakeRequest,
63+
data: UploadCredentialProps
64+
): UploadCredential {
65+
const uploadCredential = toPlainObject(copy(data))
66+
const uploadCredentialWithMethods = enhanceWithMethods(
67+
uploadCredential,
68+
createUploadCredentialsApi(makeRequest)
69+
)
70+
return freezeSys(uploadCredentialWithMethods)
71+
}

0 commit comments

Comments
 (0)