-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a type-guard function for every content type.
- Loading branch information
Showing
5 changed files
with
180 additions
and
2 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,58 @@ | ||
import { Project, SourceFile } from 'ts-morph'; | ||
import { moduleName } from '../../module-name'; | ||
import { CFContentType } from '../../types'; | ||
import { BaseContentTypeRenderer } from './base-content-type-renderer'; | ||
|
||
export class TypeGuardRenderer extends BaseContentTypeRenderer { | ||
private readonly files: SourceFile[]; | ||
|
||
private static readonly WithContentTypeLink = 'WithContentTypeLink'; | ||
constructor() { | ||
super(); | ||
this.files = []; | ||
} | ||
|
||
public override setup(project: Project): void { | ||
super.setup(project); | ||
const file = project.createSourceFile(`TypeGuardTypes.ts`, undefined, { | ||
overwrite: true, | ||
}); | ||
|
||
file.addTypeAlias({ | ||
name: TypeGuardRenderer.WithContentTypeLink, | ||
isExported: true, | ||
type: `{ sys: { contentType: { sys: { id: string } } } }`, | ||
}); | ||
file.formatText(); | ||
this.files.push(file); | ||
} | ||
|
||
public render = (contentType: CFContentType, file: SourceFile): void => { | ||
const entryInterfaceName = moduleName(contentType.sys.id); | ||
|
||
file.addImportDeclaration({ | ||
moduleSpecifier: 'TypeGuardTypes', | ||
namedImports: [TypeGuardRenderer.WithContentTypeLink], | ||
isTypeOnly: true, | ||
}); | ||
|
||
file.addFunction({ | ||
isExported: true, | ||
name: `is${entryInterfaceName}`, | ||
returnType: `entry is ${entryInterfaceName}`, | ||
parameters: [ | ||
{ | ||
name: 'entry', | ||
type: TypeGuardRenderer.WithContentTypeLink, | ||
}, | ||
], | ||
statements: `return entry.sys.contentType.sys.id === '${contentType.sys.id}'`, | ||
}); | ||
|
||
file.organizeImports({ | ||
ensureNewLineAtEndOfFile: true, | ||
}); | ||
|
||
file.formatText(); | ||
}; | ||
} |
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,82 @@ | ||
import { Project, ScriptTarget, SourceFile } from 'ts-morph'; | ||
import { CFContentType, DefaultContentTypeRenderer, TypeGuardRenderer } from '../../../src'; | ||
import stripIndent = require('strip-indent'); | ||
|
||
describe('A content type type guard renderer class', () => { | ||
let project: Project; | ||
let testFile: SourceFile; | ||
let mockContentType: CFContentType; | ||
|
||
beforeEach(() => { | ||
project = new Project({ | ||
useInMemoryFileSystem: true, | ||
compilerOptions: { | ||
target: ScriptTarget.ES5, | ||
declaration: true, | ||
}, | ||
}); | ||
|
||
mockContentType = { | ||
name: 'Animal', | ||
sys: { | ||
id: 'animal', | ||
type: 'Symbol', | ||
}, | ||
fields: [ | ||
{ | ||
id: 'bread', | ||
name: 'Bread', | ||
disabled: false, | ||
localized: false, | ||
required: true, | ||
type: 'Symbol', | ||
omitted: false, | ||
validations: [], | ||
}, | ||
], | ||
}; | ||
|
||
testFile = project.createSourceFile('test.ts'); | ||
}); | ||
|
||
describe('with default content type declarations', () => { | ||
it('renders entry type guard', () => { | ||
const defaultRenderer = new DefaultContentTypeRenderer(); | ||
defaultRenderer.setup(project); | ||
defaultRenderer.render(mockContentType, testFile); | ||
|
||
const typeGuardRenderer = new TypeGuardRenderer(); | ||
typeGuardRenderer.render(mockContentType, testFile); | ||
|
||
expect('\n' + testFile.getFullText()).toEqual( | ||
stripIndent(` | ||
import type { Entry, EntryFields } from "contentful"; | ||
import type { WithContentTypeLink } from "TypeGuardTypes"; | ||
export interface TypeAnimalFields { | ||
bread: EntryFields.Symbol; | ||
} | ||
export type TypeAnimal = Entry<TypeAnimalFields>; | ||
export function isTypeAnimal(entry: WithContentTypeLink): entry is TypeAnimal { | ||
return entry.sys.contentType.sys.id === 'animal' | ||
} | ||
`), | ||
); | ||
}); | ||
|
||
it('creates type guard helper types', () => { | ||
const typeGuardRenderer = new TypeGuardRenderer(); | ||
typeGuardRenderer.setup(project); | ||
typeGuardRenderer.render(mockContentType, testFile); | ||
const typeGuardFile = project.getSourceFiles()[1]; | ||
|
||
expect('\n' + typeGuardFile.getFullText()).toEqual( | ||
stripIndent(` | ||
export type WithContentTypeLink = { sys: { contentType: { sys: { id: string } } } }; | ||
`), | ||
); | ||
}); | ||
}); | ||
}); |