-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add runtime definition for content sources
- Loading branch information
Showing
4 changed files
with
162 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type PlainObjectValue = | ||
| number | ||
| string | ||
| boolean | ||
| PlainObject | ||
| undefined | ||
| null | ||
| PlainObjectValue[]; | ||
export type PlainObject = { | ||
[key: string]: PlainObjectValue; | ||
}; |
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,84 @@ | ||
import { | ||
ContentComputeDocumentEvent, | ||
ContentComputeRevisionEventResponse, | ||
ContentComputeRevisionEvent, | ||
Document, | ||
} from '@gitbook/api'; | ||
import { PlainObject } from './common'; | ||
import { RuntimeCallback, RuntimeContext } from './context'; | ||
|
||
export interface ContentSourceDefinition<Context extends RuntimeContext = RuntimeContext> { | ||
sourceId: string; | ||
compute: RuntimeCallback< | ||
[ContentComputeRevisionEvent | ContentComputeDocumentEvent], | ||
Promise<Response>, | ||
Context | ||
>; | ||
} | ||
|
||
/** | ||
* Create a content source. The result should be bind to the integration using `contentSources`. | ||
*/ | ||
export function createContentSource< | ||
Props extends PlainObject = {}, | ||
Context extends RuntimeContext = RuntimeContext, | ||
>(source: { | ||
/** | ||
* ID of the source, referenced in the YAML file. | ||
*/ | ||
sourceId: string; | ||
|
||
/** | ||
* Callback to generate the pages. | ||
*/ | ||
getRevision: RuntimeCallback< | ||
[ | ||
{ | ||
props: Props; | ||
}, | ||
], | ||
Promise<ContentComputeRevisionEventResponse>, | ||
Context | ||
>; | ||
|
||
/** | ||
* Callback to generate the document of a page. | ||
*/ | ||
getPageDocument: RuntimeCallback< | ||
[ | ||
{ | ||
props: Props; | ||
}, | ||
], | ||
Promise<Document | void>, | ||
Context | ||
>; | ||
}): ContentSourceDefinition<Context> { | ||
return { | ||
sourceId: source.sourceId, | ||
compute: async (event, context) => { | ||
const output = | ||
event.type === 'content_compute_revision' | ||
? await source.getRevision( | ||
{ | ||
props: event.props as Props, | ||
}, | ||
context, | ||
) | ||
: { | ||
document: await source.getPageDocument( | ||
{ | ||
props: event.props as Props, | ||
}, | ||
context, | ||
), | ||
}; | ||
|
||
return new Response(JSON.stringify(output), { | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}); | ||
}, | ||
}; | ||
} |
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