Skip to content

Commit

Permalink
docs: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Jul 8, 2021
1 parent 422078a commit 6acafca
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 0 deletions.
Empty file removed examples/.gitkeep
Empty file.
26 changes: 26 additions & 0 deletions examples/custom-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Custom Types

This examples shows how to use `@prismicio/custom-types-client` to interact with [Custom Type models](https://prismic.io/docs/core-concepts/custom-types). The client allows for fetching models, updating them, and removing them.

**Note**: This example is written for Node.js. If you plan to use this code for the browser, you can remove `node-fetch` from the example.

```diff
import * as prismic from '@prismicio/custom-types-client'
- import fetch from 'node-fetch'
```

## How to run the example

**Note**: This example requires a Custom Types API secret token to run. Running this example requires updating the repository name and secret token.

```sh
# Clone the repository to your computer
git clone https://github.com/prismicio/prismic-custom-types-client.git
cd prismic-custom-types-client/examples/custom-types

# Install the dependencies
npm install

# Run the example
node --loader ts-node/esm index.ts
```
39 changes: 39 additions & 0 deletions examples/custom-types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as prismicCustomTypes from "@prismicio/custom-types-client";
import fetch from "node-fetch";

/** The Prismic Repository name. */
const PRISMIC_REPOSITORY_NAME = "qwerty";

/** The Prismic Custom Types API secret token for the repository. */
const PRISMIC_CUSTOM_TYPES_API_TOKEN = "secret-token";

const main = async () => {
const customTypesClient = prismicCustomTypes.createClient({
repositoryName: PRISMIC_REPOSITORY_NAME,
token: PRISMIC_CUSTOM_TYPES_API_TOKEN,
fetch,
});

// Get all Custom Type models from the repository.
const models = await customTypesClient.getAll();
console.info({ models });

// Get the "page" Custom Type model from the repository.
const pageModel = await customTypesClient.getByID("page");
console.info({ pageModel });

// Update the "page" Custom Type model from the repository.
// This example disables the model from new documents being created.
await customTypesClient.update({
...pageModel,
status: false,
});

// Remove the "page" Custom Type model from the repository.
await customTypesClient.remove("page");

// Re-add the "page" Custom Type model.
await customTypesClient.insert(pageModel);
};

main();
12 changes: 12 additions & 0 deletions examples/custom-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "module",
"dependencies": {
"@prismicio/custom-types-client": "latest",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@prismicio/types": "^0.1.1",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
}
}
10 changes: 10 additions & 0 deletions examples/custom-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext"],
"moduleResolution": "Node"
},
"include": ["index.ts"]
}
26 changes: 26 additions & 0 deletions examples/shared-slices/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Shared Slices

This examples shows how to use `@prismicio/custom-types-client` to interact with [Shared Slice models](https://prismic.io/docs/core-concepts/reusing-slices#shared-slices). The client allows for fetching models, updating them, and removing them.

**Note**: This example is written for Node.js. If you plan to use this code for the browser, you can remove `node-fetch` from the example.

```diff
import * as prismic from '@prismicio/custom-types-client'
- import fetch from 'node-fetch'
```

## How to run the example

**Note**: This example requires a Custom Types API secret token to run. Running this example requires updating the repository name and secret token.

```sh
# Clone the repository to your computer
git clone https://github.com/prismicio/prismic-custom-types-client.git
cd prismic-custom-types-client/examples/shared-slices

# Install the dependencies
npm install

# Run the example
node --loader ts-node/esm index.ts
```
39 changes: 39 additions & 0 deletions examples/shared-slices/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as prismicCustomTypes from "@prismicio/custom-types-client";
import fetch from "node-fetch";

/** The Prismic Repository name. */
const PRISMIC_REPOSITORY_NAME = "qwerty";

/** The Prismic Custom Types API secret token for the repository. */
const PRISMIC_CUSTOM_TYPES_API_TOKEN = "secret-token";

const main = async () => {
const customTypesClient = prismicCustomTypes.createClient({
repositoryName: PRISMIC_REPOSITORY_NAME,
token: PRISMIC_CUSTOM_TYPES_API_TOKEN,
fetch,
});

// Get all Shared Slice models from the repository.
const models = await customTypesClient.getAllSharedSlices();
console.info({ models });

// Get the "hero" Shared Slice model from the repository.
const heroModel = await customTypesClient.getSharedSliceByID("hero");
console.info({ heroModel });

// Update the "hero" Shared Slice model from the repository.
// This example disables the model from new documents being created.
await customTypesClient.updateSharedSlice({
...heroModel,
description: "Updated description",
});

// Remove the "hero" Shared Slice model from the repository.
await customTypesClient.removeSharedSlice("hero");

// Re-add the "hero" Shared Slice model.
await customTypesClient.insertSharedSlice(heroModel);
};

main();
12 changes: 12 additions & 0 deletions examples/shared-slices/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "module",
"dependencies": {
"@prismicio/custom-types-client": "latest",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@prismicio/types": "^0.1.1",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
}
}
10 changes: 10 additions & 0 deletions examples/shared-slices/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext"],
"moduleResolution": "Node"
},
"include": ["index.ts"]
}

0 comments on commit 6acafca

Please sign in to comment.