-
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.
feat: add response type renderer (#326)
* feat: add response type renderer * Update README.md Co-authored-by: Rebecca König <[email protected]> --------- Co-authored-by: Rebecca König <[email protected]>
- Loading branch information
Showing
8 changed files
with
247 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env node | ||
(async () => { | ||
const oclif = await import('@oclif/core') | ||
await oclif.execute({type: 'cjs', development: true, dir: __dirname}) | ||
})() | ||
const oclif = await import('@oclif/core'); | ||
await oclif.execute({ type: 'cjs', development: true, dir: __dirname }); | ||
})(); |
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,5 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
const oclif = require('@oclif/core') | ||
const oclif = require('@oclif/core'); | ||
|
||
oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle')) | ||
oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle')); |
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,87 @@ | ||
import { SourceFile } from 'ts-morph'; | ||
import { CFContentType } from '../../types'; | ||
import { renderTypeGeneric, renderTypeLiteral, renderTypeUnion } from '../generic'; | ||
import { BaseContentTypeRenderer } from './base-content-type-renderer'; | ||
|
||
/* | ||
* Renders the response types for the contentful content types | ||
* Based on https://github.com/contentful/contentful.js/issues/2138#issuecomment-1921923508 | ||
*/ | ||
const ChainModifiers = { | ||
WITHOUT_UNRESOLVABLE_LINKS: 'WithoutUnresolvableLinksResponse', | ||
WITHOUT_LINK_RESOLUTION: 'WithoutLinkResolutionResponse', | ||
WITH_ALL_LOCALES: 'WithAllLocalesResponse', | ||
WITH_ALL_LOCALES_AND_WITHOUT_LINK_RESOLUTION: 'WithAllLocalesAndWithoutLinkResolutionResponse', | ||
WITH_ALL_LOCALES_AND_WITHOUT_UNRESOLVABLE_LINK: | ||
'WithAllLocalesAndWithoutUnresolvableLinksResponse', | ||
}; | ||
|
||
const LocaleWithDefaultTypeString = 'Locales extends LocaleCode = LocaleCode'; | ||
|
||
export class ResponseTypeRenderer extends BaseContentTypeRenderer { | ||
public render = (contentType: CFContentType, file: SourceFile): void => { | ||
const context = this.createContext(); | ||
|
||
const entityName = context.moduleName(contentType.sys.id); | ||
|
||
file.addTypeAlias({ | ||
name: `${entityName}${ChainModifiers.WITHOUT_LINK_RESOLUTION}`, | ||
isExported: true, | ||
type: renderTypeGeneric( | ||
entityName, | ||
renderTypeUnion([renderTypeLiteral('WITHOUT_LINK_RESOLUTION')]), | ||
), | ||
}); | ||
|
||
file.addTypeAlias({ | ||
name: `${entityName}${ChainModifiers.WITHOUT_UNRESOLVABLE_LINKS}`, | ||
isExported: true, | ||
type: renderTypeGeneric( | ||
entityName, | ||
renderTypeUnion([renderTypeLiteral('WITHOUT_UNRESOLVABLE_LINKS')]), | ||
), | ||
}); | ||
|
||
file.addTypeAlias({ | ||
name: `${entityName}${ChainModifiers.WITH_ALL_LOCALES}<${LocaleWithDefaultTypeString}>`, | ||
isExported: true, | ||
type: renderTypeGeneric( | ||
entityName, | ||
renderTypeUnion([renderTypeLiteral('WITH_ALL_LOCALES')]), | ||
'Locales', | ||
), | ||
}); | ||
|
||
file.addTypeAlias({ | ||
name: `${entityName}${ChainModifiers.WITH_ALL_LOCALES_AND_WITHOUT_LINK_RESOLUTION}<${LocaleWithDefaultTypeString}>`, | ||
isExported: true, | ||
type: renderTypeGeneric( | ||
entityName, | ||
renderTypeUnion([ | ||
renderTypeLiteral('WITH_ALL_LOCALES'), | ||
renderTypeLiteral('WITHOUT_LINK_RESOLUTION'), | ||
]), | ||
'Locales', | ||
), | ||
}); | ||
|
||
file.addTypeAlias({ | ||
name: `${entityName}${ChainModifiers.WITH_ALL_LOCALES_AND_WITHOUT_UNRESOLVABLE_LINK}<${LocaleWithDefaultTypeString}>`, | ||
isExported: true, | ||
type: renderTypeGeneric( | ||
entityName, | ||
renderTypeUnion([ | ||
renderTypeLiteral('WITH_ALL_LOCALES'), | ||
renderTypeLiteral('WITHOUT_UNRESOLVABLE_LINKS'), | ||
]), | ||
'Locales', | ||
), | ||
}); | ||
|
||
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
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,61 @@ | ||
import { Project, ScriptTarget, SourceFile } from 'ts-morph'; | ||
import { CFContentType } from '../../../src'; | ||
import { ResponseTypeRenderer } from '../../../src/renderer/type/response-type-renderer'; | ||
import stripIndent = require('strip-indent'); | ||
|
||
describe('A response type renderer class', () => { | ||
let project: Project; | ||
let testFile: SourceFile; | ||
|
||
beforeEach(() => { | ||
project = new Project({ | ||
useInMemoryFileSystem: true, | ||
compilerOptions: { | ||
target: ScriptTarget.ES5, | ||
declaration: true, | ||
}, | ||
}); | ||
testFile = project.createSourceFile('test.ts'); | ||
}); | ||
|
||
it('adds response types', () => { | ||
const renderer = new ResponseTypeRenderer(); | ||
renderer.setup(project); | ||
|
||
const contentType: CFContentType = { | ||
name: 'display name', | ||
sys: { | ||
id: 'test', | ||
type: 'Symbol', | ||
}, | ||
fields: [ | ||
{ | ||
id: 'field_id', | ||
name: 'field_name', | ||
disabled: false, | ||
localized: false, | ||
required: true, | ||
type: 'Symbol', | ||
omitted: false, | ||
validations: [], | ||
}, | ||
], | ||
}; | ||
|
||
renderer.render(contentType, testFile); | ||
|
||
expect(testFile.getFullText()).toEqual( | ||
stripIndent( | ||
` | ||
export type TypeTestWithoutLinkResolutionResponse = TypeTest<"WITHOUT_LINK_RESOLUTION">; | ||
export type TypeTestWithoutUnresolvableLinksResponse = TypeTest<"WITHOUT_UNRESOLVABLE_LINKS">; | ||
export type TypeTestWithAllLocalesResponse<Locales extends LocaleCode = LocaleCode> = TypeTest<"WITH_ALL_LOCALES", Locales>; | ||
export type TypeTestWithAllLocalesAndWithoutLinkResolutionResponse<Locales extends LocaleCode = LocaleCode> = TypeTest<"WITHOUT_LINK_RESOLUTION" | "WITH_ALL_LOCALES", Locales>; | ||
export type TypeTestWithAllLocalesAndWithoutUnresolvableLinksResponse<Locales extends LocaleCode = LocaleCode> = TypeTest<"WITHOUT_UNRESOLVABLE_LINKS" | "WITH_ALL_LOCALES", Locales>; | ||
`, | ||
) | ||
.replace(/.*/, '') | ||
.slice(1), | ||
); | ||
}); | ||
}); |
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