Skip to content

Commit

Permalink
fix(types): use rich-text-types package (#557)
Browse files Browse the repository at this point in the history
The locally defined types for rich text content are inaccurate. Instead, we use a dedicated types package for richt-text- types.
  • Loading branch information
marcolink committed Jan 13, 2021
1 parent e222883 commit f140366
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 47 deletions.
47 changes: 3 additions & 44 deletions lib/common-types.ts
@@ -1,4 +1,5 @@
import { AxiosInstance } from 'contentful-sdk-core'
import { Document as RichTextDocument } from '@contentful/rich-text-types'
export type HttpClientInstance = AxiosInstance
export interface AssetDetails {
size: number
Expand Down Expand Up @@ -151,51 +152,9 @@ export declare namespace EntryFields {
type Link<T> = Asset | Entry<T>
type Array<T = any> = symbol[] | Entry<T>[] | Asset[]
type Object<T = any> = T
interface RichText {
data: any
content: RichTextContent[]
nodeType: 'document'
}
}
interface RichTextDataTarget {
sys: {
id: string
type: 'Link'
linkType: 'Entry' | 'Asset'
}
}
interface RichTextData {
uri?: string
target?: RichTextDataTarget
}
declare type RichTextNodeType =
| 'text'
| 'heading-1'
| 'heading-2'
| 'heading-3'
| 'heading-4'
| 'heading-5'
| 'heading-6'
| 'paragraph'
| 'hyperlink'
| 'entry-hyperlink'
| 'asset-hyperlink'
| 'unordered-list'
| 'ordered-list'
| 'list-item'
| 'blockquote'
| 'hr'
| 'embedded-entry-block'
| 'embedded-entry-inline'
interface RichTextContent {
data: RichTextData
content?: RichTextContent[]
marks: {
type: 'bold' | 'underline' | 'code' | 'italic'
}[]
value?: string
nodeType: RichTextNodeType
type RichText = RichTextDocument
}

export interface Space {
sys: Sys
name: string
Expand Down
13 changes: 10 additions & 3 deletions lib/create-contentful-api.ts
Expand Up @@ -36,9 +36,14 @@ export interface ContentfulClientApi {

getContentTypes(): Promise<ContentTypeCollection>

getEntries<Fields = Record<string, any>>(query?: FieldsQueries<Fields>): Promise<EntryCollection<Fields>>
getEntries<Fields = Record<string, any>>(
query?: FieldsQueries<Fields>
): Promise<EntryCollection<Fields>>

getEntry<Fields = Record<string, any>>(id: string, query?: FieldsQueries<Fields>): Promise<Entry<Fields>>
getEntry<Fields = Record<string, any>>(
id: string,
query?: FieldsQueries<Fields>
): Promise<Entry<Fields>>

getSpace(): Promise<Space>

Expand Down Expand Up @@ -249,7 +254,9 @@ export default function createContentfulApi({
* const response = await client.getEntries()
* .console.log(response.items)
*/
async function getEntries<Fields>(query: FieldsQueries<Fields> = {}): Promise<EntryCollection<Fields>> {
async function getEntries<Fields>(
query: FieldsQueries<Fields> = {}
): Promise<EntryCollection<Fields>> {
const { resolveLinks, removeUnresolved } = getGlobalOptions({})
try {
const entries = await get({
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"postinstall": "rimraf node_modules/tsdx/node_modules"
},
"dependencies": {
"@contentful/rich-text-types": "^14.1.2",
"axios": "^0.21.1",
"contentful-resolve-response": "^1.3.0",
"contentful-sdk-core": "^6.6.0",
Expand Down

0 comments on commit f140366

Please sign in to comment.