-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides a generic interface for creating adaptors using the fetch() API
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 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,51 @@ | ||
# adaptor-fetch | ||
|
||
A generic fetch adaptor for Puck that can be used for most content repositories. | ||
|
||
## Quick start | ||
|
||
Install the package: | ||
|
||
```sh | ||
npm i @measured/puck-adaptor-fetch | ||
``` | ||
|
||
Create an adaptor using the [fetch() API](https://developer.mozilla.org/en-US/docs/Web/API/fetch): | ||
|
||
```jsx | ||
import createAdaptor from "@measured/puck-adaptor-fetch"; | ||
|
||
const movieAdaptor = createAdaptor("http://localhost:1337/api/movies", { | ||
headers: { | ||
Authorization: "Bearer abc123", | ||
}, | ||
}); | ||
``` | ||
|
||
Configure your Puck instance. In this case, we add our `movieAdaptor` to the `movie` field on our "MovieBlock" component: | ||
|
||
```jsx | ||
import { Puck } from "@measured/puck"; | ||
|
||
const config = { | ||
components: { | ||
MovieBlock: { | ||
fields: { | ||
movie: { | ||
type: "external", | ||
adaptor: moviesAdaptor, | ||
}, | ||
}, | ||
render: ({ movie }) => <h1>{movie.title}</h1>, | ||
}, | ||
}, | ||
}; | ||
|
||
export function Page() { | ||
return <Puck config={config} data={data} />; | ||
} | ||
``` | ||
|
||
## License | ||
|
||
MIT © [Measured Co.](https://github.com/measuredco) |
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,15 @@ | ||
export const createAdaptor = ( | ||
input: RequestInfo | URL, | ||
init?: RequestInit, | ||
name: string = "API" | ||
) => ({ | ||
name, | ||
fetchList: async () => { | ||
const res = await fetch(input, init); | ||
const body: { data: Record<string, any>[] } = await res.json(); | ||
|
||
return body.data; | ||
}, | ||
}); | ||
|
||
export default createAdaptor; |
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,23 @@ | ||
{ | ||
"name": "@measured/puck-adaptor-fetch", | ||
"version": "0.3.2", | ||
"private": false, | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"lint": "eslint \"**/*.ts*\"", | ||
"build": "rm -rf dist && tsup index.ts", | ||
"prepare": "yarn build" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"devDependencies": { | ||
"eslint": "^7.32.0", | ||
"eslint-config-custom": "*", | ||
"tsconfig": "*", | ||
"tsup-config": "*", | ||
"typescript": "^4.5.2" | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"extends": "tsconfig/react-library.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |
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,4 @@ | ||
import { defineConfig } from "tsup"; | ||
import tsupconfig from "../tsup-config"; | ||
|
||
export default defineConfig(tsupconfig); |