Skip to content

Commit

Permalink
feat: add adaptor-fetch package
Browse files Browse the repository at this point in the history
Provides a generic interface for creating adaptors using the fetch() API
  • Loading branch information
chrisvxd committed Jul 6, 2023
1 parent 5aeb3c7 commit eaf7875
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
51 changes: 51 additions & 0 deletions packages/adaptor-fetch/README.md
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)
15 changes: 15 additions & 0 deletions packages/adaptor-fetch/index.ts
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;
23 changes: 23 additions & 0 deletions packages/adaptor-fetch/package.json
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"
}
}
5 changes: 5 additions & 0 deletions packages/adaptor-fetch/tsconfig.json
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"]
}
4 changes: 4 additions & 0 deletions packages/adaptor-fetch/tsup.config.ts
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);

0 comments on commit eaf7875

Please sign in to comment.