Skip to content

Commit 3be3fb4

Browse files
authored
fix: replace SysLink & MetaLinkProps with Link<T> to improve type safety [CFISO-2344] (#2590)
* fix: replace `SysLink` & `MetaLinkProps` with `Link<T>` to improve type safety * chore: prettier * fix: more specific types for subject links * fix: snapshots are created by users * fix: webhook health is created by webhook defs * fix: webhook call is created by webhook defs * chore: formatting * fix: update AiAction links * fix: `createdBy` etc should be a union, not an object * fix: remove unused types * fix: remove import
1 parent 4aa59b7 commit 3be3fb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+225
-273
lines changed

lib/adapters/REST/endpoints/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export const createWithId: RestEndpoint<'Asset', 'createWithId'> = (
190190
export const createFromFiles: RestEndpoint<'Asset', 'createFromFiles'> = async (
191191
http: AxiosInstance,
192192
params: GetSpaceEnvironmentParams & { uploadTimeout?: number },
193-
data: Omit<AssetFileProp, 'sys'>
193+
data: AssetFileProp
194194
) => {
195195
const httpUpload = getUploadHttpClient(http, { uploadTimeout: params.uploadTimeout })
196196

lib/common-types.ts

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -248,41 +248,41 @@ export interface SpaceQueryOptions extends PaginationQueryOptions {
248248
spaceId?: string
249249
}
250250

251-
export interface BasicMetaSysProps {
251+
export interface BasicMetaSysProps<TSubject extends string = string> {
252252
type: string
253253
id: string
254254
version: number
255-
createdBy?: SysLink
255+
createdBy?: { [Subject in TSubject]: Link<Subject> }[TSubject]
256256
createdAt: string
257-
updatedBy?: SysLink
257+
updatedBy?: { [Subject in TSubject]: Link<Subject> }[TSubject]
258258
updatedAt: string
259259
}
260260

261-
export interface MetaSysProps extends BasicMetaSysProps {
262-
space?: SysLink
261+
export interface MetaSysProps<TSubject extends string = string>
262+
extends BasicMetaSysProps<TSubject> {
263+
space?: Link<'Space'>
263264
/**
264265
* @deprecated `status` only exists on entities. Please refactor to use a
265266
* type guard to get the correct `EntityMetaSysProps` type with this property.
266267
*/
267-
status?: SysLink
268+
status?: Link<'Status'>
268269
publishedVersion?: number
269270
archivedVersion?: number
270-
archivedBy?: SysLink
271+
archivedBy?: { [Subject in TSubject]: Link<Subject> }[TSubject]
271272
archivedAt?: string
272273
deletedVersion?: number
273-
deletedBy?: SysLink
274+
deletedBy?: { [Subject in TSubject]: Link<Subject> }[TSubject]
274275
deletedAt?: string
275276
}
276277

277-
export interface EntityMetaSysProps extends MetaSysProps {
278+
export interface EntityMetaSysProps extends MetaSysProps<'User' | 'AppDefinition'> {
278279
/**
279280
* @deprecated `contentType` only exists on entries. Please refactor to use a
280281
* type guard to get the correct `EntryMetaSysProps` type with this property.
281282
*/
282-
contentType: SysLink
283-
space: SysLink
284-
status?: SysLink
285-
environment: SysLink
283+
contentType: Link<'ContentType'>
284+
space: Link<'Space'>
285+
environment: Link<'Environment'>
286286
publishedBy?: Link<'User'> | Link<'AppDefinition'>
287287
publishedAt?: string
288288
firstPublishedAt?: string
@@ -291,26 +291,11 @@ export interface EntityMetaSysProps extends MetaSysProps {
291291
fieldStatus?: { '*': Record<string, 'draft' | 'changed' | 'published'> }
292292
}
293293

294-
export interface EntryMetaSysProps extends EntityMetaSysProps {
295-
contentType: SysLink
296-
automationTags: Link<'Tag'>[]
297-
}
298-
299-
export interface MetaLinkProps {
300-
type: string
301-
linkType: string
302-
id: string
303-
}
304-
305294
export interface MetadataProps {
306295
tags: Link<'Tag'>[]
307296
concepts?: Link<'TaxonomyConcept'>[]
308297
}
309298

310-
export interface SysLink {
311-
sys: MetaLinkProps
312-
}
313-
314299
export interface CollectionProp<TObj> {
315300
sys: {
316301
type: 'Array'
@@ -1237,7 +1222,7 @@ export type MRActions = {
12371222
}
12381223
createFromFiles: {
12391224
params: GetSpaceEnvironmentParams & { uploadTimeout?: number }
1240-
payload: Omit<AssetFileProp, 'sys'>
1225+
payload: AssetFileProp
12411226
return: AssetProps
12421227
}
12431228
processForAllLocales: {

lib/create-environment-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
11001100
* .catch(console.error)
11011101
* ```
11021102
*/
1103-
createAssetFromFiles(data: Omit<AssetFileProp, 'sys'>, options?: CreateAssetFromFilesOptions) {
1103+
createAssetFromFiles(data: AssetFileProp, options?: CreateAssetFromFilesOptions) {
11041104
const raw = this.toPlainObject() as EnvironmentProps
11051105
return makeRequest({
11061106
entityType: 'Asset',

lib/entities/access-token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import copy from 'fast-copy'
22
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
33
import enhanceWithMethods from '../enhance-with-methods'
44
import { wrapCollection } from '../common-utils'
5-
import type { DefaultElements, MakeRequest, BasicMetaSysProps, SysLink } from '../common-types'
5+
import type { DefaultElements, MakeRequest, BasicMetaSysProps, Link } from '../common-types'
66

77
type Application = {
88
id?: string
99
name?: string
10-
sys: SysLink
10+
sys: Link<'Application'>
1111
}
1212

13-
type AccessTokenSysProps = BasicMetaSysProps & {
13+
type AccessTokenSysProps = BasicMetaSysProps<'User'> & {
1414
application: Application | null
1515
expiresAt: string | null
1616
lastUsedAt: string | null

lib/entities/ai-action-invocation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
22
import copy from 'fast-copy'
3-
import type { DefaultElements, MakeRequest, SysLink } from '../common-types'
3+
import type { DefaultElements, Link, MakeRequest } from '../common-types'
44
import { wrapCollection } from '../common-utils'
55
import type { Document as RichTextDocument } from '@contentful/rich-text-types'
66

@@ -48,9 +48,9 @@ export type AiActionInvocationProps = {
4848
sys: {
4949
id: string
5050
type: 'AiActionInvocation'
51-
space: SysLink
52-
environment: SysLink
53-
aiAction: SysLink
51+
space: Link<'Space'>
52+
environment: Link<'Environment'>
53+
aiAction: Link<'AiAction'>
5454
status: InvocationStatus
5555
errorCode?: string
5656
}

lib/entities/ai-action.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
22
import copy from 'fast-copy'
3-
import type { DefaultElements, MakeRequest, MetaSysProps } from '../common-types'
3+
import type { DefaultElements, Link, MakeRequest, MetaSysProps } from '../common-types'
44
import { wrapCollection } from '../common-utils'
55
import enhanceWithMethods from '../enhance-with-methods'
66
import {
@@ -71,33 +71,19 @@ export type AiActionTestCase =
7171
}
7272
}
7373

74-
export type SysLinkUserOrApp = {
75-
sys: {
76-
id: string
77-
linkType: 'User' | 'App'
78-
type: 'Link'
79-
}
80-
}
81-
8274
export interface AiActionQueryOptions {
8375
limit?: number
8476
skip?: number
8577
status?: StatusFilter
8678
}
8779

8880
export type AiActionProps = {
89-
sys: MetaSysProps & {
81+
sys: MetaSysProps<'User' | 'AppDefinition'> & {
9082
type: 'AiAction'
91-
space: { sys: { id: string } }
92-
publishedBy?: SysLinkUserOrApp
93-
updatedBy: SysLinkUserOrApp
94-
createdBy: SysLinkUserOrApp
95-
publishedVersion?: number
83+
space: Link<'Space'>
84+
publishedBy?: Link<'User'> | Link<'AppDefinition'>
9685
version: number
9786
publishedAt?: string
98-
updatedAt: string
99-
createdAt: string
100-
id: string
10187
}
10288
name: string
10389
description: string

lib/entities/api-key.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
22
import copy from 'fast-copy'
3-
import type { DefaultElements, MakeRequest, MetaLinkProps, MetaSysProps } from '../common-types'
3+
import type { DefaultElements, Link, MakeRequest, MetaSysProps } from '../common-types'
44
import { wrapCollection } from '../common-utils'
55
import enhanceWithMethods from '../enhance-with-methods'
66

77
export type ApiKeyProps = {
8-
sys: MetaSysProps
8+
sys: MetaSysProps<'User'>
99
name: string
1010
accessToken: string
11-
environments: {
12-
sys: MetaLinkProps
13-
}[]
14-
preview_api_key: { sys: MetaLinkProps }
11+
environments: Link<'Environment'>[]
12+
preview_api_key: Link<'PreviewApiKey'>
1513
description?: string
1614
policies?: { effect: string; action: string }[]
1715
}

lib/entities/app-access-token.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import copy from 'fast-copy'
22
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
33
import type { Except } from 'type-fest'
4-
import type { BasicMetaSysProps, DefaultElements, MakeRequest, SysLink } from '../common-types'
4+
import type { BasicMetaSysProps, DefaultElements, Link, MakeRequest } from '../common-types'
55

6-
type AppAccessTokenSys = Except<BasicMetaSysProps, 'version' | 'id'> & {
7-
space: SysLink
8-
environment: SysLink
9-
appDefinition: SysLink
6+
type AppAccessTokenSys = Except<BasicMetaSysProps<'User'>, 'version' | 'id'> & {
7+
space: Link<'Space'>
8+
environment: Link<'Environment'>
9+
appDefinition: Link<'AppDefinition'>
1010
expiresAt: string
1111
}
1212

lib/entities/app-action-call.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import type {
55
BasicMetaSysProps,
66
CreateWithResponseParams,
77
DefaultElements,
8+
Link,
89
MakeRequest,
9-
SysLink,
1010
} from '../common-types'
1111
import type { WebhookCallDetailsProps } from './webhook'
1212
import enhanceWithMethods from '../enhance-with-methods'
1313

14-
type AppActionCallSys = Except<BasicMetaSysProps, 'version'> & {
15-
appDefinition: SysLink
16-
space: SysLink
17-
environment: SysLink
18-
action: SysLink
14+
type AppActionCallSys = Except<BasicMetaSysProps<'User'>, 'version'> & {
15+
appDefinition: Link<'AppDefinition'>
16+
space: Link<'Space'>
17+
environment: Link<'Environment'>
18+
action: Link<'AppAction'>
1919
}
2020

2121
type RetryOptions = Pick<CreateWithResponseParams, 'retries' | 'retryInterval'>

lib/entities/app-action.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ import { freezeSys, toPlainObject } from 'contentful-sdk-core'
22
import copy from 'fast-copy'
33
import type { Except } from 'type-fest'
44
import { wrapCollection } from '../common-utils'
5-
import type {
6-
BasicMetaSysProps,
7-
DefaultElements,
8-
Link,
9-
MakeRequest,
10-
SysLink,
11-
} from '../common-types'
5+
import type { BasicMetaSysProps, DefaultElements, Link, MakeRequest } from '../common-types'
126
import type { ParameterDefinition } from './widget-parameters'
137
import enhanceWithMethods from '../enhance-with-methods'
148

15-
type AppActionSys = Except<BasicMetaSysProps, 'version'> & {
16-
appDefinition: SysLink
17-
organization: SysLink
9+
type AppActionSys = Except<BasicMetaSysProps<'User'>, 'version'> & {
10+
appDefinition: Link<'AppDefinition'>
11+
organization: Link<'Organization'>
1812
}
1913

2014
export type AppActionParameterDefinition = Omit<ParameterDefinition, 'labels'>

0 commit comments

Comments
 (0)