https://github.com/stackbit/stackbit-sdk
https://github.com/stackbit/stackbit-sdk
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/stackbit/stackbit-sdk
- Owner: stackbit
- Created: 2021-02-03T16:31:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-28T11:22:27.000Z (over 3 years ago)
- Last Synced: 2024-10-29T06:20:58.823Z (about 1 year ago)
- Language: TypeScript
- Size: 5.31 MB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stackbit SDK
Stackbit SDK contains set of utilities to work with stackbit.yaml file.
## Add to your project
```bash
npm install @stackbit/sdk
```
## Generate stackbit.yaml
Create a `FileBrowser` with `FileSystemFileBrowserAdapter` or `GitHubFileBrowserAdapter`:
- Analyzing a local project:
```js
import { FileSystemFileBrowserAdapter, FileBrowser } from '@stackbit/sdk';
const fileBrowserAdapter = new FileSystemFileBrowserAdapter({ dirPath: inputDir });
const fileBrowser = new FileBrowser({ fileBrowserAdapter });
```
- Analyzing a remote GitHub project:
```js
import { GitHubFileBrowserAdapter, FileBrowser } from '@stackbit/sdk';
const fileBrowserAdapter = new GitHubFileBrowserAdapter({
owner: 'stackbit',
repo: 'theme',
branch: 'master',
auth: GITHUB_PERSONAL_ACCESS_TOKEN
});
const fileBrowser = new FileBrowser({ fileBrowserAdapter });
```
Then, pass the `fileBrowser` to the `analyzeSite()` method, get the result and save the `config` as `stackbit.yaml`:
```js
import { writeConfig, analyzeSite } from '@stackbit/sdk';
const analyzeResult = await analyzeSite({ fileBrowser });
await writeConfig({ dirPath: inputDir, config: analyzeResult.config });
```
## Validate stackbit.yaml
Load and validate `stackbit.yaml`. Any errors will be returned within the `errors` array.
```js
import { loadConfig } from '@stackbit/sdk';
const configResult = await loadConfig({ dirPath: inputDir });
configResult.errors.forEach((error) => {
console.log(error.message);
});
```
If `configResult.config` is not null, pass it to load and validate web-site's content. Any errors will be returned within the `errors` array, and loaded content within the `contentItems`:
```js
import { loadContent } from '@stackbit/sdk';
if (configResult.config) {
return;
}
const contentResult = await loadContent({ dirPath: inputDir, config: configResult.config });
contentResult.contentItems.forEach((contentItem) => {
console.log(contentItem.__metadata.filePath);
});
contentResult.errors.forEach((error) => {
console.log(error.message);
});
```