-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generates type guards for delivery models
- Loading branch information
Showing
7 changed files
with
108 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ContentTypeModels } from '@kontent-ai/management-sdk'; | ||
import { deliveryConfig } from '../../config.js'; | ||
import { ContentTypeNameResolver, mapName } from '../../core/resolvers.js'; | ||
|
||
export interface DeliveryTypeGuardGeneratorConfig { | ||
readonly nameResolvers?: { | ||
readonly contentType?: ContentTypeNameResolver; | ||
}; | ||
} | ||
|
||
export function deliveryTypeGuardGenerator(config: DeliveryTypeGuardGeneratorConfig) { | ||
const nameResolvers = { | ||
typeGuardFunctionName: mapName(config.nameResolvers?.contentType, 'pascalCase', { | ||
prefix: 'is' | ||
}), | ||
typeName: mapName(config.nameResolvers?.contentType, 'pascalCase') | ||
}; | ||
|
||
const getTypeGuardFunction = (contentType: Readonly<ContentTypeModels.ContentType>): string => { | ||
return `export function ${nameResolvers.typeGuardFunctionName(contentType)}(item: ${deliveryConfig.coreContentTypeName}): item is ${nameResolvers.typeName(contentType)} { | ||
return item.system.type === '${contentType.codename}'; | ||
}`; | ||
}; | ||
|
||
return { | ||
getTypeGuardFunction | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
/** | ||
* This file has been auto-generated by '@kontent-ai/[email protected]'. | ||
* | ||
* (c) Kontent.ai | ||
* | ||
* ------------------------------------------------------------------------------- | ||
* | ||
* Project: Movie Database | ||
* Environment: Production | ||
* Id: da5abe9f-fdad-4168-97cd-b3464be2ccb9 | ||
* | ||
* ------------------------------------------------------------------------------- | ||
**/ | ||
|
||
/** | ||
* This file has been auto-generated by '@kontent-ai/[email protected]'. | ||
* | ||
* (c) Kontent.ai | ||
* | ||
* ------------------------------------------------------------------------------- | ||
* | ||
* Project: Movie Database | ||
* Environment: Production | ||
* Id: da5abe9f-fdad-4168-97cd-b3464be2ccb9 | ||
* | ||
* ------------------------------------------------------------------------------- | ||
**/ | ||
|
||
export * from './content-type-snippets/index.js'; | ||
export * from './content-types/index.js'; | ||
|