-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
422078a
commit 6acafca
Showing
9 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,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 | ||
``` |
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,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(); |
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,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" | ||
} | ||
} |
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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"lib": ["ESNext"], | ||
"moduleResolution": "Node" | ||
}, | ||
"include": ["index.ts"] | ||
} |
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,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 | ||
``` |
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,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(); |
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,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" | ||
} | ||
} |
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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"lib": ["ESNext"], | ||
"moduleResolution": "Node" | ||
}, | ||
"include": ["index.ts"] | ||
} |